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