1 //==========================================================================
5 // POSIX pthread test 2 - cancellation
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.
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.
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
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.
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.
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.
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####
44 // Contributors: nickg, jlarmour
46 // Description: Tests POSIX cancellation.
48 //####DESCRIPTIONEND####
49 //==========================================================================
51 #include <cyg/infra/testcase.h>
52 #include <pkgconf/posix.h>
54 #ifndef CYGPKG_POSIX_PTHREAD
55 #define NA_MSG "POSIX threads not enabled"
67 #include <sys/types.h>
69 #include <unistd.h> // sleep()
71 //--------------------------------------------------------------------------
76 char thread_stack[NTHREADS][PTHREAD_STACK_MIN*2];
78 pthread_t thread[NTHREADS];
80 void *pthread_entry1( void *arg);
81 void *pthread_entry2( void *arg);
82 void *pthread_entry3( void *arg);
84 void *(*pthread_entry[NTHREADS])(void *) =
91 //--------------------------------------------------------------------------
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];
98 //--------------------------------------------------------------------------
100 void cancel_handler1( void * arg )
102 CYG_TEST_INFO( "cancel_handler1 called" );
104 CYG_TEST_CHECK( (long)arg == 0x12340000, "cancel_handler1: bad arg value");
106 cancel_handler1_called = true;
109 //--------------------------------------------------------------------------
111 void cancel_handler2( void * arg )
113 CYG_TEST_INFO( "cancel_handler2 called" );
115 CYG_TEST_CHECK( (long)arg == 0xFFFF1111, "cancel_handler2: bad arg value");
117 cancel_handler2_called = true;
120 //--------------------------------------------------------------------------
122 void cancel_handler3( void * arg )
124 CYG_TEST_INFO( "cancel_handler3 called" );
126 CYG_TEST_CHECK( (long)arg == 0x12340001, "cancel_handler3: bad arg value");
128 cancel_handler3_called = true;
131 //--------------------------------------------------------------------------
136 pthread_cleanup_push( cancel_handler2, (void *)0xFFFF1111 );
141 pthread_testcancel();
144 pthread_cleanup_pop( 0 );
147 //--------------------------------------------------------------------------
149 void *pthread_entry1( void *arg)
153 CYG_TEST_INFO( "pthread_entry1 entered");
155 pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
157 pthread_cleanup_push( cancel_handler1, arg );
159 thread_ready[0] = true;
163 pthread_cleanup_pop( 0 );
165 pthread_exit( (void *)retval );
168 //--------------------------------------------------------------------------
170 void *pthread_entry2( void *arg)
174 CYG_TEST_INFO( "pthread_entry2 entered");
176 pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
178 pthread_cleanup_push( cancel_handler3, arg );
180 thread_ready[1] = true;
182 for(;;) sched_yield();
184 pthread_cleanup_pop( 0 );
186 pthread_exit( (void *)retval );
189 //--------------------------------------------------------------------------
191 void *pthread_entry3( void *arg)
195 CYG_TEST_INFO( "pthread_entry3 entered");
197 pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
199 thread_ready[2] = true;
201 // stop in a cancellation point
204 pthread_exit( (void *)retval );
207 //--------------------------------------------------------------------------
209 int main(int argc, char **argv)
213 void *retval[NTHREADS];
217 // Create test threads
218 for( i = 0; i < NTHREADS; i++ )
221 pthread_attr_init( &attr );
223 pthread_attr_setstackaddr( &attr, (void *)&thread_stack[i][sizeof(thread_stack[i])] );
224 pthread_attr_setstacksize( &attr, sizeof(thread_stack[i]) );
226 ret = pthread_create( &thread[i],
229 (void *)(0x12340000+i));
230 CYG_TEST_CHECK( ret == 0, "pthread_create() returned error");
233 // Let the threads get going
234 for ( i = 0; i < NTHREADS ; i++ ) {
235 while ( thread_ready[i] == false )
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++ )
245 for( i = 0; i < NTHREADS; i++ )
246 pthread_cancel( thread[i] );
248 // Now join with threads
249 for( i = 0; i < NTHREADS; i++ )
250 pthread_join( thread[i], &retval[i] );
254 for( i = 0; i < NTHREADS; i++ )
255 CYG_TEST_CHECK( retval[i] == PTHREAD_CANCELED,
256 "thread didn't exit with PTHREAD_CANCELED" );
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" );
262 CYG_TEST_PASS_FINISH( "pthread3" );
268 //--------------------------------------------------------------------------