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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [tests/] [pselect.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      pselect.c
4
//
5
//      Test pselect implementation
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:                2002-11-08
45
// Purpose:             Test pselect implementation
46
// Description:         
47
//                      
48
//                      
49
//                      
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
#include <pkgconf/system.h>
56
#include <pkgconf/isoinfra.h>
57
#ifdef CYGPKG_POSIX
58
#include <pkgconf/posix.h>
59
#endif
60
 
61
#ifndef CYGINT_ISO_PTHREAD_IMPL
62
# define NA_MSG "POSIX threads needed to run test"
63
#elif !defined(CYGPKG_NET)
64
# define NA_MSG "NET package needed to run test"
65
#elif !defined(CYGPKG_POSIX_SIGNALS)
66
# define NA_MSG "POSIX signals package needed to run test"
67
#endif
68
 
69
#include <cyg/infra/testcase.h>
70
 
71
#ifndef NA_MSG
72
 
73
#include <pkgconf/hal.h>
74
#include <pkgconf/kernel.h>
75
#include <pkgconf/io_fileio.h>
76
 
77
#define __ECOS 1                        // dont like this at all
78
 
79
#include <cyg/kernel/ktypes.h>         // base kernel types
80
#include <cyg/infra/cyg_trac.h>        // tracing macros
81
#include <cyg/infra/cyg_ass.h>         // assertion macros
82
 
83
#include <unistd.h>
84
#include <fcntl.h>
85
#include <sys/stat.h>
86
#include <errno.h>
87
#include <string.h>
88
 
89
#include <network.h>
90
#include <arpa/inet.h>
91
 
92
#include <pthread.h>
93
#include <signal.h>
94
 
95
#include <sys/select.h>
96
 
97
 
98
#include <cyg/infra/diag.h>            // HAL polled output
99
 
100
#define NUM_TEST_SIGNALS 1500          // So test completes in ~30 seconds
101
 
102
//--------------------------------------------------------------------------
103
 
104
#define SHOW_RESULT( _fn, _res ) \
105
diag_printf("INFO: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
106
 
107
//--------------------------------------------------------------------------
108
// Thread stacks
109
 
110
char thread1_stack[PTHREAD_STACK_MIN*2];
111
char thread2_stack[PTHREAD_STACK_MIN*2];
112
 
113
//--------------------------------------------------------------------------
114
// Local variables
115
 
116
// Thread IDs
117
pthread_t thread1;
118
pthread_t thread2;
119
 
120
struct sockaddr_in sa;
121
 
122
volatile int sigusr1_calls = 0;
123
volatile int sigusr1_sent = 0;
124
volatile int pselect_wakeups = 0;
125
volatile int pselect_eintr = 0;
126
 
127
volatile cyg_bool running = true;
128
 
129
//--------------------------------------------------------------------------
130
 
131
void show_fdsets( char *s, int nfd, fd_set *rd, fd_set *wr, fd_set *ex )
132
{
133
    int i;
134
    diag_printf("INFO:<%s nfd %d ",s,nfd);
135
 
136
    if( rd )
137
    {
138
        diag_printf("rd: [");
139
        for( i = 0; i < nfd ; i++ )
140
            if( FD_ISSET( i, rd ) ) diag_printf("%d ",i);
141
        diag_printf("] ");
142
    }
143
    if( wr )
144
    {
145
        diag_printf("wr: [");
146
        for( i = 0; i < nfd ; i++ )
147
            if( FD_ISSET( i, wr ) ) diag_printf("%d ",i);
148
        diag_printf("] ");
149
    }
150
    if( ex )
151
    {
152
        diag_printf("ex: [");
153
        for( i = 0; i < nfd ; i++ )
154
            if( FD_ISSET( i, ex ) ) diag_printf("%d ",i);
155
        diag_printf("] ");
156
    }
157
 
158
    diag_printf(">\n");
159
}
160
 
161
//--------------------------------------------------------------------------
162
 
163
void sigusr1( int sig, siginfo_t *info, void *context )
164
{
165
    CYG_TEST_CHECK( pthread_self() == thread1, "Sigusr1: not called by thread 1\n");
166
 
167
    sigusr1_calls++;
168
}
169
 
170
//--------------------------------------------------------------------------
171
// Selecting thread
172
 
173
// This thread just opens up a socket ready to accept a connection and
174
// then calls pselect() to wait for it. The timeout is set to 0 so we
175
// actually just poll.
176
 
177
void *pthread_entry1( void *arg)
178
{
179
    int fd = 0;
180
    int err;
181
    fd_set rd, wr;
182
    sigset_t mask, oldmask;
183
    struct sigaction sigact;
184
    struct timespec ts;
185
 
186
    CYG_TEST_INFO( "Thread 1 running" );
187
 
188
    FD_ZERO( &rd );
189
    FD_ZERO( &wr );
190
 
191
    sigfillset( &mask );
192
    pthread_sigmask( SIG_SETMASK, &mask, &oldmask );
193
 
194
    sigdelset( &mask, SIGUSR1 );
195
 
196
    sigact.sa_mask = mask;
197
    sigact.sa_flags = SA_SIGINFO;
198
    sigact.sa_sigaction = sigusr1;
199
 
200
    err = sigaction( SIGUSR1, &sigact, NULL );
201
    if( err < 0 ) SHOW_RESULT( sigact, err );
202
 
203
    CYG_TEST_INFO( "Thread1: calling socket()");
204
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
205
    if( fd < 0 ) SHOW_RESULT( socket, fd );
206
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
207
 
208
    CYG_TEST_INFO( "Thread1: calling bind()");
209
    err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
210
    if( err < 0 ) SHOW_RESULT( bind, err );
211
    CYG_TEST_CHECK( err == 0, "bind() returned error");
212
 
213
    CYG_TEST_INFO( "Thread1: calling listen()");
214
    err = listen( fd, 3);
215
    if( err < 0 ) SHOW_RESULT( listen, err );
216
    CYG_TEST_CHECK( err == 0, "listen() returned error");
217
 
218
    FD_SET( fd, &rd );
219
 
220
    ts.tv_sec = 0;
221
    ts.tv_nsec = 0;
222
 
223
 
224
    while( running )
225
    {
226
        fd_set rd_res = rd;
227
        fd_set wr_res = wr;
228
 
229
//        ts.tv_nsec = 1000000 * (pselect_wakeups % 10);
230
 
231
        err = pselect( 8, &rd_res, &wr_res, NULL, &ts, &mask );
232
        if( err < 0 )
233
        {
234
            if( errno == EINTR ) pselect_eintr++;
235
            else SHOW_RESULT( pselect, err );
236
        }
237
        if( err > 0 ) show_fdsets( "Thread1 result: ", 8, &rd_res, &wr_res, NULL );
238
        pselect_wakeups++;
239
 
240
    }
241
 
242
    // If we were interrupted at just the wrong point above we may still
243
    // have a SIGUSR1 signal pending that we didn't handle, and so won't
244
    // have accounted for. So let's look...
245
    CYG_TEST_CHECK( 0 == sigpending( &mask ), "sigpending() returned error");
246
    if (1 == sigismember(&mask, SIGUSR1) )
247
        pselect_eintr++;
248
 
249
    pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
250
 
251
    pthread_exit(arg);
252
}
253
 
254
//--------------------------------------------------------------------------
255
 
256
void *pthread_entry2( void *arg)
257
{
258
    struct timespec zzz;
259
    int err;
260
 
261
    zzz.tv_sec = 0;
262
    zzz.tv_nsec = 10*1000000;
263
 
264
    CYG_TEST_INFO( "Thread 2: running" );
265
 
266
    CYG_TEST_INFO( "Thread 2: sleeping" );
267
    nanosleep( &zzz, NULL );
268
    nanosleep( &zzz, NULL );
269
    nanosleep( &zzz, NULL );
270
 
271
    while( sigusr1_sent < NUM_TEST_SIGNALS )
272
    {
273
        nanosleep( &zzz, NULL );
274
 
275
        err = pthread_kill( thread1, SIGUSR1 );
276
        if( err != 0 )
277
            diag_printf("INFO: pthread_kill() returned %d %s\n", err, strerror(err));
278
 
279
        sigusr1_sent++;
280
 
281
        if( (sigusr1_sent % 500) == 0 )
282
            diag_printf("INFO: <Thread 2: %d signals sent>\n",sigusr1_sent);
283
    }
284
 
285
    running = false;
286
 
287
    CYG_TEST_INFO( "Thread 2: exit" );
288
    pthread_exit( arg );
289
}
290
 
291
//==========================================================================
292
// main
293
 
294
int main( int argc, char **argv )
295
{
296
    void *retval;
297
    pthread_attr_t attr;
298
    struct sched_param schedparam;
299
 
300
    CYG_TEST_INIT();
301
 
302
    sa.sin_family = AF_INET;
303
    sa.sin_len = sizeof(sa);
304
    inet_aton("127.0.0.1", &sa.sin_addr);
305
    sa.sin_port = htons(1234);
306
    init_all_network_interfaces();
307
 
308
    // Create test threads
309
 
310
    {
311
        pthread_attr_init( &attr );
312
 
313
        schedparam.sched_priority = 5;
314
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
315
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
316
        pthread_attr_setschedparam( &attr, &schedparam );
317
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
318
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
319
 
320
        pthread_create( &thread1,
321
                        &attr,
322
                        pthread_entry1,
323
                        (void *)0x12345671);
324
    }
325
 
326
    {
327
        pthread_attr_init( &attr );
328
 
329
        schedparam.sched_priority = 10;
330
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
331
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
332
        pthread_attr_setschedparam( &attr, &schedparam );
333
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
334
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
335
 
336
        pthread_create( &thread2,
337
                        &attr,
338
                        pthread_entry2,
339
                        (void *)0x12345672);
340
    }
341
 
342
    // Now join with thread1
343
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
344
    pthread_join( thread1, &retval );
345
 
346
    // And thread 2
347
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
348
    pthread_join( thread2, &retval );
349
 
350
    diag_printf("INFO: pselect returns: %d\n", pselect_wakeups );
351
    diag_printf("INFO: pselect EINTR returns: %d\n", pselect_eintr );
352
    diag_printf("INFO: SIGUSR1 sent: %d\n", sigusr1_sent );
353
    diag_printf("INFO: SIGUSR1 delivered: %d\n", sigusr1_calls );
354
 
355
    CYG_TEST_CHECK( sigusr1_sent == sigusr1_calls, "SIGUSR1 calls != delivered");
356
    CYG_TEST_CHECK( sigusr1_sent == pselect_eintr, "SIGUSR1 calls != pselect EINTR wakeups");
357
 
358
    CYG_TEST_PASS_FINISH("pselect");
359
}
360
 
361
#else
362
 
363
//==========================================================================
364
// main
365
 
366
void cyg_start(void)
367
{
368
    CYG_TEST_INIT();
369
 
370
    CYG_TEST_NA(NA_MSG);
371
}
372
 
373
#endif
374
 

powered by: WebSVN 2.1.0

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