source: rtems/testsuites/psxtests/psxbarrier01/test.c @ 39615f4

4.104.11
Last change on this file since 39615f4 was 39615f4, checked in by Ralf Corsepius <ralf.corsepius@…>, on Oct 27, 2009 at 2:10:54 PM

Use PRIxpthread_t to print pthread_t's.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 *  This test exercises the POSIX Barrier manager.
3 *
4 *  COPYRIGHT (c) 1989-2009.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#include <assert.h>
15#include <stdio.h>
16#include <errno.h>
17#include <stdlib.h>
18
19/* #define __USE_XOPEN2K XXX already defined on GNU/Linux */
20#include <pthread.h>
21
22#define NUMBER_THREADS 2
23pthread_t ThreadIds[NUMBER_THREADS];
24pthread_barrier_t Barrier;
25void *BarrierThread(void *arg);
26
27void *BarrierThread(void *arg)
28{
29  pthread_t id = *(pthread_t *) arg;
30  int       status;
31
32  printf( "pthread_barrier_wait( &Barrier ) for thread 0x%08" PRIxpthread_t "\n", id );
33  status = pthread_barrier_wait( &Barrier );
34  printf( "pthread_barrier_wait - 0x%08" PRIxpthread_t " released\n", id );
35  assert( (status == 0) || (status == PTHREAD_BARRIER_SERIAL_THREAD) );
36
37  return NULL;
38}
39
40/*
41 *  main entry point to the test
42 */
43
44#if defined(__rtems__)
45int test_main(void);
46
47int test_main(void)
48#else
49int main(
50  int    argc,
51  char **argv
52)
53#endif
54{
55  pthread_barrier_t     bad_barrier = 100;
56  pthread_barrier_t     barrier;
57  pthread_barrierattr_t attr;
58  int                   status;
59  int                   p;
60  int                   i;
61
62  puts( "\n\n*** POSIX BARRIER TEST 01 ***" );
63
64  /*************** NULL POINTER CHECKS *****************/
65  puts( "pthread_barrierattr_init( NULL ) -- EINVAL" );
66  status = pthread_barrierattr_init( NULL );
67  assert( status == EINVAL );
68
69  puts( "pthread_barrierattr_setpshared( NULL, private ) -- EINVAL" );
70  status = pthread_barrierattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE );
71  assert( status == EINVAL );
72
73  puts( "pthread_barrierattr_setpshared( NULL, shared ) -- EINVAL" );
74  status = pthread_barrierattr_setpshared( NULL, PTHREAD_PROCESS_SHARED );
75  assert( status == EINVAL );
76
77  puts( "pthread_barrierattr_getpshared( NULL, &p ) -- EINVAL" );
78  status = pthread_barrierattr_getpshared( NULL, &p );
79  assert( status == EINVAL );
80
81  puts( "pthread_barrierattr_destroy( NULL ) -- EINVAL" );
82  status = pthread_barrierattr_destroy( NULL );
83  assert( status == EINVAL );
84
85  /*************** NOT INITIALIZED CHECKS *****************/
86  /* cheat visibility */
87  attr.is_initialized = 0; 
88  puts( "pthread_barrierattr_setpshared( &attr, shared ) -- EINVAL" );
89  status = pthread_barrierattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
90  assert( status == EINVAL );
91
92  puts( "pthread_barrierattr_getpshared( &attr, NULL ) -- EINVAL" );
93  status = pthread_barrierattr_getpshared( &attr, NULL );
94  assert( status == EINVAL );
95
96  puts( "pthread_barrierattr_destroy( &attr ) -- EINVAL" );
97  status = pthread_barrierattr_destroy( &attr );
98  assert( status == EINVAL );
99
100
101  /*************** ACTUALLY WORK THIS TIME *****************/
102
103  puts( "pthread_barrierattr_init( &attr ) -- OK" );
104  status = pthread_barrierattr_init( &attr );
105  assert( status == 0 );
106
107  puts( "pthread_barrierattr_setpshared( &attr, private ) -- OK" );
108  status = pthread_barrierattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
109  assert( status == 0 );
110
111  puts( "pthread_barrierattr_getpshared( &attr, &p ) -- OK" );
112  status = pthread_barrierattr_getpshared( &attr, &p );
113  assert( status == 0 );
114  assert( p == PTHREAD_PROCESS_PRIVATE );
115
116  puts( "pthread_barrierattr_setpshared( &attr, shared ) -- OK" );
117  status = pthread_barrierattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
118  assert( status == 0 );
119
120  puts( "pthread_barrierattr_getpshared( &attr, &p ) -- OK" );
121  status = pthread_barrierattr_getpshared( &attr, &p );
122  assert( status == 0 );
123  assert( p == PTHREAD_PROCESS_SHARED );
124
125  /*************** BAD PSHARED CHECK *****************/
126  puts( "pthread_barrierattr_setpshared( &attr, private ) -- EINVAL" );
127  status = pthread_barrierattr_setpshared( &attr, ~PTHREAD_PROCESS_PRIVATE );
128  assert( status == EINVAL );
129
130  /*************** DESTROY/REUSE CHECK *****************/
131  puts( "pthread_barrierattr_destroy( &attr ) -- OK" );
132  status = pthread_barrierattr_destroy( &attr );
133  assert( status == 0 );
134
135  puts( "pthread_barrierattr_getpshared( &attr, &p ) destroyed -- EINVAL" );
136  status = pthread_barrierattr_getpshared( &attr, &p );
137  assert( status == EINVAL );
138
139  /*************** pthread_barrier_init ERROR CHECKs *********/
140  /* NULL barrier argument */
141  puts( "pthread_barrier_init( NULL, NULL, 2 ) -- EINVAL" );
142  status = pthread_barrier_init( NULL, NULL, 2 );
143  assert( status == EINVAL );
144
145  /* uninitialized attr argument */
146  puts( "pthread_barrier_init( &barrier, &attr, 2 ) -- EINVAL" );
147  status = pthread_barrier_init( &barrier, &attr, 2 );
148  assert( status == EINVAL );
149
150  /* zero count argument */
151  puts( "pthread_barrierattr_init( &attr ) -- OK" );
152  status = pthread_barrierattr_init( &attr );
153  assert( status == 0 );
154
155  puts( "pthread_barrier_init( &barrier, &attr, 0 ) -- EINVAL" );
156  status = pthread_barrier_init( &barrier, &attr, 0 );
157  assert( status == EINVAL );
158
159  /* allocating too many */
160  puts( "pthread_barrier_init( &barrier, NULL, 1 ) -- OK" );
161  status = pthread_barrier_init( &barrier, NULL, 1 );
162  assert( status == 0 );
163
164  puts( "pthread_barrier_init( &barrier, NULL, 1 ) -- EAGAIN" );
165  status = pthread_barrier_init( &barrier, NULL, 1 );
166  assert( status == EAGAIN );
167
168  /* clean up */
169  puts( "pthread_barrier_destroy( &barrier ) -- OK" );
170  status = pthread_barrier_destroy( &barrier );
171  assert( status == 0 );
172
173  puts( "pthread_barrierattr_destroy( &attr ) -- OK" );
174  status = pthread_barrierattr_destroy( &attr );
175  assert( status == 0 );
176
177  /*************** pthread_barrier_destroy ERROR CHECKs *********/
178  /* NULL barrier argument */
179  puts( "pthread_barrier_destroy( NULL ) -- EINVAL" );
180  status = pthread_barrier_destroy( NULL );
181  assert( status == EINVAL );
182
183  puts( "pthread_barrier_destroy( &bad_barrier ) -- EINVAL" );
184  status = pthread_barrier_destroy( &bad_barrier );
185  assert( status == EINVAL );
186
187  /*************** pthread_barrier_wait ERROR CHECKs *********/
188  /* NULL barrier argument */
189  puts( "pthread_barrier_wait( NULL ) -- EINVAL" );
190  status = pthread_barrier_wait( NULL );
191  assert( status == EINVAL );
192
193  puts( "pthread_barrier_wait( &bad_barrier ) -- EINVAL" );
194  status = pthread_barrier_wait( &bad_barrier );
195  assert( status == EINVAL );
196
197  /*************** ACTUALLY CREATE ONE CHECK *****************/
198  puts( "pthread_barrierattr_init( &attr ) -- OK" );
199  status = pthread_barrierattr_init( &attr );
200  assert( status == 0 );
201
202  puts( "pthread_barrier_init( &barrier, &attr, 2 ) -- OK" );
203  status = pthread_barrier_init( &barrier, &attr, 2 );
204  assert( status == 0 );
205  assert( barrier != 0 );
206
207  puts( "pthread_barrier_destroy( &barrier ) -- OK" );
208  status = pthread_barrier_destroy( &barrier );
209  assert( status == 0 );
210
211  /*************** CREATE THREADS AND LET THEM RELEASE *****************/
212  puts( "pthread_barrier_init( &Barrier, &attr, NUMBER_THREADS ) -- OK" );
213  status = pthread_barrier_init( &Barrier, &attr, NUMBER_THREADS );
214  assert( status == 0 );
215  assert( barrier != 0 );
216
217  for (i=0 ; i<NUMBER_THREADS ; i++ ) {
218
219    /* check for unable to destroy while threads waiting */
220    if (i == NUMBER_THREADS - 1) {
221      puts( "pthread_barrier_destroy( &Barrier ) -- EBUSY" );
222      status = pthread_barrier_destroy( &Barrier );
223      assert( status == EBUSY );
224    }
225
226    /* create a thread to block on the barrier */
227    printf( "Init: pthread_create - thread %d OK\n", i+1 );
228    status = pthread_create(&ThreadIds[i], NULL, BarrierThread, &ThreadIds[i]);
229    assert( !status );
230
231    sleep(1);
232  }
233
234  /*************** END OF TEST *****************/
235  puts( "*** END OF POSIX BARRIER TEST 01 ***" );
236  exit(0);
237}
Note: See TracBrowser for help on using the repository browser.