Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:36

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2009.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <sched.h>
0034 
0035 #define CONFIGURE_INIT
0036 #include "system.h"
0037 #include <errno.h>
0038 #include <limits.h>
0039 
0040 #include <rtems/score/todimpl.h>
0041 
0042 const char rtems_test_name[] = "PSX 5";
0043 
0044 #define MUTEX_BAD_ID 0xfffffffe
0045 
0046 void Print_mutexattr(
0047   char                *msg,
0048   pthread_mutexattr_t *attr
0049 );
0050 
0051 void Print_mutexattr(
0052   char                *msg,
0053   pthread_mutexattr_t *attr
0054 )
0055 {
0056   int status;
0057   int protocol;
0058   int prioceiling;
0059   int pshared;
0060 
0061   /* protocol */
0062 
0063   status = pthread_mutexattr_getprotocol( attr, &protocol );
0064   rtems_test_assert( !status );
0065 
0066   printf( "%smutex protocol is (%d) -- ", msg, protocol );
0067   switch ( protocol ) {
0068     case PTHREAD_PRIO_NONE:
0069       puts( "PTHREAD_PRIO_NONE" );
0070       break;
0071     case PTHREAD_PRIO_INHERIT:
0072       puts( "PTHREAD_PRIO_INHERIT" );
0073       break;
0074     case PTHREAD_PRIO_PROTECT:
0075       puts( "PTHREAD_PRIO_PROTECT" );
0076       break;
0077     default:
0078       puts( "UNKNOWN" );
0079       rtems_test_assert( 0 );
0080       break;
0081   }
0082 
0083   /* priority ceiling */
0084 
0085   status = pthread_mutexattr_getprioceiling( attr, &prioceiling );
0086   rtems_test_assert( !status );
0087   printf( "%smutex priority ceiling is %d\n", msg, prioceiling );
0088 
0089   /* process shared */
0090 
0091   status = pthread_mutexattr_getpshared( attr, &pshared );
0092   rtems_test_assert( !status );
0093   printf( "%smutex process shared is (%d) -- ", msg, pshared );
0094   switch ( pshared ) {
0095     case PTHREAD_PROCESS_PRIVATE:
0096       puts( "PTHREAD_PROCESS_PRIVATE" );
0097       break;
0098     case PTHREAD_PROCESS_SHARED:
0099       puts( "PTHREAD_PROCESS_SHARED" );
0100       break;
0101     default:
0102       puts( "UNKNOWN" );
0103       rtems_test_assert( 0 );
0104       break;
0105   }
0106 }
0107 
0108 static void calculate_abstimeout(
0109   struct timespec *times,
0110   int              seconds,
0111   long             nanoseconds
0112 )
0113 {
0114   struct timeval       tv1;
0115   struct timezone      tz1;
0116 
0117   gettimeofday( &tv1, &tz1 );
0118 
0119   times->tv_sec  = seconds     + tv1.tv_sec;
0120   times->tv_nsec = nanoseconds + (tv1.tv_usec * 1000);
0121 
0122   while ( times->tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) {
0123     times->tv_sec++;
0124     times->tv_nsec -= TOD_NANOSECONDS_PER_SECOND;
0125   }
0126 
0127 }
0128 
0129 static void test_get_priority( void )
0130 {
0131   int                 status;
0132   pthread_mutexattr_t attr;
0133   pthread_mutex_t     mutex;
0134   int                 policy;
0135   struct sched_param  param;
0136   int                 real_priority;
0137 
0138   status = pthread_getschedparam( pthread_self(), &policy, &param );
0139   rtems_test_assert( status == 0 );
0140 
0141   real_priority = param.sched_priority;
0142 
0143   status = pthread_mutexattr_init( &attr );
0144   rtems_test_assert( status == 0 );
0145 
0146   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
0147   rtems_test_assert( !status );
0148 
0149   status = pthread_mutexattr_setprioceiling( &attr, real_priority + 1 );
0150   rtems_test_assert( status == 0 );
0151 
0152   status = pthread_mutex_init( &mutex, &attr );
0153   rtems_test_assert( status == 0 );
0154 
0155   status = pthread_mutexattr_destroy( &attr );
0156   rtems_test_assert( status == 0 );
0157 
0158   status = pthread_mutex_lock( &mutex );
0159   rtems_test_assert( status == 0 );
0160 
0161   status = pthread_getschedparam( pthread_self(), &policy, &param );
0162   rtems_test_assert( status == 0 );
0163 
0164   rtems_test_assert( real_priority == param.sched_priority );
0165 
0166   status = pthread_mutex_unlock( &mutex );
0167   rtems_test_assert( status == 0 );
0168 
0169   status = pthread_mutex_destroy( &mutex );
0170   rtems_test_assert( status == 0 );
0171 }
0172 
0173 static void *counter_task(void *arg)
0174 {
0175   int *counter;
0176 
0177   counter = arg;
0178 
0179   while ( *counter >= 0 ) {
0180     ++(*counter);
0181 
0182     sched_yield();
0183   }
0184 
0185   return counter;
0186 }
0187 
0188 static void test_set_priority( void )
0189 {
0190   int                 status;
0191   int                 policy;
0192   struct sched_param  param;
0193   pthread_t           thread;
0194   int                 counter;
0195   void               *exit_code;
0196 
0197   counter = 0;
0198 
0199   status = pthread_getschedparam( pthread_self(), &policy, &param );
0200   rtems_test_assert( status == 0 );
0201 
0202   status = pthread_create( &thread, NULL, counter_task, &counter);
0203   rtems_test_assert( status == 0 );
0204 
0205   ++param.sched_priority;
0206   status = pthread_setschedparam( pthread_self(), policy, &param );
0207   rtems_test_assert( status == 0 );
0208 
0209   rtems_test_assert( counter == 0 );
0210 
0211   --param.sched_priority;
0212   status = pthread_setschedparam( pthread_self(), policy, &param );
0213   rtems_test_assert( status == 0 );
0214 
0215   rtems_test_assert( counter == 1 );
0216 
0217   status = pthread_setschedprio( pthread_self(), param.sched_priority + 1 );
0218   rtems_test_assert( status == 0 );
0219 
0220   rtems_test_assert( counter == 1 );
0221 
0222   status = pthread_setschedprio( pthread_self(), param.sched_priority );
0223   rtems_test_assert( status == 0 );
0224 
0225   rtems_test_assert( counter == 1 );
0226 
0227   counter = -1;
0228   sched_yield();
0229 
0230   status = pthread_join( thread, &exit_code );
0231   rtems_test_assert( status == 0 );
0232   rtems_test_assert( exit_code == &counter );
0233   rtems_test_assert( counter == -1 );
0234 }
0235 
0236 static void test_errors_pthread_setschedprio( void )
0237 {
0238   int                status;
0239   int                policy;
0240   struct sched_param param;
0241 
0242   status = pthread_getschedparam( pthread_self(), &policy, &param );
0243   rtems_test_assert( status == 0 );
0244 
0245   status = pthread_setschedprio( pthread_self(), INT_MAX );
0246   rtems_test_assert( status == EINVAL );
0247 
0248   status = pthread_setschedprio( 0xdeadbeef, param.sched_priority );
0249   rtems_test_assert( status == ESRCH );
0250 
0251   status = pthread_setschedprio( pthread_self(), param.sched_priority );
0252   rtems_test_assert( status == 0 );
0253 }
0254 
0255 static void test_mutex_pshared_init(void)
0256 {
0257   pthread_mutex_t mutex;
0258   pthread_mutexattr_t attr;
0259   int eno;
0260 
0261   eno = pthread_mutexattr_init(&attr);
0262   rtems_test_assert(eno == 0);
0263 
0264   eno = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
0265   rtems_test_assert(eno == 0);
0266 
0267   eno = pthread_mutex_init(&mutex, &attr);
0268   rtems_test_assert(eno == 0);
0269 
0270   eno = pthread_mutex_destroy(&mutex);
0271   rtems_test_assert(eno == 0);
0272 
0273   eno = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
0274   rtems_test_assert(eno == 0);
0275 
0276   eno = pthread_mutex_init(&mutex, &attr);
0277   rtems_test_assert(eno == 0);
0278 
0279   eno = pthread_mutex_destroy(&mutex);
0280   rtems_test_assert(eno == 0);
0281 
0282   attr.process_shared = -1;
0283 
0284   eno = pthread_mutex_init(&mutex, &attr);
0285   rtems_test_assert(eno == EINVAL);
0286 
0287   eno = pthread_mutexattr_destroy(&attr);
0288   rtems_test_assert(eno == 0);
0289 }
0290 
0291 static void test_mutex_null( void )
0292 {
0293   struct timespec to;
0294   int eno;
0295 
0296   eno = pthread_mutex_destroy( NULL );
0297   rtems_test_assert( eno == EINVAL );
0298 
0299   eno = pthread_mutex_init( NULL, NULL );
0300   rtems_test_assert( eno == EINVAL );
0301 
0302   eno = pthread_mutex_lock( NULL );
0303   rtems_test_assert( eno == EINVAL );
0304 
0305   to.tv_sec = 1;
0306   to.tv_nsec = 1;
0307   eno = pthread_mutex_timedlock( NULL, &to );
0308   rtems_test_assert( eno == EINVAL );
0309 
0310   eno = pthread_mutex_trylock( NULL );
0311   rtems_test_assert( eno == EINVAL );
0312 
0313   eno = pthread_mutex_unlock( NULL );
0314   rtems_test_assert( eno == EINVAL );
0315 }
0316 
0317 static void test_mutex_not_initialized( void )
0318 {
0319   pthread_mutex_t mutex;
0320   struct timespec to;
0321   int eno;
0322 
0323   memset( &mutex, 0xff, sizeof( mutex ) );
0324 
0325   eno = pthread_mutex_destroy( &mutex );
0326   rtems_test_assert( eno == EINVAL );
0327 
0328   eno = pthread_mutex_lock( &mutex );
0329   rtems_test_assert( eno == EINVAL );
0330 
0331   to.tv_sec = 1;
0332   to.tv_nsec = 1;
0333   eno = pthread_mutex_timedlock( &mutex, &to );
0334   rtems_test_assert( eno == EINVAL );
0335 
0336   eno = pthread_mutex_trylock( &mutex );
0337   rtems_test_assert( eno == EINVAL );
0338 
0339   eno = pthread_mutex_unlock( &mutex );
0340   rtems_test_assert( eno == EINVAL );
0341 }
0342 
0343 static void test_mutex_invalid_copy( void )
0344 {
0345   pthread_mutex_t mutex;
0346   pthread_mutex_t mutex2;
0347   struct timespec to;
0348   int eno;
0349 
0350   eno = pthread_mutex_init( &mutex, NULL );
0351   rtems_test_assert( eno == 0 );
0352 
0353   memcpy( &mutex2, &mutex, sizeof( mutex2 ) );
0354 
0355   eno = pthread_mutex_destroy( &mutex2 );
0356   rtems_test_assert( eno == EINVAL );
0357 
0358   eno = pthread_mutex_lock( &mutex2 );
0359   rtems_test_assert( eno == EINVAL );
0360 
0361   to.tv_sec = 1;
0362   to.tv_nsec = 1;
0363   eno = pthread_mutex_timedlock( &mutex2, &to );
0364   rtems_test_assert( eno == EINVAL );
0365 
0366   eno = pthread_mutex_trylock( &mutex2 );
0367   rtems_test_assert( eno == EINVAL );
0368 
0369   eno = pthread_mutex_unlock( &mutex2 );
0370   rtems_test_assert( eno == EINVAL );
0371 
0372   eno = pthread_mutex_destroy( &mutex );
0373   rtems_test_assert( eno == 0 );
0374 }
0375 
0376 static void test_mutex_auto_initialization( void )
0377 {
0378   struct timespec to;
0379   int eno;
0380 
0381   {
0382     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
0383 
0384     eno = pthread_mutex_destroy( &mutex );
0385     rtems_test_assert( eno == 0 );
0386 
0387     eno = pthread_mutex_destroy( &mutex );
0388     rtems_test_assert( eno == EINVAL );
0389   }
0390 
0391   {
0392     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
0393 
0394     eno = pthread_mutex_lock( &mutex );
0395     rtems_test_assert( eno == 0 );
0396 
0397     eno = pthread_mutex_unlock( &mutex );
0398     rtems_test_assert( eno == 0 );
0399 
0400     eno = pthread_mutex_destroy( &mutex );
0401     rtems_test_assert( eno == 0 );
0402   }
0403 
0404   {
0405     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
0406 
0407     to.tv_sec = 1;
0408     to.tv_nsec = 1;
0409     eno = pthread_mutex_timedlock( &mutex, &to );
0410     rtems_test_assert( eno == 0 );
0411   }
0412 
0413   {
0414     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
0415 
0416     eno = pthread_mutex_trylock( &mutex );
0417     rtems_test_assert( eno == 0 );
0418   }
0419 
0420   {
0421     pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
0422 
0423     eno = pthread_mutex_unlock( &mutex );
0424     rtems_test_assert( eno == EPERM );
0425   }
0426 }
0427 
0428 static void test_mutex_prio_protect_with_cv( void )
0429 {
0430   pthread_mutex_t mutex;
0431   pthread_mutexattr_t attr;
0432   pthread_cond_t cond;
0433   int eno;
0434   struct timespec timeout;
0435 
0436   eno = pthread_mutexattr_init( &attr );
0437   rtems_test_assert( eno == 0 );
0438 
0439   eno = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
0440   rtems_test_assert( eno == 0 );
0441 
0442   eno = pthread_mutex_init( &mutex, &attr );
0443   rtems_test_assert( eno == 0 );
0444 
0445   eno = pthread_mutexattr_destroy( &attr );
0446   rtems_test_assert( eno == 0 );
0447 
0448   eno = pthread_cond_init( &cond, NULL );
0449   rtems_test_assert( eno == 0 );
0450 
0451   eno = pthread_mutex_lock( &mutex );
0452   rtems_test_assert( eno == 0 );
0453 
0454   timeout.tv_sec = 0;
0455   timeout.tv_nsec = 0;
0456 
0457   eno = pthread_cond_timedwait( &cond, &mutex, &timeout );
0458   rtems_test_assert( eno == ETIMEDOUT );
0459 
0460   eno = pthread_mutex_unlock( &mutex );
0461   rtems_test_assert( eno == 0 );
0462 
0463   eno = pthread_mutex_destroy( &mutex );
0464   rtems_test_assert( eno == 0 );
0465 
0466   eno = pthread_cond_destroy( &cond );
0467   rtems_test_assert( eno == 0 );
0468 }
0469 
0470 void *POSIX_Init(
0471   void *argument
0472 )
0473 {
0474   int                  status;
0475   pthread_mutexattr_t  attr;
0476   pthread_mutexattr_t  destroyed_attr;
0477   struct timespec      times;
0478   struct sched_param   param;
0479   int                  pshared;
0480   int                  policy;
0481   int                  protocol;
0482   int                  ceiling;
0483   int                  old_ceiling;
0484   int                  priority;
0485 
0486   Mutex_bad_id = NULL;
0487 
0488   TEST_BEGIN();
0489 
0490   test_mutex_pshared_init();
0491   test_mutex_null();
0492   test_mutex_not_initialized();
0493   test_mutex_invalid_copy();
0494   test_mutex_auto_initialization();
0495   test_mutex_prio_protect_with_cv();
0496   test_get_priority();
0497   test_set_priority();
0498   test_errors_pthread_setschedprio();
0499 
0500   /* set the time of day, and print our buffer in multiple ways */
0501 
0502   set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );
0503 
0504   /* get id of this thread */
0505 
0506   Init_id = pthread_self();
0507   printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );
0508 
0509   /* test pthread_mutex_attr_init */
0510 
0511   puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" );
0512   status = pthread_mutexattr_init( NULL );
0513   rtems_test_assert( status == EINVAL );
0514 
0515   puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
0516   status = pthread_mutexattr_init( &attr );
0517   rtems_test_assert( !status );
0518 
0519   Print_mutexattr( "Init: ", &attr );
0520 
0521   /* create an "uninitialized" attribute structure */
0522 
0523   status = pthread_mutexattr_init( &destroyed_attr );
0524   rtems_test_assert( !status );
0525 
0526   puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
0527   status = pthread_mutexattr_destroy( &destroyed_attr );
0528   rtems_test_assert( !status );
0529 
0530   puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" );
0531   status = pthread_mutexattr_destroy( NULL );
0532   rtems_test_assert( status == EINVAL );
0533 
0534   puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" );
0535   status = pthread_mutexattr_destroy( &destroyed_attr );
0536   rtems_test_assert( status == EINVAL );
0537 
0538   /* error cases for set and get pshared attribute */
0539 
0540   empty_line();
0541 
0542   puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" );
0543   status = pthread_mutexattr_getpshared( NULL, &pshared );
0544   rtems_test_assert( status == EINVAL );
0545 
0546   puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" );
0547   status = pthread_mutexattr_getpshared( &attr, NULL );
0548   rtems_test_assert( status == EINVAL );
0549 
0550   puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" );
0551   status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared );
0552   rtems_test_assert( status == EINVAL );
0553 
0554   pshared = PTHREAD_PROCESS_PRIVATE;
0555   puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" );
0556   status = pthread_mutexattr_setpshared( NULL, pshared );
0557   rtems_test_assert( status == EINVAL );
0558 
0559   pshared = PTHREAD_PROCESS_PRIVATE;
0560   puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" );
0561   status = pthread_mutexattr_setpshared( &destroyed_attr, pshared );
0562   rtems_test_assert( status == EINVAL );
0563 
0564   /* error cases for set and get protocol attribute */
0565 
0566   empty_line();
0567 
0568   puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" );
0569   status = pthread_mutexattr_getprotocol( NULL, &protocol );
0570   rtems_test_assert( status == EINVAL );
0571 
0572   puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" );
0573   status = pthread_mutexattr_getprotocol( &attr, NULL );
0574   rtems_test_assert( status == EINVAL );
0575 
0576   puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" );
0577   status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol );
0578   rtems_test_assert( status == EINVAL );
0579 
0580   puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" );
0581   status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE );
0582   rtems_test_assert( status == EINVAL );
0583 
0584   puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" );
0585   status = pthread_mutexattr_setprotocol( &attr, -1 );
0586   rtems_test_assert( status == EINVAL );
0587 
0588   puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" );
0589   status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 );
0590   rtems_test_assert( status == EINVAL );
0591 
0592   /* error cases for set and get prioceiling attribute */
0593 
0594   empty_line();
0595 
0596   puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" );
0597   status = pthread_mutexattr_getprioceiling( NULL, &ceiling );
0598   rtems_test_assert( status == EINVAL );
0599 
0600   puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" );
0601   status = pthread_mutexattr_getprioceiling( &attr, NULL );
0602   rtems_test_assert( status == EINVAL );
0603 
0604   puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" );
0605   status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling );
0606   rtems_test_assert( status == EINVAL );
0607 
0608   puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" );
0609   status = pthread_mutexattr_setprioceiling( NULL, 128 );
0610   rtems_test_assert( status == EINVAL );
0611 
0612   puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL (priority INT_MAX)" );
0613   status = pthread_mutexattr_setprioceiling( &attr, INT_MAX );
0614   rtems_test_assert( status == 0 );
0615 
0616   puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL (priority INT_MIN)" );
0617   status = pthread_mutexattr_setprioceiling( &attr, INT_MIN );
0618   rtems_test_assert( status == 0 );
0619 
0620   puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" );
0621   status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 );
0622   rtems_test_assert( status == EINVAL );
0623 
0624   /* create a thread */
0625 
0626   status = pthread_create( &Task_id, NULL, Task_1, NULL );
0627   rtems_test_assert( !status );
0628 
0629   /* now try some basic mutex operations */
0630 
0631   empty_line();
0632 
0633   puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" );
0634   status = pthread_mutex_init( NULL, &attr );
0635   rtems_test_assert( status == EINVAL );
0636 
0637   puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" );
0638   status = pthread_mutex_init( &Mutex_id, &destroyed_attr );
0639   rtems_test_assert( status == EINVAL );
0640 
0641   /* must get around error checks in attribute set routines */
0642   attr.protocol = -1;
0643 
0644   puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" );
0645   status = pthread_mutex_init( &Mutex_id, &attr );
0646   rtems_test_assert( status == EINVAL );
0647 
0648   puts( "Init: pthread_mutexattr_setprotocol - SUCCESSFUL" );
0649   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
0650   rtems_test_assert( !status );
0651 
0652   puts( "Init: pthread_mutexattr_setprioceiling - SUCCESSFUL" );
0653   status = pthread_mutexattr_setprioceiling( &attr, -1 );
0654   rtems_test_assert( !status );
0655 
0656   puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" );
0657   status = pthread_mutex_init( &Mutex_id, &attr );
0658   rtems_test_assert( status == EINVAL );
0659 
0660   /* must get around various error checks before checking bad scope */
0661   puts( "Init: Resetting mutex attributes" );
0662   status = pthread_mutexattr_init( &attr );
0663   rtems_test_assert( !status );
0664 
0665   puts( "Init: pthread_mutex_init - process shared scope" );
0666   status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
0667   rtems_test_assert( status == 0 );
0668   status = pthread_mutex_init( &Mutex_id, &attr );
0669   rtems_test_assert( status == 0 );
0670   status = pthread_mutex_destroy( &Mutex_id );
0671   rtems_test_assert( status == 0 );
0672 
0673   puts( "Init: pthread_mutex_init - EINVAL (invalid scope)" );
0674   attr.process_shared = -1;
0675   status = pthread_mutex_init( &Mutex_id, &attr );
0676   rtems_test_assert( status == EINVAL );
0677 
0678   /* bad kind */
0679   status = pthread_mutexattr_init( &attr );
0680   rtems_test_assert( !status );
0681 
0682   puts( "Init: pthread_mutex_init - EINVAL (invalid type)" );
0683   attr.type = -1;
0684   status = pthread_mutex_init( &Mutex_id, &attr );
0685   rtems_test_assert( status == EINVAL );
0686 
0687   /* now set up for a success pthread_mutex_init */
0688 
0689   puts( "Init: Resetting mutex attributes" );
0690   status = pthread_mutexattr_init( &attr );
0691   rtems_test_assert( !status );
0692 
0693   puts( "Init: Changing mutex attributes" );
0694   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
0695   rtems_test_assert( !status );
0696 
0697   status = pthread_mutexattr_setprioceiling(
0698     &attr,
0699     (sched_get_priority_max(SCHED_FIFO) / 2) + 1
0700   );
0701   rtems_test_assert( !status );
0702 
0703   status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED );
0704   rtems_test_assert( !status );
0705 
0706   Print_mutexattr( "Init: ", &attr );
0707 
0708   puts( "Init: Resetting mutex attributes" );
0709   status = pthread_mutexattr_init( &attr );
0710   rtems_test_assert( !status );
0711 
0712   /*
0713    *  Set the protocol to priority ceiling so the owner check happens
0714    *  and the EPERM test (later) will work.
0715    */
0716 
0717   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
0718   rtems_test_assert( !status );
0719 
0720   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0721   status = pthread_mutex_init( &Mutex_id, &attr );
0722   if ( status )
0723     printf( "status = %d\n", status );
0724   rtems_test_assert( !status );
0725 
0726   /*
0727    *  This is not required to be an error and when it is, there are
0728    *  behavioral conflicts with other implementations.
0729    */
0730   puts( "Init: pthread_mutex_init - EBUSY (reinitialize an existing mutex) - skipped" );
0731 
0732 #if 0
0733   status = pthread_mutex_init( &Mutex_id, &attr );
0734   if ( !status )
0735     printf( "status = %d\n", status );
0736   rtems_test_assert( status == EBUSY );
0737 #endif
0738 
0739   puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" );
0740   status = pthread_mutex_trylock( Mutex_bad_id );
0741   if ( status != EINVAL )
0742     printf( "status = %d\n", status );
0743   rtems_test_assert( status == EINVAL );
0744 
0745   puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
0746   status = pthread_mutex_trylock( &Mutex_id );
0747   if ( status )
0748     printf( "status = %d\n", status );
0749   rtems_test_assert( !status );
0750 
0751   puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" );
0752   status = pthread_mutex_trylock( &Mutex_id );
0753   if ( status != EBUSY )
0754     printf( "status = %d\n", status );
0755   rtems_test_assert( status == EBUSY );
0756 
0757   puts( "Init: pthread_mutex_lock - EINVAL (NULL id)" );
0758   status = pthread_mutex_lock( NULL );
0759   if ( status != EINVAL )
0760     printf( "status = %d\n", status );
0761   rtems_test_assert( status == EINVAL );
0762 
0763   puts( "Init: pthread_mutex_unlock - EINVAL (NULL id)" );
0764   status = pthread_mutex_unlock( NULL );
0765   if ( status != EINVAL )
0766     printf( "status = %d\n", status );
0767   rtems_test_assert( status == EINVAL );
0768 
0769   puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" );
0770   status = pthread_mutex_lock( &Mutex_id );
0771   if ( status != EDEADLK )
0772     printf( "status = %d\n", status );
0773   rtems_test_assert( status == EDEADLK );
0774 
0775   puts( "Init: Sleep 1 second" );
0776 
0777   sleep( 1 );
0778 
0779      /* switch to task 1 */
0780 
0781   puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" );
0782   status = pthread_mutex_unlock( Mutex_bad_id );
0783   if ( status != EINVAL )
0784     printf( "status = %d\n", status );
0785   rtems_test_assert( status == EINVAL );
0786 
0787   puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
0788   status = pthread_mutex_unlock( &Mutex_id );
0789   if ( status )
0790     printf( "status = %d\n", status );
0791   rtems_test_assert( !status );
0792 
0793   puts( "Init: pthread_mutex_unlock - EPERM (not owner)" );
0794   status = pthread_mutex_unlock( &Mutex_id );
0795   if ( status != EPERM )
0796     printf( "status = %d\n", status );
0797   rtems_test_assert( status == EPERM );
0798 
0799   puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" );
0800   calculate_abstimeout( &times, 0, (TOD_NANOSECONDS_PER_SECOND / 2) );
0801 
0802   status = pthread_mutex_timedlock( &Mutex_id, &times );
0803   if ( status != ETIMEDOUT )
0804     printf( "status = %d\n", status );
0805   rtems_test_assert( status == ETIMEDOUT );
0806 
0807   puts( "Init: pthread_mutex_timedlock - time out in the past" );
0808   calculate_abstimeout( &times, -1, (TOD_NANOSECONDS_PER_SECOND / 2) );
0809 
0810   status = pthread_mutex_timedlock( &Mutex_id, &times );
0811   if ( status != ETIMEDOUT )
0812     printf( "status = %d\n", status );
0813   rtems_test_assert( status == ETIMEDOUT );
0814 
0815      /* switch to idle */
0816 
0817   puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" );
0818 
0819   /* destroy a mutex */
0820 
0821   empty_line();
0822 
0823   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0824   status = pthread_mutex_init( &Mutex2_id, &attr );
0825   if ( status )
0826     printf( "status = %d\n", status );
0827   rtems_test_assert( !status );
0828 
0829   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0830   status = pthread_mutex_init( &Mutex3_id, &attr );
0831   rtems_test_assert( !status );
0832 
0833   puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
0834   status = pthread_mutexattr_destroy( &attr );
0835   rtems_test_assert( !status );
0836 
0837   puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
0838   status = pthread_mutex_destroy( &Mutex3_id );
0839   rtems_test_assert( !status );
0840 
0841   puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
0842   status = pthread_mutex_destroy( &Mutex2_id );
0843   rtems_test_assert( !status );
0844 
0845   puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" );
0846   status = pthread_mutex_destroy( Mutex_bad_id );
0847   rtems_test_assert( status == EINVAL );
0848 
0849   /* destroy a busy mutex */
0850 
0851   empty_line();
0852 
0853   puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
0854   status = pthread_mutexattr_init( &attr );
0855   rtems_test_assert( !status );
0856 
0857   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0858   status = pthread_mutex_init( &Mutex2_id, &attr );
0859   rtems_test_assert( !status );
0860 
0861   puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
0862   status = pthread_mutex_trylock( &Mutex2_id );
0863   if ( status )
0864     printf( "status = %d\n", status );
0865   rtems_test_assert( !status );
0866 
0867   puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" );
0868   status = pthread_mutex_destroy( &Mutex2_id );
0869   if ( status != EBUSY )
0870     printf( "status = %d\n", status );
0871   rtems_test_assert( status == EBUSY );
0872 
0873   puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
0874   status = pthread_mutex_unlock( &Mutex2_id );
0875   rtems_test_assert( !status );
0876 
0877   puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
0878   status = pthread_mutex_destroy( &Mutex2_id );
0879   rtems_test_assert( !status );
0880 
0881   /* priority inherit mutex */
0882 
0883   empty_line();
0884 
0885   puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
0886   status = pthread_mutexattr_init( &attr );
0887   rtems_test_assert( !status );
0888 
0889   puts(
0890     "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)"
0891   );
0892   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
0893   rtems_test_assert( !status );
0894 
0895   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0896   status = pthread_mutex_init( &Mutex2_id, &attr );
0897   rtems_test_assert( !status );
0898 
0899   puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
0900   status = pthread_mutex_trylock( &Mutex2_id );
0901   rtems_test_assert( !status );
0902 
0903   /* create a thread at a lower priority */
0904 
0905   status = pthread_create( &Task2_id, NULL, Task_2, NULL );
0906   rtems_test_assert( !status );
0907 
0908   /* set priority of Task2 to highest priority */
0909 
0910   param.sched_priority = sched_get_priority_max( SCHED_FIFO );
0911 
0912   puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" );
0913   status = pthread_setschedparam( Task2_id, SCHED_FIFO, &param );
0914   rtems_test_assert( !status );
0915 
0916   /* switching to Task2 */
0917 
0918   status = pthread_getschedparam( pthread_self(), &policy, &param );
0919   rtems_test_assert( !status );
0920   printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority);
0921 
0922   puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
0923   status = pthread_mutex_unlock( &Mutex2_id );
0924   rtems_test_assert( !status );
0925 
0926   puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" );
0927   status = pthread_mutexattr_destroy( &attr );
0928   rtems_test_assert( !status );
0929 
0930   puts( "Init: pthread_mutex_destroy - SUCCESSFUL" );
0931   status = pthread_mutex_destroy( &Mutex2_id );
0932   rtems_test_assert( !status );
0933 
0934   /* priority ceiling mutex */
0935 
0936   empty_line();
0937 
0938   puts( "Init: pthread_mutexattr_init - SUCCESSFUL" );
0939   status = pthread_mutexattr_init( &attr );
0940   rtems_test_assert( !status );
0941 
0942   puts(
0943     "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)"
0944   );
0945   status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT );
0946   rtems_test_assert( !status );
0947 
0948   puts( "Init: pthread_mutex_init - SUCCESSFUL" );
0949   status = pthread_mutex_init( &Mutex2_id, &attr );
0950   rtems_test_assert( !status );
0951 
0952   puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" );
0953   status = pthread_mutex_getprioceiling( Mutex_bad_id, &ceiling );
0954   rtems_test_assert( status == EINVAL );
0955 
0956   puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" );
0957   status = pthread_mutex_getprioceiling( &Mutex2_id, NULL );
0958   rtems_test_assert( status == EINVAL );
0959 
0960   status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
0961   rtems_test_assert( !status );
0962   printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling );
0963 
0964   puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" );
0965   status = pthread_mutex_setprioceiling( Mutex_bad_id, 200, &old_ceiling );
0966   rtems_test_assert( status == EINVAL );
0967 
0968   puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" );
0969   status = pthread_mutex_setprioceiling( &Mutex2_id, INT_MAX, &old_ceiling );
0970   rtems_test_assert( status == EINVAL );
0971 
0972   puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" );
0973   status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL );
0974   rtems_test_assert( status == EINVAL );
0975 
0976   /* normal cases of set priority ceiling */
0977 
0978   priority = sched_get_priority_max( SCHED_FIFO );
0979   priority = (priority == 254) ? 200 : 13;
0980 
0981   printf( "Init: pthread_mutex_setprioceiling - new ceiling = %d\n", priority );
0982   status = pthread_mutex_setprioceiling( &Mutex2_id, priority, &old_ceiling );
0983   rtems_test_assert( !status );
0984   printf(
0985     "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling
0986   );
0987 
0988   status = pthread_getschedparam( pthread_self(), &policy, &param );
0989   rtems_test_assert( !status );
0990   printf(
0991     "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
0992   );
0993 
0994   puts( "Init: pthread_mutex_trylock - SUCCESSFUL" );
0995   status = pthread_mutex_trylock( &Mutex2_id );
0996   rtems_test_assert( !status );
0997 
0998   status = pthread_getschedparam( pthread_self(), &policy, &param );
0999   rtems_test_assert( !status );
1000   printf(
1001     "Init: pthread_getschedparam - priority = %d\n", param.sched_priority
1002   );
1003 
1004   /* create a thread at a higher priority */
1005 
1006   status = pthread_create( &Task3_id, NULL, Task_3, NULL );
1007   rtems_test_assert( !status );
1008 
1009   /* set priority of Task3 to highest priority */
1010 
1011   param.sched_priority = --priority;
1012 
1013   status = pthread_setschedparam( Task3_id, SCHED_FIFO, &param );
1014   rtems_test_assert( !status );
1015   puts( "Init: pthread_setschedparam - set Task3 priority to highest" );
1016 
1017   /* DOES NOT SWITCH to Task3 */
1018 
1019   puts( "Init: Sleep 1 second" );
1020   rtems_test_assert( !status );
1021   sleep( 1 );
1022 
1023   /* switch to task 3 */
1024 
1025   puts( "Init: pthread_mutex_unlock - SUCCESSFUL" );
1026   status = pthread_mutex_unlock( &Mutex2_id );
1027   rtems_test_assert( !status );
1028 
1029   status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling );
1030   rtems_test_assert( !status );
1031   printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling );
1032 
1033   /* set priority of Init to highest priority */
1034 
1035   param.sched_priority = sched_get_priority_max(SCHED_FIFO);
1036 
1037   status = pthread_setschedparam( Init_id, SCHED_FIFO, &param );
1038   rtems_test_assert( !status );
1039   puts( "Init: pthread_setschedparam - set Init priority to highest" );
1040 
1041   puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" );
1042   status = pthread_mutex_lock( &Mutex2_id );
1043   if ( status != EINVAL )
1044     printf( "status = %d\n", status );
1045   rtems_test_assert( status == EINVAL );
1046 
1047   /* mutexinit.c: Initialising recursive mutex */
1048 
1049   puts( "Init: Recursive Mutex" );
1050 
1051   status = pthread_mutex_destroy( &Mutex2_id );
1052   if( status )
1053     printf( "status mutex destroy:%d\n", status );
1054 
1055   status = pthread_mutexattr_init( &attr );
1056   if( status )
1057     printf( "status mutexattr:%d\n", status );
1058 
1059   attr.recursive=true;
1060   status = pthread_mutex_init( &Mutex2_id, &attr );
1061   if ( status )
1062     printf( "status recursive mutex :%d\n", status );
1063   rtems_test_assert( !status );
1064 
1065   TEST_END();
1066   rtems_test_exit( 0 );
1067 
1068   return NULL; /* just so the compiler thinks we returned something */
1069 }