source: rtems/c/src/tests/psxtests/psx05/init.c @ 60b791ad

4.104.114.84.95
Last change on this file since 60b791ad was 60b791ad, checked in by Joel Sherrill <joel.sherrill@…>, on 02/17/98 at 23:46:28

updated copyright to 1998

  • Property mode set to 100644
File size: 16.9 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-1998.
3 *  On-Line Applications Research Corporation (OAR).
4 *  Copyright assigned to U.S. Government, 1994.
5 *
6 *  The license and distribution terms for this file may be
7 *  found in the file LICENSE in this distribution or at
8 *  http://www.OARcorp.com/rtems/license.html.
9 *
10 *  $Id$
11 */
12
13#define CONFIGURE_INIT
14#include "system.h"
15#include <errno.h>
16
17#define MUTEX_BAD_ID 0xfffffffe
18
19void Print_mutexattr(
20  char                *msg,
21  pthread_mutexattr_t *attr
22)
23{
24  int status;
25  int protocol;
26  int prioceiling;
27  int pshared;
28
29  /* protocol */
30
31  status = pthread_mutexattr_getprotocol( attr, &protocol );
32  assert( !status );
33
34  printf( "%smutex protocol is (%d) -- ", msg, protocol );
35  switch ( protocol ) {
36    case PTHREAD_PRIO_NONE:
37      puts( "PTHREAD_PRIO_NONE" );
38      break;
39    case PTHREAD_PRIO_INHERIT:
40      puts( "PTHREAD_PRIO_INHERIT" );
41      break;
42    case PTHREAD_PRIO_PROTECT:
43      puts( "PTHREAD_PRIO_PROTECT" );
44      break;
45    default:
46      puts( "UNKNOWN" );
47      assert( 0 );
48      break;
49  }
50
51  /* priority ceiling */
52
53  status = pthread_mutexattr_getprioceiling( attr, &prioceiling );
54  assert( !status );
55  printf( "%smutex priority ceiling is %d\n", msg, prioceiling );
56
57  /* process shared */
58
59  status = pthread_mutexattr_getpshared( attr, &pshared );
60  assert( !status );
61  printf( "%smutex process shared is (%d) -- ", msg, pshared );
62  switch ( pshared ) {
63    case PTHREAD_PROCESS_PRIVATE:
64      puts( "PTHREAD_PROCESS_PRIVATE" );
65      break;
66    case PTHREAD_PROCESS_SHARED:
67      puts( "PTHREAD_PROCESS_SHARED" );
68      break;
69    default:
70      puts( "UNKNOWN" );
71      assert( 0 );
72      break;
73  }
74}
75
76void *POSIX_Init(
77  void *argument
78)
79{
80  int                  status;
81  pthread_mutexattr_t  attr;
82  pthread_mutexattr_t  destroyed_attr;
83  struct timespec      times;
84  struct sched_param   param;
85  int                  pshared;
86  int                  policy;
87  int                  protocol;
88  int                  ceiling;
89  int                  old_ceiling;
90
91  assert( MUTEX_BAD_ID != PTHREAD_MUTEX_INITIALIZER );
92  Mutex_bad_id = MUTEX_BAD_ID;
93
94  puts( "\n\n*** POSIX TEST 5 ***" );
95
96  /* set the time of day, and print our buffer in multiple ways */
97
98  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
99
100  /* get id of this thread */
101
102  Init_id = pthread_self();
103  printf( "Init's ID is 0x%08x\n", Init_id );
104 
105  /* tes pthread_mutex_attr_init */
106
107  puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" );
108  status = pthread_mutexattr_init( NULL );
109  assert( status == EINVAL );
110
111  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
112  status = pthread_mutexattr_init( &attr );
113  assert( !status );
114
115  Print_mutexattr( "Init: ", &attr );
116
117  /* create an "uninitialized" attribute structure */
118
119  status = pthread_mutexattr_init( &destroyed_attr );
120  assert( !status );
121
122  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
123  status = pthread_mutexattr_destroy( &destroyed_attr );
124  assert( !status );
125
126  puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" );
127  status = pthread_mutexattr_destroy( NULL );
128  assert( status == EINVAL );
129
130  puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" );
131  status = pthread_mutexattr_destroy( &destroyed_attr );
132  assert( status == EINVAL );
133
134  /* error cases for set and get pshared attribute */
135
136  empty_line();
137
138  puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" );
139  status = pthread_mutexattr_getpshared( NULL, &pshared );
140  assert( status == EINVAL );
141 
142  puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" );
143  status = pthread_mutexattr_getpshared( &attr, NULL );
144  assert( status == EINVAL );
145 
146  puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" );
147  status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared );
148  assert( status == EINVAL );
149
150  pshared = PTHREAD_PROCESS_PRIVATE;
151  puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" );
152  status = pthread_mutexattr_setpshared( NULL, pshared );
153  assert( status == EINVAL );
154 
155  pshared = PTHREAD_PROCESS_PRIVATE;
156  puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" );
157  status = pthread_mutexattr_setpshared( &destroyed_attr, pshared );
158  assert( status == EINVAL );
159
160  /* error cases for set and get protocol attribute */
161
162  empty_line();
163
164  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" );
165  status = pthread_mutexattr_getprotocol( NULL, &protocol );
166  assert( status == EINVAL );
167 
168  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" );
169  status = pthread_mutexattr_getprotocol( &attr, NULL );
170  assert( status == EINVAL );
171 
172  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" );
173  status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol );
174  assert( status == EINVAL );
175 
176  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" );
177  status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE );
178  assert( status == EINVAL );
179 
180  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" );
181  status = pthread_mutexattr_setprotocol( &attr, -1 );
182  assert( status == EINVAL );
183 
184  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" );
185  status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 );
186  assert( status == EINVAL );
187
188  /* error cases for set and get prioceiling attribute */
189
190  empty_line();
191 
192  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" );
193  status = pthread_mutexattr_getprioceiling( NULL, &ceiling );
194  assert( status == EINVAL );
195 
196  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" );
197  status = pthread_mutexattr_getprioceiling( &attr, NULL );
198  assert( status == EINVAL );
199 
200  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" );
201  status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling );
202  assert( status == EINVAL );
203 
204  puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" );
205  status = pthread_mutexattr_setprioceiling( NULL, 128 );
206  assert( status == EINVAL );
207 
208  puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (invalid priority)" );
209  status = pthread_mutexattr_setprioceiling( &attr, 512 );
210  if ( status != EINVAL )
211    printf( "status = %d\n", status );
212  assert( status == EINVAL );
213 
214  puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" );
215  status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 );
216  assert( status == EINVAL );
217
218  /* create a thread */
219
220  status = pthread_create( &Task_id, NULL, Task_1, NULL );
221  assert( !status );
222
223  /* now try some basic mutex operations */
224
225  empty_line();
226
227  puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" );
228  status = pthread_mutex_init( NULL, &attr );
229  assert( status == EINVAL );
230
231  puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" );
232  status = pthread_mutex_init( &Mutex_id, &destroyed_attr );
233  assert( status == EINVAL );
234
235  /* must get around error checks in attribute set routines */
236  attr.protocol = -1;
237
238  puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" );
239  status = pthread_mutex_init( &Mutex_id, &attr );
240  assert( status == EINVAL );
241
242  /* must get around error checks in attribute set routines */
243  attr.protocol = PTHREAD_PRIO_INHERIT;
244  attr.prio_ceiling = -1;
245
246  puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" );
247  status = pthread_mutex_init( &Mutex_id, &attr );
248  assert( status == EINVAL );
249
250  /* now set up for a success pthread_mutex_init */
251
252  puts( "Init: Resetting mutex attributes" );
253  status = pthread_mutexattr_init( &attr );
254  assert( !status );
255
256  puts( "Init: Changing mutex attributes" );
257  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
258  assert( !status );
259 
260  status = pthread_mutexattr_setprioceiling( &attr, 128 );
261  assert( !status );
262 
263  status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
264  assert( !status );
265 
266  Print_mutexattr( "Init: ", &attr );
267 
268  puts( "Init: Resetting mutex attributes" );
269  status = pthread_mutexattr_init( &attr );
270  assert( !status );
271
272  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
273  status = pthread_mutex_init( &Mutex_id, &attr );
274  if ( status )
275    printf( "status = %d\n", status );
276  assert( !status );
277
278  puts( "Init: pthread_mutex_init - EBUSY (attempt to initialize an existing mutex)" );
279  status = pthread_mutex_init( &Mutex_id, &attr );
280  if ( !status )
281    printf( "status = %d\n", status );
282  assert( status == EBUSY );
283
284  puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" );
285  status = pthread_mutex_trylock( &Mutex_bad_id );
286  if ( status != EINVAL )
287    printf( "status = %d\n", status );
288  assert( status == EINVAL );
289
290  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
291  status = pthread_mutex_trylock( &Mutex_id );
292  if ( status )
293    printf( "status = %d\n", status );
294  assert( !status );
295
296  puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
297  status = pthread_mutex_trylock( &Mutex_id );
298  if ( status != EDEADLK )
299    printf( "status = %d\n", status );
300  assert( status == EDEADLK );
301
302  puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
303  status = pthread_mutex_lock( &Mutex_id );
304  if ( status != EDEADLK )
305    printf( "status = %d\n", status );
306  assert( status == EDEADLK );
307
308  puts( "Init: Sleep 1 second" );
309
310  sleep( 1 );
311 
312     /* switch to task 1 */
313
314  puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" );
315  status = pthread_mutex_unlock( &Mutex_bad_id );
316  if ( status != EINVAL )
317    printf( "status = %d\n", status );
318  assert( status == EINVAL );
319
320  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
321  status = pthread_mutex_unlock( &Mutex_id );
322  if ( status )
323    printf( "status = %d\n", status );
324  assert( !status );
325
326  puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
327  status = pthread_mutex_unlock( &Mutex_id );
328  if ( status != EPERM )
329    printf( "status = %d\n", status );
330  assert( status == EPERM );
331
332  times.tv_sec = 0;
333  times.tv_nsec = 500000000;
334  puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
335  status = pthread_mutex_timedlock( &Mutex_id, &times );
336  if ( status != EAGAIN )
337    printf( "status = %d\n", status );
338  assert( status == EAGAIN );
339
340     /* switch to idle */
341
342  puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
343
344  /* destroy a mutex */
345
346  empty_line();
347
348  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
349  status = pthread_mutex_init( &Mutex2_id, &attr );
350  if ( status )
351    printf( "status = %d\n", status );
352  assert( !status );
353
354  puts( "Init: pthread_mutex_init - EAGAIN (too many)" );
355  status = pthread_mutex_init( &Mutex3_id, &attr );
356  assert( status == EAGAIN );
357
358  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
359  status = pthread_mutexattr_destroy( &attr );
360  assert( !status );
361
362  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
363  status = pthread_mutex_destroy( &Mutex2_id );
364  assert( !status );
365 
366  puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" );
367  status = pthread_mutex_destroy( &Mutex_bad_id );
368  assert( status == EINVAL );
369 
370  /* destroy a busy mutex */
371 
372  empty_line();
373 
374  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
375  status = pthread_mutexattr_init( &attr );
376  assert( !status );
377
378  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
379  status = pthread_mutex_init( &Mutex2_id, &attr );
380  assert( !status );
381 
382  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
383  status = pthread_mutex_trylock( &Mutex2_id );
384  if ( status )
385    printf( "status = %d\n", status );
386  assert( !status );
387
388  puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
389  status = pthread_mutex_destroy( &Mutex2_id );
390  if ( status != EBUSY )
391    printf( "status = %d\n", status );
392  assert( status == EBUSY );
393
394  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
395  status = pthread_mutex_unlock( &Mutex2_id );
396  assert( !status );
397
398  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
399  status = pthread_mutex_destroy( &Mutex2_id );
400  assert( !status );
401
402  /* priority inherit mutex */
403
404  empty_line();
405 
406  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
407  status = pthread_mutexattr_init( &attr );
408  assert( !status );
409
410  puts(
411    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
412  );
413  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
414  assert( !status );
415
416  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
417  status = pthread_mutex_init( &Mutex2_id, &attr );
418  assert( !status );
419
420  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
421  status = pthread_mutex_trylock( &Mutex2_id );
422  assert( !status );
423
424  /* create a thread at a lower priority */
425 
426  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
427  assert( !status );
428 
429  /* set priority of Task2 to highest priority */
430 
431  param.sched_priority = 254;
432 
433  puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
434  status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
435  assert( !status );
436
437  /* switching to Task2 */
438
439  status = pthread_getschedparam( pthread_self(), &policy, &param );
440  assert( !status );
441  printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
442
443  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
444  status = pthread_mutex_unlock( &Mutex2_id );
445  assert( !status );
446 
447  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
448  status = pthread_mutexattr_destroy( &attr );
449  assert( !status );
450
451  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
452  status = pthread_mutex_destroy( &Mutex2_id );
453  assert( !status );
454 
455  /* priority ceiling mutex */
456 
457  empty_line();
458 
459  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
460  status = pthread_mutexattr_init( &attr );
461  assert( !status );
462 
463  puts(
464    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
465  );
466  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
467  assert( !status );
468 
469  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
470  status = pthread_mutex_init( &Mutex2_id, &attr );
471  assert( !status );
472 
473  puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" );
474  status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling );
475  assert( status == EINVAL );
476
477  puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" );
478  status = pthread_mutex_getprioceiling( &Mutex2_id, NULL );
479  assert( status == EINVAL );
480
481  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
482  assert( !status );
483  printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
484 
485  puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" );
486  status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling );
487  assert( status == EINVAL );
488
489  puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" );
490  status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling );
491  assert( status == EINVAL );
492
493  puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" );
494  status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL );
495  assert( status == EINVAL );
496
497  /* normal cases of set priority ceiling */
498
499  puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" );
500  status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling );
501  assert( !status );
502  printf(
503    "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
504  );
505 
506  status = pthread_getschedparam( pthread_self(), &policy, &param );
507  assert( !status );
508  printf(
509    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
510  );
511
512  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
513  status = pthread_mutex_trylock( &Mutex2_id );
514  assert( !status );
515 
516  status = pthread_getschedparam( pthread_self(), &policy, &param );
517  assert( !status );
518  printf(
519    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
520  );
521
522  /* create a thread at a higher priority */
523 
524  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
525  assert( !status );
526 
527  /* set priority of Task3 to highest priority */
528 
529  param.sched_priority = 199;
530 
531  status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
532  assert( !status );
533  puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
534 
535  /* DOES NOT SWITCH to Task3 */
536
537  puts( "Init: Sleep 1 second" );
538  assert( !status );
539  sleep( 1 );
540 
541  /* switch to task 3 */
542 
543  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
544  status = pthread_mutex_unlock( &Mutex2_id );
545  assert( !status );
546 
547  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
548  assert( !status );
549  printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
550 
551  /* set priority of Init to highest priority */
552 
553  param.sched_priority = 254;
554 
555  status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
556  assert( !status );
557  puts( "Init: pthread_setschedparam - set Init priority to highest" );
558 
559  puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
560  status = pthread_mutex_lock( &Mutex2_id );
561  if ( status != EINVAL )
562    printf( "status = %d\n", status );
563  assert( status == EINVAL );
564
565  puts( "*** END OF POSIX TEST 5 ***" );
566  exit( 0 );
567
568  return NULL; /* just so the compiler thinks we returned something */
569}
Note: See TracBrowser for help on using the repository browser.