source: rtems/testsuites/psxtests/psxsignal03/init.c @ c61e631

4.104.115
Last change on this file since c61e631 was c61e631, checked in by Ralf Corsepius <ralf.corsepius@…>, on Nov 2, 2009 at 7:10:36 AM

Print sigval.sival_ptr instead of "union sigval".

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