Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Copyright (C) 2020 embedded brains GmbH & Co. KG
0005  *
0006  *  COPYRIGHT (c) 1989-2012.
0007  *  On-Line Applications Research Corporation (OAR).
0008  *
0009  * Redistribution and use in source and binary forms, with or without
0010  * modification, are permitted provided that the following conditions
0011  * are met:
0012  * 1. Redistributions of source code must retain the above copyright
0013  *    notice, this list of conditions and the following disclaimer.
0014  * 2. Redistributions in binary form must reproduce the above copyright
0015  *    notice, this list of conditions and the following disclaimer in the
0016  *    documentation and/or other materials provided with the distribution.
0017  *
0018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0028  * POSSIBILITY OF SUCH DAMAGE.
0029  */
0030 
0031 #ifdef HAVE_CONFIG_H
0032 #include "config.h"
0033 #endif
0034 
0035 #include <rtems/test.h>
0036 #include <rtems/test-info.h>
0037 
0038 #include <rtems/score/threadimpl.h>
0039 
0040 #if defined(FIFO_NO_TIMEOUT)
0041   #define TEST_NAME                "1"
0042   #define TEST_STRING              FIFOWithoutTimeout
0043   #define SEMAPHORE_OBTAIN_TIMEOUT 0
0044   #define SEMAPHORE_ATTRIBUTES     RTEMS_DEFAULT_ATTRIBUTES
0045 
0046 #elif defined(FIFO_WITH_TIMEOUT)
0047   #define TEST_NAME                "2"
0048   #define TEST_STRING              FIFOWithTimeout
0049   #define SEMAPHORE_OBTAIN_TIMEOUT 10
0050   #define SEMAPHORE_ATTRIBUTES     RTEMS_DEFAULT_ATTRIBUTES
0051 
0052 #elif defined(PRIORITY_NO_TIMEOUT)
0053   #define TEST_NAME                "3"
0054   #define TEST_STRING              PriorityWithoutTimeout
0055   #define SEMAPHORE_OBTAIN_TIMEOUT 0
0056   #define SEMAPHORE_ATTRIBUTES     RTEMS_PRIORITY
0057 
0058 #elif defined(PRIORITY_WITH_TIMEOUT)
0059   #define TEST_NAME                "4"
0060   #define TEST_STRING              PriorityWithTimeout
0061   #define SEMAPHORE_OBTAIN_TIMEOUT 10
0062   #define SEMAPHORE_ATTRIBUTES     RTEMS_PRIORITY
0063 
0064 #elif defined(PRIORITY_NO_TIMEOUT_REVERSE)
0065   #define TEST_NAME                "5"
0066   #define TEST_STRING              PriorityWithoutTimeoutReverse
0067   #define SEMAPHORE_OBTAIN_TIMEOUT 0
0068   #define SEMAPHORE_ATTRIBUTES     RTEMS_PRIORITY
0069 
0070 #else
0071 
0072   #error "Test Mode not defined"
0073 #endif
0074 
0075 const char rtems_test_name[] = "SPINTRCRITICAL " TEST_NAME;
0076 
0077 typedef struct {
0078   Thread_Control *thread;
0079   rtems_id        semaphore;
0080 } test_context;
0081 
0082 static bool is_blocked( Thread_Wait_flags flags )
0083 {
0084   return flags == ( THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_BLOCKED );
0085 }
0086 
0087 static bool interrupts_blocking_op( Thread_Wait_flags flags )
0088 {
0089   return flags
0090     == ( THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK );
0091 }
0092 
0093 static T_interrupt_test_state interrupt( void *arg )
0094 {
0095   test_context           *ctx;
0096   T_interrupt_test_state  state;
0097   Thread_Wait_flags       flags;
0098   rtems_status_code       status;
0099 
0100   ctx = arg;
0101   flags = _Thread_Wait_flags_get( ctx->thread );
0102 
0103   if ( interrupts_blocking_op( flags ) ) {
0104     state = T_INTERRUPT_TEST_DONE;
0105   } else if ( is_blocked( flags ) ) {
0106     state = T_INTERRUPT_TEST_LATE;
0107   } else {
0108     state = T_INTERRUPT_TEST_EARLY;
0109   }
0110 
0111   status = rtems_semaphore_release( ctx->semaphore );
0112   T_quiet_rsc_success( status );
0113 
0114   return state;
0115 }
0116 
0117 static void action( void *arg )
0118 {
0119   test_context      *ctx;
0120   rtems_status_code  status;
0121 
0122   ctx = arg;
0123   status = rtems_semaphore_obtain(
0124     ctx->semaphore,
0125     RTEMS_DEFAULT_OPTIONS,
0126     SEMAPHORE_OBTAIN_TIMEOUT
0127   );
0128   T_quiet_rsc_success( status );
0129 }
0130 
0131 static const T_interrupt_test_config config = {
0132   .action = action,
0133   .interrupt = interrupt,
0134   .max_iteration_count = 10000
0135 };
0136 
0137 T_TEST_CASE( RTEMS_XCONCAT( SemaphoreRelease, TEST_STRING ) )
0138 {
0139   test_context           ctx;
0140   rtems_status_code      status;
0141   T_interrupt_test_state state;
0142 
0143   ctx.thread = _Thread_Get_executing();
0144 
0145   status = rtems_semaphore_create(
0146     rtems_build_name( 'S', 'M', '1', ' ' ),
0147     0,
0148     SEMAPHORE_ATTRIBUTES,
0149     RTEMS_NO_PRIORITY,
0150     &ctx.semaphore
0151   );
0152   T_rsc_success( status );
0153 
0154   state = T_interrupt_test( &config, &ctx );
0155   T_eq_int( state, T_INTERRUPT_TEST_DONE );
0156 
0157   rtems_semaphore_delete( ctx.semaphore );
0158   T_rsc_success( status );
0159 }
0160 
0161 static rtems_task Init( rtems_task_argument argument )
0162 {
0163   rtems_test_run( argument, TEST_STATE );
0164 }
0165 
0166 /* configuration information */
0167 
0168 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0169 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0170 
0171 #define CONFIGURE_MAXIMUM_TASKS       1
0172 #define CONFIGURE_MAXIMUM_SEMAPHORES  1
0173 #define CONFIGURE_MICROSECONDS_PER_TICK  1000
0174 #if defined(PRIORITY_NO_TIMEOUT_REVERSE)
0175   #define CONFIGURE_INIT_TASK_PRIORITY   250
0176 #endif
0177 #define CONFIGURE_MICROSECONDS_PER_TICK  1000
0178 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0179 
0180 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0181 
0182 #define CONFIGURE_INIT
0183 #include <rtems/confdefs.h>
0184 
0185 /* global variables */