source: rtems/testsuites/psxtests/psx10/init.c @ 5222488

5
Last change on this file since 5222488 was 5222488, checked in by Sebastian Huber <sebastian.huber@…>, on 09/26/17 at 05:49:17

posix: Implement self-contained POSIX condvar

POSIX condition variables are now available in all configurations and no
longer depend on --enable-posix.

Update #2514.
Update #3113.

  • Property mode set to 100644
File size: 12.9 KB
Line 
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
18const char rtems_test_name[] = "PSX 10";
19
20static void test_cond_null( void )
21{
22  pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
23  int eno;
24  struct timespec to;
25
26  eno = pthread_cond_init( NULL, NULL );
27  rtems_test_assert( eno == EINVAL );
28
29  eno = pthread_mutex_lock( &mtx );
30  rtems_test_assert( eno == 0 );
31
32  eno = pthread_cond_wait( NULL, &mtx );
33  rtems_test_assert( eno == EINVAL );
34
35  to.tv_sec = 1;
36  to.tv_nsec = 1;
37  eno = pthread_cond_timedwait( NULL, &mtx, &to );
38  rtems_test_assert( eno == EINVAL );
39
40  eno = pthread_mutex_unlock( &mtx );
41  rtems_test_assert( eno == 0 );
42
43  eno = pthread_cond_signal( NULL );
44  rtems_test_assert( eno == EINVAL );
45
46  eno = pthread_cond_broadcast( NULL );
47  rtems_test_assert( eno == EINVAL );
48
49  eno = pthread_cond_destroy( NULL );
50  rtems_test_assert( eno == EINVAL );
51
52  eno = pthread_mutex_destroy( &mtx );
53  rtems_test_assert( eno == 0 );
54}
55
56static void test_cond_not_initialized( void )
57{
58  pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
59  pthread_cond_t cond;
60  int eno;
61  struct timespec to;
62
63  memset( &cond, 0xff, sizeof( cond ) );
64
65  eno = pthread_mutex_lock( &mtx );
66  rtems_test_assert( eno == 0 );
67
68  eno = pthread_cond_wait( &cond, &mtx );
69  rtems_test_assert( eno == EINVAL );
70
71  to.tv_sec = 1;
72  to.tv_nsec = 1;
73  eno = pthread_cond_timedwait( &cond, &mtx, &to );
74  rtems_test_assert( eno == EINVAL );
75
76  eno = pthread_mutex_unlock( &mtx );
77  rtems_test_assert( eno == 0 );
78
79  eno = pthread_cond_signal( &cond );
80  rtems_test_assert( eno == EINVAL );
81
82  eno = pthread_cond_broadcast( &cond );
83  rtems_test_assert( eno == EINVAL );
84
85  eno = pthread_cond_destroy( &cond );
86  rtems_test_assert( eno == EINVAL );
87
88  eno = pthread_mutex_destroy( &mtx );
89  rtems_test_assert( eno == 0 );
90}
91
92static void test_cond_invalid_copy( void )
93{
94  pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
95  pthread_cond_t cond;
96  pthread_cond_t cond2;
97  int eno;
98  struct timespec to;
99
100  eno = pthread_cond_init( &cond, NULL );
101  rtems_test_assert( eno == 0 );
102
103  memcpy( &cond2, &cond, sizeof( cond2 ) );
104
105  eno = pthread_mutex_lock( &mtx );
106  rtems_test_assert( eno == 0 );
107
108  eno = pthread_cond_wait( &cond2, &mtx );
109  rtems_test_assert( eno == EINVAL );
110
111  to.tv_sec = 1;
112  to.tv_nsec = 1;
113  eno = pthread_cond_timedwait( &cond2, &mtx, &to );
114  rtems_test_assert( eno == EINVAL );
115
116  eno = pthread_mutex_unlock( &mtx );
117  rtems_test_assert( eno == 0 );
118
119  eno = pthread_cond_signal( &cond2 );
120  rtems_test_assert( eno == EINVAL );
121
122  eno = pthread_cond_broadcast( &cond2 );
123  rtems_test_assert( eno == EINVAL );
124
125  eno = pthread_cond_destroy( &cond2 );
126  rtems_test_assert( eno == EINVAL );
127
128  eno = pthread_cond_destroy( &cond );
129  rtems_test_assert( eno == 0 );
130
131  eno = pthread_mutex_destroy( &mtx );
132  rtems_test_assert( eno == 0 );
133}
134
135void *POSIX_Init(
136  void *argument
137)
138{
139  int                 status;
140  pthread_condattr_t  attr;
141  pthread_condattr_t  attr_error;
142  int                 pshared;
143  pthread_cond_t      cond;
144  struct timespec     timeout;
145
146  TEST_BEGIN();
147
148  test_cond_null();
149  test_cond_not_initialized();
150  test_cond_invalid_copy();
151
152  puts( "Init: pthread_condattr_init" );
153  status = pthread_condattr_init( &attr );
154  rtems_test_assert( !status );
155
156  puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" );
157  status = pthread_condattr_init( NULL );
158  if ( status != EINVAL )
159    printf( "status = %d\n", status );
160  rtems_test_assert( status == EINVAL );
161
162  puts( "Init: pthread_condattr_destroy" );
163  status = pthread_condattr_destroy( &attr );
164  rtems_test_assert( !status );
165
166  puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" );
167  status = pthread_condattr_destroy( NULL );
168  if ( status != EINVAL )
169    printf( "status = %d\n", status );
170  rtems_test_assert( status == EINVAL );
171
172  puts( "Init: pthread_condattr_init" );
173  status = pthread_condattr_init( &attr );
174  rtems_test_assert( !status );
175
176  puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" );
177  status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
178  rtems_test_assert( !status );
179
180  puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" );
181  status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
182  rtems_test_assert( !status );
183
184  status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE );
185  if ( status != EINVAL )
186    printf( "status = %d\n", status );
187  rtems_test_assert( status == EINVAL );
188  puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" );
189
190  status = pthread_condattr_setpshared( &attr, 0x7FFF );
191  if ( status != EINVAL )
192    printf( "status = %d\n", status );
193  rtems_test_assert( status == EINVAL );
194  puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" );
195
196  status = pthread_condattr_getpshared( &attr, &pshared );
197  rtems_test_assert( !status );
198  printf( "Init: pthread_condattr_getpshared - %d\n", pshared );
199
200  status = pthread_condattr_getpshared( NULL, &pshared );
201  if ( status != EINVAL )
202    printf( "status = %d\n", status );
203  rtems_test_assert( status == EINVAL );
204  puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" );
205
206  puts( "Init: pthread_cond_init - NULL attr" );
207  status = pthread_cond_init( &cond, NULL );
208  rtems_test_assert( !status );
209
210/* error for attribute not initialized */
211
212  attr_error.is_initialized = FALSE;
213  status = pthread_cond_init( &cond, &attr_error );
214  if ( status != EINVAL )
215    printf( "status = %d\n", status );
216  rtems_test_assert( status == EINVAL );
217  puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" );
218
219/* error for bad condition variable passed */
220
221  status = pthread_cond_destroy( NULL );
222  if ( status != EINVAL )
223    printf( "status = %d\n", status );
224  rtems_test_assert( status == EINVAL );
225  puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" );
226
227/* pshared tests */
228
229  puts( "Init: pthread_cond_init - EINVAL (invalid pshared)" );
230  attr.process_shared = -1;
231  status = pthread_cond_init( &cond, &attr );
232  rtems_test_assert( status == EINVAL );
233
234  puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" );
235  status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
236  rtems_test_assert( status == 0 );
237
238  puts( "Init: pthread_cond_init - OK" );
239  status = pthread_cond_init( &cond, &attr );
240  rtems_test_assert( status == 0 );
241
242  puts( "Init: pthread_cond_destroy - OK" );
243  status = pthread_cond_destroy( &cond );
244  rtems_test_assert( status == 0 );
245
246/* initiailize the attribute for the rest of the test */
247
248  puts( "Init: pthread_cond_init - attr" );
249  status = pthread_cond_init( &Cond1_id, &attr );
250  rtems_test_assert( !status );
251
252/* signal task1 with a condition variable */
253
254  empty_line();
255
256  status = pthread_create( &Task_id, NULL, Task_1, NULL );
257  rtems_test_assert( !status );
258
259/* switch to task1 to allow it to wait for a condition variable */
260
261  puts( "Init: sleep to switch to Task_1" );
262  sleep( 1 );
263
264  status = pthread_cond_destroy( &Cond1_id );
265  if ( status != EBUSY )
266    printf( "status = %d\n", status );
267  rtems_test_assert( status == EBUSY );
268  puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" );
269
270  puts( "Init: pthread_cond_signal" );
271  status = pthread_cond_signal( &Cond1_id );
272  rtems_test_assert( !status );
273
274  empty_line();
275
276  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
277  rtems_test_assert( !status );
278
279/* switch to task1 and task2 to allow them to wait for broadcast signal */
280
281  puts( "Init: sleep - switch to Task_1 and Task_2" );
282  sleep( 1 );
283
284/* broadcast a condition variable to task1 and task2 */
285
286  puts( "Init: pthread_cond_broadcast" );
287  status = pthread_cond_broadcast( &Cond1_id );
288  rtems_test_assert( !status );
289
290  puts( "Init: sleep - switch to Task_1" );
291  sleep( 0 );
292
293/* timedwait case - timeout */
294
295  status = pthread_mutex_lock( &Mutex_id );
296  rtems_test_assert( !status );
297
298/* set timeout to 3 seconds */
299
300  status = clock_gettime( CLOCK_REALTIME, &timeout );
301  rtems_test_assert( !status );
302  timeout.tv_sec += 3;
303  timeout.tv_nsec = 0;
304
305  puts( "Init: pthread_cond_timedwait for 3 seconds" );
306  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
307  if ( status != ETIMEDOUT )
308    printf( "status = %d\n", status );
309  rtems_test_assert( status == ETIMEDOUT );
310  puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" );
311
312  status = pthread_mutex_unlock( &Mutex_id );
313  rtems_test_assert( !status );
314
315/* remaining error messages */
316
317  empty_line();
318
319/* errors for bad variable passed */
320
321  status = pthread_cond_signal( NULL );
322  if ( status != EINVAL )
323    printf( "status = %d\n", status );
324  rtems_test_assert( status == EINVAL );
325  puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" );
326
327  status = pthread_cond_broadcast( NULL );
328  if ( status != EINVAL )
329    printf( "status = %d\n", status );
330  rtems_test_assert( status == EINVAL );
331  puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" );
332
333/* acquire mutex so errors will occur */
334
335  status = pthread_mutex_lock( &Mutex_id );
336  rtems_test_assert( !status );
337
338  status = pthread_cond_wait( NULL, &Mutex_id );
339  if ( status != EINVAL )
340    printf( "status = %d\n", status );
341  rtems_test_assert( status == EINVAL );
342  puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" );
343
344  status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout );
345  if ( status != EINVAL )
346    printf( "status = %d\n", status );
347  rtems_test_assert( status == EINVAL );
348  puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" );
349
350  status = pthread_cond_wait( &Cond1_id, NULL );
351  if ( status != EINVAL )
352    printf( "status = %d\n", status );
353  rtems_test_assert( status == EINVAL );
354  puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" );
355
356  status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout );
357  if ( status != EINVAL )
358    printf( "status = %d\n", status );
359  rtems_test_assert( status == EINVAL );
360  puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" );
361
362  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL );
363  if ( status != EINVAL )
364    printf( "status = %d\n", status );
365  rtems_test_assert( status == EINVAL );
366  puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" );
367
368  status = clock_gettime( CLOCK_REALTIME, &timeout );
369  rtems_test_assert( !status );
370  timeout.tv_sec -= 1;
371  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
372  if ( status != ETIMEDOUT )
373    printf( "status = %d\n", status );
374  rtems_test_assert( status == ETIMEDOUT );
375  puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_sec < current time)" );
376  status = pthread_mutex_unlock( &Mutex_id );
377  rtems_test_assert( !status );
378
379  status = pthread_mutex_lock( &Mutex_id );
380  rtems_test_assert( !status );
381
382  /* ensure we do not catch a 0 nanosecond boundary */
383  do {
384    status = clock_gettime( CLOCK_REALTIME, &timeout );
385    rtems_test_assert( !status );
386    timeout.tv_nsec -= 1;
387  } while ( timeout.tv_nsec < 0);
388
389  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
390  if ( status != ETIMEDOUT )
391    printf( "status = %d\n", status );
392  rtems_test_assert( status == ETIMEDOUT );
393  puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_nsec < current time)" );
394  status = pthread_mutex_unlock( &Mutex_id );
395  rtems_test_assert( !status );
396
397/* wait and timedwait without mutex */
398
399/* XXX - this case is commented out in the code pending review
400 *
401 *   status = pthread_cond_wait( &Cond1_id, &Mutex_id );
402 *   if ( status != EINVAL )
403 *     printf( "status = %d\n", status );
404 *   rtems_test_assert( status == EINVAL );
405 */
406  puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" );
407
408/* XXX - this case is commented out in the code pending review
409 *
410 *  status = clock_gettime( CLOCK_REALTIME, &timeout );
411 *  rtems_test_assert( !status );
412 *  timeout.tv_sec += 1;
413 *  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
414 *  if ( status != EINVAL )
415 *    printf( "status = %d\n", status );
416 *  rtems_test_assert( status == EINVAL );
417 */
418  puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)");
419
420  empty_line();
421
422  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
423  rtems_test_assert( !status );
424
425/* switch to task3 to allow it to wait for broadcast signal */
426
427  puts( "Init: sleep - switch to Task_3" );
428  sleep( 1 );
429
430/* destroy the mutex so Task3 can not acguire at the end of Wait_support */
431
432  status = pthread_mutex_destroy( &Mutex_id );
433  rtems_test_assert( !status );
434
435/* signal a condition variable to task3 */
436
437  puts( "Init: pthread_cond_signal" );
438  status = pthread_cond_signal( &Cond1_id );
439
440  puts( "Init: sleep - switch to Task_3" );
441  sleep( 1 );
442
443  TEST_END();
444  rtems_test_exit( 0 );
445
446  return NULL; /* just so the compiler thinks we returned something */
447}
Note: See TracBrowser for help on using the repository browser.