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

5
Last change on this file since af43554 was af43554, checked in by Sebastian Huber <sebastian.huber@…>, on 10/26/17 at 11:59:11

tests: Remove TEST_INIT

The TEST_EXTERN is a used only by the system.h style tests and they use
CONFIGURE_INIT appropriately.

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 28.7 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2009.
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.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <sched.h>
15
16#define CONFIGURE_INIT
17#include "system.h"
18#include <errno.h>
19#include <limits.h>
20
21#include <rtems/score/todimpl.h>
22
23const char rtems_test_name[] = "PSX 5";
24
25#define MUTEX_BAD_ID 0xfffffffe
26
27void Print_mutexattr(
28  char                *msg,
29  pthread_mutexattr_t *attr
30);
31
32void Print_mutexattr(
33  char                *msg,
34  pthread_mutexattr_t *attr
35)
36{
37  int status;
38  int protocol;
39  int prioceiling;
40  int pshared;
41
42  /* protocol */
43
44  status = pthread_mutexattr_getprotocol( attr, &protocol );
45  rtems_test_assert( !status );
46
47  printf( "%smutex protocol is (%d) -- ", msg, protocol );
48  switch ( protocol ) {
49    case PTHREAD_PRIO_NONE:
50      puts( "PTHREAD_PRIO_NONE" );
51      break;
52    case PTHREAD_PRIO_INHERIT:
53      puts( "PTHREAD_PRIO_INHERIT" );
54      break;
55    case PTHREAD_PRIO_PROTECT:
56      puts( "PTHREAD_PRIO_PROTECT" );
57      break;
58    default:
59      puts( "UNKNOWN" );
60      rtems_test_assert( 0 );
61      break;
62  }
63
64  /* priority ceiling */
65
66  status = pthread_mutexattr_getprioceiling( attr, &prioceiling );
67  rtems_test_assert( !status );
68  printf( "%smutex priority ceiling is %d\n", msg, prioceiling );
69
70  /* process shared */
71
72  status = pthread_mutexattr_getpshared( attr, &pshared );
73  rtems_test_assert( !status );
74  printf( "%smutex process shared is (%d) -- ", msg, pshared );
75  switch ( pshared ) {
76    case PTHREAD_PROCESS_PRIVATE:
77      puts( "PTHREAD_PROCESS_PRIVATE" );
78      break;
79    case PTHREAD_PROCESS_SHARED:
80      puts( "PTHREAD_PROCESS_SHARED" );
81      break;
82    default:
83      puts( "UNKNOWN" );
84      rtems_test_assert( 0 );
85      break;
86  }
87}
88
89static void calculate_abstimeout(
90  struct timespec *times,
91  int              seconds,
92  long             nanoseconds
93)
94{
95  struct timeval       tv1;
96  struct timezone      tz1;
97
98  gettimeofday( &tv1, &tz1 );
99
100  times->tv_sec  = seconds     + tv1.tv_sec;
101  times->tv_nsec = nanoseconds + (tv1.tv_usec * 1000);
102
103  while ( times->tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) {
104    times->tv_sec++;
105    times->tv_nsec -= TOD_NANOSECONDS_PER_SECOND;
106  }
107
108}
109
110static void test_get_priority( void )
111{
112  int                 status;
113  pthread_mutexattr_t attr;
114  pthread_mutex_t     mutex;
115  int                 policy;
116  struct sched_param  param;
117  int                 real_priority;
118
119  status = pthread_getschedparam( pthread_self(), &policy, &param );
120  rtems_test_assert( status == 0 );
121
122  real_priority = param.sched_priority;
123
124  status = pthread_mutexattr_init( &attr );
125  rtems_test_assert( status == 0 );
126
127  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
128  rtems_test_assert( !status );
129
130  status = pthread_mutexattr_setprioceiling( &attr, real_priority + 1 );
131  rtems_test_assert( status == 0 );
132
133  status = pthread_mutex_init( &mutex, &attr );
134  rtems_test_assert( status == 0 );
135
136  status = pthread_mutexattr_destroy( &attr );
137  rtems_test_assert( status == 0 );
138
139  status = pthread_mutex_lock( &mutex );
140  rtems_test_assert( status == 0 );
141
142  status = pthread_getschedparam( pthread_self(), &policy, &param );
143  rtems_test_assert( status == 0 );
144
145  rtems_test_assert( real_priority == param.sched_priority );
146
147  status = pthread_mutex_unlock( &mutex );
148  rtems_test_assert( status == 0 );
149
150  status = pthread_mutex_destroy( &mutex );
151  rtems_test_assert( status == 0 );
152}
153
154static void *counter_task(void *arg)
155{
156  int *counter;
157
158  counter = arg;
159
160  while ( *counter >= 0 ) {
161    ++(*counter);
162
163    sched_yield();
164  }
165
166  return counter;
167}
168
169static void test_set_priority( void )
170{
171  int                 status;
172  int                 policy;
173  struct sched_param  param;
174  pthread_t           thread;
175  int                 counter;
176  void               *exit_code;
177
178  counter = 0;
179
180  status = pthread_getschedparam( pthread_self(), &policy, &param );
181  rtems_test_assert( status == 0 );
182
183  status = pthread_create( &thread, NULL, counter_task, &counter);
184  rtems_test_assert( status == 0 );
185
186  ++param.sched_priority;
187  status = pthread_setschedparam( pthread_self(), policy, &param );
188  rtems_test_assert( status == 0 );
189
190  rtems_test_assert( counter == 0 );
191
192  --param.sched_priority;
193  status = pthread_setschedparam( pthread_self(), policy, &param );
194  rtems_test_assert( status == 0 );
195
196  rtems_test_assert( counter == 1 );
197
198  status = pthread_setschedprio( pthread_self(), param.sched_priority + 1 );
199  rtems_test_assert( status == 0 );
200
201  rtems_test_assert( counter == 1 );
202
203  status = pthread_setschedprio( pthread_self(), param.sched_priority );
204  rtems_test_assert( status == 0 );
205
206  rtems_test_assert( counter == 1 );
207
208  counter = -1;
209  sched_yield();
210
211  status = pthread_join( thread, &exit_code );
212  rtems_test_assert( status == 0 );
213  rtems_test_assert( exit_code == &counter );
214  rtems_test_assert( counter == -1 );
215}
216
217static void test_errors_pthread_setschedprio( void )
218{
219  int                status;
220  int                policy;
221  struct sched_param param;
222
223  status = pthread_getschedparam( pthread_self(), &policy, &param );
224  rtems_test_assert( status == 0 );
225
226  status = pthread_setschedprio( pthread_self(), INT_MAX );
227  rtems_test_assert( status == EINVAL );
228
229  status = pthread_setschedprio( 0xdeadbeef, param.sched_priority );
230  rtems_test_assert( status == ESRCH );
231
232  status = pthread_setschedprio( pthread_self(), param.sched_priority );
233  rtems_test_assert( status == 0 );
234}
235
236static void test_mutex_pshared_init(void)
237{
238  pthread_mutex_t mutex;
239  pthread_mutexattr_t attr;
240  int eno;
241
242  eno = pthread_mutexattr_init(&attr);
243  rtems_test_assert(eno == 0);
244
245  eno = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
246  rtems_test_assert(eno == 0);
247
248  eno = pthread_mutex_init(&mutex, &attr);
249  rtems_test_assert(eno == 0);
250
251  eno = pthread_mutex_destroy(&mutex);
252  rtems_test_assert(eno == 0);
253
254  eno = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
255  rtems_test_assert(eno == 0);
256
257  eno = pthread_mutex_init(&mutex, &attr);
258  rtems_test_assert(eno == 0);
259
260  eno = pthread_mutex_destroy(&mutex);
261  rtems_test_assert(eno == 0);
262
263  attr.process_shared = -1;
264
265  eno = pthread_mutex_init(&mutex, &attr);
266  rtems_test_assert(eno == EINVAL);
267
268  eno = pthread_mutexattr_destroy(&attr);
269  rtems_test_assert(eno == 0);
270}
271
272static void test_mutex_null( void )
273{
274  struct timespec to;
275  int eno;
276
277  eno = pthread_mutex_destroy( NULL );
278  rtems_test_assert( eno == EINVAL );
279
280  eno = pthread_mutex_init( NULL, NULL );
281  rtems_test_assert( eno == EINVAL );
282
283  eno = pthread_mutex_lock( NULL );
284  rtems_test_assert( eno == EINVAL );
285
286  to.tv_sec = 1;
287  to.tv_nsec = 1;
288  eno = pthread_mutex_timedlock( NULL, &to );
289  rtems_test_assert( eno == EINVAL );
290
291  eno = pthread_mutex_trylock( NULL );
292  rtems_test_assert( eno == EINVAL );
293
294  eno = pthread_mutex_unlock( NULL );
295  rtems_test_assert( eno == EINVAL );
296}
297
298static void test_mutex_not_initialized( void )
299{
300  pthread_mutex_t mutex;
301  struct timespec to;
302  int eno;
303
304  memset( &mutex, 0xff, sizeof( mutex ) );
305
306  eno = pthread_mutex_destroy( &mutex );
307  rtems_test_assert( eno == EINVAL );
308
309  eno = pthread_mutex_lock( &mutex );
310  rtems_test_assert( eno == EINVAL );
311
312  to.tv_sec = 1;
313  to.tv_nsec = 1;
314  eno = pthread_mutex_timedlock( &mutex, &to );
315  rtems_test_assert( eno == EINVAL );
316
317  eno = pthread_mutex_trylock( &mutex );
318  rtems_test_assert( eno == EINVAL );
319
320  eno = pthread_mutex_unlock( &mutex );
321  rtems_test_assert( eno == EINVAL );
322}
323
324static void test_mutex_invalid_copy( void )
325{
326  pthread_mutex_t mutex;
327  pthread_mutex_t mutex2;
328  struct timespec to;
329  int eno;
330
331  eno = pthread_mutex_init( &mutex, NULL );
332  rtems_test_assert( eno == 0 );
333
334  memcpy( &mutex2, &mutex, sizeof( mutex2 ) );
335
336  eno = pthread_mutex_destroy( &mutex2 );
337  rtems_test_assert( eno == EINVAL );
338
339  eno = pthread_mutex_lock( &mutex2 );
340  rtems_test_assert( eno == EINVAL );
341
342  to.tv_sec = 1;
343  to.tv_nsec = 1;
344  eno = pthread_mutex_timedlock( &mutex2, &to );
345  rtems_test_assert( eno == EINVAL );
346
347  eno = pthread_mutex_trylock( &mutex2 );
348  rtems_test_assert( eno == EINVAL );
349
350  eno = pthread_mutex_unlock( &mutex2 );
351  rtems_test_assert( eno == EINVAL );
352
353  eno = pthread_mutex_destroy( &mutex );
354  rtems_test_assert( eno == 0 );
355}
356
357static void test_mutex_auto_initialization( void )
358{
359  struct timespec to;
360  int eno;
361
362  {
363    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
364
365    eno = pthread_mutex_destroy( &mutex );
366    rtems_test_assert( eno == 0 );
367
368    eno = pthread_mutex_destroy( &mutex );
369    rtems_test_assert( eno == EINVAL );
370  }
371
372  {
373    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
374
375    eno = pthread_mutex_lock( &mutex );
376    rtems_test_assert( eno == 0 );
377  }
378
379  {
380    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
381
382    to.tv_sec = 1;
383    to.tv_nsec = 1;
384    eno = pthread_mutex_timedlock( &mutex, &to );
385    rtems_test_assert( eno == 0 );
386  }
387
388  {
389    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
390
391    eno = pthread_mutex_trylock( &mutex );
392    rtems_test_assert( eno == 0 );
393  }
394
395  {
396    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
397
398    eno = pthread_mutex_unlock( &mutex );
399    rtems_test_assert( eno == EPERM );
400  }
401}
402
403void *POSIX_Init(
404  void *argument
405)
406{
407  int                  status;
408  pthread_mutexattr_t  attr;
409  pthread_mutexattr_t  destroyed_attr;
410  struct timespec      times;
411  struct sched_param   param;
412  int                  pshared;
413  int                  policy;
414  int                  protocol;
415  int                  ceiling;
416  int                  old_ceiling;
417  int                  priority;
418
419  Mutex_bad_id = NULL;
420
421  TEST_BEGIN();
422
423  test_mutex_pshared_init();
424  test_mutex_null();
425  test_mutex_not_initialized();
426  test_mutex_invalid_copy();
427  test_mutex_auto_initialization();
428  test_get_priority();
429  test_set_priority();
430  test_errors_pthread_setschedprio();
431
432  /* set the time of day, and print our buffer in multiple ways */
433
434  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
435
436  /* get id of this thread */
437
438  Init_id = pthread_self();
439  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
440
441  /* test pthread_mutex_attr_init */
442
443  puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" );
444  status = pthread_mutexattr_init( NULL );
445  rtems_test_assert( status == EINVAL );
446
447  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
448  status = pthread_mutexattr_init( &attr );
449  rtems_test_assert( !status );
450
451  Print_mutexattr( "Init: ", &attr );
452
453  /* create an "uninitialized" attribute structure */
454
455  status = pthread_mutexattr_init( &destroyed_attr );
456  rtems_test_assert( !status );
457
458  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
459  status = pthread_mutexattr_destroy( &destroyed_attr );
460  rtems_test_assert( !status );
461
462  puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" );
463  status = pthread_mutexattr_destroy( NULL );
464  rtems_test_assert( status == EINVAL );
465
466  puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" );
467  status = pthread_mutexattr_destroy( &destroyed_attr );
468  rtems_test_assert( status == EINVAL );
469
470  /* error cases for set and get pshared attribute */
471
472  empty_line();
473
474  puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" );
475  status = pthread_mutexattr_getpshared( NULL, &pshared );
476  rtems_test_assert( status == EINVAL );
477
478  puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" );
479  status = pthread_mutexattr_getpshared( &attr, NULL );
480  rtems_test_assert( status == EINVAL );
481
482  puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" );
483  status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared );
484  rtems_test_assert( status == EINVAL );
485
486  pshared = PTHREAD_PROCESS_PRIVATE;
487  puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" );
488  status = pthread_mutexattr_setpshared( NULL, pshared );
489  rtems_test_assert( status == EINVAL );
490
491  pshared = PTHREAD_PROCESS_PRIVATE;
492  puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" );
493  status = pthread_mutexattr_setpshared( &destroyed_attr, pshared );
494  rtems_test_assert( status == EINVAL );
495
496  /* error cases for set and get protocol attribute */
497
498  empty_line();
499
500  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" );
501  status = pthread_mutexattr_getprotocol( NULL, &protocol );
502  rtems_test_assert( status == EINVAL );
503
504  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" );
505  status = pthread_mutexattr_getprotocol( &attr, NULL );
506  rtems_test_assert( status == EINVAL );
507
508  puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" );
509  status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol );
510  rtems_test_assert( status == EINVAL );
511
512  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" );
513  status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE );
514  rtems_test_assert( status == EINVAL );
515
516  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" );
517  status = pthread_mutexattr_setprotocol( &attr, -1 );
518  rtems_test_assert( status == EINVAL );
519
520  puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" );
521  status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 );
522  rtems_test_assert( status == EINVAL );
523
524  /* error cases for set and get prioceiling attribute */
525
526  empty_line();
527
528  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" );
529  status = pthread_mutexattr_getprioceiling( NULL, &ceiling );
530  rtems_test_assert( status == EINVAL );
531
532  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" );
533  status = pthread_mutexattr_getprioceiling( &attr, NULL );
534  rtems_test_assert( status == EINVAL );
535
536  puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" );
537  status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling );
538  rtems_test_assert( status == EINVAL );
539
540  puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" );
541  status = pthread_mutexattr_setprioceiling( NULL, 128 );
542  rtems_test_assert( status == EINVAL );
543
544  puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL (priority INT_MAX)" );
545  status = pthread_mutexattr_setprioceiling( &attr, INT_MAX );
546  rtems_test_assert( status == 0 );
547
548  puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL (priority INT_MIN)" );
549  status = pthread_mutexattr_setprioceiling( &attr, INT_MIN );
550  rtems_test_assert( status == 0 );
551
552  puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" );
553  status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 );
554  rtems_test_assert( status == EINVAL );
555
556  /* create a thread */
557
558  status = pthread_create( &Task_id, NULL, Task_1, NULL );
559  rtems_test_assert( !status );
560
561  /* now try some basic mutex operations */
562
563  empty_line();
564
565  puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" );
566  status = pthread_mutex_init( NULL, &attr );
567  rtems_test_assert( status == EINVAL );
568
569  puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" );
570  status = pthread_mutex_init( &Mutex_id, &destroyed_attr );
571  rtems_test_assert( status == EINVAL );
572
573  /* must get around error checks in attribute set routines */
574  attr.protocol = -1;
575
576  puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" );
577  status = pthread_mutex_init( &Mutex_id, &attr );
578  rtems_test_assert( status == EINVAL );
579
580  puts( "Init: pthread_mutexattr_setprotocol - SUCCESSFUL" );
581  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
582  rtems_test_assert( !status );
583
584  puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL" );
585  status = pthread_mutexattr_setprioceiling( &attr, -1 );
586  rtems_test_assert( !status );
587
588  puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" );
589  status = pthread_mutex_init( &Mutex_id, &attr );
590  rtems_test_assert( status == EINVAL );
591
592  /* must get around various error checks before checking bad scope */
593  puts( "Init: Resetting mutex attributes" );
594  status = pthread_mutexattr_init( &attr );
595  rtems_test_assert( !status );
596
597  puts( "Init: pthread_mutex_init - process shared scope" );
598  status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
599  rtems_test_assert( status == 0 );
600  status = pthread_mutex_init( &Mutex_id, &attr );
601  rtems_test_assert( status == 0 );
602  status = pthread_mutex_destroy( &Mutex_id );
603  rtems_test_assert( status == 0 );
604
605  puts( "Init: pthread_mutex_init - EINVAL (invalid scope)" );
606  attr.process_shared = -1;
607  status = pthread_mutex_init( &Mutex_id, &attr );
608  rtems_test_assert( status == EINVAL );
609
610  /* bad kind */
611  status = pthread_mutexattr_init( &attr );
612  rtems_test_assert( !status );
613
614  puts( "Init: pthread_mutex_init - EINVAL (invalid type)" );
615  attr.type = -1;
616  status = pthread_mutex_init( &Mutex_id, &attr );
617  rtems_test_assert( status == EINVAL );
618
619  /* now set up for a success pthread_mutex_init */
620
621  puts( "Init: Resetting mutex attributes" );
622  status = pthread_mutexattr_init( &attr );
623  rtems_test_assert( !status );
624
625  puts( "Init: Changing mutex attributes" );
626  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
627  rtems_test_assert( !status );
628
629  status = pthread_mutexattr_setprioceiling(
630    &attr,
631    (sched_get_priority_max(SCHED_FIFO) / 2) + 1
632  );
633  rtems_test_assert( !status );
634
635  status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
636  rtems_test_assert( !status );
637
638  Print_mutexattr( "Init: ", &attr );
639
640  puts( "Init: Resetting mutex attributes" );
641  status = pthread_mutexattr_init( &attr );
642  rtems_test_assert( !status );
643
644  /*
645   *  Set the protocol to priority ceiling so the owner check happens
646   *  and the EPERM test (later) will work.
647   */
648
649  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
650  rtems_test_assert( !status );
651
652  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
653  status = pthread_mutex_init( &Mutex_id, &attr );
654  if ( status )
655    printf( "status = %d\n", status );
656  rtems_test_assert( !status );
657
658  /*
659   *  This is not required to be an error and when it is, there are
660   *  behavioral conflicts with other implementations.
661   */
662  puts( "Init: pthread_mutex_init - EBUSY (reinitialize an existing mutex) - skipped" );
663
664#if 0
665  status = pthread_mutex_init( &Mutex_id, &attr );
666  if ( !status )
667    printf( "status = %d\n", status );
668  rtems_test_assert( status == EBUSY );
669#endif
670
671  puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" );
672  status = pthread_mutex_trylock( Mutex_bad_id );
673  if ( status != EINVAL )
674    printf( "status = %d\n", status );
675  rtems_test_assert( status == EINVAL );
676
677  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
678  status = pthread_mutex_trylock( &Mutex_id );
679  if ( status )
680    printf( "status = %d\n", status );
681  rtems_test_assert( !status );
682
683  puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
684  status = pthread_mutex_trylock( &Mutex_id );
685  if ( status != EBUSY )
686    printf( "status = %d\n", status );
687  rtems_test_assert( status == EBUSY );
688
689  puts( "Init: pthread_mutex_lock - EINVAL (NULL id)" );
690  status = pthread_mutex_lock( NULL );
691  if ( status != EINVAL )
692    printf( "status = %d\n", status );
693  rtems_test_assert( status == EINVAL );
694
695  puts( "Init: pthread_mutex_unlock - EINVAL (NULL id)" );
696  status = pthread_mutex_unlock( NULL );
697  if ( status != EINVAL )
698    printf( "status = %d\n", status );
699  rtems_test_assert( status == EINVAL );
700
701  puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
702  status = pthread_mutex_lock( &Mutex_id );
703  if ( status != EDEADLK )
704    printf( "status = %d\n", status );
705  rtems_test_assert( status == EDEADLK );
706
707  puts( "Init: Sleep 1 second" );
708
709  sleep( 1 );
710
711     /* switch to task 1 */
712
713  puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" );
714  status = pthread_mutex_unlock( Mutex_bad_id );
715  if ( status != EINVAL )
716    printf( "status = %d\n", status );
717  rtems_test_assert( status == EINVAL );
718
719  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
720  status = pthread_mutex_unlock( &Mutex_id );
721  if ( status )
722    printf( "status = %d\n", status );
723  rtems_test_assert( !status );
724
725  puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
726  status = pthread_mutex_unlock( &Mutex_id );
727  if ( status != EPERM )
728    printf( "status = %d\n", status );
729  rtems_test_assert( status == EPERM );
730
731  puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
732  calculate_abstimeout( &times, 0, (TOD_NANOSECONDS_PER_SECOND / 2) );
733
734  status = pthread_mutex_timedlock( &Mutex_id, &times );
735  if ( status != ETIMEDOUT )
736    printf( "status = %d\n", status );
737  rtems_test_assert( status == ETIMEDOUT );
738
739  puts( "Init: pthread_mutex_timedlock - time out in the past" );
740  calculate_abstimeout( &times, -1, (TOD_NANOSECONDS_PER_SECOND / 2) );
741
742  status = pthread_mutex_timedlock( &Mutex_id, &times );
743  if ( status != ETIMEDOUT )
744    printf( "status = %d\n", status );
745  rtems_test_assert( status == ETIMEDOUT );
746
747     /* switch to idle */
748
749  puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
750
751  /* destroy a mutex */
752
753  empty_line();
754
755  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
756  status = pthread_mutex_init( &Mutex2_id, &attr );
757  if ( status )
758    printf( "status = %d\n", status );
759  rtems_test_assert( !status );
760
761  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
762  status = pthread_mutex_init( &Mutex3_id, &attr );
763  rtems_test_assert( !status );
764
765  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
766  status = pthread_mutexattr_destroy( &attr );
767  rtems_test_assert( !status );
768
769  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
770  status = pthread_mutex_destroy( &Mutex3_id );
771  rtems_test_assert( !status );
772
773  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
774  status = pthread_mutex_destroy( &Mutex2_id );
775  rtems_test_assert( !status );
776
777  puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" );
778  status = pthread_mutex_destroy( Mutex_bad_id );
779  rtems_test_assert( status == EINVAL );
780
781  /* destroy a busy mutex */
782
783  empty_line();
784
785  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
786  status = pthread_mutexattr_init( &attr );
787  rtems_test_assert( !status );
788
789  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
790  status = pthread_mutex_init( &Mutex2_id, &attr );
791  rtems_test_assert( !status );
792
793  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
794  status = pthread_mutex_trylock( &Mutex2_id );
795  if ( status )
796    printf( "status = %d\n", status );
797  rtems_test_assert( !status );
798
799  puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
800  status = pthread_mutex_destroy( &Mutex2_id );
801  if ( status != EBUSY )
802    printf( "status = %d\n", status );
803  rtems_test_assert( status == EBUSY );
804
805  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
806  status = pthread_mutex_unlock( &Mutex2_id );
807  rtems_test_assert( !status );
808
809  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
810  status = pthread_mutex_destroy( &Mutex2_id );
811  rtems_test_assert( !status );
812
813  /* priority inherit mutex */
814
815  empty_line();
816
817  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
818  status = pthread_mutexattr_init( &attr );
819  rtems_test_assert( !status );
820
821  puts(
822    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
823  );
824  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
825  rtems_test_assert( !status );
826
827  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
828  status = pthread_mutex_init( &Mutex2_id, &attr );
829  rtems_test_assert( !status );
830
831  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
832  status = pthread_mutex_trylock( &Mutex2_id );
833  rtems_test_assert( !status );
834
835  /* create a thread at a lower priority */
836
837  status = pthread_create( &Task2_id, NULL, Task_2, NULL );
838  rtems_test_assert( !status );
839
840  /* set priority of Task2 to highest priority */
841
842  param.sched_priority = sched_get_priority_max( SCHED_FIFO );
843
844  puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
845  status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
846  rtems_test_assert( !status );
847
848  /* switching to Task2 */
849
850  status = pthread_getschedparam( pthread_self(), &policy, &param );
851  rtems_test_assert( !status );
852  printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
853
854  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
855  status = pthread_mutex_unlock( &Mutex2_id );
856  rtems_test_assert( !status );
857
858  puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
859  status = pthread_mutexattr_destroy( &attr );
860  rtems_test_assert( !status );
861
862  puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
863  status = pthread_mutex_destroy( &Mutex2_id );
864  rtems_test_assert( !status );
865
866  /* priority ceiling mutex */
867
868  empty_line();
869
870  puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
871  status = pthread_mutexattr_init( &attr );
872  rtems_test_assert( !status );
873
874  puts(
875    "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
876  );
877  status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
878  rtems_test_assert( !status );
879
880  puts( "Init: pthread_mutex_init - SUCCESSFUL" );
881  status = pthread_mutex_init( &Mutex2_id, &attr );
882  rtems_test_assert( !status );
883
884  puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" );
885  status = pthread_mutex_getprioceiling( Mutex_bad_id, &ceiling );
886  rtems_test_assert( status == EINVAL );
887
888  puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" );
889  status = pthread_mutex_getprioceiling( &Mutex2_id, NULL );
890  rtems_test_assert( status == EINVAL );
891
892  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
893  rtems_test_assert( !status );
894  printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
895
896  puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" );
897  status = pthread_mutex_setprioceiling( Mutex_bad_id, 200, &old_ceiling );
898  rtems_test_assert( status == EINVAL );
899
900  puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" );
901  status = pthread_mutex_setprioceiling( &Mutex2_id, INT_MAX, &old_ceiling );
902  rtems_test_assert( status == EINVAL );
903
904  puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" );
905  status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL );
906  rtems_test_assert( status == EINVAL );
907
908  /* normal cases of set priority ceiling */
909
910  priority = sched_get_priority_max( SCHED_FIFO );
911  priority = (priority == 254) ? 200 : 13;
912
913  printf( "Init: pthread_mutex_setprioceiling - new ceiling = %d\n", priority );
914  status = pthread_mutex_setprioceiling( &Mutex2_id, priority, &old_ceiling );
915  rtems_test_assert( !status );
916  printf(
917    "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
918  );
919
920  status = pthread_getschedparam( pthread_self(), &policy, &param );
921  rtems_test_assert( !status );
922  printf(
923    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
924  );
925
926  puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
927  status = pthread_mutex_trylock( &Mutex2_id );
928  rtems_test_assert( !status );
929
930  status = pthread_getschedparam( pthread_self(), &policy, &param );
931  rtems_test_assert( !status );
932  printf(
933    "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
934  );
935
936  /* create a thread at a higher priority */
937
938  status = pthread_create( &Task3_id, NULL, Task_3, NULL );
939  rtems_test_assert( !status );
940
941  /* set priority of Task3 to highest priority */
942
943  param.sched_priority = --priority;
944
945  status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
946  rtems_test_assert( !status );
947  puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
948
949  /* DOES NOT SWITCH to Task3 */
950
951  puts( "Init: Sleep 1 second" );
952  rtems_test_assert( !status );
953  sleep( 1 );
954
955  /* switch to task 3 */
956
957  puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
958  status = pthread_mutex_unlock( &Mutex2_id );
959  rtems_test_assert( !status );
960
961  status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
962  rtems_test_assert( !status );
963  printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
964
965  /* set priority of Init to highest priority */
966
967  param.sched_priority = sched_get_priority_max(SCHED_FIFO);
968
969  status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
970  rtems_test_assert( !status );
971  puts( "Init: pthread_setschedparam - set Init priority to highest" );
972
973  puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
974  status = pthread_mutex_lock( &Mutex2_id );
975  if ( status != EINVAL )
976    printf( "status = %d\n", status );
977  rtems_test_assert( status == EINVAL );
978
979  /* mutexinit.c: Initialising recursive mutex */
980
981  puts( "Init: Recursive Mutex" );
982
983  status = pthread_mutex_destroy( &Mutex2_id );
984  if( status )
985    printf( "status mutex destroy:%d\n", status );
986
987  status = pthread_mutexattr_init( &attr );
988  if( status )
989    printf( "status mutexattr:%d\n", status );
990
991  attr.recursive=true;
992  status = pthread_mutex_init( &Mutex2_id, &attr );
993  if ( status )
994    printf( "status recursive mutex :%d\n", status );
995  rtems_test_assert( !status );
996
997  TEST_END();
998  rtems_test_exit( 0 );
999
1000  return NULL; /* just so the compiler thinks we returned something */
1001}
Note: See TracBrowser for help on using the repository browser.