1 /*
2  * File: condvar6.c
3  *
4  *
5  * --------------------------------------------------------------------------
6  *
7  *      Pthreads-win32 - POSIX Threads Library for Win32
8  *      Copyright(C) 1998 John E. Bossom
9  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
10  *
11  *      Contact Email: rpj@callisto.canberra.edu.au
12  *
13  *      The current list of contributors is contained
14  *      in the file CONTRIBUTORS included with the source
15  *      code distribution. The list can also be seen at the
16  *      following World Wide Web location:
17  *      http://sources.redhat.com/pthreads-win32/contributors.html
18  *
19  *      This library is free software; you can redistribute it and/or
20  *      modify it under the terms of the GNU Lesser General Public
21  *      License as published by the Free Software Foundation; either
22  *      version 2 of the License, or (at your option) any later version.
23  *
24  *      This library is distributed in the hope that it will be useful,
25  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  *      Lesser General Public License for more details.
28  *
29  *      You should have received a copy of the GNU Lesser General Public
30  *      License along with this library in the file COPYING.LIB;
31  *      if not, write to the Free Software Foundation, Inc.,
32  *      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
33  *
34  * --------------------------------------------------------------------------
35  *
36  * Test Synopsis:
37  * - Test pthread_cond_broadcast.
38  *
39  * Test Method (Validation or Falsification):
40  * - Validation
41  *
42  * Requirements Tested:
43  * -
44  *
45  * Features Tested:
46  * -
47  *
48  * Cases Tested:
49  * -
50  *
51  * Description:
52  * - Test broadcast with NUMTHREADS (=5) waiting CVs.
53  *
54  * Environment:
55  * -
56  *
57  * Input:
58  * - None.
59  *
60  * Output:
61  * - File name, Line number, and failed expression on failure.
62  * - No output on success.
63  *
64  * Assumptions:
65  * -
66  *
67  * Pass Criteria:
68  * - Process returns zero exit status.
69  *
70  * Fail Criteria:
71  * - Process returns non-zero exit status.
72  */
73 
74 #include "test.h"
75 #include <sys/timeb.h>
76 
77 /*
78  * Create NUMTHREADS threads in addition to the Main thread.
79  */
80 enum {
81   NUMTHREADS = 5
82 };
83 
84 typedef struct bag_t_ bag_t;
85 struct bag_t_ {
86   int threadnum;
87   int started;
88   /* Add more per-thread state variables here */
89 };
90 
91 static bag_t threadbag[NUMTHREADS + 1];
92 
93 typedef struct cvthing_t_ cvthing_t;
94 
95 struct cvthing_t_ {
96   pthread_cond_t notbusy;
97   pthread_mutex_t lock;
98   int shared;
99 };
100 
101 static cvthing_t cvthing = {
102   PTHREAD_COND_INITIALIZER,
103   PTHREAD_MUTEX_INITIALIZER,
104   0
105 };
106 
107 static pthread_mutex_t start_flag = PTHREAD_MUTEX_INITIALIZER;
108 
109 static struct timespec abstime = { 0, 0 };
110 
111 static int awoken;
112 
113 void *
mythread(void * arg)114 mythread(void * arg)
115 {
116   bag_t * bag = (bag_t *) arg;
117 
118   assert(bag == &threadbag[bag->threadnum]);
119   assert(bag->started == 0);
120   bag->started = 1;
121 
122   /* Wait for the start gun */
123   assert(pthread_mutex_lock(&start_flag) == 0);
124   assert(pthread_mutex_unlock(&start_flag) == 0);
125 
126   assert(pthread_mutex_lock(&cvthing.lock) == 0);
127 
128   while (! (cvthing.shared > 0))
129     assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
130 
131   assert(cvthing.shared > 0);
132 
133   awoken++;
134 
135   assert(pthread_mutex_unlock(&cvthing.lock) == 0);
136 
137   return (void *) 0;
138 }
139 
140 int
main()141 main()
142 {
143   int failed = 0;
144   int i;
145   pthread_t t[NUMTHREADS + 1];
146 
147   PTW32_STRUCT_TIMEB currSysTime;
148   const DWORD NANOSEC_PER_MILLISEC = 1000000;
149 
150   cvthing.shared = 0;
151 
152   assert((t[0] = pthread_self()).p != NULL);
153 
154   assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
155 
156   assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
157 
158   assert(pthread_mutex_lock(&start_flag) == 0);
159 
160   PTW32_FTIME(&currSysTime);
161 
162   abstime.tv_sec = (long)currSysTime.time;
163   abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
164 
165   abstime.tv_sec += 5;
166 
167   assert((t[0] = pthread_self()).p != NULL);
168 
169   awoken = 0;
170 
171   for (i = 1; i <= NUMTHREADS; i++)
172     {
173       threadbag[i].started = 0;
174       threadbag[i].threadnum = i;
175       assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
176     }
177 
178   /*
179    * Code to control or munipulate child threads should probably go here.
180    */
181 
182   assert(pthread_mutex_unlock(&start_flag) == 0);
183 
184   /*
185    * Give threads time to start.
186    */
187   Sleep(1000);
188 
189   assert(pthread_mutex_lock(&cvthing.lock) == 0);
190   cvthing.shared++;
191   assert(pthread_mutex_unlock(&cvthing.lock) == 0);
192 
193   assert(pthread_cond_broadcast(&cvthing.notbusy) == 0);
194 
195   /*
196    * Give threads time to complete.
197    */
198   for (i = 1; i <= NUMTHREADS; i++)
199     {
200       assert(pthread_join(t[i], NULL) == 0);
201     }
202 
203   /*
204    * Cleanup the CV.
205    */
206 
207   assert(pthread_mutex_destroy(&cvthing.lock) == 0);
208 
209   assert(cvthing.lock == NULL);
210 
211   assert(pthread_cond_destroy(&cvthing.notbusy) == 0);
212 
213   assert(cvthing.notbusy == NULL);
214 
215   /*
216    * Standard check that all threads started.
217    */
218   for (i = 1; i <= NUMTHREADS; i++)
219     {
220       failed = !threadbag[i].started;
221 
222       if (failed)
223 	{
224 	  fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
225 	}
226     }
227 
228   assert(!failed);
229 
230   /*
231    * Check any results here.
232    */
233 
234   assert(awoken == NUMTHREADS);
235 
236   /*
237    * Success.
238    */
239   return 0;
240 }
241 
242 
243