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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [compat/] [posix/] [v2_0/] [tests/] [pthread3.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
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 Red Hat, 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 version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):     nickg
44
// Contributors:  nickg, jlarmour
45
// Date:          2000-04-10
46
// Description:   Tests POSIX cancellation.
47
//
48
//####DESCRIPTIONEND####
49
//==========================================================================
50
 
51
#include <cyg/infra/testcase.h>
52
#include <pkgconf/posix.h>
53
 
54
#ifndef CYGPKG_POSIX_PTHREAD
55
#define NA_MSG "POSIX threads not enabled"
56
#endif
57
 
58
#ifdef NA_MSG
59
void
60
cyg_start(void)
61
{
62
    CYG_TEST_INIT();
63
    CYG_TEST_NA(NA_MSG);
64
}
65
#else
66
 
67
#include <sys/types.h>
68
#include <pthread.h>
69
#include <unistd.h> // sleep()
70
 
71
//--------------------------------------------------------------------------
72
// Thread info
73
 
74
#define NTHREADS 3
75
 
76
char thread_stack[NTHREADS][PTHREAD_STACK_MIN*2];
77
 
78
pthread_t thread[NTHREADS];
79
 
80
void *pthread_entry1( void *arg);
81
void *pthread_entry2( void *arg);
82
void *pthread_entry3( void *arg);
83
 
84
void *(*pthread_entry[NTHREADS])(void *) =
85
{
86
    pthread_entry1,
87
    pthread_entry2,
88
    pthread_entry3
89
};
90
 
91
//--------------------------------------------------------------------------
92
 
93
volatile cyg_bool cancel_handler1_called = false;
94
volatile cyg_bool cancel_handler2_called = false;
95
volatile cyg_bool cancel_handler3_called = false;
96
volatile cyg_bool thread_ready[NTHREADS];
97
 
98
//--------------------------------------------------------------------------
99
 
100
void cancel_handler1( void * arg )
101
{
102
    CYG_TEST_INFO( "cancel_handler1 called" );
103
 
104
    CYG_TEST_CHECK( (long)arg == 0x12340000, "cancel_handler1: bad arg value");
105
 
106
    cancel_handler1_called = true;
107
}
108
 
109
//--------------------------------------------------------------------------
110
 
111
void cancel_handler2( void * arg )
112
{
113
    CYG_TEST_INFO( "cancel_handler2 called" );
114
 
115
    CYG_TEST_CHECK( (long)arg == 0xFFFF1111, "cancel_handler2: bad arg value");
116
 
117
    cancel_handler2_called = true;
118
}
119
 
120
//--------------------------------------------------------------------------
121
 
122
void cancel_handler3( void * arg )
123
{
124
    CYG_TEST_INFO( "cancel_handler3 called" );
125
 
126
    CYG_TEST_CHECK( (long)arg == 0x12340001, "cancel_handler3: bad arg value");
127
 
128
    cancel_handler3_called = true;
129
}
130
 
131
//--------------------------------------------------------------------------
132
 
133
void function1(void)
134
{
135
 
136
    pthread_cleanup_push( cancel_handler2, (void *)0xFFFF1111 );
137
 
138
    for(;;)
139
    {
140
        sched_yield();
141
        pthread_testcancel();
142
    }
143
 
144
    pthread_cleanup_pop( 0 );
145
}
146
 
147
//--------------------------------------------------------------------------
148
 
149
void *pthread_entry1( void *arg)
150
{
151
    int retval = 1;
152
 
153
    CYG_TEST_INFO( "pthread_entry1 entered");
154
 
155
    pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
156
 
157
    pthread_cleanup_push( cancel_handler1, arg );
158
 
159
    thread_ready[0] = true;
160
 
161
    function1();
162
 
163
    pthread_cleanup_pop( 0 );
164
 
165
    pthread_exit( (void *)retval );
166
}
167
 
168
//--------------------------------------------------------------------------
169
 
170
void *pthread_entry2( void *arg)
171
{
172
    int retval = 1;
173
 
174
    CYG_TEST_INFO( "pthread_entry2 entered");
175
 
176
    pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
177
 
178
    pthread_cleanup_push( cancel_handler3, arg );
179
 
180
    thread_ready[1] = true;
181
 
182
    for(;;) sched_yield();
183
 
184
    pthread_cleanup_pop( 0 );
185
 
186
    pthread_exit( (void *)retval );
187
}
188
 
189
//--------------------------------------------------------------------------
190
 
191
void *pthread_entry3( void *arg)
192
{
193
    int retval = 1;
194
 
195
    CYG_TEST_INFO( "pthread_entry3 entered");
196
 
197
    pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
198
 
199
    thread_ready[2] = true;
200
 
201
    // stop in a cancellation point
202
    sleep( 99999 );
203
 
204
    pthread_exit( (void *)retval );
205
}
206
 
207
//--------------------------------------------------------------------------
208
 
209
int main(int argc, char **argv)
210
{
211
    int i, j;
212
    int ret;
213
    void *retval[NTHREADS];
214
 
215
    CYG_TEST_INIT();
216
 
217
    // Create test threads
218
    for( i = 0; i < NTHREADS; i++ )
219
    {
220
        pthread_attr_t attr;
221
        pthread_attr_init( &attr );
222
 
223
        pthread_attr_setstackaddr( &attr, (void *)&thread_stack[i][sizeof(thread_stack[i])] );
224
        pthread_attr_setstacksize( &attr, sizeof(thread_stack[i]) );
225
 
226
        ret = pthread_create( &thread[i],
227
                              &attr,
228
                              pthread_entry[i],
229
                              (void *)(0x12340000+i));
230
        CYG_TEST_CHECK( ret == 0, "pthread_create() returned error");
231
    }
232
 
233
    // Let the threads get going    
234
    for ( i = 0; i < NTHREADS ; i++ ) {
235
        while ( thread_ready[i] == false )
236
            sched_yield();
237
    }
238
 
239
    // Now wait a bit to be sure that the other threads have reached
240
    // their cancellation points.
241
    for ( j = 0; j < 20 ; j++ )
242
        sched_yield();
243
 
244
    // Now cancel them
245
    for( i = 0; i < NTHREADS; i++ )
246
        pthread_cancel( thread[i] );
247
 
248
    // Now join with threads
249
    for( i = 0; i < NTHREADS; i++ )
250
        pthread_join( thread[i], &retval[i] );
251
 
252
 
253
    // check retvals
254
    for( i = 0; i < NTHREADS; i++ )
255
        CYG_TEST_CHECK( retval[i] == PTHREAD_CANCELED,
256
                        "thread didn't exit with PTHREAD_CANCELED" );
257
 
258
    CYG_TEST_CHECK( cancel_handler1_called, "cancel_handler1 not called" );
259
    CYG_TEST_CHECK( cancel_handler2_called, "cancel_handler2 not called" );
260
    CYG_TEST_CHECK( cancel_handler3_called, "cancel_handler3 not called" );
261
 
262
    CYG_TEST_PASS_FINISH( "pthread3" );
263
 
264
}
265
 
266
#endif
267
 
268
//--------------------------------------------------------------------------
269
// end of pthread3.c

powered by: WebSVN 2.1.0

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