Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Copyright (C) 2019 embedded brains GmbH & Co. KG
0005  *  Copyright (C) 2019 Sebastian Huber
0006  *
0007  *  COPYRIGHT (c) 1989-2009.
0008  *  On-Line Applications Research Corporation (OAR).
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  */
0031 
0032 #ifdef HAVE_CONFIG_H
0033 #include "config.h"
0034 #endif
0035 
0036 #define CONFIGURE_INIT
0037 #include "system.h"
0038 
0039 const char rtems_test_name[] = "PSXONCE 1";
0040 
0041 static pthread_once_t once_a = PTHREAD_ONCE_INIT;
0042 
0043 static pthread_once_t once_b = PTHREAD_ONCE_INIT;
0044 
0045 static rtems_id master;
0046 
0047 static int test_init_routine_call_counter = 0;
0048 
0049 static void Test_init_routine( void )
0050 {
0051   puts( "Test_init_routine: invoked" );
0052   ++test_init_routine_call_counter;
0053 }
0054 
0055 static void routine_b( void )
0056 {
0057   rtems_status_code sc;
0058 
0059   rtems_test_assert( test_init_routine_call_counter == 2 );
0060   ++test_init_routine_call_counter;
0061 
0062   sc = rtems_event_send( master, RTEMS_EVENT_0 );
0063   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0064 }
0065 
0066 static void use_b( rtems_task_argument arg )
0067 {
0068   int status;
0069 
0070   (void) arg;
0071 
0072   status = pthread_once( &once_b, routine_b );
0073   rtems_test_assert( status == 0 );
0074 
0075   rtems_task_exit();
0076 }
0077 
0078 static void routine_a( void )
0079 {
0080   rtems_status_code sc;
0081   rtems_id id;
0082   rtems_event_set events;
0083 
0084   rtems_test_assert( test_init_routine_call_counter == 1 );
0085   ++test_init_routine_call_counter;
0086 
0087   master = rtems_task_self();
0088 
0089   sc = rtems_task_create(
0090     rtems_build_name( 'T', 'A', 'S', 'K' ),
0091     RTEMS_MINIMUM_PRIORITY,
0092     RTEMS_MINIMUM_STACK_SIZE,
0093     RTEMS_DEFAULT_MODES,
0094     RTEMS_DEFAULT_ATTRIBUTES,
0095     &id
0096   );
0097   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0098 
0099   sc = rtems_task_start( id, use_b, 0 );
0100   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0101 
0102   events = 0;
0103   sc = rtems_event_receive(
0104     RTEMS_EVENT_0,
0105     RTEMS_EVENT_ANY | RTEMS_WAIT,
0106     RTEMS_NO_TIMEOUT,
0107     &events
0108   );
0109   rtems_test_assert( sc == RTEMS_SUCCESSFUL );
0110   rtems_test_assert( events == RTEMS_EVENT_0 );
0111 
0112   rtems_test_assert( test_init_routine_call_counter == 3 );
0113 }
0114 
0115 rtems_task Init(rtems_task_argument argument)
0116 {
0117   int status;
0118   pthread_once_t once = PTHREAD_ONCE_INIT;
0119 
0120   TEST_BEGIN();
0121 
0122   puts( "Init: pthread_once - EINVAL (NULL once_control)" );
0123   status = pthread_once( NULL, Test_init_routine );
0124   rtems_test_assert( status == EINVAL );
0125 
0126   puts( "Init: pthread_once - EINVAL (NULL init_routine)" );
0127   status = pthread_once( &once, NULL );
0128   rtems_test_assert( status == EINVAL );
0129 
0130   puts( "Init: pthread_once - SUCCESSFUL (init_routine executes)" );
0131   status = pthread_once( &once, Test_init_routine );
0132   rtems_test_assert( !status );
0133   printf( "Init: call counter: %d\n", test_init_routine_call_counter );
0134   rtems_test_assert( test_init_routine_call_counter == 1 );
0135 
0136   puts( "Init: pthread_once - SUCCESSFUL (init_routine does not execute)" );
0137   status = pthread_once( &once, Test_init_routine );
0138   rtems_test_assert( !status );
0139   printf( "Init: call counter: %d\n", test_init_routine_call_counter );
0140   rtems_test_assert( test_init_routine_call_counter == 1 );
0141 
0142   status = pthread_once( &once_a, routine_a );
0143   rtems_test_assert( status == 0 );
0144   rtems_test_assert( test_init_routine_call_counter == 3 );
0145 
0146   TEST_END();
0147   rtems_test_exit( 0 );
0148 }