void * oldThread(void * arg) { do { (void) old_mutex_lock(&ox1); (void) old_mutex_lock(&ox2); (void) old_mutex_unlock(&ox1); sched_yield(); (void) old_mutex_unlock(&ox2); } while (running); return NULL; }
int main (int argc, char *argv[]) { pthread_t t; assert(pthread_mutexattr_init(&ma) == 0); printf( "=============================================================================\n"); printf( "\nTrylock on a locked mutex.\n"); printf( "%ld iterations.\n\n", ITERATIONS); printf( "%-45s %15s %15s\n", "Test", "Total(msec)", "average(usec)"); printf( "-----------------------------------------------------------------------------\n"); /* * Time the loop overhead so we can subtract it from the actual test times. */ TESTSTART TESTSTOP durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; overHeadMilliSecs = durationMilliSecs; old_mutex_use = OLD_WIN32CS; assert(old_mutex_init(&ox, NULL) == 0); assert(old_mutex_lock(&ox) == 0); assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0); assert(pthread_join(t, NULL) == 0); assert(old_mutex_unlock(&ox) == 0); assert(old_mutex_destroy(&ox) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Critical Section (WNT)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); old_mutex_use = OLD_WIN32MUTEX; assert(old_mutex_init(&ox, NULL) == 0); assert(old_mutex_lock(&ox) == 0); assert(pthread_create(&t, NULL, oldTrylockThread, 0) == 0); assert(pthread_join(t, NULL) == 0); assert(old_mutex_unlock(&ox) == 0); assert(old_mutex_destroy(&ox) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Win32 Mutex (W9x)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); printf( ".............................................................................\n"); /* * Now we can start the actual tests */ #ifdef __PTW32_MUTEX_TYPES runTest("PTHREAD_MUTEX_DEFAULT", PTHREAD_MUTEX_DEFAULT); runTest("PTHREAD_MUTEX_NORMAL", PTHREAD_MUTEX_NORMAL); runTest("PTHREAD_MUTEX_ERRORCHECK", PTHREAD_MUTEX_ERRORCHECK); runTest("PTHREAD_MUTEX_RECURSIVE", PTHREAD_MUTEX_RECURSIVE); #else runTest("Non-blocking lock", 0); #endif printf( ".............................................................................\n"); pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST); #ifdef __PTW32_MUTEX_TYPES runTest("PTHREAD_MUTEX_DEFAULT (Robust)", PTHREAD_MUTEX_DEFAULT); runTest("PTHREAD_MUTEX_NORMAL (Robust)", PTHREAD_MUTEX_NORMAL); runTest("PTHREAD_MUTEX_ERRORCHECK (Robust)", PTHREAD_MUTEX_ERRORCHECK); runTest("PTHREAD_MUTEX_RECURSIVE (Robust)", PTHREAD_MUTEX_RECURSIVE); #else runTest("Non-blocking lock", 0); #endif printf( "=============================================================================\n"); /* * End of tests. */ pthread_mutexattr_destroy(&ma); return 0; }
int main (int argc, char *argv[]) { int i = 0; CRITICAL_SECTION cs; old_mutex_t ox; pthread_mutexattr_init(&ma); printf( "=============================================================================\n"); printf( "\nLock plus unlock on an unlocked mutex.\n%ld iterations\n\n", ITERATIONS); printf( "%-45s %15s %15s\n", "Test", "Total(msec)", "average(usec)"); printf( "-----------------------------------------------------------------------------\n"); /* * Time the loop overhead so we can subtract it from the actual test times. */ TESTSTART assert(1 == one); assert(1 == one); TESTSTOP durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; overHeadMilliSecs = durationMilliSecs; TESTSTART assert((dummy_call(&i), 1) == one); assert((dummy_call(&i), 1) == one); TESTSTOP durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Dummy call x 2", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); TESTSTART assert((interlocked_inc_with_conditionals(&i), 1) == one); assert((interlocked_dec_with_conditionals(&i), 1) == one); TESTSTOP durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Dummy call -> Interlocked with cond x 2", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); TESTSTART assert((InterlockedIncrement(&i), 1) == one); assert((InterlockedDecrement(&i), 1) == one); TESTSTOP durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "InterlockedOp x 2", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); InitializeCriticalSection(&cs); TESTSTART assert((EnterCriticalSection(&cs), 1) == one); assert((LeaveCriticalSection(&cs), 1) == one); TESTSTOP DeleteCriticalSection(&cs); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Simple Critical Section", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); old_mutex_use = OLD_WIN32CS; assert(old_mutex_init(&ox, NULL) == 0); TESTSTART assert(old_mutex_lock(&ox) == zero); assert(old_mutex_unlock(&ox) == zero); TESTSTOP assert(old_mutex_destroy(&ox) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Critical Section (WNT)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); old_mutex_use = OLD_WIN32MUTEX; assert(old_mutex_init(&ox, NULL) == 0); TESTSTART assert(old_mutex_lock(&ox) == zero); assert(old_mutex_unlock(&ox) == zero); TESTSTOP assert(old_mutex_destroy(&ox) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Win32 Mutex (W9x)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS); printf( ".............................................................................\n"); /* * Now we can start the actual tests */ #ifdef PTW32_MUTEX_TYPES runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT); runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL); runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK); runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE); #else runTest("Non-blocking lock", 0); #endif printf( "=============================================================================\n"); /* * End of tests. */ pthread_mutexattr_destroy(&ma); return 0; }
int main (int argc, char *argv[]) { assert(pthread_mutexattr_init(&ma) == 0); printf( "=============================================================================\n"); printf( "\nLock plus unlock on a locked mutex.\n"); printf("%ld iterations, four locks/unlocks per iteration.\n\n", ITERATIONS); printf( "%-45s %15s %15s\n", "Test", "Total(msec)", "average(usec)"); printf( "-----------------------------------------------------------------------------\n"); /* * Time the loop overhead so we can subtract it from the actual test times. */ running = 1; assert(pthread_create(&worker, NULL, overheadThread, NULL) == 0); TESTSTART sched_yield(); sched_yield(); TESTSTOP running = 0; assert(pthread_join(worker, NULL) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; overHeadMilliSecs = durationMilliSecs; InitializeCriticalSection(&cs1); InitializeCriticalSection(&cs2); EnterCriticalSection(&cs1); EnterCriticalSection(&cs2); running = 1; assert(pthread_create(&worker, NULL, CSThread, NULL) == 0); TESTSTART LeaveCriticalSection(&cs1); sched_yield(); LeaveCriticalSection(&cs2); EnterCriticalSection(&cs1); EnterCriticalSection(&cs2); TESTSTOP running = 0; LeaveCriticalSection(&cs2); LeaveCriticalSection(&cs1); assert(pthread_join(worker, NULL) == 0); DeleteCriticalSection(&cs2); DeleteCriticalSection(&cs1); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Simple Critical Section", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS / 4 ); old_mutex_use = OLD_WIN32CS; assert(old_mutex_init(&ox1, NULL) == 0); assert(old_mutex_init(&ox2, NULL) == 0); assert(old_mutex_lock(&ox1) == 0); assert(old_mutex_lock(&ox2) == 0); running = 1; assert(pthread_create(&worker, NULL, oldThread, NULL) == 0); TESTSTART (void) old_mutex_unlock(&ox1); sched_yield(); (void) old_mutex_unlock(&ox2); (void) old_mutex_lock(&ox1); (void) old_mutex_lock(&ox2); TESTSTOP running = 0; assert(old_mutex_unlock(&ox1) == 0); assert(old_mutex_unlock(&ox2) == 0); assert(pthread_join(worker, NULL) == 0); assert(old_mutex_destroy(&ox2) == 0); assert(old_mutex_destroy(&ox1) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Critical Section (WNT)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS / 4); old_mutex_use = OLD_WIN32MUTEX; assert(old_mutex_init(&ox1, NULL) == 0); assert(old_mutex_init(&ox2, NULL) == 0); assert(old_mutex_lock(&ox1) == 0); assert(old_mutex_lock(&ox2) == 0); running = 1; assert(pthread_create(&worker, NULL, oldThread, NULL) == 0); TESTSTART (void) old_mutex_unlock(&ox1); sched_yield(); (void) old_mutex_unlock(&ox2); (void) old_mutex_lock(&ox1); (void) old_mutex_lock(&ox2); TESTSTOP running = 0; assert(old_mutex_unlock(&ox1) == 0); assert(old_mutex_unlock(&ox2) == 0); assert(pthread_join(worker, NULL) == 0); assert(old_mutex_destroy(&ox2) == 0); assert(old_mutex_destroy(&ox1) == 0); durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; printf( "%-45s %15ld %15.3f\n", "Old PT Mutex using a Win32 Mutex (W9x)", durationMilliSecs, (float) durationMilliSecs * 1E3 / ITERATIONS / 4); printf( ".............................................................................\n"); /* * Now we can start the actual tests */ #ifdef PTW32_MUTEX_TYPES runTest("PTHREAD_MUTEX_DEFAULT (W9x,WNT)", PTHREAD_MUTEX_DEFAULT); runTest("PTHREAD_MUTEX_NORMAL (W9x,WNT)", PTHREAD_MUTEX_NORMAL); runTest("PTHREAD_MUTEX_ERRORCHECK (W9x,WNT)", PTHREAD_MUTEX_ERRORCHECK); runTest("PTHREAD_MUTEX_RECURSIVE (W9x,WNT)", PTHREAD_MUTEX_RECURSIVE); #else runTest("Blocking locks", 0); #endif printf( "=============================================================================\n"); /* * End of tests. */ pthread_mutexattr_destroy(&ma); return 0; }