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 <errno.h>
0036 #include "tmacros.h"
0037 #include "pmacros.h"
0038 
0039 const char rtems_test_name[] = "PSXKEY 10";
0040 
0041 /* forward declarations to avoid warnings */
0042 rtems_task Init(rtems_task_argument argument);
0043 rtems_task Test_Thread(rtems_task_argument argument);
0044 
0045 void destructor(void *value);
0046 
0047 int Data_array[1] = {1};
0048 
0049 pthread_key_t key;
0050 volatile bool destructor_ran;
0051 
0052 void destructor(void *value)
0053 {
0054   destructor_ran = true;
0055 }
0056 
0057 rtems_task Test_Thread( rtems_task_argument arg )
0058 {
0059   void *argument = (void *) arg;
0060   int sc;
0061 
0062   puts( "Test_Thread - key pthread_setspecific - OK" );
0063   sc = pthread_setspecific( key, argument );
0064   rtems_test_assert( !sc );
0065 
0066   puts( "Test_Thread - pthread key delete - OK" );
0067   sc = pthread_key_delete( key );
0068   rtems_test_assert( sc == 0 );
0069 
0070   puts( "Test_Thread - exit but don't run key destructors - OK" );
0071   rtems_task_exit();
0072 }
0073 
0074 rtems_task Init( rtems_task_argument ignored )
0075 {
0076   rtems_id          thread;
0077   rtems_status_code rc;
0078   int               sc;
0079   struct timespec   delay_request;
0080 
0081   TEST_BEGIN();
0082 
0083   puts( "Init - pthread key create with destructor - OK" );
0084   sc = pthread_key_create( &key, destructor );
0085   rtems_test_assert( !sc );
0086 
0087   puts( "Init - thread create - OK" );
0088   rc = rtems_task_create(
0089     rtems_build_name( 'T', 'E', 'S', 'T' ), 
0090     1,
0091     RTEMS_MINIMUM_STACK_SIZE,
0092     RTEMS_DEFAULT_MODES,
0093     RTEMS_DEFAULT_ATTRIBUTES,
0094     &thread
0095   );
0096   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0097 
0098   rc = rtems_task_start( thread, Test_Thread, 0 );
0099   rtems_test_assert( rc == RTEMS_SUCCESSFUL );
0100 
0101   puts( "Init - sleep - let thread run - OK" );
0102   delay_request.tv_sec = 0;
0103   delay_request.tv_nsec = 8 * 100000000;
0104   sc = nanosleep( &delay_request, NULL );
0105   rtems_test_assert( !sc );
0106 
0107   puts( "Init - verify destructor did NOT run - OK" );
0108   rtems_test_assert( destructor_ran == false );
0109 
0110   /* puts( "Init - pthread key delete - OK" ); */
0111   /* sc = pthread_key_delete( key ); */
0112   /* rtems_test_assert( sc == 0 ); */
0113 
0114   TEST_END();
0115   rtems_test_exit(0);
0116 }
0117 
0118 /* configuration information */
0119 
0120 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0121 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0122 
0123 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0124 
0125 #define CONFIGURE_MAXIMUM_TASKS          2
0126 #define CONFIGURE_MAXIMUM_POSIX_KEYS     1
0127 
0128 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0129 
0130 #define CONFIGURE_INIT
0131 #include <rtems/confdefs.h>
0132 
0133 /* global variables */