comparison packages/compat/posix/current/tests/pthread3.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 6bd9d475ed4b
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // pthread3.cxx
4 //
5 // POSIX pthread test 2 - cancellation
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): nickg
35 // Contributors: nickg
36 // Date: 2000-04-10
37 // Description: Tests POSIX cancellation.
38 //
39 //####DESCRIPTIONEND####
40 //==========================================================================
41
42 #include <sys/types.h>
43 #include <pthread.h>
44
45 #include <cyg/infra/testcase.h>
46
47 //--------------------------------------------------------------------------
48 // Thread info
49
50 #define NTHREADS 2
51
52 char thread_stack[NTHREADS][PTHREAD_STACK_MIN*2];
53
54 pthread_t thread[NTHREADS];
55
56 void *pthread_entry1( void *arg);
57 void *pthread_entry2( void *arg);
58
59 void *(*pthread_entry[NTHREADS])(void *) =
60 {
61 pthread_entry1,
62 pthread_entry2
63 };
64
65 //--------------------------------------------------------------------------
66
67 volatile cyg_bool cancel_handler1_called = false;
68 volatile cyg_bool cancel_handler2_called = false;
69 volatile cyg_bool cancel_handler3_called = false;
70
71 //--------------------------------------------------------------------------
72
73 void cancel_handler1( void * arg )
74 {
75 CYG_TEST_INFO( "cancel_handler1 called" );
76
77 CYG_TEST_CHECK( (long)arg == 0x12340000, "cancel_handler1: bad arg value");
78
79 cancel_handler1_called = true;
80 }
81
82 //--------------------------------------------------------------------------
83
84 void cancel_handler2( void * arg )
85 {
86 CYG_TEST_INFO( "cancel_handler2 called" );
87
88 CYG_TEST_CHECK( (long)arg == 0xFFFF1111, "cancel_handler2: bad arg value");
89
90 cancel_handler2_called = true;
91 }
92
93 //--------------------------------------------------------------------------
94
95 void cancel_handler3( void * arg )
96 {
97 CYG_TEST_INFO( "cancel_handler3 called" );
98
99 CYG_TEST_CHECK( (long)arg == 0x12340001, "cancel_handler3: bad arg value");
100
101 cancel_handler3_called = true;
102 }
103
104 //--------------------------------------------------------------------------
105
106 void function1(void)
107 {
108
109 pthread_cleanup_push( cancel_handler2, (void *)0xFFFF1111 );
110
111 for(;;)
112 {
113 sched_yield();
114 pthread_testcancel();
115 }
116
117 pthread_cleanup_pop( 0 );
118 }
119
120 //--------------------------------------------------------------------------
121
122 void *pthread_entry1( void *arg)
123 {
124 int retval = 1;
125
126 CYG_TEST_INFO( "pthread_entry1 entered");
127
128 pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, NULL );
129
130 pthread_cleanup_push( cancel_handler1, arg );
131
132 function1();
133
134 pthread_cleanup_pop( 0 );
135
136 pthread_exit( (void *)retval );
137 }
138
139 //--------------------------------------------------------------------------
140
141 void *pthread_entry2( void *arg)
142 {
143 int retval = 1;
144
145 CYG_TEST_INFO( "pthread_entry2 entered");
146
147 pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL );
148
149 pthread_cleanup_push( cancel_handler3, arg );
150
151 for(;;) sched_yield();
152
153 pthread_cleanup_pop( 0 );
154
155 pthread_exit( (void *)retval );
156 }
157
158 //--------------------------------------------------------------------------
159
160 int main(int argc, char **argv)
161 {
162 int i;
163 int ret;
164 void *retval[NTHREADS];
165
166 CYG_TEST_INIT();
167
168 // Create test threads
169 for( i = 0; i < NTHREADS; i++ )
170 {
171 pthread_attr_t attr;
172 pthread_attr_init( &attr );
173
174 pthread_attr_setstackaddr( &attr, (void *)&thread_stack[i][sizeof(thread_stack)] );
175 pthread_attr_setstacksize( &attr, sizeof(thread_stack[i]) );
176
177 ret = pthread_create( &thread[i],
178 &attr,
179 pthread_entry[i],
180 (void *)(0x12340000+i));
181 CYG_TEST_CHECK( ret == 0, "pthread_create() returned error");
182 }
183
184 // Let the threads get going
185 sched_yield();
186 sched_yield();
187 sched_yield();
188
189 // Now cancel them
190 for( i = 0; i < NTHREADS; i++ )
191 pthread_cancel( thread[i] );
192
193 // Now join with threads
194 for( i = 0; i < NTHREADS; i++ )
195 pthread_join( thread[i], &retval[i] );
196
197
198 // check retvals
199 for( i = 0; i < NTHREADS; i++ )
200 if( retval[0] != PTHREAD_CANCELED )
201 CYG_TEST_FAIL_FINISH( "pthread3" );
202
203 if( !cancel_handler1_called ) CYG_TEST_FAIL_FINISH( "pthread3" );
204 if( !cancel_handler2_called ) CYG_TEST_FAIL_FINISH( "pthread3" );
205 if( !cancel_handler3_called ) CYG_TEST_FAIL_FINISH( "pthread3" );
206
207
208 CYG_TEST_PASS_FINISH( "pthread3" );
209
210 }
211
212 //--------------------------------------------------------------------------
213 // end of pthread3.c