1 /*
2 * File: condvar3_1.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 some signaled.
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 are signaled, the rest time out. 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_cond_t cv1;
85 static pthread_mutex_t mutex;
86 static pthread_mutex_t mutex1;
87 static struct timespec abstime = { 0, 0 };
88 static int timedout = 0;
89 static int signaled = 0;
90 static int awoken = 0;
91 static int waiting = 0;
92
93 enum {
94 NUMTHREADS = 30
95 };
96
97 void *
mythread(void * arg)98 mythread(void * arg)
99 {
100 int result;
101
102 assert(pthread_mutex_lock(&mutex1) == 0);
103 InterlockedIncrement((long*)&waiting);
104 assert(pthread_mutex_unlock(&mutex1) == 0);
105 assert(pthread_cond_signal(&cv1) == 0);
106
107 assert(pthread_mutex_lock(&mutex) == 0);
108 result = pthread_cond_timedwait(&cv, &mutex, &abstime);
109 if (result == ETIMEDOUT)
110 {
111 InterlockedIncrement((long*)&timedout);
112 }
113 else if (result == 0)
114 {
115 InterlockedIncrement((long*)&awoken);
116 }
117 assert(pthread_mutex_unlock(&mutex) == 0);
118
119 return arg;
120 }
121
122 int
main()123 main()
124 {
125 int i;
126 pthread_t t[NUMTHREADS + 1];
127 intptr_t result = 0;
128 struct _timeb currSysTime;
129 const DWORD NANOSEC_PER_MILLISEC = 1000000;
130 assert(pthread_cond_init(&cv, NULL) == 0);
131 assert(pthread_cond_init(&cv1, NULL) == 0);
132
133 assert(pthread_mutex_init(&mutex, NULL) == 0);
134 assert(pthread_mutex_init(&mutex1, NULL) == 0);
135
136 /* get current system time */
137 _ftime(&currSysTime);
138
139 abstime.tv_sec = currSysTime.time;
140 abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
141
142 abstime.tv_sec += 10;
143
144 assert(pthread_mutex_lock(&mutex1) == 0);
145
146 for (i = 1; i <= NUMTHREADS; i++)
147 {
148 assert(pthread_create(&t[i], NULL, mythread, (void *) (size_t) i) == 0);
149 }
150
151 do {
152 assert(pthread_cond_wait(&cv1,&mutex1) == 0);
153 } while ( NUMTHREADS > waiting );
154
155 assert(pthread_mutex_unlock(&mutex1) == 0);
156
157 for (i = NUMTHREADS/3; i <= 2*NUMTHREADS/3; i++)
158 {
159 assert(pthread_mutex_lock(&mutex) == 0);
160 assert(pthread_cond_signal(&cv) == 0);
161 assert(pthread_mutex_unlock(&mutex) == 0);
162
163 signaled++;
164 }
165
166 for (i = 1; i <= NUMTHREADS; i++)
167 {
168 assert(pthread_join(t[i], (void **) &result) == 0);
169 assert((int) result == i);
170 }
171
172 fprintf(stderr, "awk = %d\n", awoken);
173 fprintf(stderr, "sig = %d\n", signaled);
174 fprintf(stderr, "tot = %d\n", timedout);
175
176 assert(signaled == awoken);
177 assert(timedout == NUMTHREADS - signaled);
178
179 assert(pthread_cond_destroy(&cv1) == 0);
180
181 {
182 int result = pthread_cond_destroy(&cv);
183 assert(result == 0);
184 }
185
186 assert(pthread_mutex_destroy(&mutex1) == 0);
187 assert(pthread_mutex_destroy(&mutex) == 0);
188
189 return 0;
190 }
191