Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
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 <pmacros.h>
0034 #include <errno.h>
0035 
0036 const char rtems_test_name[] = "PSXCANCEL 1";
0037 
0038 /* forward declarations to avoid warnings */
0039 void *POSIX_Init(void *argument);
0040 
0041 static volatile int TSR_occurred;
0042 
0043 static volatile int TSR_status;
0044 
0045 static rtems_id  timer_id;
0046 
0047 static pthread_t thread;
0048 
0049 static void *suspend_self( void *arg )
0050 {
0051   int               eno;
0052   rtems_status_code status;
0053 
0054   (void) arg;
0055 
0056   eno = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
0057   rtems_test_assert( eno == 0 );
0058 
0059   status = rtems_task_suspend( RTEMS_SELF);
0060   rtems_test_assert( status == RTEMS_SUCCESSFUL );
0061 
0062   return NULL;
0063 }
0064 
0065 static rtems_timer_service_routine Cancel_duringISR_TSR(
0066   rtems_id  ignored_id,
0067   void     *ignored_address
0068 )
0069 {
0070   TSR_status = pthread_cancel( thread );
0071   TSR_occurred = 1;
0072 }
0073 
0074 static rtems_timer_service_routine SetState_duringISR_TSR(
0075   rtems_id  ignored_id,
0076   void     *ignored_address
0077 )
0078 {
0079   int oldstate;
0080 
0081   TSR_status = pthread_setcancelstate( 0, &oldstate );
0082   TSR_occurred = 1;
0083 }
0084 
0085 static rtems_timer_service_routine SetType_duringISR_TSR(
0086   rtems_id  ignored_id,
0087   void     *ignored_address
0088 )
0089 {
0090   int oldtype;
0091 
0092   TSR_status = pthread_setcanceltype( 0, &oldtype );
0093   TSR_occurred = 1;
0094 }
0095 
0096 static void doit(
0097   rtems_timer_service_routine (*TSR)(rtems_id, void *),
0098   const char                   *method,
0099   int                           expected_status
0100 )
0101 {
0102   rtems_interval    start;
0103   rtems_interval    end;
0104   rtems_status_code status;
0105 
0106   printf( "Init: schedule %s from a TSR\n", method );
0107 
0108   TSR_occurred = 0;
0109   TSR_status   = 0;
0110 
0111   status = rtems_timer_fire_after( timer_id, 10, TSR, NULL );
0112   rtems_test_assert( !status );
0113 
0114   start = rtems_clock_get_ticks_since_boot();
0115   do {
0116     end = rtems_clock_get_ticks_since_boot();
0117   } while ( !TSR_occurred && ((end - start) <= 800));
0118 
0119   if ( !TSR_occurred ) {
0120     printf( "%s did not occur\n", method );
0121     rtems_test_exit(0);
0122   }
0123   if ( TSR_status != expected_status ) {
0124     printf( "%s returned %s\n", method, strerror(TSR_status) );
0125     rtems_test_exit(0);
0126   }
0127   printf( "%s - from ISR returns expected status - OK\n", method );
0128 
0129 }
0130 
0131 void *POSIX_Init(
0132   void *argument
0133 )
0134 {
0135   rtems_status_code status;
0136   int               eno;
0137   void             *value;
0138 
0139   TEST_BEGIN();
0140 
0141   status = rtems_timer_create(
0142     rtems_build_name( 'T', 'M', '1', ' ' ),
0143     &timer_id
0144   );
0145   rtems_test_assert( !status );
0146 
0147   eno = pthread_create( &thread, NULL, suspend_self, NULL );
0148   rtems_test_assert( eno == 0 );
0149 
0150   doit( Cancel_duringISR_TSR, "pthread_cancel", 0 );
0151   doit( SetState_duringISR_TSR, "pthread_setcancelstate", EPROTO );
0152   doit( SetType_duringISR_TSR, "pthread_setcanceltype", EPROTO );
0153 
0154   value = NULL;
0155   eno = pthread_join( thread, &value );
0156   rtems_test_assert( eno == 0 );
0157   rtems_test_assert( value == PTHREAD_CANCELED );
0158 
0159   TEST_END();
0160   rtems_test_exit(0);
0161   return NULL; /* just so the compiler thinks we returned something */
0162 }
0163 
0164 /* configuration information */
0165 
0166 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0167 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0168 
0169 #define CONFIGURE_MAXIMUM_TIMERS        1
0170 
0171 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0172 
0173 #define CONFIGURE_MAXIMUM_POSIX_THREADS        2
0174 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0175 
0176 #define CONFIGURE_INIT
0177 #include <rtems/confdefs.h>