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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [compat/] [posix/] [current/] [tests/] [pthread3.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
//        pthread3.cxx
4
//
5
//        POSIX pthread test 2 - cancellation
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, jlarmour
44
// Date:          2000-04-10
45
// Description:   Tests POSIX cancellation.
46
//
47
//####DESCRIPTIONEND####
48
//==========================================================================
49
 
50
#include <cyg/infra/testcase.h>
51
#include <pkgconf/posix.h>
52
 
53
#ifndef CYGPKG_POSIX_PTHREAD
54
#define NA_MSG "POSIX threads not enabled"
55
#endif
56
 
57
#ifdef NA_MSG
58
void
59
cyg_start(void)
60
{
61
    CYG_TEST_INIT();
62
    CYG_TEST_NA(NA_MSG);
63
}
64
#else
65
 
66
#include <sys/types.h>
67
#include <pthread.h>
68
#include <unistd.h> // sleep()
69
 
70
//--------------------------------------------------------------------------
71
// Thread info
72
 
73
#define NTHREADS 3
74
 
75
char thread_stack[NTHREADS][PTHREAD_STACK_MIN*2];
76
 
77
pthread_t thread[NTHREADS];
78
 
79
void *pthread_entry1( void *arg);
80
void *pthread_entry2( void *arg);
81
void *pthread_entry3( void *arg);
82
 
83
void *(*pthread_entry[NTHREADS])(void *) =
84
{
85
    pthread_entry1,
86
    pthread_entry2,
87
    pthread_entry3
88
};
89
 
90
//--------------------------------------------------------------------------
91
 
92
volatile cyg_bool cancel_handler1_called = false;
93
volatile cyg_bool cancel_handler2_called = false;
94
volatile cyg_bool cancel_handler3_called = false;
95
volatile cyg_bool thread_ready[NTHREADS];
96
 
97
//--------------------------------------------------------------------------
98
 
99
void cancel_handler1( void * arg )
100
{
101
    CYG_TEST_INFO( "cancel_handler1 called" );
102
 
103
    CYG_TEST_CHECK( (long)arg == 0x12340000, "cancel_handler1: bad arg value");
104
 
105
    cancel_handler1_called = true;
106
}
107
 
108
//--------------------------------------------------------------------------
109
 
110
void cancel_handler2( void * arg )
111
{
112
    CYG_TEST_INFO( "cancel_handler2 called" );
113
 
114
    CYG_TEST_CHECK( (long)arg == 0xFFFF1111, "cancel_handler2: bad arg value");
115
 
116
    cancel_handler2_called = true;
117
}
118
 
119
//--------------------------------------------------------------------------
120
 
121
void cancel_handler3( void * arg )
122
{
123
    CYG_TEST_INFO( "cancel_handler3 called" );
124
 
125
    CYG_TEST_CHECK( (long)arg == 0x12340001, "cancel_handler3: bad arg value");
126
 
127
    cancel_handler3_called = true;
128
}
129
 
130
//--------------------------------------------------------------------------
131
 
132
void function1(void)
133
{
134
 
135
    pthread_cleanup_push( cancel_handler2, (void *)0xFFFF1111 );
136
 
137
    for(;;)
138
    {
139
        sched_yield();
140
        pthread_testcancel();
141
    }
142
 
143
    pthread_cleanup_pop( 0 );
144
}
145
 
146
//--------------------------------------------------------------------------
147
 
148
void *pthread_entry1( void *arg)
149
{
150
    int retval = 1;
151
 
152
    CYG_TEST_INFO( "pthread_entry1 entered");
153
 
154
    pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
155
 
156
    pthread_cleanup_push( cancel_handler1, arg );
157
 
158
    thread_ready[0] = true;
159
 
160
    function1();
161
 
162
    pthread_cleanup_pop( 0 );
163
 
164
    pthread_exit( (void *)retval );
165
}
166
 
167
//--------------------------------------------------------------------------
168
 
169
void *pthread_entry2( void *arg)
170
{
171
    int retval = 1;
172
 
173
    CYG_TEST_INFO( "pthread_entry2 entered");
174
 
175
    pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
176
 
177
    pthread_cleanup_push( cancel_handler3, arg );
178
 
179
    thread_ready[1] = true;
180
 
181
    for(;;) sched_yield();
182
 
183
    pthread_cleanup_pop( 0 );
184
 
185
    pthread_exit( (void *)retval );
186
}
187
 
188
//--------------------------------------------------------------------------
189
 
190
void *pthread_entry3( void *arg)
191
{
192
    int retval = 1;
193
 
194
    CYG_TEST_INFO( "pthread_entry3 entered");
195
 
196
    pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
197
 
198
    thread_ready[2] = true;
199
 
200
    // stop in a cancellation point
201
    sleep( 99999 );
202
 
203
    pthread_exit( (void *)retval );
204
}
205
 
206
//--------------------------------------------------------------------------
207
 
208
int main(int argc, char **argv)
209
{
210
    int i, j;
211
    int ret;
212
    void *retval[NTHREADS];
213
 
214
    CYG_TEST_INIT();
215
 
216
    // Create test threads
217
    for( i = 0; i < NTHREADS; i++ )
218
    {
219
        pthread_attr_t attr;
220
        pthread_attr_init( &attr );
221
 
222
        pthread_attr_setstackaddr( &attr, (void *)&thread_stack[i][sizeof(thread_stack[i])] );
223
        pthread_attr_setstacksize( &attr, sizeof(thread_stack[i]) );
224
 
225
        ret = pthread_create( &thread[i],
226
                              &attr,
227
                              pthread_entry[i],
228
                              (void *)(0x12340000+i));
229
        CYG_TEST_CHECK( ret == 0, "pthread_create() returned error");
230
    }
231
 
232
    // Let the threads get going    
233
    for ( i = 0; i < NTHREADS ; i++ ) {
234
        while ( thread_ready[i] == false )
235
            sched_yield();
236
    }
237
 
238
    // Now wait a bit to be sure that the other threads have reached
239
    // their cancellation points.
240
    for ( j = 0; j < 20 ; j++ )
241
        sched_yield();
242
 
243
    // Now cancel them
244
    for( i = 0; i < NTHREADS; i++ )
245
        pthread_cancel( thread[i] );
246
 
247
    // Now join with threads
248
    for( i = 0; i < NTHREADS; i++ )
249
        pthread_join( thread[i], &retval[i] );
250
 
251
 
252
    // check retvals
253
    for( i = 0; i < NTHREADS; i++ )
254
        CYG_TEST_CHECK( retval[i] == PTHREAD_CANCELED,
255
                        "thread didn't exit with PTHREAD_CANCELED" );
256
 
257
    CYG_TEST_CHECK( cancel_handler1_called, "cancel_handler1 not called" );
258
    CYG_TEST_CHECK( cancel_handler2_called, "cancel_handler2 not called" );
259
    CYG_TEST_CHECK( cancel_handler3_called, "cancel_handler3 not called" );
260
 
261
    CYG_TEST_PASS_FINISH( "pthread3" );
262
 
263
}
264
 
265
#endif
266
 
267
//--------------------------------------------------------------------------
268
// end of pthread3.c

powered by: WebSVN 2.1.0

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