source: rtems/testsuites/psxtests/psx05/init.c @ 1750f5a6

Last change on this file since 1750f5a6 was 1750f5a6, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 24, 2008 at 8:43:58 PM

2008-07-24 Joel Sherrill <joel.sherrill@…>

PR 1291/cpukit

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