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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        signal1.cxx
4
//
5
//        POSIX signal test 1
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 signal 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 <errno.h>
75
 
76
//--------------------------------------------------------------------------
77
// Thread stack.
78
 
79
char thread_stack[PTHREAD_STACK_MIN*2];
80
 
81
//--------------------------------------------------------------------------
82
// Local variables
83
 
84
// Sync semaphore
85
sem_t sem;
86
 
87
// Thread ID
88
pthread_t thread1;
89
 
90
volatile int sigusr2_called = 0;
91
volatile int sigalrm_called = 0;
92
 
93
//--------------------------------------------------------------------------
94
// Signal handler functions
95
 
96
static void sigusr2( int signo )
97
{
98
    CYG_TEST_INFO( "sigusr2() handler called" );
99
    CYG_TEST_CHECK( signo == SIGUSR2, "Signal not SIGUSR2");
100
    CYG_TEST_CHECK( pthread_equal(pthread_self(), thread1), "Not called in thread1");
101
 
102
    sigusr2_called++;
103
}
104
 
105
static void sigalrm( int signo )
106
{
107
    CYG_TEST_INFO( "sigalrm() handler called" );
108
    CYG_TEST_CHECK( signo == SIGALRM, "Signal not SIGALRM");
109
    CYG_TEST_CHECK( pthread_equal(pthread_self(), thread1), "Not called in thread1");
110
 
111
    sigalrm_called++;
112
}
113
 
114
//--------------------------------------------------------------------------
115
 
116
void *pthread_entry1( void *arg)
117
{
118
    sigset_t mask;
119
    siginfo_t info;
120
    struct timespec timeout;
121
    int sig, sig2, err;
122
 
123
    CYG_TEST_INFO( "Thread 1 running" );
124
 
125
    // Should have inherited parent's signal mask
126
    pthread_sigmask( 0, NULL, &mask );
127
    CYG_TEST_CHECK( sigismember( &mask, SIGALRM),
128
                                 "SIGALRM mask inherited");
129
    CYG_TEST_CHECK( sigismember( &mask, SIGUSR1),
130
                                 "SIGUSR1 mask inherited");
131
    CYG_TEST_CHECK( sigismember( &mask, SIGUSR2),
132
                                 "SIGUSR2 mask inherited");
133
    CYG_TEST_CHECK( sigismember( &mask, SIGSEGV),
134
                                 "SIGSEGV mask inherited");
135
 
136
    // Make a full set
137
    sigfillset( &mask );
138
 
139
    // remove USR2 and ALRM signals
140
    sigdelset( &mask, SIGUSR2 );
141
    sigdelset( &mask, SIGALRM );
142
 
143
    // Set signal mask
144
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
145
 
146
    // Get main thread going again
147
    sem_post( &sem );
148
 
149
    // set up timeout
150
    timeout.tv_sec = 10;
151
    timeout.tv_nsec = 0;
152
 
153
    CYG_TEST_INFO( "Thread1: calling sigtimedwait()");
154
 
155
    // Wait for a signal to be delivered
156
    sig = sigtimedwait( &mask, &info, &timeout );
157
 
158
    sig2 = info.si_signo;
159
 
160
    CYG_TEST_CHECK( sig == sig2, "sigtimedwait return value not equal");
161
    CYG_TEST_CHECK( sig == SIGUSR1, "Signal not delivered");
162
 
163
    while( sigusr2_called != 2 )
164
    {
165
        CYG_TEST_INFO( "Thread1: calling pause()");
166
        pause();
167
    }
168
 
169
    errno = 0; // strictly correct to reset errno first
170
 
171
    // now wait for SIGALRM to be delivered
172
    CYG_TEST_INFO( "Thread1: calling pause()");
173
    err = pause();
174
    CYG_TEST_CHECK( -1==err, "pause returned -1");
175
    CYG_TEST_CHECK( EINTR==errno, "errno set to EINTR");
176
 
177
    // generate another SIGALRM and wait for it to be delivered too
178
    // we need to mask it first though
179
 
180
    // Make a full set
181
    sigfillset( &mask );
182
 
183
    // Set signal mask
184
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
185
 
186
    alarm(1);
187
    CYG_TEST_INFO( "Thread1: calling sigwait()");
188
    err = sigwait( &mask, &sig);
189
    CYG_TEST_CHECK( 0==err, "sigwait returned -1");
190
    CYG_TEST_CHECK( sig==SIGALRM, "sigwait caught alarm");
191
 
192
    CYG_TEST_INFO( "Thread1: calling pthread_exit()");
193
    pthread_exit( (void *)((int)arg+sig2) );
194
}
195
 
196
//--------------------------------------------------------------------------
197
 
198
int main(int argc, char **argv)
199
{
200
    int ret;
201
    sigset_t mask;
202
    pthread_attr_t attr;
203
    void *retval;
204
    union sigval value;
205
 
206
    CYG_TEST_INIT();
207
 
208
    // Make a full signal set
209
    sigfillset( &mask );
210
 
211
 
212
    // Install signal handlers
213
    {
214
        struct sigaction sa;
215
 
216
        sa.sa_handler = sigusr2;
217
        sa.sa_mask = mask;
218
        sa.sa_flags = 0;
219
 
220
        ret = sigaction( SIGUSR2, &sa, NULL );
221
 
222
        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
223
    }
224
 
225
    {
226
        struct sigaction sa;
227
 
228
        sa.sa_handler = sigalrm;
229
        sa.sa_mask = mask;
230
        sa.sa_flags = 0;
231
 
232
        ret = sigaction( SIGALRM, &sa, NULL );
233
 
234
        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
235
    }
236
 
237
 
238
    // Mask all signals
239
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
240
 
241
    sem_init( &sem, 0, 0 );
242
 
243
    // Create test thread
244
    pthread_attr_init( &attr );
245
 
246
    pthread_attr_setstackaddr( &attr, (void *)&thread_stack[sizeof(thread_stack)] );
247
    pthread_attr_setstacksize( &attr, sizeof(thread_stack) );
248
 
249
    pthread_create( &thread1,
250
                    &attr,
251
                    pthread_entry1,
252
                    (void *)0x12345678);
253
 
254
    // Wait for other thread to get started
255
    CYG_TEST_INFO( "Main: calling sem_wait()");
256
    sem_wait( &sem );
257
 
258
    value.sival_int = 0;
259
 
260
    // send a signal to the other thread
261
    CYG_TEST_INFO( "Main: calling sigqueue(SIGUSR1)");
262
    sigqueue( 0, SIGUSR1, value );
263
 
264
    // Send the signal via kill
265
    CYG_TEST_INFO( "Main: calling kill(0, SIGUSR2)");
266
    kill( 0, SIGUSR2 );
267
 
268
    // Wait for thread1 to call pause()
269
    CYG_TEST_INFO( "Main: calling sleep(1)");
270
    sleep(1);
271
 
272
    // And again
273
    CYG_TEST_INFO( "Main: calling kill(0, SIGUSR2)");
274
    kill( 0, SIGUSR2 );
275
 
276
    // Set up an alarm for 1 second hence
277
    CYG_TEST_INFO( "Main: calling alarm(1)");
278
    alarm(1);
279
 
280
    // Wait for alarm signal to be delivered to thread1
281
    CYG_TEST_INFO( "Main: calling sleep(2)");
282
    sleep(2);
283
 
284
    // Now join with thread1
285
    CYG_TEST_INFO( "Main: calling pthread_join()");
286
    pthread_join( thread1, &retval );
287
 
288
    CYG_TEST_CHECK( sigusr2_called == 2, "SIGUSR2 signal handler not called twice" );
289
 
290
    CYG_TEST_CHECK( sigalrm_called == 1, "SIGALRM signal handler not called" );
291
 
292
    // check retval
293
 
294
    if( (long)retval == 0x12345678+SIGUSR1 )
295
        CYG_TEST_PASS_FINISH( "signal1" );
296
    else
297
        CYG_TEST_FAIL_FINISH( "signal1" );
298
}
299
 
300
#endif
301
 
302
//--------------------------------------------------------------------------
303
// end of signal1.c

powered by: WebSVN 2.1.0

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