source: rtems/testsuites/psxtests/psxsignal02/init.c @ 698c2e50

4.115
Last change on this file since 698c2e50 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

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