1 /*
2 * File: condvar3_2.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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 *
34 * --------------------------------------------------------------------------
35 *
36 * Test Synopsis:
37 * - Test timeout of multiple waits on a CV with remainder broadcast awoken.
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 * - Because some CVs are never signaled, we expect their waits to time out.
53 * Some time out, the rest are broadcast signaled. Pthread_cond_destroy() will fail
54 * unless all are accounted for, either signaled or timedout.
55 *
56 * Environment:
57 * -
58 *
59 * Input:
60 * - None.
61 *
62 * Output:
63 * - File name, Line number, and failed expression on failure.
64 * - No output on success.
65 *
66 * Assumptions:
67 * -
68 *
69 * Pass Criteria:
70 * - pthread_cond_timedwait returns ETIMEDOUT.
71 * - Process returns zero exit status.
72 *
73 * Fail Criteria:
74 * - pthread_cond_timedwait does not return ETIMEDOUT.
75 * - Process returns non-zero exit status.
76 */
77
78 #define _WIN32_WINNT 0x400
79
80 #include "test.h"
81 #include <sys/timeb.h>
82
83 static pthread_cond_t cv;
84 static pthread_mutex_t mutex;
85 static struct timespec abstime = { 0, 0 };
86 static struct timespec abstime2 = { 0, 0 };
87 static int timedout = 0;
88 static int awoken = 0;
89
90 enum {
91 NUMTHREADS = 30
92 };
93
94 void *
mythread(void * arg)95 mythread(void * arg)
96 {
97 int result;
98
99 assert(pthread_mutex_lock(&mutex) == 0);
100
101 abstime2.tv_sec = abstime.tv_sec;
102
103 if (((int) (size_t) arg) % 3 == 0)
104 {
105 abstime2.tv_sec += 2;
106 }
107
108 result = pthread_cond_timedwait(&cv, &mutex, &abstime2);
109 assert(pthread_mutex_unlock(&mutex) == 0);
110 if (result == ETIMEDOUT)
111 {
112 InterlockedIncrement((LPLONG)&timedout);
113 }
114 else
115 {
116 InterlockedIncrement((LPLONG)&awoken);
117 }
118
119
120 return arg;
121 }
122
123 int
main()124 main()
125 {
126 int i;
127 pthread_t t[NUMTHREADS + 1];
128 intptr_t result = 0;
129 struct _timeb currSysTime;
130 const DWORD NANOSEC_PER_MILLISEC = 1000000;
131
132 assert(pthread_cond_init(&cv, NULL) == 0);
133
134 assert(pthread_mutex_init(&mutex, NULL) == 0);
135
136 /* get current system time */
137 _ftime(&currSysTime);
138
139 abstime.tv_sec = abstime2.tv_sec = currSysTime.time + 5;
140 abstime.tv_nsec = abstime2.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
141
142 assert(pthread_mutex_lock(&mutex) == 0);
143
144 for (i = 1; i <= NUMTHREADS; i++)
145 {
146 assert(pthread_create(&t[i], NULL, mythread, (void *) (size_t) i) == 0);
147 }
148
149 assert(pthread_mutex_unlock(&mutex) == 0);
150
151 for (i = 1; i <= NUMTHREADS; i++)
152 {
153 assert(pthread_join(t[i], (void **) &result) == 0);
154 assert((int)result == i);
155 /*
156 * Approximately 2/3rds of the threads are expected to time out.
157 * Signal the remainder after some threads have woken up and exited
158 * and while some are still waking up after timeout.
159 * Also tests that redundant broadcasts don't return errors.
160 */
161
162 // assert(pthread_mutex_lock(&mutex) == 0);
163
164 if (InterlockedExchangeAdd((LPLONG)&awoken, 0L) > NUMTHREADS/3)
165 {
166 assert(pthread_cond_broadcast(&cv) == 0);
167 }
168
169 // assert(pthread_mutex_unlock(&mutex) == 0);
170
171 }
172
173 assert(awoken == NUMTHREADS - timedout);
174
175 {
176 int result = pthread_cond_destroy(&cv);
177 assert(result == 0);
178 }
179
180 assert(pthread_mutex_destroy(&mutex) == 0);
181
182 return 0;
183 }
184