From 346e0db637f3b0be52e1b7782cffa5ee0f2b09e8 Mon Sep 17 00:00:00 2001 From: rpj Date: Mon, 22 Feb 1999 16:51:52 +0000 Subject: 1999-02-23 Ross Johnson * Template.c: Revamp. * condvar1.c: Add. * condvar2.c: Add. * Makefile: Add condvar1 condvar2 tests. * exit1.c, exit2.c, exit3.c: Cosmetic changes. --- tests/ChangeLog | 10 +++++ tests/Makefile | 4 +- tests/Template.c | 79 ++++++++++++++++++++++++++++++------ tests/condvar1.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/condvar2.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/exit1.c | 6 +-- tests/exit2.c | 4 +- tests/exit3.c | 4 +- 8 files changed, 322 insertions(+), 21 deletions(-) create mode 100644 tests/condvar1.c create mode 100644 tests/condvar2.c (limited to 'tests') diff --git a/tests/ChangeLog b/tests/ChangeLog index ecbaa2a..c9542cb 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,13 @@ +1999-02-23 Ross Johnson + + * Template.c: Revamp. + + * condvar1.c: Add. + + * condvar2.c: Add. + + * various: Cosmetic changes. + 1999-02-23 Ross Johnson * Makefile: Some refinement. diff --git a/tests/Makefile b/tests/Makefile index 4fef3c3..12e9954 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,7 +13,7 @@ ECHO = @echo # CC = gcc CFLAGS = -g -O2 -UNDEBUG -Wall -o $@ $^ -BUILD_DIR = ..\build +BUILD_DIR = .. INCLUDES = -I./include LIBS = ./lib/libpthread32.a @@ -35,7 +35,7 @@ DLL = pthread.dll TESTS = count1 create1 equal1 exit1 exit2 exit3 \ join1 eyal1 mutex1 mutex2 mutex3 \ - once1 self1 self2 tsd1 + once1 self1 self2 condvar1 condvar2 tsd1 PASSES = $(TESTS:%=%.pass) diff --git a/tests/Template.c b/tests/Template.c index 50a81bf..470003d 100644 --- a/tests/Template.c +++ b/tests/Template.c @@ -38,31 +38,84 @@ * - */ -#include -#include +#include "test.h" -pthread_key_t key; -pthread_once_t key_once = PTHREAD_ONCE_INIT; +/* + * Create NUMTHREADS threads in addition to the Main thread. + */ +static enum { + NUMTHREADS = 2 +}; + +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int washere; + /* Add more pre-thread state variables here */ +}; + +static bag_t threadbag[NUMTHREADS]; void * mythread(void * arg) { + bag_t * bag = (bag_t *) arg; + + assert(bag == &threadbag[bag->threadnum]); + assert(bag->washere == 0); + bag->washere = 1; + + /* ... */ + return 0; } int main() { - int rc; - pthread_t t1, t2; - - rc = pthread_create(&t1, NULL, mythread, (void *) 1); - printf("pthread_create returned %d\n", rc); + int failed = 0; + pthread_t t[NUMTHREADS + 1]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].washere = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); + } + + /* + * Give threads time to run. + */ + Sleep(NUMTHREADS * 1000); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + if (threadbag[i].washere != 1) + { + failed = 1; + fprintf(stderr, "Thread %d: washere %d\n", i, threadbag[i].washere); + } + } + + assert(failed == 0); + + /* + * Check any results here. Only print ouput and set "failed" on failure. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + /* ... */ + } - rc = pthread_create(&t2, NULL, mythread, (void *) 2); - printf("pthread_create returned %d\n", rc); + assert(failed == 0); - /* Give threads time to run. */ - Sleep(2000); + /* + * Success. + */ return 0; } diff --git a/tests/condvar1.c b/tests/condvar1.c new file mode 100644 index 0000000..4c52651 --- /dev/null +++ b/tests/condvar1.c @@ -0,0 +1,121 @@ +/* + * File: condvar1.c + * + * Test Synopsis: + * - Test basic function of condition variable code. + * + * Test Method (Validation or Falsification): + * - Validation + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - + * + * Output: + * - + * + * Assumptions: + * - + * + * Pass Criteria: + * - + * + * Fail Criteria: + * - + */ + +#include "test.h" + +typedef struct cvthing_t_ cvthing_t; + +struct cvthing_t_ { + pthread_cond_t notbusy; + pthread_mutex_t lock; + int busy; + int count; +}; + +static cvthing_t cvthing; + +static enum { + NUMTHREADS = 10 +}; + +static pthread_key_t key; + +void * +mythread(void * arg) +{ + assert(pthread_mutex_lock(&cvthing.lock) == 0); + + cvthing.count++; + + while (cvthing.busy) + { + assert(pthread_cond_wait(&cvthing.notbusy, &cvthing.lock) == 0); + } + + assert(cvthing.busy == 0); + + cvthing.count--; + + assert(pthread_mutex_unlock(&cvthing.lock) == 0); + + return 0; +} + +int +main() +{ + pthread_t t[NUMTHREADS]; + int result[NUMTHREADS]; + + assert((t[0] = pthread_self()) != NULL); + + assert(pthread_cond_init(&cvthing, NULL) == 0); + + for (i = 1; i < NUMTHREADS; i++) + { + assert(pthread_create(&t[i], NULL, mythread, (void *) i) == 0); + } + + while (cvthing.count < NUMTHREADS) + {} + + assert(pthread_mutex_lock(&cvthing.lock) == 0); + cvthing.busy = 0; + assert(pthread_cond_signal(&cvthing.notbusy) == 0); + assert(pthread_mutex_unlock(&cvthing.lock) == 0); + + for (i = 1; i < NUMTHREADS; i++) + { + assert(pthread_join(t[i], (void *) &result[i]) == 0); + } + + assert(cvthing.count == 0); + + assert(pthread_cond_destroy(&cvthing) == 0); + + return 0; +} + + + + + + + diff --git a/tests/condvar2.c b/tests/condvar2.c new file mode 100644 index 0000000..14097ed --- /dev/null +++ b/tests/condvar2.c @@ -0,0 +1,115 @@ +/* + * File: condvar1.c + * + * Test Synopsis: + * - Test basic function of condition variable code. + * + * Test Method (Validation or Falsification): + * - Validation + * + * Requirements Tested: + * - + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - + * + * Output: + * - + * + * Assumptions: + * - + * + * Pass Criteria: + * - + * + * Fail Criteria: + * - + */ + +#include "test.h" + +typedef struct cvthing_t_ cvthing_t; + +struct cvthing_t_ { + pthread_cond_t notbusy; + pthread_mutex_t lock; + int busy; + int count; +}; + +static enum { + NUMTHREADS = 10 +}; + +static pthread_key_t key; + +static cvthing_t cvthing = { + PTHREAD_COND_INITIALIZER, + PTHREAD_MUTEX_INITIALIZER, + 1, + 0 +}; + +void * +mythread(void * arg) +{ + assert(pthread_mutex_lock(&cvthing.lock) == 0); + + cvthing.count++; + + while (cvthing.busy) + { + assert(pthread_cond_wait(&cvthing.notbusy, &cvthing.lock) == 0); + } + + assert(cvthing.busy == 0); + + cvthing.count--; + + assert(pthread_mutex_unlock(&cvthing.lock) == 0); + + return 0; +} + +int +main() +{ + pthread_t t[NUMTHREADS]; + int result[NUMTHREADS]; + + assert((t[0] = pthread_self()) != NULL); + + for (i = 1; i < NUMTHREADS; i++) + { + assert(pthread_create(&t[i], NULL, mythread, (void *) i) == 0); + } + + while (cvthing.count < NUMTHREADS) + {} + + assert(pthread_mutex_lock(&cvthing.lock) == 0); + cvthing.busy = 0; + assert(pthread_cond_signal(&cvthing.notbusy) == 0); + assert(pthread_mutex_unlock(&cvthing.lock) == 0); + + for (i = 1; i < NUMTHREADS; i++) + { + assert(pthread_join(t[i], (void *) &result[i]) == 0); + } + + assert(cvthing.count == 0); + + return 0; +} diff --git a/tests/exit1.c b/tests/exit1.c index 758852a..251a46c 100644 --- a/tests/exit1.c +++ b/tests/exit1.c @@ -4,14 +4,14 @@ * Depends on API functions: None. */ -#include +#include "test.h" int main(int argc, char * argv[]) { - /* A simple test firstly. */ + /* A simple test first. */ pthread_exit((void *) 0); /* Not reached */ - return 1; + assert(0); } diff --git a/tests/exit2.c b/tests/exit2.c index 786e4aa..684305b 100644 --- a/tests/exit2.c +++ b/tests/exit2.c @@ -14,7 +14,7 @@ func(void * arg) pthread_exit(arg); /* Never reached. */ - exit(1); + assert(0); } int @@ -24,7 +24,7 @@ main(int argc, char * argv[]) assert(pthread_create(&t, NULL, func, (void *) NULL) == 0); - Sleep(2000); + Sleep(1000); return 0; } diff --git a/tests/exit3.c b/tests/exit3.c index b666624..0b6ec31 100644 --- a/tests/exit3.c +++ b/tests/exit3.c @@ -12,7 +12,7 @@ func(void * arg) pthread_exit(arg); /* Never reached. */ - exit(1); + assert(0); } int @@ -27,6 +27,8 @@ main(int argc, char * argv[]) assert(pthread_create(&id[i], NULL, func, (void *) i) == 0); } + Sleep(1000); + /* Success. */ return 0; } -- cgit v1.2.3