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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [tests/] [pselect.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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 Red Hat, Inc.
12
// Copyright (C) 2002 Nick Garnett
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// 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 along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):           nickg
45
// Contributors:        nickg
46
// Date:                2002-11-08
47
// Purpose:             Test pselect implementation
48
// Description:         
49
//                      
50
//                      
51
//                      
52
//
53
//####DESCRIPTIONEND####
54
//
55
//==========================================================================
56
 
57
#include <pkgconf/system.h>
58
#include <pkgconf/isoinfra.h>
59
 
60
#ifndef CYGINT_ISO_PTHREAD_IMPL
61
# define NA_MSG "POSIX threads needed to run test"
62
#elif !defined CYGPKG_NET
63
# define NA_MSG "NET package needed to run test"
64
#endif
65
 
66
#include <cyg/infra/testcase.h>
67
 
68
#ifndef NA_MSG
69
 
70
#include <pkgconf/hal.h>
71
#include <pkgconf/kernel.h>
72
#include <pkgconf/io_fileio.h>
73
 
74
#define __ECOS 1                        // dont like this at all
75
 
76
#include <cyg/kernel/ktypes.h>         // base kernel types
77
#include <cyg/infra/cyg_trac.h>        // tracing macros
78
#include <cyg/infra/cyg_ass.h>         // assertion macros
79
 
80
#include <unistd.h>
81
#include <fcntl.h>
82
#include <sys/stat.h>
83
#include <errno.h>
84
#include <string.h>
85
 
86
#include <network.h>
87
#include <arpa/inet.h>
88
 
89
#include <pthread.h>
90
#include <signal.h>
91
 
92
#include <sys/select.h>
93
 
94
 
95
#include <cyg/infra/diag.h>            // HAL polled output
96
 
97
//--------------------------------------------------------------------------
98
 
99
#define SHOW_RESULT( _fn, _res ) \
100
diag_printf("INFO: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
101
 
102
//--------------------------------------------------------------------------
103
// Thread stacks
104
 
105
char thread1_stack[PTHREAD_STACK_MIN*2];
106
char thread2_stack[PTHREAD_STACK_MIN*2];
107
 
108
//--------------------------------------------------------------------------
109
// Local variables
110
 
111
// Thread IDs
112
pthread_t thread1;
113
pthread_t thread2;
114
 
115
struct sockaddr_in sa;
116
 
117
volatile int sigusr1_calls = 0;
118
volatile int sigusr1_sent = 0;
119
volatile int pselect_wakeups = 0;
120
volatile int pselect_eintr = 0;
121
 
122
volatile cyg_bool running = true;
123
 
124
//--------------------------------------------------------------------------
125
 
126
void show_fdsets( char *s, int nfd, fd_set *rd, fd_set *wr, fd_set *ex )
127
{
128
    int i;
129
    diag_printf("INFO:<%s nfd %d ",s,nfd);
130
 
131
    if( rd )
132
    {
133
        diag_printf("rd: [");
134
        for( i = 0; i < nfd ; i++ )
135
            if( FD_ISSET( i, rd ) ) diag_printf("%d ",i);
136
        diag_printf("] ");
137
    }
138
    if( wr )
139
    {
140
        diag_printf("wr: [");
141
        for( i = 0; i < nfd ; i++ )
142
            if( FD_ISSET( i, wr ) ) diag_printf("%d ",i);
143
        diag_printf("] ");
144
    }
145
    if( ex )
146
    {
147
        diag_printf("ex: [");
148
        for( i = 0; i < nfd ; i++ )
149
            if( FD_ISSET( i, ex ) ) diag_printf("%d ",i);
150
        diag_printf("] ");
151
    }
152
 
153
    diag_printf(">\n");
154
}
155
 
156
//--------------------------------------------------------------------------
157
 
158
void sigusr1( int sig, siginfo_t *info, void *context )
159
{
160
    CYG_TEST_CHECK( pthread_self() == thread1, "Sigusr1: not called by thread 1\n");
161
 
162
    sigusr1_calls++;
163
}
164
 
165
//--------------------------------------------------------------------------
166
// Selecting thread
167
 
168
// This thread just opens up a socket ready to accept a connection and
169
// then calls pselect() to wait for it. The timeout is set to 0 so we
170
// actually just poll.
171
 
172
void *pthread_entry1( void *arg)
173
{
174
    int fd = 0;
175
    int err;
176
    fd_set rd, wr;
177
    sigset_t mask, oldmask;
178
    struct sigaction sigact;
179
    struct timespec ts;
180
 
181
    CYG_TEST_INFO( "Thread 1 running" );
182
 
183
    FD_ZERO( &rd );
184
    FD_ZERO( &wr );
185
 
186
    sigfillset( &mask );
187
    pthread_sigmask( SIG_SETMASK, &mask, &oldmask );
188
 
189
    sigdelset( &mask, SIGUSR1 );
190
 
191
    sigact.sa_mask = mask;
192
    sigact.sa_flags = SA_SIGINFO;
193
    sigact.sa_sigaction = sigusr1;
194
 
195
    err = sigaction( SIGUSR1, &sigact, NULL );
196
    if( err < 0 ) SHOW_RESULT( sigact, err );
197
 
198
    CYG_TEST_INFO( "Thread1: calling socket()");
199
    fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
200
    if( fd < 0 ) SHOW_RESULT( socket, fd );
201
    CYG_TEST_CHECK( fd >= 0, "socket() returned error");
202
 
203
    CYG_TEST_INFO( "Thread1: calling bind()");
204
    err = bind( fd, (struct sockaddr *)&sa, sizeof(sa));
205
    if( err < 0 ) SHOW_RESULT( bind, err );
206
    CYG_TEST_CHECK( err == 0, "bind() returned error");
207
 
208
    CYG_TEST_INFO( "Thread1: calling listen()");
209
    err = listen( fd, 3);
210
    if( err < 0 ) SHOW_RESULT( listen, err );
211
    CYG_TEST_CHECK( err == 0, "listen() returned error");
212
 
213
    FD_SET( fd, &rd );
214
 
215
    ts.tv_sec = 0;
216
    ts.tv_nsec = 0;
217
 
218
 
219
    while( running )
220
    {
221
        fd_set rd_res = rd;
222
        fd_set wr_res = wr;
223
 
224
//        ts.tv_nsec = 1000000 * (pselect_wakeups % 10);
225
 
226
        err = pselect( 8, &rd_res, &wr_res, NULL, &ts, &mask );
227
        if( err < 0 )
228
        {
229
            if( errno == EINTR ) pselect_eintr++;
230
            else SHOW_RESULT( pselect, err );
231
        }
232
        if( err > 0 ) show_fdsets( "Thread1 result: ", 8, &rd_res, &wr_res, NULL );
233
        pselect_wakeups++;
234
 
235
    }
236
 
237
    pthread_sigmask( SIG_SETMASK, &oldmask, NULL );
238
 
239
    pthread_exit(arg);
240
}
241
 
242
//--------------------------------------------------------------------------
243
 
244
void *pthread_entry2( void *arg)
245
{
246
    struct timespec zzz;
247
    int err;
248
 
249
    zzz.tv_sec = 0;
250
    zzz.tv_nsec = 10*1000000;
251
 
252
    CYG_TEST_INFO( "Thread 2: running" );
253
 
254
    CYG_TEST_INFO( "Thread 2: sleeping" );
255
    nanosleep( &zzz, NULL );
256
    nanosleep( &zzz, NULL );
257
    nanosleep( &zzz, NULL );
258
 
259
    while( sigusr1_sent < 20000 )
260
    {
261
        nanosleep( &zzz, NULL );
262
 
263
        err = pthread_kill( thread1, SIGUSR1 );
264
        if( err < 0 ) SHOW_RESULT( pthread_kill, err );
265
 
266
        sigusr1_sent++;
267
 
268
        if( (sigusr1_sent % 500) == 0 )
269
            diag_printf("INFO: <Thread 2: %d signals sent>\n",sigusr1_sent);
270
    }
271
 
272
    running = false;
273
 
274
    CYG_TEST_INFO( "Thread 2: exit" );
275
    pthread_exit( arg );
276
}
277
 
278
//==========================================================================
279
// main
280
 
281
int main( int argc, char **argv )
282
{
283
    void *retval;
284
    pthread_attr_t attr;
285
    struct sched_param schedparam;
286
 
287
    CYG_TEST_INIT();
288
 
289
    sa.sin_family = AF_INET;
290
    sa.sin_len = sizeof(sa);
291
    inet_aton("127.0.0.1", &sa.sin_addr);
292
    sa.sin_port = htons(1234);
293
    init_all_network_interfaces();
294
 
295
    // Create test threads
296
 
297
    {
298
        pthread_attr_init( &attr );
299
 
300
        schedparam.sched_priority = 5;
301
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
302
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
303
        pthread_attr_setschedparam( &attr, &schedparam );
304
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
305
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );
306
 
307
        pthread_create( &thread1,
308
                        &attr,
309
                        pthread_entry1,
310
                        (void *)0x12345671);
311
    }
312
 
313
    {
314
        pthread_attr_init( &attr );
315
 
316
        schedparam.sched_priority = 10;
317
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
318
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
319
        pthread_attr_setschedparam( &attr, &schedparam );
320
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
321
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );
322
 
323
        pthread_create( &thread2,
324
                        &attr,
325
                        pthread_entry2,
326
                        (void *)0x12345672);
327
    }
328
 
329
    // Now join with thread1
330
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
331
    pthread_join( thread1, &retval );
332
 
333
    // And thread 2
334
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
335
    pthread_join( thread2, &retval );
336
 
337
    diag_printf("INFO: pselect returns: %d\n", pselect_wakeups );
338
    diag_printf("INFO: pselect EINTR returns: %d\n", pselect_eintr );
339
    diag_printf("INFO: SIGUSR1 sent: %d\n", sigusr1_sent );
340
    diag_printf("INFO: SIGUSR1 delivered: %d\n", sigusr1_calls );
341
 
342
    CYG_TEST_CHECK( sigusr1_sent == sigusr1_calls, "SIGUSR1 calls != delivered");
343
    CYG_TEST_CHECK( sigusr1_sent == pselect_eintr, "SIGUSR1 calls != pselect EINTR wakeups");
344
 
345
    CYG_TEST_PASS_FINISH("pselect");
346
}
347
 
348
#else
349
 
350
//==========================================================================
351
// main
352
 
353
void cyg_start(void)
354
{
355
    CYG_TEST_INIT();
356
 
357
    CYG_TEST_NA(NA_MSG);
358
}
359
 
360
#endif
361
 

powered by: WebSVN 2.1.0

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