4 * Test Synopsis: Test asynchronous cancelation.
6 * Test Method (Validation or Falsification):
28 * - File name, Line number, and failed expression on failure.
29 * - No output on success.
32 * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
33 * pthread_testcancel, pthread_cancel, pthread_join
36 * - Process returns zero exit status.
39 * - Process returns non-zero exit status.
45 * Create NUMTHREADS threads in addition to the Main thread.
51 typedef struct bag_t_ bag_t;
55 /* Add more per-thread state variables here */
59 static bag_t threadbag[NUMTHREADS + 1];
64 int result = ((int)PTHREAD_CANCELED + 1);
65 bag_t * bag = (bag_t *) arg;
67 assert(bag == &threadbag[bag->threadnum]);
68 assert(bag->started == 0);
71 /* Set to known state and type */
73 assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
75 assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
78 * We wait up to 10 seconds, waking every 0.1 seconds,
79 * for a cancelation to be applied to us.
81 for (bag->count = 0; bag->count < 100; bag->count++)
84 return (void *) result;
92 pthread_t t[NUMTHREADS + 1];
94 assert((t[0] = pthread_self()) != NULL);
96 for (i = 1; i <= NUMTHREADS; i++)
98 threadbag[i].started = 0;
99 threadbag[i].threadnum = i;
100 assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
104 * Code to control or munipulate child threads should probably go here.
108 for (i = 1; i <= NUMTHREADS; i++)
110 assert(pthread_cancel(t[i]) == 0);
114 * Give threads time to run.
116 Sleep(NUMTHREADS * 100);
119 * Standard check that all threads started.
121 for (i = 1; i <= NUMTHREADS; i++)
123 if (!threadbag[i].started)
125 failed |= !threadbag[i].started;
126 fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
133 * Check any results here. Set "failed" and only print output on failure.
136 for (i = 1; i <= NUMTHREADS; i++)
142 * The thread does not contain any cancelation points, so
143 * a return value of PTHREAD_CANCELED confirms that async
144 * cancelation succeeded.
146 assert(pthread_join(t[i], (void **) &result) == 0);
148 fail = (result != (int) PTHREAD_CANCELED);
152 fprintf(stderr, "Thread %d: started %d: count %d\n",
154 threadbag[i].started,
157 failed = (failed || fail);