From bbae92ba08f8bc5d4d93d45b3e032686270dc99e Mon Sep 17 00:00:00 2001 From: rpj Date: Mon, 25 Apr 2005 00:49:04 +0000 Subject: '' --- tests/benchtest1.c | 497 +++++++++++++++++++++++++++-------------------------- tests/benchtest5.c | 334 +++++++++++++++++------------------ tests/semaphore1.c | 316 +++++++++++++++++----------------- 3 files changed, 567 insertions(+), 580 deletions(-) (limited to 'tests') diff --git a/tests/benchtest1.c b/tests/benchtest1.c index c8f7f41..da0380e 100644 --- a/tests/benchtest1.c +++ b/tests/benchtest1.c @@ -1,248 +1,249 @@ -/* - * benchtest1.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Measure time taken to complete an elementary operation. - * - * - Mutex - * Single thread iteration over lock/unlock for each mutex type. - */ - -#include "test.h" -#include - -#ifdef __GNUC__ -#include -#endif - -#include "benchtest.h" - -#define PTW32_MUTEX_TYPES -#define ITERATIONS 10000000L - -pthread_mutex_t mx; -pthread_mutexattr_t ma; -struct _timeb currSysTimeStart; -struct _timeb currSysTimeStop; -long durationMilliSecs; -long overHeadMilliSecs = 0; -int one = 1; -int zero = 0; - -#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ - - (_TStart.time*1000+_TStart.millitm)) - -/* - * Dummy use of j, otherwise the loop may be removed by the optimiser - * when doing the overhead timing with an empty loop. - */ -#define TESTSTART \ - { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; - -#define TESTSTOP \ - }; _ftime(&currSysTimeStop); if (j + k == i) j++; } - - -void -runTest (char * testNameString, int mType) -{ -#ifdef PTW32_MUTEX_TYPES - assert(pthread_mutexattr_settype(&ma, mType) == 0); -#endif - assert(pthread_mutex_init(&mx, &ma) == 0); - - TESTSTART - assert(pthread_mutex_lock(&mx) == zero); - assert(pthread_mutex_unlock(&mx) == zero); - TESTSTOP - - assert(pthread_mutex_destroy(&mx) == 0); - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - testNameString, - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); -} - - -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; -} +/* + * benchtest1.c + * + * + * -------------------------------------------------------------------------- + * + * Pthreads-win32 - POSIX Threads Library for Win32 + * Copyright(C) 1998 John E. Bossom + * Copyright(C) 1999,2005 Pthreads-win32 contributors + * + * Contact Email: rpj@callisto.canberra.edu.au + * + * The current list of contributors is contained + * in the file CONTRIBUTORS included with the source + * code distribution. The list can also be seen at the + * following World Wide Web location: + * http://sources.redhat.com/pthreads-win32/contributors.html + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library in the file COPYING.LIB; + * if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * -------------------------------------------------------------------------- + * + * Measure time taken to complete an elementary operation. + * + * - Mutex + * Single thread iteration over lock/unlock for each mutex type. + */ + +#include "test.h" +#include + +#ifdef __GNUC__ +#include +#endif + +#include "benchtest.h" + +#define PTW32_MUTEX_TYPES +#define ITERATIONS 10000000L + +pthread_mutex_t mx; +pthread_mutexattr_t ma; +struct _timeb currSysTimeStart; +struct _timeb currSysTimeStop; +long durationMilliSecs; +long overHeadMilliSecs = 0; +int one = 1; +int zero = 0; + +#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ + - (_TStart.time*1000+_TStart.millitm)) + +/* + * Dummy use of j, otherwise the loop may be removed by the optimiser + * when doing the overhead timing with an empty loop. + */ +#define TESTSTART \ + { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; + +#define TESTSTOP \ + }; _ftime(&currSysTimeStop); if (j + k == i) j++; } + + +void +runTest (char * testNameString, int mType) +{ +#ifdef PTW32_MUTEX_TYPES + assert(pthread_mutexattr_settype(&ma, mType) == 0); +#endif + assert(pthread_mutex_init(&mx, &ma) == 0); + + TESTSTART + assert(pthread_mutex_lock(&mx) == zero); + assert(pthread_mutex_unlock(&mx) == zero); + TESTSTOP + + assert(pthread_mutex_destroy(&mx) == 0); + + durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; + + printf( "%-45s %15ld %15.3f\n", + testNameString, + durationMilliSecs, + (float) durationMilliSecs * 1E3 / ITERATIONS); +} + + +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((LPLONG)&i), 1) == (LONG)one); + assert((InterlockedDecrement((LPLONG)&i), 1) == (LONG)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); + + one = i; /* Dummy assignment to avoid 'variable unused' warning */ + return 0; +} diff --git a/tests/benchtest5.c b/tests/benchtest5.c index 0a8a0b2..2fba9e3 100644 --- a/tests/benchtest5.c +++ b/tests/benchtest5.c @@ -1,175 +1,159 @@ -/* - * benchtest5.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Measure time taken to complete an elementary operation. - * - * - Semaphore - * Single thread iteration over post/wait for a semaphore. - */ - -#include "test.h" -#include - -#ifdef __GNUC__ -#include -#endif - -#include "benchtest.h" - -#define ITERATIONS 10000000L - -sem_t sema; - -struct _timeb currSysTimeStart; -struct _timeb currSysTimeStop; -long durationMilliSecs; -long overHeadMilliSecs = 0; -int one = 1; -int zero = 0; - -#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ - - (_TStart.time*1000+_TStart.millitm)) - -/* - * Dummy use of j, otherwise the loop may be removed by the optimiser - * when doing the overhead timing with an empty loop. - */ -#define TESTSTART \ - { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; - -#define TESTSTOP \ - }; _ftime(&currSysTimeStop); if (j + k == i) j++; } - - -void -reportTest (char * testNameString) -{ - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - - printf( "%-45s %15ld %15.3f\n", - testNameString, - durationMilliSecs, - (float) durationMilliSecs * 1E3 / ITERATIONS); -} - - -int -main (int argc, char *argv[]) -{ - printf( "=============================================================================\n"); - printf( "\nOperations on a semaphore.\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); - TESTSTOP - - durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; - overHeadMilliSecs = durationMilliSecs; - - - /* - * Now we can start the actual tests - */ - assert(sem_init(&sema, 0, 0) == 0); - TESTSTART - assert(sem_post(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Post"); - - - assert(sem_init(&sema, 0, ITERATIONS) == 0); - TESTSTART - assert(sem_wait(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Wait without blocking"); - - - /* - * 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; - - - /* - * Now we can start the actual tests - */ - assert(sem_init(&sema, 0, 0) == 0); - TESTSTART - assert(sem_post(&sema) == zero); - assert(sem_wait(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Post then Wait without blocking"); - - - assert(sem_init(&sema, 0, 1) == 0); - TESTSTART - assert(sem_wait(&sema) == zero); - assert(sem_post(&sema) == zero); - TESTSTOP - assert(sem_destroy(&sema) == 0); - - reportTest("Wait then Post without blocking"); - - printf( "=============================================================================\n"); - - /* - * End of tests. - */ - - return 0; -} +/* + * benchtest5.c + * + * + * -------------------------------------------------------------------------- + * + * Pthreads-win32 - POSIX Threads Library for Win32 + * Copyright(C) 1998 John E. Bossom + * Copyright(C) 1999,2005 Pthreads-win32 contributors + * + * Contact Email: rpj@callisto.canberra.edu.au + * + * The current list of contributors is contained + * in the file CONTRIBUTORS included with the source + * code distribution. The list can also be seen at the + * following World Wide Web location: + * http://sources.redhat.com/pthreads-win32/contributors.html + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library in the file COPYING.LIB; + * if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * -------------------------------------------------------------------------- + * + * Measure time taken to complete an elementary operation. + * + * - Semaphore + * Single thread iteration over post/wait for a semaphore. + */ + +#include "test.h" +#include + +#ifdef __GNUC__ +#include +#endif + +#include "benchtest.h" + +#define ITERATIONS 1000000L + +sem_t sema; +HANDLE w32sema; + +struct _timeb currSysTimeStart; +struct _timeb currSysTimeStop; +long durationMilliSecs; +long overHeadMilliSecs = 0; +int one = 1; +int zero = 0; + +#define GetDurationMilliSecs(_TStart, _TStop) ((_TStop.time*1000+_TStop.millitm) \ + - (_TStart.time*1000+_TStart.millitm)) + +/* + * Dummy use of j, otherwise the loop may be removed by the optimiser + * when doing the overhead timing with an empty loop. + */ +#define TESTSTART \ + { int i, j = 0, k = 0; _ftime(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++; + +#define TESTSTOP \ + }; _ftime(&currSysTimeStop); if (j + k == i) j++; } + + +void +reportTest (char * testNameString) +{ + durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; + + printf( "%-45s %15ld %15.3f\n", + testNameString, + durationMilliSecs, + (float) durationMilliSecs * 1E3 / ITERATIONS); +} + + +int +main (int argc, char *argv[]) +{ + printf( "=============================================================================\n"); + printf( "\nOperations on a semaphore.\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); + TESTSTOP + + durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs; + overHeadMilliSecs = durationMilliSecs; + + + /* + * Now we can start the actual tests + */ + assert((w32sema = CreateSemaphore(NULL, (long) 0, (long) ITERATIONS, NULL)) != 0); + TESTSTART + assert(ReleaseSemaphore(w32sema, 1, NULL) != zero); + TESTSTOP + assert(CloseHandle(w32sema) != 0); + + reportTest("W32 Post with no waiters"); + + + assert((w32sema = CreateSemaphore(NULL, (long) ITERATIONS, (long) ITERATIONS, NULL)) != 0); + TESTSTART + assert(WaitForSingleObject(w32sema, INFINITE) == WAIT_OBJECT_0); + TESTSTOP + assert(CloseHandle(w32sema) != 0); + + reportTest("W32 Wait without blocking"); + + + assert(sem_init(&sema, 0, 0) == 0); + TESTSTART + assert(sem_post(&sema) == zero); + TESTSTOP + assert(sem_destroy(&sema) == 0); + + reportTest("POSIX Post with no waiters"); + + + assert(sem_init(&sema, 0, ITERATIONS) == 0); + TESTSTART + assert(sem_wait(&sema) == zero); + TESTSTOP + assert(sem_destroy(&sema) == 0); + + reportTest("POSIX Wait without blocking"); + + + printf( "=============================================================================\n"); + + /* + * End of tests. + */ + + return 0; +} diff --git a/tests/semaphore1.c b/tests/semaphore1.c index b11de0d..8ebf365 100644 --- a/tests/semaphore1.c +++ b/tests/semaphore1.c @@ -1,157 +1,159 @@ -/* - * File: semaphore1.c - * - * - * -------------------------------------------------------------------------- - * - * Pthreads-win32 - POSIX Threads Library for Win32 - * Copyright(C) 1998 John E. Bossom - * Copyright(C) 1999,2005 Pthreads-win32 contributors - * - * Contact Email: rpj@callisto.canberra.edu.au - * - * The current list of contributors is contained - * in the file CONTRIBUTORS included with the source - * code distribution. The list can also be seen at the - * following World Wide Web location: - * http://sources.redhat.com/pthreads-win32/contributors.html - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library in the file COPYING.LIB; - * if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - * - * -------------------------------------------------------------------------- - * - * Test Synopsis: Verify trywait() returns -1 and sets EAGAIN. - * - - * - * Test Method (Validation or Falsification): - * - Validation - * - * Requirements Tested: - * - - * - * Features Tested: - * - - * - * Cases Tested: - * - - * - * Description: - * - - * - * Environment: - * - - * - * Input: - * - None. - * - * Output: - * - File name, Line number, and failed expression on failure. - * - No output on success. - * - * Assumptions: - * - - * - * Pass Criteria: - * - Process returns zero exit status. - * - * Fail Criteria: - * - Process returns non-zero exit status. - */ - -#include "test.h" - -void * -thr(void * arg) -{ - sem_t s; - int result; - - assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); - - assert((result = sem_trywait(&s)) == -1); - - if ( result == -1 ) - { - perror("thread: sem_trywait 1: expected error"); // No error - assert(errno == EAGAIN); - } - else - { - printf("thread: ok 1\n"); - } - - assert((result = sem_post(&s)) == 0); - - assert((result = sem_trywait(&s)) == 0); - - if ( result == -1 ) - { - perror("thread: sem_trywait 2"); - } - else - { - printf("thread: ok 2\n"); - } - - assert(sem_post(&s) == 0); - - return 0; -} - - -int -main() -{ - pthread_t t; - sem_t s; - int result; - - assert(pthread_create(&t, NULL, thr, NULL) == 0); - assert(pthread_join(t, (void **)&result) == 0); - assert(result == 0); - - assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); - - assert((result = sem_trywait(&s)) == -1); - - if ( result == -1 ) - { - perror("main: sem_trywait 1: expected error"); // No error - assert(errno == EAGAIN); - } - else - { - printf("main: ok 1\n"); - } - - assert((result = sem_post(&s)) == 0); - - assert((result = sem_trywait(&s)) == 0); - - if ( result == -1 ) - { - perror("main: sem_trywait 2"); - } - else - { - printf("main: ok 2\n"); - } - - assert(sem_post(&s) == 0); - - return 0; -} - +/* + * File: semaphore1.c + * + * + * -------------------------------------------------------------------------- + * + * Pthreads-win32 - POSIX Threads Library for Win32 + * Copyright(C) 1998 John E. Bossom + * Copyright(C) 1999,2005 Pthreads-win32 contributors + * + * Contact Email: rpj@callisto.canberra.edu.au + * + * The current list of contributors is contained + * in the file CONTRIBUTORS included with the source + * code distribution. The list can also be seen at the + * following World Wide Web location: + * http://sources.redhat.com/pthreads-win32/contributors.html + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library in the file COPYING.LIB; + * if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + * + * -------------------------------------------------------------------------- + * + * Test Synopsis: Verify trywait() returns -1 and sets EAGAIN. + * - + * + * Test Method (Validation or Falsification): + * - Validation + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - None. + * + * Output: + * - File name, Line number, and failed expression on failure. + * - No output on success. + * + * Assumptions: + * - + * + * Pass Criteria: + * - Process returns zero exit status. + * + * Fail Criteria: + * - Process returns non-zero exit status. + */ + +#include "test.h" + +void * +thr(void * arg) +{ + sem_t s; + int result; + + assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); + + assert((result = sem_trywait(&s)) == -1); + + if ( result == -1 ) + { + int err = errno; + perror("thread: sem_trywait 1: expected error"); // No error + assert(err == EAGAIN); + } + else + { + printf("thread: ok 1\n"); + } + + assert((result = sem_post(&s)) == 0); + + assert((result = sem_trywait(&s)) == 0); + + if ( result == -1 ) + { + perror("thread: sem_trywait 2"); + } + else + { + printf("thread: ok 2\n"); + } + + assert(sem_post(&s) == 0); + + return 0; +} + + +int +main() +{ + pthread_t t; + sem_t s; + int result; + + assert(pthread_create(&t, NULL, thr, NULL) == 0); + assert(pthread_join(t, (void **)&result) == 0); + assert(result == 0); + + assert(sem_init(&s, PTHREAD_PROCESS_PRIVATE, 0) == 0); + + assert((result = sem_trywait(&s)) == -1); + + if ( result == -1 ) + { + int err = errno; + perror("main: sem_trywait 1: expected error"); // No error + assert(err == EAGAIN); + } + else + { + printf("main: ok 1\n"); + } + + assert((result = sem_post(&s)) == 0); + + assert((result = sem_trywait(&s)) == 0); + + if ( result == -1 ) + { + perror("main: sem_trywait 2"); + } + else + { + printf("main: ok 2\n"); + } + + assert(sem_post(&s) == 0); + + return 0; +} + -- cgit v1.2.3