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) 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 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <pthread.h>
0034 #include <errno.h>
0035 #include "tmacros.h"
0036 #include "pmacros.h"
0037 
0038 const char rtems_test_name[] = "PSXKEY 3";
0039 
0040 /* forward declarations to avoid warnings */
0041 rtems_task Init(rtems_task_argument value );
0042 rtems_task Test_Thread( rtems_task_argument value );
0043 
0044 void destructor(void *value);
0045 
0046 pthread_key_t Key;
0047 volatile bool destructor_ran;
0048 
0049 void destructor(void *value)
0050 {
0051   destructor_ran = true;
0052 }
0053 
0054 rtems_task Test_Thread( rtems_task_argument value )
0055 {
0056   int   sc;
0057   void *key_value = (void *) value;
0058 
0059   puts( "Test_Thread - pthread_setspecific - OK" );
0060   sc = pthread_setspecific( Key, key_value );
0061   rtems_test_assert( !sc );
0062 
0063   puts( "Test_Thread - pthread_exit to run key destructors - OK" );
0064   rtems_task_exit();
0065 }
0066 
0067 rtems_task Init(rtems_task_argument ignored)
0068 {
0069   rtems_id          thread;
0070   rtems_status_code rc;
0071   int               sc;
0072   struct timespec   delay_request;
0073 
0074   TEST_BEGIN();
0075 
0076   /*
0077    *  Key with NULL destructor
0078    */
0079   puts( "Init - pthread_key_create with NULL destructor - OK" );
0080   sc = pthread_key_create( &Key, NULL );
0081   rtems_test_assert( !sc );
0082 
0083   puts( "Init - create/start - OK" );
0084   rc = rtems_task_create(
0085     rtems_build_name( 'T', 'E', 'S', 'T' ), 
0086     1,
0087     RTEMS_MINIMUM_STACK_SIZE,
0088     RTEMS_DEFAULT_MODES,
0089     RTEMS_DEFAULT_ATTRIBUTES,
0090     &thread
0091   );
0092   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0093 
0094   rc = rtems_task_start( thread, Test_Thread, 0 );
0095   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0096 
0097   puts( "Init - sleep - let thread run - OK" );
0098   delay_request.tv_sec = 0;
0099   delay_request.tv_nsec = 5 * 100000000;
0100   sc = nanosleep( &delay_request, NULL );
0101   rtems_test_assert( !sc );
0102 
0103   puts( "Init - pthread_key_delete - OK" );
0104   sc = pthread_key_delete( Key );
0105   rtems_test_assert( sc == 0 );
0106 
0107   /*
0108    *  Key with non-NULL destructor
0109    */
0110   destructor_ran = false;
0111   puts( "Init - pthread_key_create with non-NULL destructor - OK" );
0112   sc = pthread_key_create( &Key, destructor );
0113   rtems_test_assert( !sc );
0114 
0115   puts( "Init - task create/start - OK" );
0116   rc = rtems_task_create(
0117     rtems_build_name( 'T', 'E', 'S', 'T' ),
0118     1,
0119     RTEMS_MINIMUM_STACK_SIZE,
0120     RTEMS_DEFAULT_MODES,
0121     RTEMS_DEFAULT_ATTRIBUTES,
0122     &thread
0123   );
0124   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0125 
0126   rc = rtems_task_start( thread, Test_Thread, 0 );
0127   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0128 
0129   puts( "Init - sleep - let thread run - OK" );
0130   sc = nanosleep( &delay_request, NULL );
0131   rtems_test_assert( !sc );
0132 
0133   puts( "Init - verify destructor did NOT ran" );
0134   rtems_test_assert( destructor_ran == false );
0135 
0136   puts( "Init - pthread_key_delete - OK" );
0137   sc = pthread_key_delete( Key );
0138   rtems_test_assert( sc == 0 );
0139 
0140   TEST_END();
0141   rtems_test_exit(0);
0142 }
0143 
0144 /* configuration information */
0145 
0146 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0147 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0148 
0149 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0150 
0151 #define CONFIGURE_MAXIMUM_TASKS          2
0152 #define CONFIGURE_MAXIMUM_POSIX_KEYS     1
0153 
0154 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0155 
0156 #define CONFIGURE_INIT
0157 #include <rtems/confdefs.h>
0158 
0159 /* global variables */