From 588b7f6616834651ea4062cf440e57a8a0cba25f Mon Sep 17 00:00:00 2001 From: rpj Date: Sat, 30 Oct 1999 09:17:28 +0000 Subject: ./ChangeLog: 1999-10-30 Ross Johnson * create.c (pthread_create): Explicitly initialise thread state to default values. * cancel.c (pthread_setcancelstate): Check for NULL 'oldstate' for compatibility with Solaris pthreads; (pthread_setcanceltype): ditto: - Erik Hensema 1999-10-23 Ross Johnson * pthread.h (ctime_r): Fix incorrect argument "_tm" - Erik Hensema tests/ChangeLog: 1999-10-30 Ross Johnson * cancel1.c: New. Test pthread_setcancelstate and pthread_setcanceltype functions. * eyal1.c (waste_time): Change calculation to avoid FP exception on Aplhas - Rich Peters --- tests/ChangeLog | 8 ++ tests/Makefile | 2 +- tests/cancel1.c | 354 ++++++++++++++++++------------------------------------ tests/eyal1.c | 2 +- tests/runall.bat | 1 + tests/runtest.bat | 4 + 6 files changed, 129 insertions(+), 242 deletions(-) (limited to 'tests') diff --git a/tests/ChangeLog b/tests/ChangeLog index ee69577..5e8fca9 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,11 @@ +1999-10-30 Ross Johnson + + * cancel1.c: New. Test pthread_setcancelstate and + pthread_setcanceltype functions. + * eyal1.c (waste_time): Change calculation to avoid FP exception + on Aplhas + - Rich Peters + Oct 14 1999 Ross Johnson * condvar7.c: New. Test broadcast after waiting thread is canceled. diff --git a/tests/Makefile b/tests/Makefile index 0263769..7056d2b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -39,7 +39,7 @@ COPYFILES = $(HDR) $(LIB) $(DLL) TESTS = mutex1 condvar1 condvar2 exit1 create1 equal1 \ exit2 exit3 \ join1 join2 mutex2 mutex3 \ - count1 once1 tsd1 self1 self2 eyal1 \ + count1 once1 tsd1 self1 self2 cancel1 eyal1 \ condvar3 condvar4 condvar5 condvar6 condvar7 condvar8 \ errno1 \ rwlock1 rwlock2 rwlock3 rwlock4 rwlock5 rwlock6 diff --git a/tests/cancel1.c b/tests/cancel1.c index 189fc64..41f393e 100644 --- a/tests/cancel1.c +++ b/tests/cancel1.c @@ -1,273 +1,147 @@ -/******************************************************** - * An example source module to accompany... +/* + * File: cancel1.c + * + * Test Synopsis: Test setting cancel state and cancel type. + * - + * + * Test Method (Validation or Falsification): + * - + * + * Requirements Tested: + * - pthread_setcancelstate function + * - pthread_setcanceltype function + * + * Features Tested: + * - + * + * Cases Tested: + * - + * + * Description: + * - + * + * Environment: + * - + * + * Input: + * - None. * - * "Using POSIX Threads: Programming with Pthreads" - * by Brad nichols, Dick Buttlar, Jackie Farrell - * O'Reilly & Associates, Inc. + * Output: + * - File name, Line number, and failed expression on failure. + * - No output on success. * - ******************************************************** - * cancel.c -- + * Assumptions: + * - pthread_create, pthread_self work. * - * Demonstrates pthread cancellation. + * Pass Criteria: + * - Process returns zero exit status. * + * Fail Criteria: + * - Process returns non-zero exit status. */ -#include -#include "pthread.h" - -#define NUM_THREADS 3 -#define MESSAGE_MAX_LEN 80 - -int count=NUM_THREADS; /* number of threads active */ -pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; /* mutual exclusion - for count */ -pthread_cond_t init_done=PTHREAD_COND_INITIALIZER; /* signaled by - each thread after - completing initial- - ization */ -int id_arg[3] = {0,1,2}; +#include "test.h" /* - * Cleanup routine: last_breath() + * Create NUMTHREADS threads in addition to the Main thread. */ -void last_breath(char *messagep) -{ - printf("\n\n%s last_breath() cleanup routine: free'ing 0x%x\n\n", - messagep, messagep); +enum { + NUMTHREADS = 2 +}; - free(messagep); -} +typedef struct bag_t_ bag_t; +struct bag_t_ { + int threadnum; + int started; + /* Add more per-thread state variables here */ +}; -/* - * print_count() - */ -void print_count(char *messagep, int id, int i) -{ - int last_type,tmp_type; - - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type); - switch(id) { - case 0: - printf("%s %4d\n", messagep, i); - break; - case 1: - printf("%s \t%4d\n", messagep, i); - break; - case 2: - printf("%s \t\t%4d\n", messagep, i); - break; - } - pthread_setcanceltype(last_type, &tmp_type); -} +static bag_t threadbag[NUMTHREADS + 1]; -/* - * bullet_proof() - */ -void *bullet_proof(void *id_p) +void * +mythread(void * arg) { - int i=0, last_state; - int *my_id = id_p; - char *messagep; - + bag_t * bag = (bag_t *) arg; - messagep = (char *)malloc(MESSAGE_MAX_LEN); - sprintf(messagep, "Bullet Proof, thread #%d: ", *my_id); + assert(bag == &threadbag[bag->threadnum]); + assert(bag->started == 0); + bag->started = 1; - printf("%s\tI'm Alive, setting general cancellation OFF\n", - messagep); - - /* push last_breath() routine onto stack */ - pthread_cleanup_push( (void *)last_breath, (void *)messagep ); - - /* We turn off general cancelability here ... */ - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &last_state); - - pthread_mutex_lock(&lock); + /* ... */ { - printf("\n%s signaling main that my init is done\n", messagep); - count -= 1; - /* signal to program that entering loop */ - pthread_cond_signal(&init_done); - pthread_mutex_unlock(&lock); + int oldstate; + int oldtype; + + assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) == 0); + assert(oldstate == PTHREAD_CANCEL_ENABLE); /* Check default */ + assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0); + assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) == 0); + assert(pthread_setcancelstate(oldstate, &oldstate) == 0); + assert(oldstate == PTHREAD_CANCEL_DISABLE); /* Check setting */ + + assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype) == 0); + assert(oldtype == PTHREAD_CANCEL_DEFERRED); /* Check default */ + assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0); + assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0); + assert(pthread_setcanceltype(oldtype, &oldtype) == 0); + assert(oldtype == PTHREAD_CANCEL_ASYNCHRONOUS); /* Check setting */ } - /* loop forever until picked off with a cancel */ - for(;;i++) { - if (i%1000 == 0) - print_count(messagep, *my_id, i); - if (i%100000 == 0) { - printf("\n%s This is the thread that never ends... #%d\n", - messagep, i); - } - } - - /* Never get this far */ - - /* This pop is required by the standard, every push must have a pop - in the same lexical block. */ - pthread_cleanup_pop(0); - - return(NULL); + return 0; } -/* - * ask_for_it() - */ -void *ask_for_it(void *id_p) +int +main() { - int i=0, last_state, last_type; - int *my_id = id_p; - char *messagep; - + int failed = 0; + int i; + pthread_t t[NUMTHREADS + 1]; - messagep = (char *)malloc(MESSAGE_MAX_LEN); - sprintf(messagep, "Ask For It, thread #%d: ", *my_id); - - /* push last_breath() routine onto stack */ - pthread_cleanup_push( (void *)last_breath, (void *)messagep); - - /* We can turn on general cancelability here. Disable async cancellation */ - printf("%s\tI'm Alive, setting deferred cancellation ON\n", - messagep); - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &last_state); - - pthread_mutex_lock(&lock); - { - printf("\n%s signaling main that my init is done\n", messagep); - count -= 1; - /* signal to program that entering loop */ - pthread_cond_signal(&init_done); - pthread_mutex_unlock(&lock); - } + assert((t[0] = pthread_self()) != NULL); - /* loop forever until picked off with a cancel */ - for(;;i++) { - if (i%1000 == 0) - print_count(messagep, *my_id, i); - if (i%10000 == 0) { - printf("\n%s\tLook, I'll tell you when you can cancel me.\n",messagep,i); + for (i = 1; i <= NUMTHREADS; i++) + { + threadbag[i].started = 0; + threadbag[i].threadnum = i; + assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0); } - pthread_testcancel(); - } - - /* never get this far */ - - /* This pop is required by the standard, every push must have a pop - in the same lexical block. */ - pthread_cleanup_pop(0); - - return(NULL); -} - -/* - * sitting_duck() - */ -void *sitting_duck(void *id_p) -{ - int i=0, last_state, last_type, last_tmp; - int *my_id = id_p; - char *messagep; - - messagep = (char *)malloc(MESSAGE_MAX_LEN); - sprintf(messagep, "Sitting Duck, thread #%d: ", *my_id); - - /* push last_breath() routine onto stack */ - pthread_cleanup_push( (void *)last_breath, (void *)messagep); - - pthread_mutex_lock(&lock); - { - printf("\n%s signaling main that my init is done\n", messagep); - count -= 1; - /* signal to program that entering loop */ - pthread_cond_signal(&init_done); - pthread_mutex_unlock(&lock); - } - - /* Now, we're safe to turn on async cancellability */ - printf("%s\tI'm Alive, setting async cancellation ON\n", - messagep); - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &last_type); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &last_state); - - /* loop forever until picked off with a cancel */ - for(;;i++) { - if (i%1000 == 0) - print_count(messagep, *my_id, i++); - if (i%10000 == 0) { - pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_tmp); - printf("\n%s\tHum, nobody here but us chickens. %d\n", messagep,i); - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &last_tmp); + /* + * Code to control or munipulate child threads should probably go here. + */ + + /* + * Give threads time to run. + */ + Sleep(NUMTHREADS * 1000); + + /* + * Standard check that all threads started. + */ + for (i = 1; i <= NUMTHREADS; i++) + { + failed = !threadbag[i].started; + + if (failed) + { + fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started); + } } - } - - /* never get this far */ - - /* This pop is required by the standard, every push must have a pop - in the same lexical block. */ - pthread_cleanup_pop(0); - - return(NULL); -} - -extern int -main(void) -{ - int i; - void * statusp; - pthread_t threads[NUM_THREADS]; - - - /* spawn the threads */ - pthread_create(&(threads[0]), - NULL, - ask_for_it, - (void *) &(id_arg[0])); - - pthread_create(&(threads[1]), - NULL, - sitting_duck, - (void *) &(id_arg[1])); - - pthread_create(&(threads[2]), - NULL, - bullet_proof, - (void *) &(id_arg[2])); - - printf("main(): %d threads created\n", NUM_THREADS); - - pthread_mutex_lock(&lock); - - /* wait until all threads have entered loops */ - while (count != 0) { - pthread_cond_wait(&init_done, &lock); - } - pthread_mutex_unlock(&lock); + assert(!failed); - printf("main(): all threads have signaled that ready\n"); - - /* cancel each thread */ - for (i=0; i 0; --i) { - f = sqrt (f) * f + 10000.0; + f = 2 * f * f / (f * f); } } diff --git a/tests/runall.bat b/tests/runall.bat index e3a927b..5c2d783 100644 --- a/tests/runall.bat +++ b/tests/runall.bat @@ -23,6 +23,7 @@ call runtest cl count1 join1 call runtest cl once1 create1 call runtest cl tsd1 join1 call runtest cl self2 create1 +call runtest cl cancel1 self2 call runtest cl eyal1 tsd1 call runtest cl condvar3 create1 call runtest cl condvar4 create1 diff --git a/tests/runtest.bat b/tests/runtest.bat index 73fae97..dc36666 100644 --- a/tests/runtest.bat +++ b/tests/runtest.bat @@ -6,8 +6,12 @@ if %3==_ goto noprereq if NOT EXIST %3.pass goto needprereq :noprereq +if EXIST %2.fail goto forcetest if EXIST %2.pass goto bypass +:forcetest +if EXIST %2.fail erase %2.fail + REM Make sure we start with only those files we expect to need if exist tmp\*.* echo y | erase tmp\*.* > nul: rmdir tmp -- cgit v1.2.3