Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2014.
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 #define _GNU_SOURCE
0030 
0031 #ifdef HAVE_CONFIG_H
0032 #include "config.h"
0033 #endif
0034 
0035 #include <tmacros.h>
0036 #include <errno.h>
0037 #include <sched.h>
0038 #include <pthread.h>
0039 #include <rtems/posix/pthreadimpl.h>
0040 
0041 const char rtems_test_name[] = "PSXGETATTRNP 1";
0042 
0043 /* forward declarations to avoid warnings */
0044 void *POSIX_Init(void *argument);
0045 
0046 void *Thread_1(void *argument);
0047 
0048 pthread_t           Init_id;
0049 pthread_t           Thread_id;
0050 pthread_attr_t      Thread_attr;
0051 int                 max_priority;
0052 
0053 static int attribute_compare(
0054   const pthread_attr_t *attr1,
0055   const pthread_attr_t *attr2
0056 )
0057 {
0058   if ( attr1->is_initialized  !=  attr2->is_initialized )
0059     return 1;
0060 
0061   if (
0062     attr1->stackaddr != NULL &&
0063       attr2->stackaddr != NULL &&
0064       attr1->stackaddr != attr2->stackaddr )
0065     return 1;
0066 
0067   if (
0068     attr1->stacksize != 0 &&
0069       attr2->stacksize != 0 &&
0070        attr1->stacksize != attr2->stacksize )
0071     return 1;
0072 
0073   if ( attr1->contentionscope != attr2->contentionscope )
0074     return 1;
0075 
0076   if ( attr1->inheritsched != attr2->inheritsched )
0077     return 1;
0078 
0079   if ( attr1->schedpolicy != attr2->schedpolicy )
0080     return 1;
0081 
0082   if ( attr1->schedparam.sched_priority != attr2->schedparam.sched_priority )
0083     return 1;
0084 
0085   if ( attr1->guardsize != attr2->guardsize )
0086     return 1;
0087 
0088   #if defined(_POSIX_THREAD_CPUTIME)
0089     if ( attr1->cputime_clock_allowed != attr2->cputime_clock_allowed )
0090       return 1;
0091   #endif
0092 
0093   if ( attr1->detachstate != attr2->detachstate )
0094     return 1;
0095 
0096   if ( attr1->affinitysetsize != attr2->affinitysetsize )
0097     return 1;
0098 
0099   if (!CPU_EQUAL_S(
0100     attr1->affinitysetsize,
0101     attr1->affinityset,
0102     attr2->affinityset
0103   ))
0104     return 1;
0105 
0106   if (!CPU_EQUAL_S(
0107     attr1->affinitysetsize,
0108     &attr1->affinitysetpreallocated,
0109     &attr2->affinitysetpreallocated
0110   ))
0111     return 1;
0112 
0113   return 0;
0114 }
0115 
0116 void *Thread_1(
0117   void *argument
0118 )
0119 {
0120   pthread_attr_t      attr;
0121   struct sched_param  param;
0122   int                 sc;
0123   int                 value;
0124   void               *stackaddr;
0125   size_t              stacksize;
0126   cpu_set_t           set;
0127 
0128   puts("Thread - pthread_getattr_np - Verify value");
0129   sc = pthread_getattr_np( Thread_id, &attr );
0130   rtems_test_assert( sc == 0 );
0131   rtems_test_assert( ! attribute_compare(&attr, &Thread_attr) );
0132 
0133   param.sched_priority = max_priority;
0134 
0135   puts( "Thread - pthread_setschedparam: Setting highest priority SCHED_FIFO" );
0136   sc = pthread_setschedparam( Thread_id, SCHED_FIFO, &param );
0137   rtems_test_assert( !sc );
0138 
0139   puts("Thread - Detach");
0140   sc = pthread_detach( Thread_id );
0141   rtems_test_assert( !sc );
0142 
0143   puts("Thread - pthread_getattr_np");
0144   sc = pthread_getattr_np( Thread_id, &attr );
0145   rtems_test_assert( !sc );
0146 
0147   puts("Thread - Verify get stack");
0148   stackaddr = NULL;
0149   stacksize = 0;
0150   sc = pthread_attr_getstack( &attr, &stackaddr, &stacksize );
0151   rtems_test_assert( sc == 0 );
0152   rtems_test_assert( stackaddr != NULL );
0153   rtems_test_assert( stacksize != 0 );
0154 
0155   puts("Thread - Verify contention scope");
0156   sc = pthread_attr_getscope( &attr, &value );
0157   rtems_test_assert( sc == 0 );
0158   rtems_test_assert( value == PTHREAD_SCOPE_PROCESS );
0159 
0160   puts("Thread - Verify explicit scheduler");
0161   sc = pthread_attr_getinheritsched( &attr, &value );
0162   rtems_test_assert( sc == 0 );
0163   rtems_test_assert( value == PTHREAD_EXPLICIT_SCHED );
0164 
0165   puts("Thread - Verify SCHED_FIFO policy");
0166   sc = pthread_attr_getschedpolicy( &attr, &value );
0167   rtems_test_assert( !sc );
0168   rtems_test_assert( value == SCHED_FIFO );
0169 
0170   puts("Thread - Verify max priority");
0171   sc = pthread_attr_getschedparam( &attr, &param );
0172   rtems_test_assert( !sc );
0173   rtems_test_assert( param.sched_priority == max_priority );
0174 
0175   puts("Thread - Verify detached");
0176   sc = pthread_attr_getdetachstate( &attr, &value );
0177   rtems_test_assert( sc == 0 );
0178   rtems_test_assert( value == PTHREAD_CREATE_DETACHED );
0179 
0180   puts("Thread - Verify affinity");
0181   CPU_ZERO( &set );
0182   sc = pthread_attr_getaffinity_np( &attr, sizeof( set ), &set );
0183   rtems_test_assert( sc == 0 );
0184   rtems_test_assert( CPU_ISSET( 0, &set ) );
0185   rtems_test_assert( !CPU_ISSET( 1, &set ) );
0186 
0187   puts("Thread - Destroy");
0188   sc = pthread_attr_destroy( &attr );
0189   rtems_test_assert( sc == 0 );
0190 
0191   return NULL; /* just so the compiler thinks we returned something */
0192 }
0193 
0194 void *POSIX_Init(
0195   void *ignored
0196 )
0197 {
0198   int                 sc;
0199   pthread_attr_t      attribute;
0200   void               *stackaddr;
0201   size_t              stacksize;
0202   size_t              guardsize;
0203   struct sched_param  param;
0204   cpu_set_t           set;
0205 
0206   TEST_BEGIN();
0207 
0208   /* Initialize thread id */
0209   Init_id = pthread_self();
0210   max_priority = sched_get_priority_max( SCHED_FIFO );
0211 
0212   puts( "Init - pthread_getattr_np - attr NULL - EINVAL" );
0213   sc = pthread_getattr_np( Init_id, NULL );
0214   rtems_test_assert( sc == EINVAL );
0215 
0216   puts( "Init - pthread_getattr_np - invalid id - ESRCH" );
0217   sc = pthread_getattr_np( 0xffff, &attribute );
0218   rtems_test_assert( sc == ESRCH );
0219 
0220 
0221   /* init task attributes */
0222   puts("Init - pthread_attr_init");
0223   sc = pthread_attr_init(&Thread_attr);
0224   rtems_test_assert(!sc);
0225 
0226   puts("Init - pthread_attr_setaffinity_np");
0227   CPU_ZERO( &set );
0228   CPU_SET( 0, &set );
0229   sc = pthread_attr_setaffinity_np( &Thread_attr, sizeof( set ), &set );
0230   rtems_test_assert(!sc);
0231 
0232   puts("Init - pthread_attr_setinheritsched - PTHREAD_EXPLICIT_SCHED");
0233   sc = pthread_attr_setinheritsched( &Thread_attr, PTHREAD_EXPLICIT_SCHED );
0234   rtems_test_assert(!sc);
0235   rtems_test_assert( Thread_attr.inheritsched == PTHREAD_EXPLICIT_SCHED );
0236 
0237   puts("Init - pthread_attr_setschedpolicy to SCHED_RR");
0238   sc = pthread_attr_setschedpolicy(&Thread_attr, SCHED_RR);
0239   rtems_test_assert(!sc);
0240 
0241   puts("Init - pthread_attr_setschedparam to minimum priority + 2");
0242   param.sched_priority = sched_get_priority_min( SCHED_RR ) + 2;
0243   sc = pthread_attr_setschedparam( &Thread_attr, &param );
0244   rtems_test_assert(!sc);
0245 
0246   puts("Init - pthread_attr_getstack");
0247   sc = pthread_attr_getstack( &Thread_attr, &stackaddr, &stacksize );
0248   rtems_test_assert(!sc);
0249 
0250   stacksize *= 2;
0251   puts("Init - pthread_attr_setstack double the stacksize");
0252   sc = pthread_attr_setstacksize( &Thread_attr, stacksize );
0253   rtems_test_assert(!sc);
0254 
0255   puts("Init - pthread_attr_getguardsize");
0256   sc = pthread_attr_getguardsize( &Thread_attr, &guardsize );
0257   rtems_test_assert(!sc);
0258 
0259   guardsize *= 2;
0260   puts("Init - pthread_attr_setguardsize double the guardsize");
0261   sc = pthread_attr_setguardsize( &Thread_attr, guardsize );
0262   rtems_test_assert(!sc);
0263 
0264   puts("Init - raise priority to max");
0265   param.sched_priority = max_priority;
0266   sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
0267   rtems_test_assert( !sc );
0268 
0269  puts("Init - pthread_create");
0270   sc = pthread_create( &Thread_id, &Thread_attr, Thread_1, NULL );
0271   rtems_test_assert( !sc );
0272 
0273   puts("Init - Lower priority");
0274   fflush(stdout);
0275   param.sched_priority = sched_get_priority_min( SCHED_RR );
0276   sc = pthread_setschedparam( Init_id, SCHED_RR, &param );
0277   rtems_test_assert(!sc);
0278 
0279 #if 0
0280   sc = pthread_join( Thread_id, NULL );
0281   rtems_test_assert( !sc );
0282 #endif
0283 
0284   TEST_END();
0285   rtems_test_exit(0);
0286   return NULL; /* just so the compiler thinks we returned something */
0287 }
0288 
0289 /* configuration information */
0290 
0291 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0292 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0293 
0294 #define CONFIGURE_MAXIMUM_POSIX_THREADS  2
0295 
0296 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0297 
0298 #define CONFIGURE_INIT
0299 #include <rtems/confdefs.h>
0300 
0301 /* global variables */