]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/compat/posix/v2_0/tests/sigsetjmp.c
6fd0f1185b2730d9401801243709f358fd207fdd
[karo-tx-redboot.git] / packages / compat / posix / v2_0 / tests / sigsetjmp.c
1 //==========================================================================
2 //
3 //        sigsetjmp.c
4 //
5 //        POSIX sigsetjmp test
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     nickg
44 // Contributors:  nickg
45 // Date:          2000-04-10
46 // Description:   Tests POSIX sigsetjmp functionality.
47 //
48 //####DESCRIPTIONEND####
49 //==========================================================================
50
51 #include <cyg/infra/testcase.h>
52 #include <pkgconf/posix.h>
53
54 #if !defined(CYGPKG_POSIX_SIGNALS)
55 #define NA_MSG "POSIX signals not enabled"
56 #elif !defined(CYGPKG_POSIX_PTHREAD)
57 #define NA_MSG "POSIX threads not enabled"
58 #endif
59
60 #ifdef NA_MSG
61 void
62 cyg_start(void)
63 {
64     CYG_TEST_INIT();
65     CYG_TEST_NA(NA_MSG);
66 }
67 #else
68
69 #include <sys/types.h>
70 #include <pthread.h>
71 #include <signal.h>
72 #include <semaphore.h>
73 #include <time.h>
74 #include <setjmp.h>
75
76 #include <cyg/infra/testcase.h>
77
78 //--------------------------------------------------------------------------
79 // Thread stack.
80
81 char thread1_stack[PTHREAD_STACK_MIN*2];
82 char thread2_stack[PTHREAD_STACK_MIN*2];
83
84 //--------------------------------------------------------------------------
85 // Local variables
86
87 // Sync semaphore
88 sem_t sem;
89
90 // Thread IDs
91 pthread_t thread1;
92 pthread_t thread2;
93
94 timer_t timer1;
95 timer_t timer2;
96
97 volatile int sigusr1_called = 0;
98 volatile int sigusr2_called = 0;
99
100 sigjmp_buf jmpbuf1;
101 sigjmp_buf jmpbuf2;
102
103 //--------------------------------------------------------------------------
104 // Signal handler functions
105
106 static void sigusr1( int signo, siginfo_t *info, void *context )
107 {
108     CYG_TEST_INFO( "sigusr1() handler called" );
109     CYG_TEST_CHECK( signo == SIGUSR1, "Signal not SIGUSR1");
110     CYG_TEST_CHECK( signo == info->si_signo, "Bad signal number in siginfo" );
111     CYG_TEST_CHECK( info->si_code == SI_TIMER, "Siginfo code not SI_TIMER" );
112     CYG_TEST_CHECK( info->si_value.sival_int == 0xABCDEF01, "Siginfo value wrong");
113     CYG_TEST_CHECK( pthread_equal(pthread_self(), thread1), "Not called in thread1");
114
115     sigusr1_called++;
116
117     CYG_TEST_INFO( "sigusr1() handler calling siglongjmp()" );
118     
119     siglongjmp( jmpbuf1, sigusr1_called );
120 }
121
122 static void sigusr2( int signo, siginfo_t *info, void *context )
123 {
124     CYG_TEST_INFO( "sigusr2() handler called" );
125     CYG_TEST_CHECK( signo == SIGUSR2, "Signal not SIGUSR2");
126     CYG_TEST_CHECK( signo == info->si_signo, "Bad signal number in siginfo" );
127     CYG_TEST_CHECK( info->si_code == SI_TIMER, "Siginfo code not SI_TIMER" );
128     CYG_TEST_CHECK( info->si_value.sival_int == 0xABCDEF02, "Siginfo value wrong");
129     CYG_TEST_CHECK( pthread_equal(pthread_self(), thread2), "Not called in thread2");
130
131     sigusr2_called++;
132
133     CYG_TEST_INFO( "sigusr2() handler calling siglongjmp()" );
134     siglongjmp( jmpbuf2, sigusr2_called );    
135 }
136
137 //--------------------------------------------------------------------------
138
139 void *pthread_entry1( void *arg)
140 {
141     sigset_t mask;
142
143     
144     CYG_TEST_INFO( "Thread 1 running" );
145
146     // Make a full set
147     sigfillset( &mask );
148
149     // remove USR1 signal
150     sigdelset( &mask, SIGUSR1 );
151
152     // Set signal mask
153     pthread_sigmask( SIG_SETMASK, &mask, NULL );
154     
155     // Get main thread going again
156     sem_post( &sem );
157
158     do
159     {
160         sigset_t curmask;
161
162         CYG_TEST_INFO( "Thread1: calling sigsetjmp()");                
163         if( sigsetjmp( jmpbuf1, 1 ) != 0 )
164             CYG_TEST_INFO( "Thread1: sigsetjmp() returned non-zero");
165         
166         pthread_sigmask( SIG_SETMASK, NULL, &curmask );
167         CYG_TEST_CHECK( curmask == mask, "Thread1: Signal masks not equal" );
168
169         if ( sigusr1_called >= 1 )
170             break;
171         
172         CYG_TEST_INFO( "Thread1: calling pause()");        
173         pause();
174
175         CYG_TEST_INFO( "Thread1: pause() returned");        
176     } while(1);
177
178     CYG_TEST_INFO( "Thread1: calling pthread_exit()");    
179     pthread_exit( arg );
180 }
181
182 //--------------------------------------------------------------------------
183
184 void *pthread_entry2( void *arg)
185 {
186     sigset_t mask;
187     
188     CYG_TEST_INFO( "Thread 2 running" );
189
190     // Make a full set
191     sigfillset( &mask );
192
193     // remove USR2 signal
194     sigdelset( &mask, SIGUSR2 );
195
196     // Set signal mask
197     pthread_sigmask( SIG_SETMASK, &mask, NULL );
198     
199     // Get main thread going again
200     sem_post( &sem );
201
202     do 
203     {
204         sigset_t curmask;
205
206         CYG_TEST_INFO( "Thread2: calling sigsetjmp()");
207         if( sigsetjmp( jmpbuf2, 1 ) != 0 )
208             CYG_TEST_INFO( "Thread2: sigsetjmp() returned non-zero");
209         
210         pthread_sigmask( SIG_SETMASK, NULL, &curmask );
211         CYG_TEST_CHECK( curmask == mask, "Thread2: Signal masks not equal" );
212
213         if ( sigusr2_called >= 6 )
214             break;
215         
216         CYG_TEST_INFO( "Thread2: calling pause()");        
217         pause();
218
219         CYG_TEST_INFO( "Thread2: pause() returned");
220     } while(1);
221
222     CYG_TEST_INFO( "Thread2: calling pthread_exit()");    
223     pthread_exit( arg );
224 }
225
226 //--------------------------------------------------------------------------
227
228 int main(int argc, char **argv)
229 {
230     int ret;
231     sigset_t mask;
232     pthread_attr_t attr;
233     void *retval;
234     
235     CYG_TEST_INIT();
236
237     // Make a full signal set
238     sigfillset( &mask );
239
240     
241     // Install signal handlers
242     {
243         struct sigaction sa;
244
245         sa.sa_sigaction = sigusr1;
246         sa.sa_mask = mask;
247         sa.sa_flags = SA_SIGINFO;
248
249         ret = sigaction( SIGUSR1, &sa, NULL );
250
251         CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
252     }
253     {
254         struct sigaction sa;
255
256         sa.sa_sigaction = sigusr2;
257         sa.sa_mask = mask;
258         sa.sa_flags = SA_SIGINFO;
259
260         ret = sigaction( SIGUSR2, &sa, NULL );
261
262         CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
263     }
264
265
266     // Create the timers
267
268     {
269         struct sigevent sev;
270         struct itimerspec value;
271         
272         sev.sigev_notify                = SIGEV_SIGNAL;
273         sev.sigev_signo                 = SIGUSR1;
274         sev.sigev_value.sival_int       = 0xABCDEF01;
275
276         value.it_value.tv_sec           = 1;
277         value.it_value.tv_nsec          = 0;
278         value.it_interval.tv_sec        = 0;
279         value.it_interval.tv_nsec       = 0;
280         
281         ret = timer_create( CLOCK_REALTIME, &sev, &timer1 );
282
283         CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
284
285         ret = timer_settime( timer1, 0, &value, NULL );
286
287         CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
288     }
289
290     {
291         struct sigevent sev;
292         struct itimerspec value;
293         
294         sev.sigev_notify                = SIGEV_SIGNAL;
295         sev.sigev_signo                 = SIGUSR2;
296         sev.sigev_value.sival_int       = 0xABCDEF02;
297
298         value.it_value.tv_sec           = 0;
299         value.it_value.tv_nsec          = 500000000;
300         value.it_interval.tv_sec        = 0;
301         value.it_interval.tv_nsec       = 250000000;
302         
303         ret = timer_create( CLOCK_REALTIME, &sev, &timer2 );
304
305         CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
306
307         ret = timer_settime( timer2, 0, &value, NULL );
308
309         CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
310     }
311
312     
313     // Mask all signals
314     pthread_sigmask( SIG_SETMASK, &mask, NULL );
315     
316     sem_init( &sem, 0, 0 );
317     
318     // Create test threads
319
320     {
321         pthread_attr_init( &attr );
322
323         pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
324         pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
325
326         pthread_create( &thread1,
327                         &attr,
328                         pthread_entry1,
329                         (void *)0x12345671);
330     }
331
332     {
333         pthread_attr_init( &attr );
334
335         pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
336         pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
337
338         pthread_create( &thread2,
339                         &attr,
340                         pthread_entry2,
341                         (void *)0x12345672);
342     }
343     
344     // Wait for other thread to get started
345     CYG_TEST_INFO( "Main: calling sem_wait()");
346     sem_wait( &sem );
347     CYG_TEST_INFO( "Main: calling sem_wait() again");    
348     sem_wait( &sem );
349
350     // Now join with thread1
351     CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
352     pthread_join( thread1, &retval );
353
354     CYG_TEST_CHECK( retval == (void *)0x12345671, "Thread 1 retval wrong");
355     
356     // And thread 2
357     CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
358     pthread_join( thread2, &retval );
359
360     // now delete the timers
361     CYG_TEST_INFO( "Main: calling timer_delete(timer1)");    
362     ret = timer_delete( timer1 );
363
364     CYG_TEST_CHECK( ret == 0 , "timer_delete(timer1) returned error");
365
366     CYG_TEST_INFO( "Main: calling timer_delete(timer2)");    
367     ret = timer_delete( timer2 );
368
369     CYG_TEST_CHECK( ret == 0 , "timer_delete(timer2) returned error");
370
371     
372     CYG_TEST_CHECK( retval == (void *)0x12345672, "Thread 2 retval wrong");
373     
374     CYG_TEST_CHECK( sigusr1_called == 1, "SIGUSR1 signal handler not called once" );
375     CYG_TEST_CHECK( sigusr2_called == 6, "SIGUSR2 signal handler not called six times" );
376
377     CYG_TEST_PASS_FINISH( "sigsetjmp" );
378 }
379
380 #endif
381
382 //--------------------------------------------------------------------------
383 // end of sigsetjmp.c