This source file includes following definitions.
- cnd_wait_routine
- test_cnd_wait
- get_ts
- cnd_timedwait_routine
- test_cnd_timedwait
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20
21
22 #define DO_TEST_COND 1
23 #define DO_TEST_TIMEDCOND 1
24
25
26
27
28 #define EXPLICIT_YIELD 1
29
30
31 #define ENABLE_DEBUGGING 0
32
33 #include <threads.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38
39 #if HAVE_DECL_ALARM
40 # include <signal.h>
41 # include <unistd.h>
42 #endif
43
44 #include "macros.h"
45
46 #if ENABLE_DEBUGGING
47 # define dbgprintf printf
48 #else
49 # define dbgprintf if (0) printf
50 #endif
51
52 #if EXPLICIT_YIELD
53 # define yield() thrd_yield ()
54 #else
55 # define yield()
56 #endif
57
58
59
60
61
62 static int cond_value = 0;
63 static cnd_t condtest;
64 static mtx_t lockcond;
65
66 static int
67 cnd_wait_routine (void *arg)
68 {
69 ASSERT (mtx_lock (&lockcond) == thrd_success);
70 while (!cond_value)
71 {
72 ASSERT (cnd_wait (&condtest, &lockcond) == thrd_success);
73 }
74 ASSERT (mtx_unlock (&lockcond) == thrd_success);
75
76 cond_value = 2;
77
78 return 0;
79 }
80
81 static void
82 test_cnd_wait ()
83 {
84 struct timespec remain;
85 thrd_t thread;
86 int ret;
87
88 remain.tv_sec = 2;
89 remain.tv_nsec = 0;
90
91 cond_value = 0;
92
93 ASSERT (thrd_create (&thread, cnd_wait_routine, NULL) == thrd_success);
94 do
95 {
96 yield ();
97 ret = thrd_sleep (&remain, &remain);
98 ASSERT (ret >= -1);
99 }
100 while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
101
102
103 ASSERT (mtx_lock (&lockcond) == thrd_success);
104 cond_value = 1;
105 ASSERT (cnd_signal (&condtest) == thrd_success);
106 ASSERT (mtx_unlock (&lockcond) == thrd_success);
107
108 ASSERT (thrd_join (thread, NULL) == thrd_success);
109
110 if (cond_value != 2)
111 abort ();
112 }
113
114
115
116
117
118 static int cond_timeout;
119
120 static void
121 get_ts (struct timespec *ts)
122 {
123 struct timeval now;
124
125 gettimeofday (&now, NULL);
126
127 ts->tv_sec = now.tv_sec + 1;
128 ts->tv_nsec = now.tv_usec * 1000;
129 }
130
131 static int
132 cnd_timedwait_routine (void *arg)
133 {
134 int ret;
135 struct timespec ts;
136
137 ASSERT (mtx_lock (&lockcond) == thrd_success);
138 while (!cond_value)
139 {
140 get_ts (&ts);
141 ret = cnd_timedwait (&condtest, &lockcond, &ts);
142 if (ret == thrd_timedout)
143 cond_timeout = 1;
144 }
145 ASSERT (mtx_unlock (&lockcond) == thrd_success);
146
147 return 0;
148 }
149
150 static void
151 test_cnd_timedwait (void)
152 {
153 struct timespec remain;
154 thrd_t thread;
155 int ret;
156
157 remain.tv_sec = 2;
158 remain.tv_nsec = 0;
159
160 cond_value = cond_timeout = 0;
161
162 ASSERT (thrd_create (&thread, cnd_timedwait_routine, NULL) == thrd_success);
163 do
164 {
165 yield ();
166 ret = thrd_sleep (&remain, &remain);
167 ASSERT (ret >= -1);
168 }
169 while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
170
171
172 ASSERT (mtx_lock (&lockcond) == thrd_success);
173 cond_value = 1;
174 ASSERT (cnd_signal (&condtest) == thrd_success);
175 ASSERT (mtx_unlock (&lockcond) == thrd_success);
176
177 ASSERT (thrd_join (thread, NULL) == thrd_success);
178
179 if (!cond_timeout)
180 abort ();
181 }
182
183 int
184 main ()
185 {
186 #if HAVE_DECL_ALARM
187
188
189 int alarm_value = 600;
190 signal (SIGALRM, SIG_DFL);
191 alarm (alarm_value);
192 #endif
193
194 ASSERT (cnd_init (&condtest) == thrd_success);
195 ASSERT (mtx_init (&lockcond, mtx_plain) == thrd_success);
196
197 #if DO_TEST_COND
198 printf ("Starting test_cnd_wait ..."); fflush (stdout);
199 test_cnd_wait ();
200 printf (" OK\n"); fflush (stdout);
201 #endif
202 #if DO_TEST_TIMEDCOND
203 printf ("Starting test_cnd_timedwait ..."); fflush (stdout);
204 test_cnd_timedwait ();
205 printf (" OK\n"); fflush (stdout);
206 #endif
207
208 return 0;
209 }