source: rtems/testsuites/psxtests/psx05/init.c @ fb7d080

4.104.114.84.95
Last change on this file since fb7d080 was fb7d080, checked in by Joel Sherrill <joel.sherrill@…>, on 08/14/96 at 20:25:57

pthread_mutex_init returns EAGAIN not ENOMEM when there are too many mutexes.

  • Property mode set to 100644
File size: 16.6 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 <errno.h>
16
17#define MUTEX_BAD_ID -2
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 (not initialized attr)" );
228  status = pthread_mutex_init( &Mutex_id, &destroyed_attr );
229  assert( status == EINVAL );
230
231  /* must get around error checks in attribute set routines */
232  attr.protocol = -1;
233
234  puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" );
235  status = pthread_mutex_init( &Mutex_id, &attr );
236  assert( status == EINVAL );
237
238  /* must get around error checks in attribute set routines */
239  attr.protocol = PTHREAD_PRIO_INHERIT;
240  attr.prio_ceiling = -1;
241
242  puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" );
243  status = pthread_mutex_init( &Mutex_id, &attr );
244  assert( status == EINVAL );
245
246  /* now set up for a success pthread_mutex_init */
247
248  puts( "Init: Resetting mutex attributes" );
249  status = pthread_mutexattr_init( &attr );
250  assert( !status );
251
252  puts( "Init: Changing mutex attributes" );
253  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
254  assert( !status );
255 
256  status = pthread_mutexattr_setprioceiling( &attr, 128 );
257  assert( !status );
258 
259  status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
260  assert( !status );
261 
262  Print_mutexattr( "Init: ", &attr );
263 
264  puts( "Init: Resetting mutex attributes" );
265  status = pthread_mutexattr_init( &attr );
266  assert( !status );
267
268  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
269  status = pthread_mutex_init( &Mutex_id, &attr );
270  if ( status )
271    printf( "status = %d\n", status );
272  assert( !status );
273
274  puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" );
275  status = pthread_mutex_trylock( &Mutex_bad_id );
276  if ( status != EINVAL )
277    printf( "status = %d\n", status );
278  assert( status == EINVAL );
279
280  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
281  status = pthread_mutex_trylock( &Mutex_id );
282  if ( status )
283    printf( "status = %d\n", status );
284  assert( !status );
285
286  puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
287  status = pthread_mutex_trylock( &Mutex_id );
288  if ( status != EDEADLK )
289    printf( "status = %d\n", status );
290  assert( status == EDEADLK );
291
292  puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
293  status = pthread_mutex_lock( &Mutex_id );
294  if ( status != EDEADLK )
295    printf( "status = %d\n", status );
296  assert( status == EDEADLK );
297
298  puts( "Init: Sleep 1 second" );
299
300  sleep( 1 );
301 
302     /* switch to task 1 */
303
304  puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" );
305  status = pthread_mutex_unlock( &Mutex_bad_id );
306  if ( status != EINVAL )
307    printf( "status = %d\n", status );
308  assert( status == EINVAL );
309
310  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
311  status = pthread_mutex_unlock( &Mutex_id );
312  if ( status )
313    printf( "status = %d\n", status );
314  assert( !status );
315
316  puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
317  status = pthread_mutex_unlock( &Mutex_id );
318  if ( status != EPERM )
319    printf( "status = %d\n", status );
320  assert( status == EPERM );
321
322  times.tv_sec = 0;
323  times.tv_nsec = 500000000;
324  puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
325  status = pthread_mutex_timedlock( &Mutex_id, &times );
326  if ( status != EAGAIN )
327    printf( "status = %d\n", status );
328  assert( status == EAGAIN );
329
330     /* switch to idle */
331
332  puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
333
334  /* destroy a mutex */
335
336  empty_line();
337
338  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
339  status = pthread_mutex_init( &Mutex2_id, &attr );
340  if ( status )
341    printf( "status = %d\n", status );
342  assert( !status );
343
344  puts( "Init: pthread_mutex_init - EAGAIN (too many)" );
345  status = pthread_mutex_init( &Mutex3_id, &attr );
346  assert( status == EAGAIN );
347
348  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
349  status = pthread_mutexattr_destroy( &attr );
350  assert( !status );
351
352  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
353  status = pthread_mutex_destroy( &Mutex2_id );
354  assert( !status );
355 
356  puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" );
357  status = pthread_mutex_destroy( &Mutex_bad_id );
358  assert( status == EINVAL );
359 
360  /* destroy a busy mutex */
361 
362  empty_line();
363 
364  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
365  status = pthread_mutexattr_init( &attr );
366  assert( !status );
367
368  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
369  status = pthread_mutex_init( &Mutex2_id, &attr );
370  assert( !status );
371 
372  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
373  status = pthread_mutex_trylock( &Mutex2_id );
374  if ( status )
375    printf( "status = %d\n", status );
376  assert( !status );
377
378  puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
379  status = pthread_mutex_destroy( &Mutex2_id );
380  if ( status != EBUSY )
381    printf( "status = %d\n", status );
382  assert( status == EBUSY );
383
384  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
385  status = pthread_mutex_unlock( &Mutex2_id );
386  assert( !status );
387
388  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
389  status = pthread_mutex_destroy( &Mutex2_id );
390  assert( !status );
391
392  /* priority inherit mutex */
393
394  empty_line();
395 
396  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
397  status = pthread_mutexattr_init( &attr );
398  assert( !status );
399
400  puts(
401    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
402  );
403  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
404  assert( !status );
405
406  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
407  status = pthread_mutex_init( &Mutex2_id, &attr );
408  assert( !status );
409
410  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
411  status = pthread_mutex_trylock( &Mutex2_id );
412  assert( !status );
413
414  /* create a thread at a lower priority */
415 
416  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
417  assert( !status );
418 
419  /* set priority of Task2 to highest priority */
420 
421  param.sched_priority = 255;
422 
423  puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
424  status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
425  assert( !status );
426
427  /* switching to Task2 */
428
429  status = pthread_getschedparam( pthread_self(), &policy, &param );
430  assert( !status );
431  printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
432
433  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
434  status = pthread_mutex_unlock( &Mutex2_id );
435  assert( !status );
436 
437  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
438  status = pthread_mutexattr_destroy( &attr );
439  assert( !status );
440
441  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
442  status = pthread_mutex_destroy( &Mutex2_id );
443  assert( !status );
444 
445  /* priority ceiling mutex */
446 
447  empty_line();
448 
449  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
450  status = pthread_mutexattr_init( &attr );
451  assert( !status );
452 
453  puts(
454    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
455  );
456  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
457  assert( !status );
458 
459  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
460  status = pthread_mutex_init( &Mutex2_id, &attr );
461  assert( !status );
462 
463  puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" );
464  status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling );
465  assert( status == EINVAL );
466
467  puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" );
468  status = pthread_mutex_getprioceiling( &Mutex2_id, NULL );
469  assert( status == EINVAL );
470
471  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
472  assert( !status );
473  printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
474 
475  puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" );
476  status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling );
477  assert( status == EINVAL );
478
479  puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" );
480  status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling );
481  assert( status == EINVAL );
482
483  puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" );
484  status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL );
485  assert( status == EINVAL );
486
487  /* normal cases of set priority ceiling */
488
489  puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" );
490  status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling );
491  assert( !status );
492  printf(
493    "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
494  );
495 
496  status = pthread_getschedparam( pthread_self(), &policy, &param );
497  assert( !status );
498  printf(
499    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
500  );
501
502  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
503  status = pthread_mutex_trylock( &Mutex2_id );
504  assert( !status );
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  /* create a thread at a higher priority */
513 
514  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
515  assert( !status );
516 
517  /* set priority of Task3 to highest priority */
518 
519  param.sched_priority = 199;
520 
521  status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
522  assert( !status );
523  puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
524 
525  /* DOES NOT SWITCH to Task3 */
526
527  puts( "Init: Sleep 1 second" );
528  assert( !status );
529  sleep( 1 );
530 
531  /* switch to task 3 */
532 
533  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
534  status = pthread_mutex_unlock( &Mutex2_id );
535  assert( !status );
536 
537  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
538  assert( !status );
539  printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
540 
541  /* set priority of Init to highest priority */
542 
543  param.sched_priority = 255;
544 
545  status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
546  assert( !status );
547  puts( "Init: pthread_setschedparam - set Init priority to highest" );
548 
549  puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
550  status = pthread_mutex_lock( &Mutex2_id );
551  if ( status != EINVAL )
552    printf( "status = %d\n", status );
553  assert( status == EINVAL );
554
555  puts( "*** END OF POSIX TEST 5 ***" );
556  exit( 0 );
557
558  return NULL; /* just so the compiler thinks we returned something */
559}
Note: See TracBrowser for help on using the repository browser.