source: rtems/testsuites/psxtests/psx05/init.c @ 41616f6

4.104.114.84.95
Last change on this file since 41616f6 was 41616f6, checked in by Joel Sherrill <joel.sherrill@…>, on Dec 3, 1998 at 11:01:22 PM

Changed to account for ownership only being tracked when a priority
blocking protocol is used.

  • Property mode set to 100644
File size: 17.1 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  /*
273   *  Set the protocol to priority ceiling so the owner check happens
274   *  and the EPERM test (later) will work.
275   */
276
277  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
278  assert( !status );
279 
280  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
281  status = pthread_mutex_init( &Mutex_id, &attr );
282  if ( status )
283    printf( "status = %d\n", status );
284  assert( !status );
285
286  puts( "Init: pthread_mutex_init - EBUSY (attempt to initialize an existing mutex)" );
287  status = pthread_mutex_init( &Mutex_id, &attr );
288  if ( !status )
289    printf( "status = %d\n", status );
290  assert( status == EBUSY );
291
292  puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" );
293  status = pthread_mutex_trylock( &Mutex_bad_id );
294  if ( status != EINVAL )
295    printf( "status = %d\n", status );
296  assert( status == EINVAL );
297
298  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
299  status = pthread_mutex_trylock( &Mutex_id );
300  if ( status )
301    printf( "status = %d\n", status );
302  assert( !status );
303
304  puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
305  status = pthread_mutex_trylock( &Mutex_id );
306  if ( status != EDEADLK )
307    printf( "status = %d\n", status );
308  assert( status == EDEADLK );
309
310  puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
311  status = pthread_mutex_lock( &Mutex_id );
312  if ( status != EDEADLK )
313    printf( "status = %d\n", status );
314  assert( status == EDEADLK );
315
316  puts( "Init: Sleep 1 second" );
317
318  sleep( 1 );
319 
320     /* switch to task 1 */
321
322  puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" );
323  status = pthread_mutex_unlock( &Mutex_bad_id );
324  if ( status != EINVAL )
325    printf( "status = %d\n", status );
326  assert( status == EINVAL );
327
328  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
329  status = pthread_mutex_unlock( &Mutex_id );
330  if ( status )
331    printf( "status = %d\n", status );
332  assert( !status );
333
334  puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
335  status = pthread_mutex_unlock( &Mutex_id );
336  if ( status != EPERM )
337    printf( "status = %d\n", status );
338  assert( status == EPERM );
339
340  times.tv_sec = 0;
341  times.tv_nsec = 500000000;
342  puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
343  status = pthread_mutex_timedlock( &Mutex_id, &times );
344  if ( status != EAGAIN )
345    printf( "status = %d\n", status );
346  assert( status == EAGAIN );
347
348     /* switch to idle */
349
350  puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
351
352  /* destroy a mutex */
353
354  empty_line();
355
356  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
357  status = pthread_mutex_init( &Mutex2_id, &attr );
358  if ( status )
359    printf( "status = %d\n", status );
360  assert( !status );
361
362  puts( "Init: pthread_mutex_init - EAGAIN (too many)" );
363  status = pthread_mutex_init( &Mutex3_id, &attr );
364  assert( status == EAGAIN );
365
366  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
367  status = pthread_mutexattr_destroy( &attr );
368  assert( !status );
369
370  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
371  status = pthread_mutex_destroy( &Mutex2_id );
372  assert( !status );
373 
374  puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" );
375  status = pthread_mutex_destroy( &Mutex_bad_id );
376  assert( status == EINVAL );
377 
378  /* destroy a busy mutex */
379 
380  empty_line();
381 
382  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
383  status = pthread_mutexattr_init( &attr );
384  assert( !status );
385
386  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
387  status = pthread_mutex_init( &Mutex2_id, &attr );
388  assert( !status );
389 
390  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
391  status = pthread_mutex_trylock( &Mutex2_id );
392  if ( status )
393    printf( "status = %d\n", status );
394  assert( !status );
395
396  puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
397  status = pthread_mutex_destroy( &Mutex2_id );
398  if ( status != EBUSY )
399    printf( "status = %d\n", status );
400  assert( status == EBUSY );
401
402  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
403  status = pthread_mutex_unlock( &Mutex2_id );
404  assert( !status );
405
406  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
407  status = pthread_mutex_destroy( &Mutex2_id );
408  assert( !status );
409
410  /* priority inherit mutex */
411
412  empty_line();
413 
414  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
415  status = pthread_mutexattr_init( &attr );
416  assert( !status );
417
418  puts(
419    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
420  );
421  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
422  assert( !status );
423
424  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
425  status = pthread_mutex_init( &Mutex2_id, &attr );
426  assert( !status );
427
428  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
429  status = pthread_mutex_trylock( &Mutex2_id );
430  assert( !status );
431
432  /* create a thread at a lower priority */
433 
434  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
435  assert( !status );
436 
437  /* set priority of Task2 to highest priority */
438 
439  param.sched_priority = 254;
440 
441  puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
442  status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
443  assert( !status );
444
445  /* switching to Task2 */
446
447  status = pthread_getschedparam( pthread_self(), &policy, &param );
448  assert( !status );
449  printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
450
451  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
452  status = pthread_mutex_unlock( &Mutex2_id );
453  assert( !status );
454 
455  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
456  status = pthread_mutexattr_destroy( &attr );
457  assert( !status );
458
459  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
460  status = pthread_mutex_destroy( &Mutex2_id );
461  assert( !status );
462 
463  /* priority ceiling mutex */
464 
465  empty_line();
466 
467  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
468  status = pthread_mutexattr_init( &attr );
469  assert( !status );
470 
471  puts(
472    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
473  );
474  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
475  assert( !status );
476 
477  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
478  status = pthread_mutex_init( &Mutex2_id, &attr );
479  assert( !status );
480 
481  puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" );
482  status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling );
483  assert( status == EINVAL );
484
485  puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" );
486  status = pthread_mutex_getprioceiling( &Mutex2_id, NULL );
487  assert( status == EINVAL );
488
489  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
490  assert( !status );
491  printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
492 
493  puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" );
494  status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling );
495  assert( status == EINVAL );
496
497  puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" );
498  status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling );
499  assert( status == EINVAL );
500
501  puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" );
502  status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL );
503  assert( status == EINVAL );
504
505  /* normal cases of set priority ceiling */
506
507  puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" );
508  status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling );
509  assert( !status );
510  printf(
511    "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
512  );
513 
514  status = pthread_getschedparam( pthread_self(), &policy, &param );
515  assert( !status );
516  printf(
517    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
518  );
519
520  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
521  status = pthread_mutex_trylock( &Mutex2_id );
522  assert( !status );
523 
524  status = pthread_getschedparam( pthread_self(), &policy, &param );
525  assert( !status );
526  printf(
527    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
528  );
529
530  /* create a thread at a higher priority */
531 
532  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
533  assert( !status );
534 
535  /* set priority of Task3 to highest priority */
536 
537  param.sched_priority = 199;
538 
539  status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
540  assert( !status );
541  puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
542 
543  /* DOES NOT SWITCH to Task3 */
544
545  puts( "Init: Sleep 1 second" );
546  assert( !status );
547  sleep( 1 );
548 
549  /* switch to task 3 */
550 
551  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
552  status = pthread_mutex_unlock( &Mutex2_id );
553  assert( !status );
554 
555  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
556  assert( !status );
557  printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
558 
559  /* set priority of Init to highest priority */
560 
561  param.sched_priority = 254;
562 
563  status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
564  assert( !status );
565  puts( "Init: pthread_setschedparam - set Init priority to highest" );
566 
567  puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
568  status = pthread_mutex_lock( &Mutex2_id );
569  if ( status != EINVAL )
570    printf( "status = %d\n", status );
571  assert( status == EINVAL );
572
573  puts( "*** END OF POSIX TEST 5 ***" );
574  exit( 0 );
575
576  return NULL; /* just so the compiler thinks we returned something */
577}
Note: See TracBrowser for help on using the repository browser.