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 "tmacros.h"
0035 
0036 #include <stdio.h>
0037 #include <rtems.h>
0038 #include <pthread.h>
0039 #include <unistd.h>
0040 #include <errno.h>
0041 
0042 #include <rtems/libcsupport.h>
0043 
0044 const char rtems_test_name[] = "PSXKEY 8";
0045 
0046 static pthread_key_t Key;
0047 static int created_task_count, setted_task_count, got_task_count;
0048 static int all_thread_created;
0049 static rtems_id sema1, sema2;
0050 static rtems_name name1, name2;
0051 
0052 static rtems_task test_task(rtems_task_argument arg)
0053 {
0054   rtems_status_code sc = 0;
0055   const void *value_p;
0056   const void *value_p2;
0057 
0058   value_p = &sc;
0059   sc = pthread_setspecific( Key, value_p );
0060   rtems_test_assert( !sc );
0061   ++setted_task_count;
0062   sc = rtems_semaphore_release( sema1 );
0063 
0064   /* Blocked until all tasks have been created */
0065   sc = rtems_semaphore_obtain( sema2 , RTEMS_WAIT, 0 );
0066   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0067 
0068   sc = rtems_semaphore_release( sema2 );
0069   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0070 
0071   value_p2 = pthread_getspecific( Key );
0072   rtems_test_assert( value_p == value_p2 );
0073   ++got_task_count;
0074 
0075   rtems_task_exit();
0076 }
0077 
0078 static rtems_task Init(rtems_task_argument arg)
0079 {
0080   rtems_status_code        sc;
0081   int                      eno;
0082   rtems_resource_snapshot  snapshot;
0083   uintptr_t                max_free_size = 13 * RTEMS_MINIMUM_STACK_SIZE;
0084   void                    *greedy;
0085 
0086   all_thread_created = 0;
0087 
0088   TEST_BEGIN();
0089   rtems_resource_snapshot_take(&snapshot);
0090 
0091   puts( "Init - Semaphore 1 create - OK" );
0092   name1 = rtems_build_name('S', 'E', 'M', '1');
0093   sc = rtems_semaphore_create(
0094     name1, 0,
0095     RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
0096     0,
0097     &sema1
0098   );
0099   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0100 
0101   puts( "Init - Semaphore 2 create - OK" );
0102   name2 = rtems_build_name('S', 'E', 'M', '2');
0103   sc = rtems_semaphore_create(
0104     name2,
0105     0,
0106     RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO,
0107     0,
0108     &sema2
0109   );
0110   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0111 
0112   puts( "Init - pthread Key create - OK" );
0113   eno = pthread_key_create( &Key, NULL );
0114   rtems_test_assert( eno == 0 );
0115 
0116   /* Reduce workspace size if necessary to shorten test time */
0117   greedy = rtems_workspace_greedy_allocate( &max_free_size, 1 );
0118 
0119   for ( ; ; ) {
0120     rtems_id task_id;
0121 
0122     sc = rtems_task_create(
0123       rtems_build_name('T','A',created_task_count, ' '),
0124       1,
0125       RTEMS_MINIMUM_STACK_SIZE,
0126       RTEMS_DEFAULT_MODES,
0127       RTEMS_DEFAULT_ATTRIBUTES,
0128       &task_id
0129     );
0130     rtems_test_assert(
0131       (sc == RTEMS_UNSATISFIED) ||
0132       (sc == RTEMS_TOO_MANY) ||
0133       (sc == RTEMS_SUCCESSFUL)
0134     );
0135 
0136     /**
0137      * when return is RTEMS_TOO_MANY or RTEMS_UNSATISFIED, there is not
0138      * enough source to create task.
0139      */
0140     if ( (sc == RTEMS_TOO_MANY) || (sc == RTEMS_UNSATISFIED) ) {
0141       break;
0142     }
0143     ++created_task_count;
0144 
0145     sc = rtems_task_start( task_id,  test_task, 0 );
0146     rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0147 
0148     sc = rtems_semaphore_obtain( sema1, RTEMS_WAIT, 0 );
0149     rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0150   }
0151 
0152   rtems_workspace_greedy_free( greedy );
0153 
0154   printf(
0155     "Init - %d tasks have been created - OK\n"
0156     "Init - %d tasks have been setted key data - OK\n",
0157     setted_task_count,
0158     created_task_count
0159   );
0160   rtems_test_assert( created_task_count == setted_task_count );
0161 
0162   /* Ready all created tasks one by one to let them check the key data.*/
0163   puts( "Init - release semaphore 2 - OK" );
0164   sc = rtems_semaphore_release( sema2 );
0165   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0166 
0167   printf( "Init - %d Tasks have been got key data - OK\n", got_task_count );
0168   rtems_test_assert( created_task_count == got_task_count );
0169   puts( "Init - pthread Key delete - OK" );
0170   eno = pthread_key_delete( Key );
0171   rtems_test_assert( eno == 0 );
0172 
0173   puts( "Init - semaphore 1 delete - OK" );
0174   sc = rtems_semaphore_delete( sema1 );
0175   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0176 
0177   puts( "Init - semaphore 2 delete - OK" );
0178   sc = rtems_semaphore_delete( sema2 );
0179   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0180 
0181   rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
0182   TEST_END();
0183   exit(0);
0184 }
0185 
0186 /* configuration information */
0187 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0188 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0189 
0190 #define CONFIGURE_MAXIMUM_TASKS 14
0191 #define CONFIGURE_MAXIMUM_SEMAPHORES 2
0192 #define CONFIGURE_MAXIMUM_POSIX_KEYS 1
0193 
0194 #define CONFIGURE_INIT_TASK_INITIAL_MODES \
0195   (RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_ASR | RTEMS_INTERRUPT_LEVEL(0))
0196 
0197 #define CONFIGURE_INIT_TASK_PRIORITY 4
0198 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0199 
0200 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0201 
0202 #define CONFIGURE_INIT
0203 #include <rtems/confdefs.h>
0204 /* end of file */