summaryrefslogtreecommitdiff
path: root/tests/condvar9.c
diff options
context:
space:
mode:
authorrpj <rpj>1999-10-16 16:24:42 +0000
committerrpj <rpj>1999-10-16 16:24:42 +0000
commit8c238590dc9ad996710abc28a1868e9f1a41ab0a (patch)
tree972e0edb16402e27c7cc3248c6273fe65bac59e7 /tests/condvar9.c
parentcd324bb05bc5c76589c2ef7a2fcce61345100682 (diff)
1999-10-17 Ross Johnson <rpj@ixobrychus.canberra.edu.au>
* rwlock.c (pthread_rwlock_destroy): Add cast to remove compile warning. * condvar.c (pthread_cond_broadcast): Only release semaphores if there are waiting threads. 1999-10-15 Ross Johnson <rpj@ixobrychus.canberra.edu.au> * condvar.c (cond_wait_cleanup): New static cleanup handler for cond_timedwait; (cond_timedwait): pthread_cleanup_push args changed; canceling a thread while it's in pthread_cond_wait will now decrement the waiters count and cleanup if it's the last waiter. - Lorin Hochstein <lmh@xiphos.ca> and Peter Slacik <Peter.Slacik@tatramed.sk>; the last waiter will now reset the CV's wasBroadcast flag - Graham Dumpleton <Graham.Dumpleton@ra.pad.otc.telstra.com.au>.
Diffstat (limited to 'tests/condvar9.c')
-rw-r--r--tests/condvar9.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/tests/condvar9.c b/tests/condvar9.c
new file mode 100644
index 0000000..49a8cab
--- /dev/null
+++ b/tests/condvar9.c
@@ -0,0 +1,231 @@
+/*
+ * File: condvar9.c
+ *
+ * Test Synopsis:
+ * - Test multiple pthread_cond_broadcasts with thread cancelation.
+ *
+ * Test Method (Validation or Falsification):
+ * - Validation
+ *
+ * Requirements Tested:
+ * -
+ *
+ * Features Tested:
+ * -
+ *
+ * Cases Tested:
+ * -
+ *
+ * Description:
+ * - Make NUMTHREADS threads wait on CV, cancel one, broadcast signal them,
+ * and then repeat.
+ *
+ * Environment:
+ * -
+ *
+ * Input:
+ * - None.
+ *
+ * Output:
+ * - File name, Line number, and failed expression on failure.
+ * - No output on success.
+ *
+ * Assumptions:
+ * -
+ *
+ * Pass Criteria:
+ * - Process returns zero exit status.
+ *
+ * Fail Criteria:
+ * - Process returns non-zero exit status.
+ */
+
+#include "test.h"
+#include <sys/timeb.h>
+
+/*
+ * Create NUMTHREADS threads in addition to the Main thread.
+ */
+enum {
+ NUMTHREADS = 9
+};
+
+typedef struct bag_t_ bag_t;
+struct bag_t_ {
+ int threadnum;
+ int started;
+ /* Add more per-thread state variables here */
+};
+
+static bag_t threadbag[NUMTHREADS + 1];
+
+typedef struct cvthing_t_ cvthing_t;
+
+struct cvthing_t_ {
+ pthread_cond_t notbusy;
+ pthread_mutex_t lock;
+ int shared;
+};
+
+static cvthing_t cvthing = {
+ PTHREAD_COND_INITIALIZER,
+ PTHREAD_MUTEX_INITIALIZER,
+ 0
+};
+
+static pthread_mutex_t start_flag = PTHREAD_MUTEX_INITIALIZER;
+
+static struct timespec abstime = { 0, 0 };
+
+static int awoken;
+
+static void *
+mythread(void * arg)
+{
+ bag_t * bag = (bag_t *) arg;
+
+ assert(bag == &threadbag[bag->threadnum]);
+ assert(bag->started == 0);
+ bag->started = 1;
+
+ /* Wait for the start gun */
+ assert(pthread_mutex_lock(&start_flag) == 0);
+ assert(pthread_mutex_unlock(&start_flag) == 0);
+
+ assert(pthread_mutex_lock(&cvthing.lock) == 0);
+
+ /*
+ * pthread_cond_timedwait is a cancelation point and we
+ * going to cancel one deliberately.
+ */
+ pthread_cleanup_push(pthread_mutex_unlock, (void *) &cvthing.lock);
+
+ while (! cvthing.shared > 0)
+ assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
+
+ pthread_cleanup_pop(0);
+
+ assert(cvthing.shared > 0);
+
+ awoken++;
+
+ assert(pthread_mutex_unlock(&cvthing.lock) == 0);
+
+ return 0;
+}
+
+int
+main()
+{
+ int failed = 0;
+ int i;
+ int first, last;
+ int canceledThreads = 0;
+ pthread_t t[NUMTHREADS + 1];
+
+#if defined(__MINGW32__)
+ struct timeb currSysTime;
+#else
+ struct _timeb currSysTime;
+#endif
+ const DWORD NANOSEC_PER_MILLISEC = 1000000;
+
+ assert((t[0] = pthread_self()) != NULL);
+
+ assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
+
+ assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
+
+ _ftime(&currSysTime);
+
+ abstime.tv_sec = currSysTime.time;
+ abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
+
+ abstime.tv_sec += 5;
+
+ assert((t[0] = pthread_self()) != NULL);
+
+ awoken = 0;
+
+ for (first = 1, last = NUMTHREADS / 2;
+ first < NUMTHREADS;
+ first = last + 1, last = NUMTHREADS)
+ {
+ assert(pthread_mutex_lock(&start_flag) == 0);
+
+ for (i = first; i <= last; i++)
+ {
+ threadbag[i].started = 0;
+ threadbag[i].threadnum = i;
+ assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
+ assert(pthread_detach(t[i]) == 0);
+ }
+
+ /*
+ * Code to control or munipulate child threads should probably go here.
+ */
+ cvthing.shared = 0;
+
+ assert(pthread_mutex_unlock(&start_flag) == 0);
+
+ /*
+ * Give threads time to start.
+ */
+ Sleep(1000);
+
+ assert(pthread_mutex_lock(&cvthing.lock) == 0);
+
+ cvthing.shared++;
+
+ assert(pthread_mutex_unlock(&cvthing.lock) == 0);
+
+ assert(pthread_cancel(t[(first + last) / 2]) == 0);
+ canceledThreads++;
+
+ assert(pthread_cond_broadcast(&cvthing.notbusy) == 0);
+
+ /*
+ * Give threads time to complete.
+ */
+ Sleep(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);
+ }
+ }
+
+ /*
+ * Cleanup the CV.
+ */
+
+ assert(pthread_mutex_destroy(&cvthing.lock) == 0);
+
+ assert(cvthing.lock == NULL);
+
+ assert(pthread_cond_destroy(&cvthing.notbusy) == 0);
+
+ assert(cvthing.notbusy == NULL);
+
+ assert(!failed);
+
+ /*
+ * Check any results here.
+ */
+
+ assert(awoken == NUMTHREADS - canceledThreads);
+
+ /*
+ * Success.
+ */
+ return 0;
+}