• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _WIN32_WINNT 0x400
2 
3 #include "test.h"
4 #include <sys/timeb.h>
5 
6 static pthread_cond_t cv;
7 static pthread_mutex_t mutex;
8 static struct timespec abstime = { 0, 0 };
9 
10 enum {
11   NUMTHREADS = 30
12 };
13 
14 void *
mythread(void * arg)15 mythread(void * arg)
16 {
17   assert(pthread_mutex_lock(&mutex) == 0);
18 
19   assert(pthread_cond_timedwait(&cv, &mutex, &abstime) == ETIMEDOUT);
20 
21   assert(pthread_mutex_unlock(&mutex) == 0);
22 
23   return arg;
24 }
25 
26 
27 
28 int
main()29 main()
30 {
31   int i;
32   pthread_t t[NUMTHREADS + 1];
33   intptr_t result = 0;
34   struct _timeb currSysTime;
35   const DWORD NANOSEC_PER_MILLISEC = 1000000;
36 
37   assert(pthread_cond_init(&cv, NULL) == 0);
38 
39   assert(pthread_mutex_init(&mutex, NULL) == 0);
40 
41   /* get current system time */
42   _ftime(&currSysTime);
43 
44   abstime.tv_sec = currSysTime.time;
45   abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
46 
47   abstime.tv_sec += 5;
48 
49   assert(pthread_mutex_lock(&mutex) == 0);
50 
51   for (i = 1; i <= NUMTHREADS; i++)
52     {
53       assert(pthread_create(&t[i], NULL, mythread, ((void *) (size_t) i)) == 0);
54     }
55 
56   assert(pthread_mutex_unlock(&mutex) == 0);
57 
58   for (i = 1; i <= NUMTHREADS; i++)
59     {
60       assert(pthread_join(t[i], (void **) &result) == 0);
61 	assert((int)result == i);
62     }
63 
64   {
65   int result = pthread_cond_destroy(&cv);
66   if (result != 0)
67     {
68       fprintf(stderr, "Result = %s\n", error_string[result]);
69 #if 0
70 	fprintf(stderr, "\tWaitersBlocked = %ld\n", cv->nWaitersBlocked);
71 	fprintf(stderr, "\tWaitersGone = %ld\n", cv->nWaitersGone);
72 	fprintf(stderr, "\tWaitersToUnblock = %ld\n", cv->nWaitersToUnblock);
73 #endif
74 	fflush(stderr);
75     }
76   assert(result == 0);
77   }
78 
79   return 0;
80 }
81