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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        timer1.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
#ifndef CYGPKG_POSIX_SIGNALS
54
#define NA_MSG "No POSIX signals"
55
#elif !defined(CYGPKG_POSIX_TIMERS)
56
#define NA_MSG "No POSIX timers"
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
 
76
//--------------------------------------------------------------------------
77
// Thread stack.
78
 
79
char thread1_stack[PTHREAD_STACK_MIN*2];
80
char thread2_stack[PTHREAD_STACK_MIN*2];
81
 
82
//--------------------------------------------------------------------------
83
// Local variables
84
 
85
// Sync semaphore
86
sem_t sem;
87
 
88
// Thread IDs
89
pthread_t thread1;
90
pthread_t thread2;
91
 
92
timer_t timer1;
93
timer_t timer2;
94
 
95
volatile int sigusr1_called = 0;
96
volatile int sigusr2_called = 0;
97
 
98
//--------------------------------------------------------------------------
99
// Signal handler functions
100
 
101
static void sigusr1( int signo, siginfo_t *info, void *context )
102
{
103
    CYG_TEST_INFO( "sigusr1() handler called" );
104
    CYG_TEST_CHECK( signo == SIGUSR1, "Signal not SIGUSR1");
105
    CYG_TEST_CHECK( signo == info->si_signo, "Bad signal number in siginfo" );
106
    CYG_TEST_CHECK( info->si_code == SI_TIMER, "Siginfo code not SI_TIMER" );
107
    CYG_TEST_CHECK( info->si_value.sival_int == 0xABCDEF01, "Siginfo value wrong");
108
    CYG_TEST_CHECK( pthread_equal(pthread_self(), thread1), "Not called in thread1");
109
 
110
    sigusr1_called++;
111
}
112
 
113
static void sigusr2( int signo, siginfo_t *info, void *context )
114
{
115
    CYG_TEST_INFO( "sigusr2() handler called" );
116
    CYG_TEST_CHECK( signo == SIGUSR2, "Signal not SIGUSR2");
117
    CYG_TEST_CHECK( signo == info->si_signo, "Bad signal number in siginfo" );
118
    CYG_TEST_CHECK( info->si_code == SI_TIMER, "Siginfo code not SI_TIMER" );
119
    CYG_TEST_CHECK( info->si_value.sival_int == 0xABCDEF02, "Siginfo value wrong");
120
    CYG_TEST_CHECK( pthread_equal(pthread_self(), thread2), "Not called in thread2");
121
 
122
    sigusr2_called++;
123
}
124
 
125
//--------------------------------------------------------------------------
126
 
127
void *pthread_entry1( void *arg)
128
{
129
    sigset_t mask;
130
 
131
 
132
    CYG_TEST_INFO( "Thread 1 running" );
133
 
134
    // Make a full set
135
    sigfillset( &mask );
136
 
137
    // remove USR1 signal
138
    sigdelset( &mask, SIGUSR1 );
139
 
140
    // Set signal mask
141
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
142
 
143
    // Get main thread going again
144
    sem_post( &sem );
145
 
146
    while( sigusr1_called < 1 )
147
    {
148
        CYG_TEST_INFO( "Thread1: calling pause()");
149
        pause();
150
    }
151
 
152
    CYG_TEST_INFO( "Thread1: calling pthread_exit()");
153
    pthread_exit( arg );
154
}
155
 
156
//--------------------------------------------------------------------------
157
 
158
void *pthread_entry2( void *arg)
159
{
160
    sigset_t mask;
161
 
162
    CYG_TEST_INFO( "Thread 2 running" );
163
 
164
    // Make a full set
165
    sigfillset( &mask );
166
 
167
    // remove USR2 signal
168
    sigdelset( &mask, SIGUSR2 );
169
 
170
    // Set signal mask
171
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
172
 
173
    // Get main thread going again
174
    sem_post( &sem );
175
 
176
    while( sigusr2_called < 6 )
177
    {
178
        CYG_TEST_INFO( "Thread2: calling pause()");
179
        pause();
180
    }
181
 
182
    CYG_TEST_INFO( "Thread2: calling pthread_exit()");
183
    pthread_exit( arg );
184
}
185
 
186
//--------------------------------------------------------------------------
187
 
188
int main(int argc, char **argv)
189
{
190
    int ret;
191
    sigset_t mask;
192
    pthread_attr_t attr;
193
    void *retval;
194
 
195
    CYG_TEST_INIT();
196
 
197
    // Make a full signal set
198
    sigfillset( &mask );
199
 
200
 
201
    // Install signal handlers
202
    {
203
        struct sigaction sa;
204
 
205
        sa.sa_sigaction = sigusr1;
206
        sa.sa_mask = mask;
207
        sa.sa_flags = SA_SIGINFO;
208
 
209
        ret = sigaction( SIGUSR1, &sa, NULL );
210
 
211
        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
212
    }
213
    {
214
        struct sigaction sa;
215
 
216
        sa.sa_sigaction = sigusr2;
217
        sa.sa_mask = mask;
218
        sa.sa_flags = SA_SIGINFO;
219
 
220
        ret = sigaction( SIGUSR2, &sa, NULL );
221
 
222
        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
223
    }
224
 
225
 
226
    // Create the timers
227
 
228
    {
229
        struct sigevent sev;
230
        struct itimerspec value;
231
 
232
        sev.sigev_notify                = SIGEV_SIGNAL;
233
        sev.sigev_signo                 = SIGUSR1;
234
        sev.sigev_value.sival_int       = 0xABCDEF01;
235
 
236
        value.it_value.tv_sec           = 1;
237
        value.it_value.tv_nsec          = 0;
238
        value.it_interval.tv_sec        = 0;
239
        value.it_interval.tv_nsec       = 0;
240
 
241
        ret = timer_create( CLOCK_REALTIME, &sev, &timer1 );
242
 
243
        CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
244
 
245
        ret = timer_settime( timer1, 0, &value, NULL );
246
 
247
        CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
248
    }
249
 
250
#if 1    
251
    {
252
        struct sigevent sev;
253
        struct itimerspec value;
254
 
255
        sev.sigev_notify                = SIGEV_SIGNAL;
256
        sev.sigev_signo                 = SIGUSR2;
257
        sev.sigev_value.sival_int       = 0xABCDEF02;
258
 
259
        value.it_value.tv_sec           = 0;
260
        value.it_value.tv_nsec          = 500000000;
261
        value.it_interval.tv_sec        = 0;
262
        value.it_interval.tv_nsec       = 250000000;
263
 
264
        ret = timer_create( CLOCK_REALTIME, &sev, &timer2 );
265
 
266
        CYG_TEST_CHECK( ret == 0 , "timer_create returned error");
267
 
268
        ret = timer_settime( timer2, 0, &value, NULL );
269
 
270
        CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
271
    }
272
#endif    
273
 
274
    // Mask all signals
275
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
276
 
277
    sem_init( &sem, 0, 0 );
278
 
279
    // Create test threads
280
 
281
    {
282
        pthread_attr_init( &attr );
283
 
284
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
285
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
286
 
287
        pthread_create( &thread1,
288
                        &attr,
289
                        pthread_entry1,
290
                        (void *)0x12345671);
291
    }
292
 
293
    {
294
        pthread_attr_init( &attr );
295
 
296
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
297
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
298
 
299
        pthread_create( &thread2,
300
                        &attr,
301
                        pthread_entry2,
302
                        (void *)0x12345672);
303
    }
304
 
305
    // Wait for other thread to get started
306
    CYG_TEST_INFO( "Main: calling sem_wait()");
307
    sem_wait( &sem );
308
    CYG_TEST_INFO( "Main: calling sem_wait() again");
309
    sem_wait( &sem );
310
 
311
    // Now join with thread1
312
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
313
    pthread_join( thread1, &retval );
314
 
315
    CYG_TEST_CHECK( retval == (void *)0x12345671, "Thread 1 retval wrong");
316
 
317
    // And thread 2
318
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
319
    pthread_join( thread2, &retval );
320
 
321
    // now delete the timers
322
    CYG_TEST_INFO( "Main: calling timer_delete(timer1)");
323
    ret = timer_delete( timer1 );
324
 
325
    CYG_TEST_CHECK( ret == 0 , "timer_delete(timer1) returned error");
326
 
327
    CYG_TEST_INFO( "Main: calling timer_delete(timer2)");
328
    ret = timer_delete( timer2 );
329
 
330
    CYG_TEST_CHECK( ret == 0 , "timer_delete(timer2) returned error");
331
 
332
 
333
    CYG_TEST_CHECK( retval == (void *)0x12345672, "Thread 2 retval wrong");
334
 
335
    CYG_TEST_CHECK( sigusr1_called == 1, "SIGUSR1 signal handler not called once" );
336
    CYG_TEST_CHECK( sigusr2_called == 6, "SIGUSR2 signal handler not called six times" );
337
 
338
    CYG_TEST_PASS_FINISH( "timer1" );
339
}
340
 
341
#endif
342
 
343
//--------------------------------------------------------------------------
344
// end of timer1.c

powered by: WebSVN 2.1.0

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