📄 tssync.c
字号:
j += i; } rw_lock_s_lock(&rw2); if (qprint) printf("S-Thread %ld got lock 2 on round %ld!\n", n, k); rw_lock_s_unlock(&rw1); if (qprint) printf("S-Thread %ld released lock 1 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_s_lock(&rw3); if (qprint) printf("S-Thread %ld got lock 3 on round %ld!\n", n, k); rw_lock_s_unlock(&rw2); if (qprint) printf("S-Thread %ld released lock 2 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_s_lock(&rw4); if (qprint) printf("S-Thread %ld got lock 4 on round %ld!\n", n, k); rw_lock_s_unlock(&rw3); if (qprint) printf("S-Thread %ld released lock 3 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_s_unlock(&rw4); if (qprint) printf("S-Thread %ld released lock 4 on round %ld!\n", n, k); } printf("S-Thread %ld exits!\n", n); return(j);}/********************************************************************Start function for the competing x-threads in test6. The function teststhe behavior lock-coupling through 4 rw-locks. */ulintthread_qx(volatile void* arg)/*========================*/{ ulint i, j, k, n; arg = arg; n = os_thread_get_curr_id(); printf("X-Thread %ld started, thread id %lu\n", n, os_thread_get_curr_id()); for (k = 0; k < 1000 * UNIV_DBC; k++) { if (qprint) printf("X-Thread %ld round %ld!\n", n, k); rw_lock_x_lock(&rw1); if (qprint) printf("X-Thread %ld got lock 1 on round %ld!\n", n, k); if (last_thr != n) { switch_count++; last_thr = n; } j = 0; for (i = 1; i < 400; i++) { j += i; } rw_lock_x_lock(&rw2); if (qprint) printf("X-Thread %ld got lock 2 on round %ld!\n", n, k); rw_lock_x_unlock(&rw1); if (qprint) printf("X-Thread %ld released lock 1 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_x_lock(&rw3); if (qprint) printf("X-Thread %ld got lock 3 on round %ld!\n", n, k); rw_lock_x_unlock(&rw2); if (qprint) printf("X-Thread %ld released lock 2 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_x_lock(&rw4); if (qprint) printf("X-Thread %ld got lock 4 on round %ld!\n", n, k); rw_lock_x_unlock(&rw3); if (qprint) printf("X-Thread %ld released lock 3 on round %ld!\n", n, k); for (i = 1; i < 400; i++) { j += i; } rw_lock_x_unlock(&rw4); if (qprint) printf("X-Thread %ld released lock 4 on round %ld!\n", n, k); } printf("X-Thread %ld exits!\n", n); return(j);}/******************************************************************Test function for possible queuing problems with rw-locks. */void test6(void)/*=======*/{ os_thread_t thr1, thr2, thr3, thr4, thr5; os_thread_id_t id1, id2, id3, id4, id5; ulint tm, oldtm; ulint n1, n2, n3, n4, n5; printf("-------------------------------------------\n"); printf( "SYNC-TEST 6. Test of possible queuing problems with rw-locks.\n");/* sync_array_print_info(sync_primary_wait_array);*/ rw_lock_create(&rw2); rw_lock_create(&rw3); rw_lock_create(&rw4); switch_count = 0; oldtm = ut_clock(); n1 = 1; thr1 = os_thread_create(thread_qs, &n1, &id1); os_thread_wait(thr1); tm = ut_clock(); printf("Wall clock time for single s-lock thread %ld milliseconds\n", tm - oldtm); oldtm = ut_clock(); n1 = 1; thr1 = os_thread_create(thread_qx, &n1, &id1); os_thread_wait(thr1); tm = ut_clock(); printf("Wall clock time for single x-lock thread %ld milliseconds\n", tm - oldtm); switch_count = 0; oldtm = ut_clock(); n1 = 1; thr1 = os_thread_create(thread_qx, &n1, &id1); n2 = 2; thr2 = os_thread_create(thread_qs, &n2, &id2); n3 = 3; thr3 = os_thread_create(thread_qx, &n3, &id3); n4 = 4; thr4 = os_thread_create(thread_qs, &n4, &id4); n5 = 5; thr5 = os_thread_create(thread_qx, &n5, &id5); os_thread_wait(thr1); os_thread_wait(thr2); os_thread_wait(thr3); os_thread_wait(thr4); os_thread_wait(thr5); tm = ut_clock(); printf("Wall clock time for 5 threads %ld milliseconds\n", tm - oldtm); printf("at least %ld thread switches occurred\n", switch_count); printf( "If this is not 2 x s-thread + 3 x x-thread time, possibly convoy!\n"); rw_lock_list_print_info(); sync_array_print_info(sync_primary_wait_array);}/********************************************************************Start function for thread in test7. */ulintip_thread(void* arg)/*================*/{ ulint i, j; void* arg2; ulint ret; ulint tm, oldtm; arg2 = arg; printf("Thread started!\n"); oldtm = ut_clock(); ret = ip_mutex_enter(iph, 100000);/* ut_a(ret == SYNC_TIME_EXCEEDED);*/ tm = ut_clock(); printf("Wall clock time for wait failure %ld ms\n", tm - oldtm); ret = ip_mutex_enter(iph, SYNC_INFINITE_TIME); ut_a(ret == 0); printf("Thread owns now the ip mutex!\n"); j = 0; for (i = 1; i < 1000000; i++) { j += i; } printf("Thread releases now the ip mutex!\n"); ip_mutex_exit(iph); return(j);}/*********************************************************************Test for interprocess mutex. */void test7(void)/*=======*/{ os_thread_t thr1; os_thread_id_t id1; ulint i, j; ulint tm, oldtm; printf("-------------------------------------------\n"); printf("SYNC-TEST 7. Test of ip mutex.\n"); printf("Main thread %ld starts!\n", os_thread_get_curr_id()); ip_mutex_create(&ip_mutex, "IPMUTEX", &iph); oldtm = ut_clock(); for (i = 0; i < 100000 * UNIV_DBC; i++) { ip_mutex_enter(iph, SYNC_INFINITE_TIME); ip_mutex_exit(iph); } tm = ut_clock(); printf("Wall clock time for %ld ip mutex lock-unlock %ld ms\n", i, tm - oldtm); ip_mutex_enter(iph, SYNC_INFINITE_TIME); thr1 = os_thread_create(ip_thread, NULL, &id1); printf("Thread created, id %ld \n", id1); j = 0; for (i = 1; i < 100000000; i++) { j += i; } printf("Main thread releases now ip mutex!\n"); ip_mutex_exit(iph); os_thread_wait(thr1); ip_mutex_free(iph);}/********************************************************************Start function for the competing threads in test8. The function teststhe behavior lock-coupling through 4 ip mutexes. */ulintthread_ipn(volatile void* arg)/*========================*/{ ulint i, j, k, n; n = *((ulint*)arg); printf("Thread %ld started!\n", n); for (k = 0; k < 2000 * UNIV_DBC; k++) { ip_mutex_enter(iph1, SYNC_INFINITE_TIME); if (last_thr != n) { switch_count++; last_thr = n; } j = 0; for (i = 1; i < 400; i++) { j += i; } ip_mutex_enter(iph2, SYNC_INFINITE_TIME); ip_mutex_exit(iph1); for (i = 1; i < 400; i++) { j += i; } ip_mutex_enter(iph3, SYNC_INFINITE_TIME); ip_mutex_exit(iph2); for (i = 1; i < 400; i++) { j += i; } ip_mutex_enter(iph4, SYNC_INFINITE_TIME); ip_mutex_exit(iph3); for (i = 1; i < 400; i++) { j += i; } ip_mutex_exit(iph4); } printf("Thread %ld exits!\n", n); return(j);}/******************************************************************Test function for ip mutex. */void test8(void)/*=======*/{ os_thread_t thr1, thr2, thr3, thr4, thr5; os_thread_id_t id1, id2, id3, id4, id5; ulint tm, oldtm; ulint n1, n2, n3, n4, n5; printf("-------------------------------------------\n"); printf("SYNC-TEST 8. Test for ip mutex.\n"); ip_mutex_create(&ip_mutex1, "jhfhk", &iph1); ip_mutex_create(&ip_mutex2, "jggfg", &iph2); ip_mutex_create(&ip_mutex3, "hfdx", &iph3); ip_mutex_create(&ip_mutex4, "kjghg", &iph4); switch_count = 0; oldtm = ut_clock(); n1 = 1; thr1 = os_thread_create(thread_ipn, &n1, &id1); os_thread_wait(thr1); tm = ut_clock(); printf("Wall clock time for single thread %lu milliseconds\n", tm - oldtm); switch_count = 0; oldtm = ut_clock(); n1 = 1; thr1 = os_thread_create(thread_ipn, &n1, &id1); n2 = 2; thr2 = os_thread_create(thread_ipn, &n2, &id2); n3 = 3; thr3 = os_thread_create(thread_ipn, &n3, &id3); n4 = 4; thr4 = os_thread_create(thread_ipn, &n4, &id4); n5 = 5; thr5 = os_thread_create(thread_ipn, &n5, &id5); os_thread_wait(thr1); os_thread_wait(thr2); os_thread_wait(thr3); os_thread_wait(thr4); os_thread_wait(thr5); tm = ut_clock(); printf("Wall clock time for 5 threads %ld milliseconds\n", tm - oldtm); printf("%ld thread switches occurred\n", switch_count); printf("If this is not 5 x single thread time, possibly convoy!\n"); ip_mutex_free(iph1); ip_mutex_free(iph2); ip_mutex_free(iph3); ip_mutex_free(iph4);}/********************************************************************Start function for s-lock thread in test9. */ulintthread_srw9(void* arg)/*==================*/{ void* arg2; arg2 = arg; printf("Thread_srw9 started!\n"); rw_lock_x_lock(&rw10); printf("Thread_srw9 has now x-lock on rw10, wait for mutex!\n"); mutex_enter(&mutex9); return(0);}/********************************************************************Start function for x-lock thread in test9. */ulintthread_xrw9(void* arg)/*==================*/{ void* arg2; arg2 = arg; printf("Thread_xrw started!\n"); mutex_enter(&mutex9); printf("Thread_xrw9 has now mutex9, wait for rw9!\n"); rw_lock_x_lock(&rw9); return(0);}void test9(void)/*=======*/{ os_thread_t thr1, thr2; os_thread_id_t id1, id2; printf("-------------------------------------------\n"); printf("SYNC-TEST 9. Test of deadlock detection.\n"); printf("Main thread %ld starts!\n", os_thread_get_curr_id()); rw_lock_create(&rw9); rw_lock_create(&rw10); mutex_create(&mutex9); rw_lock_s_lock(&rw9); printf("Main thread has now s-lock on rw9\n"); thr2 = os_thread_create(thread_xrw9, NULL, &id2); printf("Thread_xrw9 created, id %ld \n", id2); os_thread_sleep(1000000); thr1 = os_thread_create(thread_srw9, NULL, &id1); printf("Thread_srw9 created, id %ld \n", id1); os_thread_sleep(1000000); sync_array_print_info(sync_primary_wait_array); printf("Now we should have a deadlock of 3 threads:\n"); rw_lock_s_lock(&rw10);}void test10(void)/*=======*/{ printf("-------------------------------------------\n"); printf("SYNC-TEST 10. Test of deadlock detection on self-deadlock.\n"); printf("Main thread %ld starts!\n", os_thread_get_curr_id()); mutex_create(&mutex9); printf("Now we should have a deadlock of this thread on mutex:\n"); mutex_enter(&mutex9); mutex_enter(&mutex9);}void test11(void)/*=======*/{ printf("-------------------------------------------\n"); printf("SYNC-TEST 11. Test of deadlock detection on self-deadlock.\n"); printf("Main thread %ld starts!\n", os_thread_get_curr_id()); rw_lock_create(&rw9); printf("Now we should have a deadlock of this thread on X-lock:\n"); rw_lock_x_lock(&rw9); rw_lock_s_lock_gen(&rw9, 567);}/************************************************************************Main test function. */void main(void) /*======*/{ ulint tm, oldtm; sync_init(); mem_init(); oldtm = ut_clock(); test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); /* This test SHOULD result in assert on deadlock! *//* test9();*/ /* This test SHOULD result in assert on deadlock! *//* test10();*/ /* This test SHOULD result in assert on deadlock! *//* test11();*/ ut_ad(0 == mutex_n_reserved()); ut_ad(0 == rw_lock_n_locked()); ut_ad(sync_all_freed()); ut_ad(mem_all_freed()); sync_close(); tm = ut_clock(); printf("Wall clock time for test %ld milliseconds\n", tm - oldtm); printf("System call count %lu\n", mutex_system_call_count); printf("TESTS COMPLETED SUCCESSFULLY!\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -