source: rtems/c/src/tests/psxtests/psx10/init.c @ 520949bf

4.104.114.84.95
Last change on this file since 520949bf was 520949bf, checked in by Mark Johannes <Mark.Johannes@…>, on 08/14/96 at 17:16:14

init: added error case for wait timedwait, and wait support

  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
3 *  On-Line Applications Research Corporation (OAR).
4 *  All rights assigned to U.S. Government, 1994.
5 *
6 *  This material may be reproduced by or for the U.S. Government pursuant
7 *  to the copyright license under the clause at DFARS 252.227-7013.  This
8 *  notice must appear in all copies of this file and its derivatives.
9 *
10 *  $Id$
11 */
12
13#define CONFIGURE_INIT
14#include "system.h"
15#include <sched.h>
16
17
18void *POSIX_Init(
19  void *argument
20)
21{
22  int                 status;
23  pthread_condattr_t  attr;
24  pthread_condattr_t  attr_error;
25  int                 pshared;
26  pthread_cond_t      cond;
27  struct timespec     timeout;
28
29  puts( "\n\n*** POSIX TEST 10 ***" );
30
31  puts( "Init: pthread_condattr_init" );
32  status = pthread_condattr_init( &attr );
33  assert( !status );
34
35  puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" );
36  status = pthread_condattr_init( NULL );
37  if ( status != EINVAL )
38    printf( "status = %d\n", status );
39  assert( status == EINVAL );
40
41  puts( "Init: pthread_condattr_destroy" );
42  status = pthread_condattr_destroy( &attr );
43  assert( !status );
44
45  puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" );
46  status = pthread_condattr_destroy( NULL );
47  if ( status != EINVAL )
48    printf( "status = %d\n", status );
49  assert( status == EINVAL );
50
51  puts( "Init: pthread_condattr_init" );
52  status = pthread_condattr_init( &attr );
53  assert( !status );
54
55  puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" );
56  status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
57  assert( !status );
58
59  puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" );
60  status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE );
61  assert( !status );
62
63  status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE );
64  if ( status != EINVAL )
65    printf( "status = %d\n", status );
66  assert( status == EINVAL );
67  puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" );
68
69  status = pthread_condattr_setpshared( &attr, 0xFFFFFF );
70  if ( status != EINVAL )
71    printf( "status = %d\n", status );
72  assert( status == EINVAL );
73  puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" );
74
75  status = pthread_condattr_getpshared( &attr, &pshared );
76  assert( !status );
77  printf( "Init: pthread_condattr_getpshared - %d\n", pshared );
78
79  status = pthread_condattr_getpshared( NULL, &pshared );
80  if ( status != EINVAL )
81    printf( "status = %d\n", status );
82  assert( status == EINVAL );
83  puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" );
84
85  puts( "Init: pthread_cond_init - NULL attr" );
86  status = pthread_cond_init( &cond, NULL );
87  assert( !status );
88
89/* error for attribute not initialized */
90
91  status = pthread_cond_init( &cond, &attr_error );
92  if ( status != EINVAL )
93    printf( "status = %d\n", status );
94  assert( status == EINVAL );
95  puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" );
96
97  status = pthread_cond_init( &cond, NULL );
98  if ( status != ENOMEM )
99    printf( "status = %d\n", status );
100  assert( status == ENOMEM );
101  puts( "Init: pthread_cond_init - ENOMEM (too many conds)" );
102
103  puts( "Init: pthread_cond_destroy" );
104  status = pthread_cond_destroy( &cond );
105  assert( !status );
106
107/* error for bad condition variable passed */
108
109  status = pthread_cond_destroy( NULL );
110  if ( status != EINVAL )
111    printf( "status = %d\n", status );
112  assert( status == EINVAL );
113  puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" );
114
115/* initiailize the attribute for the rest of the test */
116
117  puts( "Init: pthread_cond_init - attr" );
118  status = pthread_cond_init( &Cond1_id, &attr );
119  assert( !status );
120
121/* signal task1 with a condition variable */
122
123  empty_line();
124
125  status = pthread_create( &Task_id, NULL, Task_1, NULL );
126  assert( !status );
127
128/* switch to task1 to allow it to wait for a condition variable */
129
130  puts( "Init: sleep to switch to Task_1" );
131  sleep( 1 );
132
133  status = pthread_cond_destroy( &Cond1_id );
134  if ( status != EBUSY )
135    printf( "status = %d\n", status );
136  assert( status == EBUSY );
137  puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" );
138
139  puts( "Init: pthread_cond_signal" );
140  status = pthread_cond_signal( &Cond1_id );
141  assert( !status );
142
143  empty_line();
144
145  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
146  assert( !status );
147
148/* switch to task1 and task2 to allow them to wait for broadcast signal */
149
150  puts( "Init: sleep - switch to Task_1 and Task_2" );
151  sleep( 1 );
152
153/* broadcast a condition variable to task1 and task2 */
154
155  puts( "Init: pthread_cond_broadcast" );
156  status = pthread_cond_broadcast( &Cond1_id );
157  assert( !status );
158
159  puts( "Init: sleep - switch to Task_1" );
160  sleep( 0 );
161
162/* timedwait case - timeout */
163
164  status = pthread_mutex_lock( &Mutex_id );
165  assert( !status );
166
167/* set timeout to 3 seconds */
168
169  timeout.tv_sec = 3;
170
171  puts( "Init: pthread_cond_timedwait for 3 seconds" );
172  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
173  if ( status != ETIMEDOUT )
174    printf( "status = %d\n", status );
175  assert( status == ETIMEDOUT );
176  puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" );
177
178/* remaining error messages */
179
180  empty_line();
181
182/* errors for bad variable passed */
183
184  status = pthread_cond_signal( NULL );
185  if ( status != EINVAL )
186    printf( "status = %d\n", status );
187  assert( status == EINVAL );
188  puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" );
189
190  status = pthread_cond_broadcast( NULL );
191  if ( status != EINVAL )
192    printf( "status = %d\n", status );
193  assert( status == EINVAL );
194  puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" );
195
196/* acquire mutex so errors will occur */
197
198  status = pthread_mutex_lock( &Mutex_id );
199  assert( !status );
200
201  status = pthread_cond_wait( NULL, &Mutex_id );
202  if ( status != EINVAL )
203    printf( "status = %d\n", status );
204  assert( status == EINVAL );
205  puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" );
206
207  status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout );
208  if ( status != EINVAL )
209    printf( "status = %d\n", status );
210  assert( status == EINVAL );
211  puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" );
212
213  status = pthread_cond_wait( &Cond1_id, NULL );
214  if ( status != EINVAL )
215    printf( "status = %d\n", status );
216  assert( status == EINVAL );
217  puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" );
218 
219  status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout );
220  if ( status != EINVAL )
221    printf( "status = %d\n", status );
222  assert( status == EINVAL );
223  puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" );
224
225  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL );
226  if ( status != EINVAL )
227    printf( "status = %d\n", status );
228  assert( status == EINVAL );
229  puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" );
230
231  timeout.tv_sec = -1;
232  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
233  if ( status != EINVAL )
234    printf( "status = %d\n", status );
235  assert( status == EINVAL );
236  puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_sec invalid)" );
237
238  timeout.tv_sec = 2;
239  timeout.tv_nsec = -1;
240  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
241  if ( status != EINVAL )
242    printf( "status = %d\n", status );
243  assert( status == EINVAL );
244  puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_nsec invalid)" );
245
246  timeout.tv_sec = 2;
247  timeout.tv_nsec = 2;
248  timeout.tv_nsec = 0x7FFFFFFF;
249  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
250  if ( status != EINVAL )
251    printf( "status = %d\n", status );
252  assert( status == EINVAL );
253  puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_nsec to large)" );
254
255/* unlock mutex for rest of test */
256
257  status = pthread_mutex_unlock( &Mutex_id );
258  assert( !status );
259
260/* wait and timedwait without mutex */
261
262  status = pthread_cond_wait( &Cond1_id, &Mutex_id );
263  if ( status != EINVAL )
264    printf( "status = %d\n", status );
265  assert( status == EINVAL );
266  puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" );
267
268  status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout );
269  if ( status != EINVAL )
270    printf( "status = %d\n", status );
271  assert( status == EINVAL );
272  puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)");
273
274  empty_line();
275
276  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
277  assert( !status );
278
279/* switch to task3 to allow it to wait for broadcast signal */
280
281  puts( "Init: sleep - switch to Task_3" );
282  sleep( 1 );
283
284/* destroy the mutex so Task3 can not acguire at the end of Wait_support */
285
286  status = pthread_mutex_destroy( &Mutex_id );
287  assert( !status );
288
289/* signal a condition variable to task3 */
290
291  puts( "Init: pthread_cond_signal" );
292  status = pthread_cond_signal( &Cond1_id );
293
294  puts( "Init: sleep - switch to Task_3" );
295  sleep( 1 );
296
297  puts( "*** END OF POSIX TEST 5 ***" );
298  exit( 0 );
299
300  return NULL; /* just so the compiler thinks we returned something */
301}
Note: See TracBrowser for help on using the repository browser.