OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [compat/] [posix/] [current/] [tests/] [sigsetjmp.c] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.