source: rtems/testsuites/psxtests/psx05/init.c @ 08311cc3

4.104.114.84.95
Last change on this file since 08311cc3 was 08311cc3, checked in by Joel Sherrill <joel.sherrill@…>, on 11/17/99 at 17:51:34

Updated copyright notice.

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