From f8af93c39f8deebc46aee1b25be9d5c40035d0d8 Mon Sep 17 00:00:00 2001 From: rpj Date: Sun, 14 Mar 1999 05:29:18 +0000 Subject: Mon Mar 15 00:20:13 1999 Ross Johnson * condvar.c (pthread_cond_init): fix possible uninitialised use of cv. Sun Mar 14 21:01:59 1999 Ross Johnson * condvar.c (pthread_cond_destroy): don't do full cleanup if static initialised cv has never been used. (cond_timedwait): check result of auto-initialisation. tests/ChangeLog Mon Mar 15 00:17:55 1999 Ross Johnson * mutex1.c: only test mutex init and destroy; add assertions. * count1.c: raise number of spawned threads to 60 (appears to be the limit under Win98). Sun Mar 14 21:31:02 1999 Ross Johnson * test.h (assert): add assertion trace option. Use: "#define ASSERT_TRACE 1" to turn it on, "#define ASSERT_TRACE 0" to turn it off (default). * condvar3.c (main): add more assertions. * condvar4.c (main): add more assertions. * condvar1.c (main): add more assertions. --- ChangeLog | 11 +++++++++++ condvar.c | 31 ++++++++++++++++++++----------- tests/ChangeLog | 22 ++++++++++++++++++++++ tests/Makefile | 28 +++++++++++++++++++++++++--- tests/condvar1.c | 8 +++++++- tests/condvar2.c | 3 +++ tests/condvar3.c | 14 ++++++++++---- tests/condvar4.c | 31 +++++++++++++++++++++++++++---- tests/count1.c | 20 ++++++++------------ tests/mutex1.c | 12 +++++++----- tests/mutex2.c | 4 ++++ tests/test.h | 14 +++++++++++--- 12 files changed, 155 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6f63ee6..d889525 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Mon Mar 15 00:20:13 1999 Ross Johnson + + * condvar.c (pthread_cond_init): fix possible uninitialised use + of cv. + +Sun Mar 14 21:01:59 1999 Ross Johnson + + * condvar.c (pthread_cond_destroy): don't do full cleanup if + static initialised cv has never been used. + (cond_timedwait): check result of auto-initialisation. + Thu Mar 11 09:01:48 1999 Ross Johnson * pthread.h (pthread_mutex_t): revert to (pthread_mutex_t *); diff --git a/condvar.c b/condvar.c index 80dc1c4..ce46ebf 100644 --- a/condvar.c +++ b/condvar.c @@ -327,7 +327,7 @@ pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr) */ { int result = EAGAIN; - pthread_cond_t cv; + pthread_cond_t cv = NULL; if (cond == NULL) { @@ -443,16 +443,20 @@ pthread_cond_destroy (pthread_cond_t * cond) cv = *cond; - if (cv->waiters > 0) + if (cv != (pthread_cond_t) _PTHREAD_OBJECT_AUTO_INIT) { - return EBUSY; - } + if (cv->waiters > 0) + { + return EBUSY; + } - (void) _pthread_sem_destroy (&(cv->sema)); - (void) pthread_mutex_destroy (&(cv->waitersLock)); - (void) CloseHandle (cv->waitersDone); + (void) _pthread_sem_destroy (&(cv->sema)); + (void) pthread_mutex_destroy (&(cv->waitersLock)); + (void) CloseHandle (cv->waitersDone); + + free(cv); + } - free(cv); *cond = NULL; return (result); @@ -473,19 +477,24 @@ cond_timedwait (pthread_cond_t * cond, return EINVAL; } - cv = *cond; - /* * We do a quick check to see if we need to do more work * to initialise a static condition variable. We check * again inside the guarded section of _cond_check_need_init() * to avoid race conditions. */ - if (cv == (pthread_cond_t) _PTHREAD_OBJECT_AUTO_INIT) + if (*cond == (pthread_cond_t) _PTHREAD_OBJECT_AUTO_INIT) { result = _cond_check_need_init(cond); } + if (result != 0 && result != EBUSY) + { + return result; + } + + cv = *cond; + /* * OK to increment cond->waiters because the caller locked 'mutex' */ diff --git a/tests/ChangeLog b/tests/ChangeLog index 6f540b0..16af11f 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,5 +1,27 @@ +Mon Mar 15 00:17:55 1999 Ross Johnson + + * mutex1.c: only test mutex init and destroy; add assertions. + + * count1.c: raise number of spawned threads to 60 (appears to + be the limit under Win98). + +Sun Mar 14 21:31:02 1999 Ross Johnson + + * test.h (assert): add assertion trace option. + Use: + "#define ASSERT_TRACE 1" to turn it on, + "#define ASSERT_TRACE 0" to turn it off (default). + + * condvar3.c (main): add more assertions. + + * condvar4.c (main): add more assertions. + + * condvar1.c (main): add more assertions. + Fri Mar 12 08:34:15 1999 Ross Johnson + * condvar4.c (cvthing): switch the order of the INITIALIZERs. + * eyal1.c (main): Fix trylock loop; was not waiting for thread to lock the "started" mutex. diff --git a/tests/Makefile b/tests/Makefile index e5994f6..430eed2 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -33,15 +33,37 @@ DLL = pthread.dll # If a test case returns a non-zero exit code to the shell, make will # stop. -TESTS = count1 create1 equal1 exit1 exit2 exit3 \ - join1 mutex1 mutex2 mutex3 \ - once1 self1 self2 condvar1 condvar2 condvar3 condvar4 tsd1 +TESTS = mutex1 condvar1 condvar2 exit1 create1 equal1 \ + exit2 exit3 \ + join1 mutex2 mutex3 \ + count1 once1 tsd1 self1 self2 eyal1 \ + condvar3 condvar4 PASSES = $(TESTS:%=%.pass) all: $(PASSES) @ $(ECHO) ALL TESTS PASSED! Congratulations! +mutex1.pass: +mutex2.pass: +exit1.pass: +condvar1.pass: +self1.pass: +condvar2.pass: condvar1.pass +create1.pass: mutex2.pass +mutex3.pass: create1.pass +equal1.pass: create1.pass +exit2.pass: create1.pass +exit3.pass: create1.pass +join1.pass: create1.pass +count1.pass: join1.pass +once1.pass: create1.pass +tsd1.pass: join1.pass +self2.pass: create1.pass +eyal1.pass: tsd1.pass +condvar3.pass: create1.pass +condvar4.pass: create1.pass + %.pass: %.exe $(LIB) $(DLL) $(HDR) $* @$(ECHO) Passed diff --git a/tests/condvar1.c b/tests/condvar1.c index 97c870b..daa0f42 100644 --- a/tests/condvar1.c +++ b/tests/condvar1.c @@ -46,14 +46,20 @@ #include "test.h" -pthread_cond_t cv; +static pthread_cond_t cv = NULL; int main() { + assert(cv == NULL); + assert(pthread_cond_init(&cv, NULL) == 0); + assert(cv != NULL); + assert(pthread_cond_destroy(&cv) == 0); + assert(cv == NULL); + return 0; } diff --git a/tests/condvar2.c b/tests/condvar2.c index 09e4576..b636a62 100644 --- a/tests/condvar2.c +++ b/tests/condvar2.c @@ -80,3 +80,6 @@ main() return 0; } + + + diff --git a/tests/condvar3.c b/tests/condvar3.c index 27223ab..c2b08e6 100644 --- a/tests/condvar3.c +++ b/tests/condvar3.c @@ -48,8 +48,9 @@ #include "test.h" #include -pthread_cond_t cv; -pthread_mutex_t mutex; +static pthread_cond_t cv; +static pthread_mutex_t mutex; +static int shared = 0; enum { NUMTHREADS = 2 /* Including the primary thread. */ @@ -62,10 +63,12 @@ mythread(void * arg) assert(pthread_mutex_lock(&mutex) == 0); - assert(pthread_cond_signal(&cv) == 0); + shared++; assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_cond_signal(&cv) == 0); + return 0; } @@ -99,7 +102,10 @@ main() abstime.tv_sec += 5; - assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == 0); + while (! shared > 0) + assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == 0); + + assert(shared > 0); assert(pthread_mutex_unlock(&mutex) == 0); diff --git a/tests/condvar4.c b/tests/condvar4.c index f93db27..ecb5e2d 100644 --- a/tests/condvar4.c +++ b/tests/condvar4.c @@ -1,5 +1,5 @@ /* - * File: condvar1.c + * File: condvar4.c * * Test Synopsis: * - Test PTHREAD_COND_INITIALIZER. @@ -50,11 +50,13 @@ typedef struct cvthing_t_ cvthing_t; struct cvthing_t_ { pthread_cond_t notbusy; pthread_mutex_t lock; + int shared; }; static cvthing_t cvthing = { - PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + 0 }; enum { @@ -66,10 +68,12 @@ mythread(void * arg) { assert(pthread_mutex_lock(&cvthing.lock) == 0); - assert(pthread_cond_signal(&cvthing.notbusy) == 0); + cvthing.shared++; assert(pthread_mutex_unlock(&cvthing.lock) == 0); + assert(pthread_cond_signal(&cvthing.notbusy) == 0); + return 0; } @@ -85,10 +89,18 @@ main() #endif const DWORD NANOSEC_PER_MILLISEC = 1000000; + cvthing.shared = 0; + assert((t[0] = pthread_self()) != NULL); + assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER); + + assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER); + assert(pthread_mutex_lock(&cvthing.lock) == 0); + assert(cvthing.lock != PTHREAD_MUTEX_INITIALIZER); + /* get current system time */ _ftime(&currSysTime); @@ -99,6 +111,8 @@ main() assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == ETIMEDOUT); + assert(cvthing.notbusy != PTHREAD_COND_INITIALIZER); + assert(pthread_create(&t[1], NULL, mythread, (void *) 1) == 0); _ftime(&currSysTime); @@ -108,11 +122,20 @@ main() abstime.tv_sec += 5; - assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0); + while (! cvthing.shared > 0) + assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0); + + assert(cvthing.shared > 0); assert(pthread_mutex_unlock(&cvthing.lock) == 0); + assert(pthread_mutex_destroy(&cvthing.lock) == 0); + + assert(cvthing.lock == NULL); + assert(pthread_cond_destroy(&cvthing.notbusy) == 0); + assert(cvthing.notbusy == NULL); + return 0; } diff --git a/tests/count1.c b/tests/count1.c index 61e82e0..9783fcc 100644 --- a/tests/count1.c +++ b/tests/count1.c @@ -1,17 +1,17 @@ /* * count1.c * - * Written by Ben Elliston . - * * Description: * Test some basic assertions about the number of threads at runtime. */ #include "test.h" +#define NUMTHREADS (60) + static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_t threads[10]; -static unsigned numThreads = 1; +static pthread_t threads[NUMTHREADS]; +static unsigned numThreads = 0; void * myfunc(void *arg) @@ -30,9 +30,8 @@ main() int maxThreads = sizeof(threads) / sizeof(pthread_t); /* - * Spawn ten threads. Each thread should increment the numThreads - * variable, sleep for one second, decrement the variable and then - * exit. The final result of numThreads should be 1 again. + * Spawn NUMTHREADS threads. Each thread should increment the + * numThreads variable, sleep for one second. */ for (i = 0; i < maxThreads; i++) { @@ -45,15 +44,12 @@ main() for (i = 0; i < maxThreads; i++) { assert(pthread_join(threads[i], NULL) == 0); - assert(pthread_mutex_lock(&lock) == 0); - numThreads--; - assert(pthread_mutex_unlock(&lock) == 0); } /* - * Check the number of live threads. + * Check the number of threads created. */ - assert(numThreads == 1); + assert(numThreads == maxThreads); /* * Success. diff --git a/tests/mutex1.c b/tests/mutex1.c index 60bb4e7..c997963 100644 --- a/tests/mutex1.c +++ b/tests/mutex1.c @@ -13,18 +13,20 @@ #include "test.h" -pthread_mutex_t mutex1; +pthread_mutex_t mutex = NULL; int main() { - assert(pthread_mutex_init(&mutex1, NULL) == 0); + assert(mutex == NULL); - assert(pthread_mutex_lock(&mutex1) == 0); + assert(pthread_mutex_init(&mutex, NULL) == 0); - assert(pthread_mutex_unlock(&mutex1) == 0); + assert(mutex != NULL); - assert(pthread_mutex_destroy(&mutex1) == 0); + assert(pthread_mutex_destroy(&mutex) == 0); + + assert(mutex == NULL); return 0; } diff --git a/tests/mutex2.c b/tests/mutex2.c index b161899..731c47f 100644 --- a/tests/mutex2.c +++ b/tests/mutex2.c @@ -26,5 +26,9 @@ main() assert(pthread_mutex_unlock(&mutex) == 0); + assert(pthread_mutex_destroy(&mutex) == 0); + + assert(mutex == NULL); + return 0; } diff --git a/tests/test.h b/tests/test.h index 23bcd48..36dc397 100644 --- a/tests/test.h +++ b/tests/test.h @@ -71,10 +71,18 @@ char * error_string[] = { # undef assert #endif +#ifndef ASSERT_TRACE +#define ASSERT_TRACE 0 +#endif + #define assert(e) \ - ((e) ? (void) 0 : \ - (fprintf(stderr, "Assertion failed: (%s), file %s, line %d\n", \ - #e, __FILE__, (int) __LINE__), exit(1))) + ((e) ? ((ASSERT_TRACE) ? fprintf(stderr, \ + "Assertion succeeded: (%s), file %s, line %d\n", \ + #e, __FILE__, (int) __LINE__), \ + fflush(stderr) : \ + (void) 0) : \ + (fprintf(stderr, "Assertion failed: (%s), file %s, line %d\n", \ + #e, __FILE__, (int) __LINE__), exit(1))) #endif /* NDEBUG */ -- cgit v1.2.3