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

4.115
Last change on this file since 698c2e50 was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 25, 2014 at 7:06:16 AM

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

  • Property mode set to 100644
File size: 6.3 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#if defined(USE_USER_SIGNALS_PROCESS)
15  #define TEST_NAME                "PSXSIGNAL 3"
16  #define TEST_STRING              "User Signals to Process"
17  #define SIGNAL_ONE               SIGUSR1
18  #define SIGNAL_TWO               SIGUSR2
19  #define SEND_SIGNAL(_sig)        kill( getpid(), _sig )
20  #define TO_PROCESS
21
22#elif defined(USE_REAL_TIME_SIGNALS_PROCESS)
23  #define TEST_NAME                "PSXSIGNAL 4"
24  #define TEST_STRING              "Real-Time Signals to Process"
25  #define SIGNAL_ONE               SIGRTMIN
26  #define SIGNAL_TWO               SIGRTMAX
27  #define SEND_SIGNAL(_sig)        kill( getpid(), _sig )
28  #define TO_PROCESS
29
30#elif defined(USE_USER_SIGNALS_THREAD)
31  #define TEST_NAME                "PSXSIGNAL 5"
32  #define TEST_STRING              "User Signals to Thread"
33  #define SIGNAL_ONE               SIGUSR1
34  #define SIGNAL_TWO               SIGUSR2
35  #define SEND_SIGNAL(_sig)        pthread_kill( id, _sig )
36  #define TO_THREAD
37
38#elif defined(USE_REAL_TIME_SIGNALS_THREAD)
39  #define TEST_NAME                "PSXSIGNAL 5"
40  #define TEST_STRING              "Real-Time Signals to Thread"
41  #define SIGNAL_ONE               SIGRTMIN
42  #define SIGNAL_TWO               SIGRTMAX
43  #define SEND_SIGNAL(_sig)        pthread_kill( id, _sig )
44  #define TO_THREAD
45
46#else
47  #error "Test Mode not defined"
48#endif
49
50#include <pmacros.h>
51#include <signal.h>
52#include <errno.h>
53#include <pthread.h>
54#include <sched.h>
55
56const char rtems_test_name[] = TEST_NAME;
57
58/* forward declarations to avoid warnings */
59void *POSIX_Init(void *argument);
60void *Test_Thread(void *arg);
61void Signal_handler(int signo, siginfo_t *info, void *arg);
62const char *signal_name(int signo);
63
64volatile bool      Signal_occurred;
65volatile pthread_t Signal_thread;
66
67void Signal_handler(
68  int        signo,
69  siginfo_t *info,
70  void      *arg
71)
72{
73  Signal_occurred = true;
74  Signal_thread   = pthread_self();
75}
76
77const char *signal_name(int signo)
78{
79  if (signo == SIGUSR1)
80    return "SIGUSR1";
81  if (signo == SIGUSR2)
82    return "SIGUSR2";
83  if (signo == SIGRTMIN)
84    return "SIGRTMIN";
85  if (signo == SIGRTMAX)
86    return "SIGRTMAX";
87  return "unknown-signal";
88}
89
90void *Test_Thread(void *arg)
91{
92  bool        blocked = *((bool *)arg);
93  const char *name;
94  int         sc;
95  sigset_t    mask;
96  sigset_t    wait_mask;
97  siginfo_t   info;
98
99  if ( blocked )
100    name = "SignalBlocked";
101  else
102    name = "SignalNotBlocked";
103
104  /* build unblocked mask */
105  sc = sigemptyset( &mask );
106  rtems_test_assert( !sc );
107
108  printf( "%s - Unblock %s\n", name, signal_name(SIGNAL_ONE) );
109  sc = sigaddset( &mask, SIGNAL_ONE );
110  rtems_test_assert( !sc );
111
112  if ( !blocked ) {
113    printf( "%s - Unblock %s\n", name, signal_name(SIGNAL_TWO) );
114    sc = sigaddset( &mask, SIGNAL_TWO );
115    rtems_test_assert( !sc );
116  }
117
118  /* unblocked signals */
119  sc = pthread_sigmask( SIG_UNBLOCK, &mask, NULL );
120  rtems_test_assert( !sc );
121
122  /* build wait mask */
123  sc = sigemptyset( &wait_mask );
124  rtems_test_assert( !sc );
125
126  sc = sigaddset( &wait_mask, SIGNAL_ONE );
127  rtems_test_assert( !sc );
128
129  /* wait for a signal */
130  memset( &info, 0, sizeof(info) );
131
132  printf( "%s - Wait for %s unblocked\n", name, signal_name(SIGNAL_ONE) );
133  sigwaitinfo( &wait_mask, &info );
134  rtems_test_assert( !sc );
135
136  printf( "%s - siginfo.si_signo=%d\n", name, info.si_signo );
137  printf( "%s - siginfo.si_code=%d\n", name, info.si_code );
138  /* FIXME: Instead of casting to (uintptr_t) and using PRIxPTR, we
139   * likely should use %p. However, this would render this test's
140   * behavior non-deterministic, because %p's behavior is
141   * "implementation defined" */
142  printf( "%s - siginfo.si_value=0x%08" PRIxPTR "\n", name, (uintptr_t) info.si_value.sival_ptr );
143
144  rtems_test_assert( info.si_signo == SIGNAL_TWO );
145  rtems_test_assert( info.si_code == SI_USER );
146
147  printf( "%s - exiting\n", name );
148  return NULL;
149}
150
151void *POSIX_Init(
152  void *argument
153)
154{
155  int                 sc;
156  pthread_t           id;
157  struct sigaction    act;
158  bool                trueArg = true;
159  bool                falseArg = false;
160  struct timespec     delay_request;
161
162  TEST_BEGIN();
163  puts( "Init - Variation is: " TEST_STRING );
164
165  Signal_occurred = false;
166
167  act.sa_handler = NULL;
168  act.sa_sigaction = Signal_handler;
169  act.sa_flags   = SA_SIGINFO;
170  sigaction( SIGNAL_ONE, &act, NULL );
171  sigaction( SIGNAL_TWO, &act, NULL );
172
173  /* create threads */
174  sc = pthread_create( &id, NULL, Test_Thread, &falseArg );
175  rtems_test_assert( !sc );
176
177  sc = pthread_create( &id, NULL, Test_Thread, &trueArg );
178  rtems_test_assert( !sc );
179
180  puts( "Init - sleep - let threads settle - OK" );
181  delay_request.tv_sec = 0;
182  delay_request.tv_nsec = 5 * 100000000;
183  sc = nanosleep( &delay_request, NULL );
184  rtems_test_assert( !sc );
185
186  puts( "Init - sleep - SignalBlocked thread settle - OK" );
187  sc = nanosleep( &delay_request, NULL );
188  rtems_test_assert( !sc );
189
190  printf( "Init - sending %s - deliver to one thread\n",
191          signal_name(SIGNAL_TWO));
192  sc =  SEND_SIGNAL( SIGNAL_TWO );
193  rtems_test_assert( !sc );
194
195  printf( "Init - sending %s - deliver to other thread\n",
196          signal_name(SIGNAL_TWO));
197  sc =  SEND_SIGNAL( SIGNAL_TWO );
198  rtems_test_assert( !sc );
199
200  #if defined(TO_PROCESS)
201    printf( "Init - sending %s - expect EAGAIN\n", signal_name(SIGNAL_TWO) );
202    sc =  SEND_SIGNAL( SIGNAL_TWO );
203    rtems_test_assert( sc == -1 );
204    rtems_test_assert( errno == EAGAIN );
205  #endif
206
207  puts( "Init - sleep - let thread report if it unblocked - OK" );
208  usleep(500000);
209
210  /* we are just sigwait'ing the signal, not delivering it */
211  rtems_test_assert( Signal_occurred == true );
212
213  TEST_END();
214  rtems_test_exit(0);
215
216  return NULL; /* just so the compiler thinks we returned something */
217}
218
219/* configuration information */
220
221#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
222#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
223
224#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
225
226#define CONFIGURE_MAXIMUM_POSIX_THREADS        3
227#define CONFIGURE_MAXIMUM_POSIX_QUEUED_SIGNALS 1
228
229#define CONFIGURE_POSIX_INIT_THREAD_TABLE
230
231#define CONFIGURE_INIT
232#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.