1 | /* |
---|
2 | * COPYRIGHT (c) 1989-1998. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * Copyright assigned to U.S. Government, 1994. |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.OARcorp.com/rtems/license.html. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <sched.h> |
---|
16 | |
---|
17 | |
---|
18 | void *POSIX_Init( |
---|
19 | void *argument |
---|
20 | ) |
---|
21 | { |
---|
22 | int status; |
---|
23 | pthread_condattr_t attr; |
---|
24 | pthread_condattr_t attr_error; |
---|
25 | int pshared; |
---|
26 | pthread_cond_t cond; |
---|
27 | struct timespec timeout; |
---|
28 | |
---|
29 | puts( "\n\n*** POSIX TEST 10 ***" ); |
---|
30 | |
---|
31 | puts( "Init: pthread_condattr_init" ); |
---|
32 | status = pthread_condattr_init( &attr ); |
---|
33 | assert( !status ); |
---|
34 | |
---|
35 | puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" ); |
---|
36 | status = pthread_condattr_init( NULL ); |
---|
37 | if ( status != EINVAL ) |
---|
38 | printf( "status = %d\n", status ); |
---|
39 | assert( status == EINVAL ); |
---|
40 | |
---|
41 | puts( "Init: pthread_condattr_destroy" ); |
---|
42 | status = pthread_condattr_destroy( &attr ); |
---|
43 | assert( !status ); |
---|
44 | |
---|
45 | puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" ); |
---|
46 | status = pthread_condattr_destroy( NULL ); |
---|
47 | if ( status != EINVAL ) |
---|
48 | printf( "status = %d\n", status ); |
---|
49 | assert( status == EINVAL ); |
---|
50 | |
---|
51 | puts( "Init: pthread_condattr_init" ); |
---|
52 | status = pthread_condattr_init( &attr ); |
---|
53 | assert( !status ); |
---|
54 | |
---|
55 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" ); |
---|
56 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
57 | assert( !status ); |
---|
58 | |
---|
59 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" ); |
---|
60 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE ); |
---|
61 | assert( !status ); |
---|
62 | |
---|
63 | status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
64 | if ( status != EINVAL ) |
---|
65 | printf( "status = %d\n", status ); |
---|
66 | assert( status == EINVAL ); |
---|
67 | puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" ); |
---|
68 | |
---|
69 | status = pthread_condattr_setpshared( &attr, 0xFFFFFF ); |
---|
70 | if ( status != EINVAL ) |
---|
71 | printf( "status = %d\n", status ); |
---|
72 | assert( status == EINVAL ); |
---|
73 | puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" ); |
---|
74 | |
---|
75 | status = pthread_condattr_getpshared( &attr, &pshared ); |
---|
76 | assert( !status ); |
---|
77 | printf( "Init: pthread_condattr_getpshared - %d\n", pshared ); |
---|
78 | |
---|
79 | status = pthread_condattr_getpshared( NULL, &pshared ); |
---|
80 | if ( status != EINVAL ) |
---|
81 | printf( "status = %d\n", status ); |
---|
82 | assert( status == EINVAL ); |
---|
83 | puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" ); |
---|
84 | |
---|
85 | puts( "Init: pthread_cond_init - NULL attr" ); |
---|
86 | status = pthread_cond_init( &cond, NULL ); |
---|
87 | assert( !status ); |
---|
88 | |
---|
89 | /* error for attribute not initialized */ |
---|
90 | |
---|
91 | attr_error.is_initialized = FALSE; |
---|
92 | status = pthread_cond_init( &cond, &attr_error ); |
---|
93 | if ( status != EINVAL ) |
---|
94 | printf( "status = %d\n", status ); |
---|
95 | assert( status == EINVAL ); |
---|
96 | puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" ); |
---|
97 | |
---|
98 | status = pthread_cond_init( &cond, NULL ); |
---|
99 | if ( status != ENOMEM ) |
---|
100 | printf( "status = %d\n", status ); |
---|
101 | assert( status == ENOMEM ); |
---|
102 | puts( "Init: pthread_cond_init - ENOMEM (too many conds)" ); |
---|
103 | |
---|
104 | puts( "Init: pthread_cond_destroy" ); |
---|
105 | status = pthread_cond_destroy( &cond ); |
---|
106 | assert( !status ); |
---|
107 | |
---|
108 | /* error for bad condition variable passed */ |
---|
109 | |
---|
110 | status = pthread_cond_destroy( NULL ); |
---|
111 | if ( status != EINVAL ) |
---|
112 | printf( "status = %d\n", status ); |
---|
113 | assert( status == EINVAL ); |
---|
114 | puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" ); |
---|
115 | |
---|
116 | /* initiailize the attribute for the rest of the test */ |
---|
117 | |
---|
118 | puts( "Init: pthread_cond_init - attr" ); |
---|
119 | status = pthread_cond_init( &Cond1_id, &attr ); |
---|
120 | assert( !status ); |
---|
121 | |
---|
122 | /* signal task1 with a condition variable */ |
---|
123 | |
---|
124 | empty_line(); |
---|
125 | |
---|
126 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
127 | assert( !status ); |
---|
128 | |
---|
129 | /* switch to task1 to allow it to wait for a condition variable */ |
---|
130 | |
---|
131 | puts( "Init: sleep to switch to Task_1" ); |
---|
132 | sleep( 1 ); |
---|
133 | |
---|
134 | status = pthread_cond_destroy( &Cond1_id ); |
---|
135 | if ( status != EBUSY ) |
---|
136 | printf( "status = %d\n", status ); |
---|
137 | assert( status == EBUSY ); |
---|
138 | puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" ); |
---|
139 | |
---|
140 | puts( "Init: pthread_cond_signal" ); |
---|
141 | status = pthread_cond_signal( &Cond1_id ); |
---|
142 | assert( !status ); |
---|
143 | |
---|
144 | empty_line(); |
---|
145 | |
---|
146 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
147 | assert( !status ); |
---|
148 | |
---|
149 | /* switch to task1 and task2 to allow them to wait for broadcast signal */ |
---|
150 | |
---|
151 | puts( "Init: sleep - switch to Task_1 and Task_2" ); |
---|
152 | sleep( 1 ); |
---|
153 | |
---|
154 | /* broadcast a condition variable to task1 and task2 */ |
---|
155 | |
---|
156 | puts( "Init: pthread_cond_broadcast" ); |
---|
157 | status = pthread_cond_broadcast( &Cond1_id ); |
---|
158 | assert( !status ); |
---|
159 | |
---|
160 | puts( "Init: sleep - switch to Task_1" ); |
---|
161 | sleep( 0 ); |
---|
162 | |
---|
163 | /* timedwait case - timeout */ |
---|
164 | |
---|
165 | status = pthread_mutex_lock( &Mutex_id ); |
---|
166 | assert( !status ); |
---|
167 | |
---|
168 | /* set timeout to 3 seconds */ |
---|
169 | |
---|
170 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
171 | assert( !status ); |
---|
172 | timeout.tv_sec += 3; |
---|
173 | timeout.tv_nsec = 0; |
---|
174 | |
---|
175 | puts( "Init: pthread_cond_timedwait for 3 seconds" ); |
---|
176 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
177 | if ( status != ETIMEDOUT ) |
---|
178 | printf( "status = %d\n", status ); |
---|
179 | assert( status == ETIMEDOUT ); |
---|
180 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" ); |
---|
181 | |
---|
182 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
183 | assert( !status ); |
---|
184 | |
---|
185 | /* remaining error messages */ |
---|
186 | |
---|
187 | empty_line(); |
---|
188 | |
---|
189 | /* errors for bad variable passed */ |
---|
190 | |
---|
191 | status = pthread_cond_signal( NULL ); |
---|
192 | if ( status != EINVAL ) |
---|
193 | printf( "status = %d\n", status ); |
---|
194 | assert( status == EINVAL ); |
---|
195 | puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" ); |
---|
196 | |
---|
197 | status = pthread_cond_broadcast( NULL ); |
---|
198 | if ( status != EINVAL ) |
---|
199 | printf( "status = %d\n", status ); |
---|
200 | assert( status == EINVAL ); |
---|
201 | puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" ); |
---|
202 | |
---|
203 | /* acquire mutex so errors will occur */ |
---|
204 | |
---|
205 | status = pthread_mutex_lock( &Mutex_id ); |
---|
206 | assert( !status ); |
---|
207 | |
---|
208 | status = pthread_cond_wait( NULL, &Mutex_id ); |
---|
209 | if ( status != EINVAL ) |
---|
210 | printf( "status = %d\n", status ); |
---|
211 | assert( status == EINVAL ); |
---|
212 | puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" ); |
---|
213 | |
---|
214 | status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout ); |
---|
215 | if ( status != EINVAL ) |
---|
216 | printf( "status = %d\n", status ); |
---|
217 | assert( status == EINVAL ); |
---|
218 | puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" ); |
---|
219 | |
---|
220 | status = pthread_cond_wait( &Cond1_id, NULL ); |
---|
221 | if ( status != EINVAL ) |
---|
222 | printf( "status = %d\n", status ); |
---|
223 | assert( status == EINVAL ); |
---|
224 | puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" ); |
---|
225 | |
---|
226 | status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout ); |
---|
227 | if ( status != EINVAL ) |
---|
228 | printf( "status = %d\n", status ); |
---|
229 | assert( status == EINVAL ); |
---|
230 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" ); |
---|
231 | |
---|
232 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL ); |
---|
233 | if ( status != EINVAL ) |
---|
234 | printf( "status = %d\n", status ); |
---|
235 | assert( status == EINVAL ); |
---|
236 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" ); |
---|
237 | |
---|
238 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
239 | assert( !status ); |
---|
240 | timeout.tv_sec -= 1; |
---|
241 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
242 | if ( status != ETIMEDOUT ) |
---|
243 | printf( "status = %d\n", status ); |
---|
244 | assert( status == ETIMEDOUT ); |
---|
245 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_sec < current time)" ); |
---|
246 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
247 | assert( !status ); |
---|
248 | |
---|
249 | status = pthread_mutex_lock( &Mutex_id ); |
---|
250 | assert( !status ); |
---|
251 | status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
252 | assert( !status ); |
---|
253 | timeout.tv_nsec -= 1; |
---|
254 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
255 | if ( status != ETIMEDOUT ) |
---|
256 | printf( "status = %d\n", status ); |
---|
257 | assert( status == ETIMEDOUT ); |
---|
258 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT (abstime->tv_nsec < current time)" ); |
---|
259 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
260 | assert( !status ); |
---|
261 | |
---|
262 | /* wait and timedwait without mutex */ |
---|
263 | |
---|
264 | /* XXX - this case is commented out in the code pending review |
---|
265 | * |
---|
266 | * status = pthread_cond_wait( &Cond1_id, &Mutex_id ); |
---|
267 | * if ( status != EINVAL ) |
---|
268 | * printf( "status = %d\n", status ); |
---|
269 | * assert( status == EINVAL ); |
---|
270 | */ |
---|
271 | puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" ); |
---|
272 | |
---|
273 | /* XXX - this case is commented out in the code pending review |
---|
274 | * |
---|
275 | * status = clock_gettime( CLOCK_REALTIME, &timeout ); |
---|
276 | * assert( !status ); |
---|
277 | * timeout.tv_sec += 1; |
---|
278 | * status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
279 | * if ( status != EINVAL ) |
---|
280 | * printf( "status = %d\n", status ); |
---|
281 | * assert( status == EINVAL ); |
---|
282 | */ |
---|
283 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)"); |
---|
284 | |
---|
285 | empty_line(); |
---|
286 | |
---|
287 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
288 | assert( !status ); |
---|
289 | |
---|
290 | /* switch to task3 to allow it to wait for broadcast signal */ |
---|
291 | |
---|
292 | puts( "Init: sleep - switch to Task_3" ); |
---|
293 | sleep( 1 ); |
---|
294 | |
---|
295 | /* destroy the mutex so Task3 can not acguire at the end of Wait_support */ |
---|
296 | |
---|
297 | status = pthread_mutex_destroy( &Mutex_id ); |
---|
298 | assert( !status ); |
---|
299 | |
---|
300 | /* signal a condition variable to task3 */ |
---|
301 | |
---|
302 | puts( "Init: pthread_cond_signal" ); |
---|
303 | status = pthread_cond_signal( &Cond1_id ); |
---|
304 | |
---|
305 | puts( "Init: sleep - switch to Task_3" ); |
---|
306 | sleep( 1 ); |
---|
307 | |
---|
308 | puts( "*** END OF POSIX TEST 10 ***" ); |
---|
309 | exit( 0 ); |
---|
310 | |
---|
311 | return NULL; /* just so the compiler thinks we returned something */ |
---|
312 | } |
---|