1 //==========================================================================
5 // POSIX sigsetjmp test
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.
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.
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
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.
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.
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.
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####
44 // Contributors: nickg
46 // Description: Tests POSIX sigsetjmp functionality.
48 //####DESCRIPTIONEND####
49 //==========================================================================
51 #include <cyg/infra/testcase.h>
52 #include <pkgconf/posix.h>
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"
69 #include <sys/types.h>
72 #include <semaphore.h>
76 #include <cyg/infra/testcase.h>
78 //--------------------------------------------------------------------------
81 char thread1_stack[PTHREAD_STACK_MIN*2];
82 char thread2_stack[PTHREAD_STACK_MIN*2];
84 //--------------------------------------------------------------------------
97 volatile int sigusr1_called = 0;
98 volatile int sigusr2_called = 0;
103 //--------------------------------------------------------------------------
104 // Signal handler functions
106 static void sigusr1( int signo, siginfo_t *info, void *context )
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");
117 CYG_TEST_INFO( "sigusr1() handler calling siglongjmp()" );
119 siglongjmp( jmpbuf1, sigusr1_called );
122 static void sigusr2( int signo, siginfo_t *info, void *context )
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");
133 CYG_TEST_INFO( "sigusr2() handler calling siglongjmp()" );
134 siglongjmp( jmpbuf2, sigusr2_called );
137 //--------------------------------------------------------------------------
139 void *pthread_entry1( void *arg)
144 CYG_TEST_INFO( "Thread 1 running" );
149 // remove USR1 signal
150 sigdelset( &mask, SIGUSR1 );
153 pthread_sigmask( SIG_SETMASK, &mask, NULL );
155 // Get main thread going again
162 CYG_TEST_INFO( "Thread1: calling sigsetjmp()");
163 if( sigsetjmp( jmpbuf1, 1 ) != 0 )
164 CYG_TEST_INFO( "Thread1: sigsetjmp() returned non-zero");
166 pthread_sigmask( SIG_SETMASK, NULL, &curmask );
167 CYG_TEST_CHECK( curmask == mask, "Thread1: Signal masks not equal" );
169 if ( sigusr1_called >= 1 )
172 CYG_TEST_INFO( "Thread1: calling pause()");
175 CYG_TEST_INFO( "Thread1: pause() returned");
178 CYG_TEST_INFO( "Thread1: calling pthread_exit()");
182 //--------------------------------------------------------------------------
184 void *pthread_entry2( void *arg)
188 CYG_TEST_INFO( "Thread 2 running" );
193 // remove USR2 signal
194 sigdelset( &mask, SIGUSR2 );
197 pthread_sigmask( SIG_SETMASK, &mask, NULL );
199 // Get main thread going again
206 CYG_TEST_INFO( "Thread2: calling sigsetjmp()");
207 if( sigsetjmp( jmpbuf2, 1 ) != 0 )
208 CYG_TEST_INFO( "Thread2: sigsetjmp() returned non-zero");
210 pthread_sigmask( SIG_SETMASK, NULL, &curmask );
211 CYG_TEST_CHECK( curmask == mask, "Thread2: Signal masks not equal" );
213 if ( sigusr2_called >= 6 )
216 CYG_TEST_INFO( "Thread2: calling pause()");
219 CYG_TEST_INFO( "Thread2: pause() returned");
222 CYG_TEST_INFO( "Thread2: calling pthread_exit()");
226 //--------------------------------------------------------------------------
228 int main(int argc, char **argv)
237 // Make a full signal set
241 // Install signal handlers
245 sa.sa_sigaction = sigusr1;
247 sa.sa_flags = SA_SIGINFO;
249 ret = sigaction( SIGUSR1, &sa, NULL );
251 CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
256 sa.sa_sigaction = sigusr2;
258 sa.sa_flags = SA_SIGINFO;
260 ret = sigaction( SIGUSR2, &sa, NULL );
262 CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
270 struct itimerspec value;
272 sev.sigev_notify = SIGEV_SIGNAL;
273 sev.sigev_signo = SIGUSR1;
274 sev.sigev_value.sival_int = 0xABCDEF01;
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;
281 ret = timer_create( CLOCK_REALTIME, &sev, &timer1 );
283 CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
285 ret = timer_settime( timer1, 0, &value, NULL );
287 CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
292 struct itimerspec value;
294 sev.sigev_notify = SIGEV_SIGNAL;
295 sev.sigev_signo = SIGUSR2;
296 sev.sigev_value.sival_int = 0xABCDEF02;
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;
303 ret = timer_create( CLOCK_REALTIME, &sev, &timer2 );
305 CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
307 ret = timer_settime( timer2, 0, &value, NULL );
309 CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
314 pthread_sigmask( SIG_SETMASK, &mask, NULL );
316 sem_init( &sem, 0, 0 );
318 // Create test threads
321 pthread_attr_init( &attr );
323 pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
324 pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
326 pthread_create( &thread1,
333 pthread_attr_init( &attr );
335 pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
336 pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
338 pthread_create( &thread2,
344 // Wait for other thread to get started
345 CYG_TEST_INFO( "Main: calling sem_wait()");
347 CYG_TEST_INFO( "Main: calling sem_wait() again");
350 // Now join with thread1
351 CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
352 pthread_join( thread1, &retval );
354 CYG_TEST_CHECK( retval == (void *)0x12345671, "Thread 1 retval wrong");
357 CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
358 pthread_join( thread2, &retval );
360 // now delete the timers
361 CYG_TEST_INFO( "Main: calling timer_delete(timer1)");
362 ret = timer_delete( timer1 );
364 CYG_TEST_CHECK( ret == 0 , "timer_delete(timer1) returned error");
366 CYG_TEST_INFO( "Main: calling timer_delete(timer2)");
367 ret = timer_delete( timer2 );
369 CYG_TEST_CHECK( ret == 0 , "timer_delete(timer2) returned error");
372 CYG_TEST_CHECK( retval == (void *)0x12345672, "Thread 2 retval wrong");
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" );
377 CYG_TEST_PASS_FINISH( "sigsetjmp" );
382 //--------------------------------------------------------------------------
383 // end of sigsetjmp.c