source: rtems/testsuites/psxtests/psxsignal02/init.c @ 8798372

5
Last change on this file since 8798372 was 8798372, checked in by Joel Sherrill <joel@…>, on 09/10/19 at 17:53:31

Correct initial POSIX signals mask

+ Modify POSIX thread create extension to ensure expected

initial signal mask is provided to system threads, initial
tasks and threads, and inheritied by tasks and threads.

+ Adds psxsignal07 to verify functionality when using a POSIX

Initialization thread and POSIX threads.

+ Adds psxsignal08 to verify functionality when using a Classic API

Initialization task and Classic API tasks.

Closes #3794.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <pmacros.h>
15#include <signal.h>
16#include <errno.h>
17#include <pthread.h>
18#include <sched.h>
19
20const char rtems_test_name[] = "PSXSIGNAL 2";
21
22/* forward declarations to avoid warnings */
23void *POSIX_Init(void *argument);
24void *Test_Thread(void *arg);
25void Signal_handler(int signo);
26void Install_Signal_Handler(const char *task_name);
27
28volatile bool      Signal_occurred;
29volatile pthread_t Signal_thread;
30
31static void block_all_signals(void)
32{
33  int               sc;
34  sigset_t          mask;
35
36  sc = sigfillset( &mask );
37  rtems_test_assert( !sc );
38
39  sc = pthread_sigmask( SIG_BLOCK, &mask, NULL );
40  rtems_test_assert( !sc );
41}
42
43void Signal_handler(
44  int signo
45)
46{
47  Signal_occurred = true;
48  Signal_thread   = pthread_self();
49}
50
51void Install_Signal_Handler(
52  const char *task_name
53)
54{
55  int               sc;
56  sigset_t          mask;
57
58  sc = sigemptyset( &mask );
59  rtems_test_assert( !sc );
60
61  sc = sigaddset( &mask, SIGUSR1 );
62  rtems_test_assert( !sc );
63
64  printf( "%s - Unblock SIGUSR1\n", task_name );
65  sc = pthread_sigmask( SIG_UNBLOCK, &mask, NULL );
66  rtems_test_assert( !sc );
67}
68
69/*
70
71Tasks and actions, created in this order, all interested in SIGUSR1
72  - 20     - interested, suspend?
73  - 18     - interested, suspend?
74  - 16     - interested, spins
75  - 14     - interested, spins
76  - 12     - interested, sleeps
77  - 10     - interested, suspends
78  - 8      - interested, sleeps
79
80Order is critical because the algorithm works by thread index
81*/
82
83typedef enum {
84  SUSPEND,
85  SPIN,
86  SLEEP
87} Action_t;
88
89const char *Actions[] = {
90  "Suspends self",
91  "Spins",
92  "Sleeps"
93};
94
95
96typedef struct {
97  int         priority;
98  Action_t    action;
99  const char *name;
100} Test_t;
101
102Test_t Threads[] = {
103  {  8, SUSPEND,  "P8"  },  /* base priority */
104  {  7, SUSPEND,  "P7"  },  /* lower priority -- no change */
105  { 12, SUSPEND,  "P12" },  /* higher priority, blocked */
106  { 12, SUSPEND,  "P12" },  /* equal priority, blocked */
107  { 12, SLEEP,    "P12" },  /* equal priority, interruptible */
108  { 12, SLEEP,    "P12" },  /* equal priority, interruptible */
109  { 12, SPIN,     "P12" },  /* equal priority, ready */
110  { 12, SPIN,     "P12" },  /* equal priority, ready -- no change */
111  { -1, 0,        ""    },
112};
113
114void *Test_Thread(void *arg)
115{
116  Test_t *test = (Test_t *)arg;
117
118  Install_Signal_Handler( test->name );
119
120  printf( "%s - %s\n", test->name, Actions[test->action] );
121  switch ( test->action ) {
122    case SUSPEND:
123      (void) rtems_task_suspend( RTEMS_SELF );
124      break;
125    case SPIN:
126      while (1) ;
127      break;
128    case SLEEP:
129      sleep( 30 );
130      break;
131  }
132
133  printf( "%s - exiting\n", test->name );
134  return NULL;
135
136}
137
138void *POSIX_Init(
139  void *argument
140)
141{
142  int                 i;
143  int                 sc;
144  pthread_t           id;
145  pthread_attr_t      attr;
146  struct sched_param  param;
147  Test_t             *test;
148  struct sigaction    act;
149  struct timespec     delay_request;
150
151  TEST_BEGIN();
152
153  Signal_occurred = false;
154
155  block_all_signals();
156
157  act.sa_handler = Signal_handler;
158  act.sa_flags   = 0;
159  sigaction( SIGUSR1, &act, NULL );
160
161  puts( "Init - Raise my priority" );
162  sc = pthread_attr_init( &attr );
163  rtems_test_assert( !sc );
164
165  param.sched_priority = 30;
166  sc = pthread_setschedparam( pthread_self(), SCHED_RR, &param );
167  rtems_test_assert( !sc );
168
169  for ( i=0, test=Threads ; test->priority != -1 ; i++, test++ ) {
170    printf( "Init - Create thread %d, priority=%d\n", i, test->priority );
171    sc = pthread_attr_init( &attr );
172    rtems_test_assert( !sc );
173
174    sc = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
175    rtems_test_assert( !sc );
176
177    sc = pthread_attr_setschedpolicy( &attr, SCHED_RR );
178    rtems_test_assert( !sc );
179
180    param.sched_priority = test->priority;
181    sc = pthread_attr_setschedparam( &attr, &param );
182    rtems_test_assert( !sc );
183
184    sc = pthread_create( &id, &attr, Test_Thread, test );
185    rtems_test_assert( !sc );
186
187    puts( "Init - sleep - let thread settle - OK" );
188    delay_request.tv_sec = 0;
189    delay_request.tv_nsec = 50000000;
190    sc = nanosleep( &delay_request, NULL );
191    rtems_test_assert( !sc );
192  }
193
194  puts( "Init - sending SIGUSR1" );
195  sc =  kill( getpid(), SIGUSR1 );
196  rtems_test_assert( !sc );
197
198  /* we are just scheduling the signal, not delivering it */
199  rtems_test_assert( Signal_occurred == false );
200
201  TEST_END();
202  rtems_test_exit(0);
203
204  return NULL; /* just so the compiler thinks we returned something */
205}
206
207/* configuration information */
208
209#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
210#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
211
212#define CONFIGURE_MICROSECONDS_PER_TICK        1000
213#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
214
215#define CONFIGURE_MAXIMUM_POSIX_THREADS        9
216
217#define CONFIGURE_POSIX_INIT_THREAD_TABLE
218
219#define CONFIGURE_INIT
220#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.