Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Copyright (c) 2012 Zhongwei Yao.
0005  *  COPYRIGHT (c) 1989-2014.
0006  *  On-Line Applications Research Corporation (OAR).
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0027  * POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 
0030 #ifdef HAVE_CONFIG_H
0031 #include "config.h"
0032 #endif
0033 
0034 #include <pthread.h>
0035 #include <sched.h>
0036 #include <errno.h>
0037 #include "tmacros.h"
0038 #include "pmacros.h"
0039 
0040 const char rtems_test_name[] = "PSXKEY 7";
0041 
0042 #define INITIAL_TASK_COUNT 10
0043 
0044 #define ADDITIONAL_TASK_COUNT 13
0045 
0046 static pthread_key_t Key;
0047 static int created_thread_count, setted_thread_count, got_thread_count;
0048 static int all_thread_created;
0049 static pthread_mutex_t mutex1, mutex2;
0050 static pthread_cond_t create_condition_var, set_condition_var;
0051 
0052 static rtems_task Test_Thread(rtems_task_argument argument)
0053 {
0054   int sc;
0055   int *value_p, *value_p2;
0056 
0057   value_p = malloc( sizeof( int ) );
0058   rtems_test_assert(value_p != NULL);
0059 
0060   *value_p = 123;
0061   sc = pthread_setspecific( Key, value_p );
0062   rtems_test_assert( !sc );
0063 
0064   pthread_mutex_lock( &mutex1 );
0065   ++setted_thread_count;
0066   pthread_cond_signal( &set_condition_var );
0067   pthread_mutex_unlock( &mutex1 );
0068 
0069   /**
0070    * blocked untill all threads have been created.
0071    */
0072   pthread_mutex_lock( &mutex2 );
0073   while( !all_thread_created )
0074     pthread_cond_wait( &create_condition_var, &mutex2 );
0075   pthread_mutex_unlock( &mutex2 );
0076 
0077   value_p2 = pthread_getspecific( Key );
0078   rtems_test_assert( value_p == value_p2 );
0079   ++got_thread_count;
0080 
0081   rtems_task_exit();
0082 }
0083 
0084 static rtems_task Init(rtems_task_argument argument)
0085 {
0086   rtems_status_code  rc;
0087   int                sc;
0088   struct timespec    delay_request;
0089   uintptr_t          max_free_size =
0090                        ADDITIONAL_TASK_COUNT * RTEMS_MINIMUM_STACK_SIZE;
0091   void              *greedy;
0092 
0093   all_thread_created = 0;
0094 
0095   TEST_BEGIN();
0096 
0097   puts( "Init - Mutex 1 create - OK" );
0098   sc = pthread_mutex_init( &mutex1, NULL );
0099   rtems_test_assert( !sc );
0100 
0101   puts( "Init - Mutex 2 create - OK" );
0102   sc = pthread_mutex_init( &mutex2, NULL );
0103   rtems_test_assert( !sc );
0104 
0105   puts( "Init - Condition variable 1 create - OK" );
0106   sc = pthread_cond_init( &create_condition_var, NULL );
0107   rtems_test_assert( !sc );
0108 
0109   puts( "Init - Condition variable 2 create - OK" );
0110   sc = pthread_cond_init( &set_condition_var, NULL );
0111   rtems_test_assert( !sc );
0112 
0113   puts( "Init - pthread Key create - OK" );
0114   sc = pthread_key_create( &Key, NULL );
0115   rtems_test_assert( !sc );
0116 
0117   /* Reduce workspace size if necessary to shorten test time */
0118   greedy = rtems_workspace_greedy_allocate( &max_free_size, 1 );
0119 
0120   for ( ; ; ) {
0121     rtems_id task_id;
0122 
0123     pthread_mutex_lock( &mutex1 );
0124 
0125     rc = rtems_task_create(
0126       rtems_build_name( 'T', 'E', 'S', 'T' ),
0127       1,
0128       RTEMS_MINIMUM_STACK_SIZE,
0129       RTEMS_DEFAULT_MODES,
0130       RTEMS_DEFAULT_ATTRIBUTES,
0131       &task_id
0132     );
0133     rtems_test_assert(
0134       ( rc == RTEMS_SUCCESSFUL ) || ( rc == RTEMS_UNSATISFIED )
0135         || ( rc == RTEMS_TOO_MANY )
0136     );
0137 
0138     if ( rc == RTEMS_SUCCESSFUL ) {
0139       rc = rtems_task_start( task_id, Test_Thread, 0 );
0140       rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0141     }
0142 
0143     /**
0144      * check if return is not successful, it means RTEMS Workspace RAM
0145      * have been exhausted.
0146      */
0147     if ( rc != RTEMS_SUCCESSFUL ) {
0148       pthread_mutex_unlock( &mutex1 );
0149       break;
0150     }
0151     ++created_thread_count;
0152 
0153     /**
0154      * wait for test thread set key, the while loop here is used to
0155      * avoid suprious wakeup.
0156      */
0157     while( created_thread_count > setted_thread_count )
0158       pthread_cond_wait( &set_condition_var, &mutex1 );
0159     pthread_mutex_unlock( &mutex1 );
0160   }
0161 
0162   rtems_workspace_greedy_free( greedy );
0163 
0164   printf(
0165     "Init - %d pthreads have been created - OK\n"
0166     "Init - %d pthreads have been setted key data - OK\n",
0167     created_thread_count,
0168     setted_thread_count
0169   );
0170   rtems_test_assert( created_thread_count == setted_thread_count );
0171 
0172   /* unblock all created pthread to let them set key data.*/
0173   pthread_mutex_lock( &mutex2 );
0174   all_thread_created = 1;
0175   pthread_cond_broadcast( &create_condition_var );
0176   pthread_mutex_unlock( &mutex2 );
0177 
0178   puts( "Init - sleep - let threads run - OK" );
0179   delay_request.tv_sec = 0;
0180   delay_request.tv_nsec = 8 * 100000000;
0181   sc = nanosleep( &delay_request, NULL );
0182   rtems_test_assert( !sc );
0183 
0184   printf(
0185     "Init - %d pthreads have been got key data - OK\n",
0186     got_thread_count
0187   );
0188   rtems_test_assert( created_thread_count == got_thread_count );
0189 
0190   puts( "Init - pthread Key delete - OK" );
0191   sc = pthread_key_delete( Key );
0192   rtems_test_assert( sc == 0 );
0193 
0194   puts( "Init - Mutex1 delete - OK" );
0195   sc = pthread_mutex_destroy( &mutex1 );
0196   rtems_test_assert( !sc );
0197 
0198   puts( "Init - Mutex2 delete - OK" );
0199   sc = pthread_mutex_destroy( &mutex2 );
0200   rtems_test_assert( !sc );
0201 
0202   puts( "Init - Condition variable 1 delete - OK" );
0203   sc = pthread_cond_destroy( &create_condition_var );
0204   rtems_test_assert( !sc );
0205 
0206   puts( "Init - Condition variable 2 delete - OK" );
0207   sc = pthread_cond_destroy( &set_condition_var );
0208   rtems_test_assert( !sc );
0209 
0210   TEST_END();
0211   rtems_test_exit(0);
0212 }
0213 
0214 /* configuration information */
0215 
0216 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0217 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0218 
0219 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0220 
0221 #define CONFIGURE_MAXIMUM_TASKS rtems_resource_unlimited(INITIAL_TASK_COUNT)
0222 #define CONFIGURE_MAXIMUM_POSIX_KEYS 1
0223 #define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS \
0224   (INITIAL_TASK_COUNT + ADDITIONAL_TASK_COUNT)
0225 #define CONFIGURE_UNIFIED_WORK_AREAS
0226 
0227 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0228 
0229 #define CONFIGURE_INIT
0230 #include <rtems/confdefs.h>
0231 
0232 /* global variables */