1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2009. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #define CONFIGURE_INIT |
---|
15 | #include "system.h" |
---|
16 | #include <sched.h> |
---|
17 | |
---|
18 | const char rtems_test_name[] = "PSX 10"; |
---|
19 | |
---|
20 | void *POSIX_Init( |
---|
21 | void *argument |
---|
22 | ) |
---|
23 | { |
---|
24 | int status; |
---|
25 | pthread_condattr_t attr; |
---|
26 | pthread_condattr_t attr_error; |
---|
27 | int pshared; |
---|
28 | pthread_cond_t cond; |
---|
29 | struct timespec timeout; |
---|
30 | |
---|
31 | TEST_BEGIN(); |
---|
32 | |
---|
33 | puts( "Init: pthread_condattr_init" ); |
---|
34 | status = pthread_condattr_init( &attr ); |
---|
35 | rtems_test_assert( !status ); |
---|
36 | |
---|
37 | puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" ); |
---|
38 | status = pthread_condattr_init( NULL ); |
---|
39 | if ( status != EINVAL ) |
---|
40 | printf( "status = %d\n", status ); |
---|
41 | rtems_test_assert( status == EINVAL ); |
---|
42 | |
---|
43 | puts( "Init: pthread_condattr_destroy" ); |
---|
44 | status = pthread_condattr_destroy( &attr ); |
---|
45 | rtems_test_assert( !status ); |
---|
46 | |
---|
47 | puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" ); |
---|
48 | status = pthread_condattr_destroy( NULL ); |
---|
49 | if ( status != EINVAL ) |
---|
50 | printf( "status = %d\n", status ); |
---|
51 | rtems_test_assert( status == EINVAL ); |
---|
52 | |
---|
53 | puts( "Init: pthread_condattr_init" ); |
---|
54 | status = pthread_condattr_init( &attr ); |
---|
55 | rtems_test_assert( !status ); |
---|
56 | |
---|
57 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" ); |
---|
58 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
59 | rtems_test_assert( !status ); |
---|
60 | |
---|
61 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" ); |
---|
62 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE ); |
---|
63 | rtems_test_assert( !status ); |
---|
64 | |
---|
65 | status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
66 | if ( status != EINVAL ) |
---|
67 | printf( "status = %d\n", status ); |
---|
68 | rtems_test_assert( status == EINVAL ); |
---|
69 | puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" ); |
---|
70 | |
---|
71 | status = pthread_condattr_setpshared( &attr, 0x7FFF ); |
---|
72 | if ( status != EINVAL ) |
---|
73 | printf( "status = %d\n", status ); |
---|
74 | rtems_test_assert( status == EINVAL ); |
---|
75 | puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" ); |
---|
76 | |
---|
77 | status = pthread_condattr_getpshared( &attr, &pshared ); |
---|
78 | rtems_test_assert( !status ); |
---|
79 | printf( "Init: pthread_condattr_getpshared - %d\n", pshared ); |
---|
80 | |
---|
81 | status = pthread_condattr_getpshared( NULL, &pshared ); |
---|
82 | if ( status != EINVAL ) |
---|
83 | printf( "status = %d\n", status ); |
---|
84 | rtems_test_assert( status == EINVAL ); |
---|
85 | puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" ); |
---|
86 | |
---|
87 | puts( "Init: pthread_cond_init - NULL attr" ); |
---|
88 | status = pthread_cond_init( &cond, NULL ); |
---|
89 | rtems_test_assert( !status ); |
---|
90 | |
---|
91 | /* error for attribute not initialized */ |
---|
92 | |
---|
93 | attr_error.is_initialized = FALSE; |
---|
94 | status = pthread_cond_init( &cond, &attr_error ); |
---|
95 | if ( status != EINVAL ) |
---|
96 | printf( "status = %d\n", status ); |
---|
97 | rtems_test_assert( status == EINVAL ); |
---|
98 | puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" ); |
---|
99 | |
---|
100 | status = pthread_cond_init( &cond, NULL ); |
---|
101 | if ( status != ENOMEM ) |
---|
102 | printf( "status = %d\n", status ); |
---|
103 | rtems_test_assert( status == ENOMEM ); |
---|
104 | puts( "Init: pthread_cond_init - ENOMEM (too many conds)" ); |
---|
105 | |
---|
106 | puts( "Init: pthread_cond_destroy" ); |
---|
107 | status = pthread_cond_destroy( &cond ); |
---|
108 | rtems_test_assert( !status ); |
---|
109 | |
---|
110 | /* error for bad condition variable passed */ |
---|
111 | |
---|
112 | status = pthread_cond_destroy( NULL ); |
---|
113 | if ( status != EINVAL ) |
---|
114 | printf( "status = %d\n", status ); |
---|
115 | rtems_test_assert( status == EINVAL ); |
---|
116 | puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" ); |
---|
117 | |
---|
118 | /* initiailize the attribute for the rest of the test */ |
---|
119 | |
---|
120 | puts( "Init: pthread_cond_init - attr" ); |
---|
121 | status = pthread_cond_init( &Cond1_id, &attr ); |
---|
122 | rtems_test_assert( !status ); |
---|
123 | |
---|
124 | /* signal task1 with a condition variable */ |
---|
125 | |
---|
126 | empty_line(); |
---|
127 | |
---|
128 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
129 | rtems_test_assert( !status ); |
---|
130 | |
---|
131 | /* switch to task1 to allow it to wait for a condition variable */ |
---|
132 | |
---|
133 | puts( "Init: sleep to switch to Task_1" ); |
---|
134 | sleep( 1 ); |
---|
135 | |
---|
136 | status = pthread_cond_destroy( &Cond1_id ); |
---|
137 | if ( status != EBUSY ) |
---|
138 | printf( "status = %d\n", status ); |
---|
139 | rtems_test_assert( status == EBUSY ); |
---|
140 | puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" ); |
---|
141 | |
---|
142 | puts( "Init: pthread_cond_signal" ); |
---|
143 | status = pthread_cond_signal( &Cond1_id ); |
---|
144 | rtems_test_assert( !status ); |
---|
145 | |
---|
146 | empty_line(); |
---|
147 | |
---|
148 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
149 | rtems_test_assert( !status ); |
---|
150 | |
---|
151 | /* switch to task1 and task2 to allow them to wait for broadcast signal */ |
---|
152 | |
---|
153 | puts( "Init: sleep - switch to Task_1 and Task_2" ); |
---|
154 | sleep( 1 ); |
---|
155 | |
---|
156 | /* broadcast a condition variable to task1 and task2 */ |
---|
157 | |
---|
158 | puts( "Init: pthread_cond_broadcast" ); |
---|
159 | status = pthread_cond_broadcast( &Cond1_id ); |
---|
160 | rtems_test_assert( !status ); |
---|
161 | |
---|
162 | puts( "Init: sleep - switch to Task_1" ); |
---|
163 | sleep( 0 ); |
---|
164 | |
---|
165 | /* timedwait case - timeout */ |
---|
166 | |
---|
167 | status = pthread_mutex_lock( &Mutex_id ); |
---|
168 | rtems_test_assert( !status ); |
---|
169 | |
---|
170 | /* set timeout to 3 seconds */ |
---|
171 | |
---|
172 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
173 | rtems_test_assert( !status ); |
---|
174 | timeout.tv_sec += 3; |
---|
175 | timeout.tv_nsec = 0; |
---|
176 | |
---|
177 | puts( "Init: pthread_cond_timedwait for 3 seconds" ); |
---|
178 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
179 | if ( status != ETIMEDOUT ) |
---|
180 | printf( "status = %d\n", status ); |
---|
181 | rtems_test_assert( status == ETIMEDOUT ); |
---|
182 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" ); |
---|
183 | |
---|
184 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
185 | rtems_test_assert( !status ); |
---|
186 | |
---|
187 | /* remaining error messages */ |
---|
188 | |
---|
189 | empty_line(); |
---|
190 | |
---|
191 | /* errors for bad variable passed */ |
---|
192 | |
---|
193 | status = pthread_cond_signal( NULL ); |
---|
194 | if ( status != EINVAL ) |
---|
195 | printf( "status = %d\n", status ); |
---|
196 | rtems_test_assert( status == EINVAL ); |
---|
197 | puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" ); |
---|
198 | |
---|
199 | status = pthread_cond_broadcast( NULL ); |
---|
200 | if ( status != EINVAL ) |
---|
201 | printf( "status = %d\n", status ); |
---|
202 | rtems_test_assert( status == EINVAL ); |
---|
203 | puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" ); |
---|
204 | |
---|
205 | /* acquire mutex so errors will occur */ |
---|
206 | |
---|
207 | status = pthread_mutex_lock( &Mutex_id ); |
---|
208 | rtems_test_assert( !status ); |
---|
209 | |
---|
210 | status = pthread_cond_wait( NULL, &Mutex_id ); |
---|
211 | if ( status != EINVAL ) |
---|
212 | printf( "status = %d\n", status ); |
---|
213 | rtems_test_assert( status == EINVAL ); |
---|
214 | puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" ); |
---|
215 | |
---|
216 | status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout ); |
---|
217 | if ( status != EINVAL ) |
---|
218 | printf( "status = %d\n", status ); |
---|
219 | rtems_test_assert( status == EINVAL ); |
---|
220 | puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" ); |
---|
221 | |
---|
222 | status = pthread_cond_wait( &Cond1_id, NULL ); |
---|
223 | if ( status != EINVAL ) |
---|
224 | printf( "status = %d\n", status ); |
---|
225 | rtems_test_assert( status == EINVAL ); |
---|
226 | puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" ); |
---|
227 | |
---|
228 | status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout ); |
---|
229 | if ( status != EINVAL ) |
---|
230 | printf( "status = %d\n", status ); |
---|
231 | rtems_test_assert( status == EINVAL ); |
---|
232 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" ); |
---|
233 | |
---|
234 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL ); |
---|
235 | if ( status != EINVAL ) |
---|
236 | printf( "status = %d\n", status ); |
---|
237 | rtems_test_assert( status == EINVAL ); |
---|
238 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" ); |
---|
239 | |
---|
240 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
241 | rtems_test_assert( !status ); |
---|
242 | timeout.tv_sec -= 1; |
---|
243 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
244 | if ( status != ETIMEDOUT ) |
---|
245 | printf( "status = %d\n", status ); |
---|
246 | rtems_test_assert( status == ETIMEDOUT ); |
---|
247 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_sec < current time)" ); |
---|
248 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
249 | rtems_test_assert( !status ); |
---|
250 | |
---|
251 | status = pthread_mutex_lock( &Mutex_id ); |
---|
252 | rtems_test_assert( !status ); |
---|
253 | |
---|
254 | /* ensure we do not catch a 0 nanosecond boundary */ |
---|
255 | do { |
---|
256 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
257 | rtems_test_assert( !status ); |
---|
258 | timeout.tv_nsec -= 1; |
---|
259 | } while ( timeout.tv_nsec < 0); |
---|
260 | |
---|
261 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
262 | if ( status != ETIMEDOUT ) |
---|
263 | printf( "status = %d\n", status ); |
---|
264 | rtems_test_assert( status == ETIMEDOUT ); |
---|
265 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_nsec < current time)" ); |
---|
266 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
267 | rtems_test_assert( !status ); |
---|
268 | |
---|
269 | /* wait and timedwait without mutex */ |
---|
270 | |
---|
271 | /* XXX - this case is commented out in the code pending review |
---|
272 | * |
---|
273 | * status = pthread_cond_wait( &Cond1_id, &Mutex_id ); |
---|
274 | * if ( status != EINVAL ) |
---|
275 | * printf( "status = %d\n", status ); |
---|
276 | * rtems_test_assert( status == EINVAL ); |
---|
277 | */ |
---|
278 | puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" ); |
---|
279 | |
---|
280 | /* XXX - this case is commented out in the code pending review |
---|
281 | * |
---|
282 | * status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
283 | * rtems_test_assert( !status ); |
---|
284 | * timeout.tv_sec += 1; |
---|
285 | * status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
286 | * if ( status != EINVAL ) |
---|
287 | * printf( "status = %d\n", status ); |
---|
288 | * rtems_test_assert( status == EINVAL ); |
---|
289 | */ |
---|
290 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)"); |
---|
291 | |
---|
292 | empty_line(); |
---|
293 | |
---|
294 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
295 | rtems_test_assert( !status ); |
---|
296 | |
---|
297 | /* switch to task3 to allow it to wait for broadcast signal */ |
---|
298 | |
---|
299 | puts( "Init: sleep - switch to Task_3" ); |
---|
300 | sleep( 1 ); |
---|
301 | |
---|
302 | /* destroy the mutex so Task3 can not acguire at the end of Wait_support */ |
---|
303 | |
---|
304 | status = pthread_mutex_destroy( &Mutex_id ); |
---|
305 | rtems_test_assert( !status ); |
---|
306 | |
---|
307 | /* signal a condition variable to task3 */ |
---|
308 | |
---|
309 | puts( "Init: pthread_cond_signal" ); |
---|
310 | status = pthread_cond_signal( &Cond1_id ); |
---|
311 | |
---|
312 | puts( "Init: sleep - switch to Task_3" ); |
---|
313 | sleep( 1 ); |
---|
314 | |
---|
315 | TEST_END(); |
---|
316 | rtems_test_exit( 0 ); |
---|
317 | |
---|
318 | return NULL; /* just so the compiler thinks we returned something */ |
---|
319 | } |
---|