From 6e1c5f6a98f24308e3c9fa9f01101de6dd810153 Mon Sep 17 00:00:00 2001 From: rpj Date: Wed, 6 Apr 2005 02:16:29 +0000 Subject: fix unguarded global variable access --- tests/cleanup0.c | 19 +++++++++++++++---- tests/cleanup1.c | 19 +++++++++++++++---- tests/cleanup2.c | 19 +++++++++++++++---- tests/cleanup3.c | 23 ++++++++++++++++++----- tests/condvar6.c | 2 +- tests/once2.c | 27 +++++++++++++++++++++------ tests/once3.c | 27 +++++++++++++++++++++------ 7 files changed, 106 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/tests/cleanup0.c b/tests/cleanup0.c index 8de8c4b..77626eb 100644 --- a/tests/cleanup0.c +++ b/tests/cleanup0.c @@ -92,14 +92,21 @@ struct bag_t_ { static bag_t threadbag[NUMTHREADS + 1]; -static int pop_count = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t pop_count = {0, {0}}; static void increment_pop_count(void * arg) { - int * c = (int *) arg; + sharedInt_t * sI = (sharedInt_t *) arg; - (*c)++; + EnterCriticalSection(&sI->cs); + sI->i++; + LeaveCriticalSection(&sI->cs); } void * @@ -140,6 +147,8 @@ main() int i; pthread_t t[NUMTHREADS + 1]; + InitializeCriticalSection(&pop_count.cs); + assert((t[0] = pthread_self()).p != NULL); for (i = 1; i <= NUMTHREADS; i++) @@ -199,7 +208,9 @@ main() assert(!failed); - assert(pop_count == NUMTHREADS); + assert(pop_count.i == NUMTHREADS); + + DeleteCriticalSection(&pop_count.cs); /* * Success. diff --git a/tests/cleanup1.c b/tests/cleanup1.c index 65645df..385aed9 100644 --- a/tests/cleanup1.c +++ b/tests/cleanup1.c @@ -92,7 +92,12 @@ struct bag_t_ { static bag_t threadbag[NUMTHREADS + 1]; -static int pop_count = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t pop_count = {0, {0}}; static void #ifdef __CLEANUP_C @@ -100,9 +105,11 @@ __cdecl #endif increment_pop_count(void * arg) { - int * c = (int *) arg; + sharedInt_t * sI = (sharedInt_t *) arg; - (*c)++; + EnterCriticalSection(&sI->cs); + sI->i++; + LeaveCriticalSection(&sI->cs); } void * @@ -149,6 +156,8 @@ main() int i; pthread_t t[NUMTHREADS + 1]; + InitializeCriticalSection(&pop_count.cs); + assert((t[0] = pthread_self()).p != NULL); for (i = 1; i <= NUMTHREADS; i++) @@ -212,7 +221,9 @@ main() assert(!failed); - assert(pop_count == NUMTHREADS); + assert(pop_count.i == NUMTHREADS); + + DeleteCriticalSection(&pop_count.cs); /* * Success. diff --git a/tests/cleanup2.c b/tests/cleanup2.c index 50672fd..4c63918 100644 --- a/tests/cleanup2.c +++ b/tests/cleanup2.c @@ -92,14 +92,21 @@ struct bag_t_ { static bag_t threadbag[NUMTHREADS + 1]; -static int pop_count = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t pop_count = {0, {0}}; static void increment_pop_count(void * arg) { - int * c = (int *) arg; + sharedInt_t * sI = (sharedInt_t *) arg; - (*c)++; + EnterCriticalSection(&sI->cs); + sI->i++; + LeaveCriticalSection(&sI->cs); } void * @@ -134,6 +141,8 @@ main() int i; pthread_t t[NUMTHREADS + 1]; + InitializeCriticalSection(&pop_count.cs); + assert((t[0] = pthread_self()).p != NULL); for (i = 1; i <= NUMTHREADS; i++) @@ -187,7 +196,9 @@ main() assert(!failed); - assert(pop_count == NUMTHREADS); + assert(pop_count.i == NUMTHREADS); + + DeleteCriticalSection(&pop_count.cs); /* * Success. diff --git a/tests/cleanup3.c b/tests/cleanup3.c index a12337d..b595ab4 100644 --- a/tests/cleanup3.c +++ b/tests/cleanup3.c @@ -93,14 +93,21 @@ struct bag_t_ { static bag_t threadbag[NUMTHREADS + 1]; -static int pop_count = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t pop_count = {0, {0}}; static void increment_pop_count(void * arg) { - int * c = (int *) arg; + sharedInt_t * sI = (sharedInt_t *) arg; - (*c)++; + EnterCriticalSection(&sI->cs); + sI->i++; + LeaveCriticalSection(&sI->cs); } void * @@ -120,7 +127,9 @@ mythread(void * arg) sched_yield(); - pop_count--; + EnterCriticalSection(&pop_count.cs); + pop_count.i--; + LeaveCriticalSection(&pop_count.cs); pthread_cleanup_pop(0); #ifdef _MSC_VER @@ -137,6 +146,8 @@ main() int i; pthread_t t[NUMTHREADS + 1]; + InitializeCriticalSection(&pop_count.cs); + assert((t[0] = pthread_self()).p != NULL); for (i = 1; i <= NUMTHREADS; i++) @@ -190,7 +201,9 @@ main() assert(!failed); - assert(pop_count == -(NUMTHREADS)); + assert(pop_count.i == -(NUMTHREADS)); + + DeleteCriticalSection(&pop_count.cs); /* * Success. diff --git a/tests/condvar6.c b/tests/condvar6.c index cebcbd4..fc7a53b 100644 --- a/tests/condvar6.c +++ b/tests/condvar6.c @@ -1,5 +1,5 @@ /* - * File: + * File: condvar6.c * * * -------------------------------------------------------------------------- diff --git a/tests/once2.c b/tests/once2.c index 22881d8..248ccb1 100644 --- a/tests/once2.c +++ b/tests/once2.c @@ -49,13 +49,20 @@ pthread_once_t o = PTHREAD_ONCE_INIT; pthread_once_t once[NUM_ONCE]; -static int numOnce = 0; -static int numThreads = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t numOnce = {0, {0}}; +static sharedInt_t numThreads = {0, {0}}; void myfunc(void) { - numOnce++; + EnterCriticalSection(&numOnce.cs); + numOnce.i++; + LeaveCriticalSection(&numOnce.cs); /* Simulate slow once routine so that following threads pile up behind it */ Sleep(100); } @@ -64,7 +71,9 @@ void * mythread(void * arg) { assert(pthread_once(&once[(int) arg], myfunc) == 0); - numThreads++; + EnterCriticalSection(&numThreads.cs); + numThreads.i++; + LeaveCriticalSection(&numThreads.cs); return 0; } @@ -74,6 +83,9 @@ main() pthread_t t[NUM_THREADS][NUM_ONCE]; int i, j; + InitializeCriticalSection(&numThreads.cs); + InitializeCriticalSection(&numOnce.cs); + for (j = 0; j < NUM_ONCE; j++) { once[j] = o; @@ -87,8 +99,11 @@ main() if (pthread_join(t[i][j], NULL) != 0) printf("Join failed for [thread,once] = [%d,%d]\n", i, j); - assert(numOnce == NUM_ONCE); - assert(numThreads == NUM_THREADS * NUM_ONCE); + assert(numOnce.i == NUM_ONCE); + assert(numThreads.i == NUM_THREADS * NUM_ONCE); + + DeleteCriticalSection(&numOnce.cs); + DeleteCriticalSection(&numThreads.cs); return 0; } diff --git a/tests/once3.c b/tests/once3.c index fabed51..51d2daa 100644 --- a/tests/once3.c +++ b/tests/once3.c @@ -53,13 +53,20 @@ pthread_once_t o = PTHREAD_ONCE_INIT; pthread_once_t once[NUM_ONCE]; -static int numOnce = 0; -static int numThreads = 0; +typedef struct { + int i; + CRITICAL_SECTION cs; +} sharedInt_t; + +static sharedInt_t numOnce = {0, {0}}; +static sharedInt_t numThreads = {0, {0}}; void myfunc(void) { - numOnce++; + EnterCriticalSection(&numOnce.cs); + numOnce.i++; + LeaveCriticalSection(&numOnce.cs); /* Simulate slow once routine so that following threads pile up behind it */ Sleep(10); /* test for cancelation late so we're sure to have waiters. */ @@ -77,7 +84,9 @@ mythread(void * arg) */ pthread_cancel(pthread_self()); assert(pthread_once(&once[(int) arg], myfunc) == 0); - numThreads++; + EnterCriticalSection(&numThreads.cs); + numThreads.i++; + LeaveCriticalSection(&numThreads.cs); return 0; } @@ -87,6 +96,9 @@ main() pthread_t t[NUM_THREADS][NUM_ONCE]; int i, j; + InitializeCriticalSection(&numThreads.cs); + InitializeCriticalSection(&numOnce.cs); + for (j = 0; j < NUM_ONCE; j++) { once[j] = o; @@ -107,8 +119,11 @@ main() * pthread_once and so numThreads should never be incremented. However, * numOnce should be incremented by every thread (NUM_THREADS*NUM_ONCE). */ - assert(numOnce == NUM_ONCE * NUM_THREADS); - assert(numThreads == 0); + assert(numOnce.i == NUM_ONCE * NUM_THREADS); + assert(numThreads.i == 0); + + DeleteCriticalSection(&numOnce.cs); + DeleteCriticalSection(&numThreads.cs); return 0; } -- cgit v1.2.3