1 | /* |
---|
2 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * All rights assigned to U.S. Government, 1994. |
---|
5 | * |
---|
6 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
7 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
8 | * notice must appear in all copies of this file and its derivatives. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <errno.h> |
---|
16 | |
---|
17 | void Print_mutexattr( |
---|
18 | char *msg, |
---|
19 | pthread_mutexattr_t *attr |
---|
20 | ) |
---|
21 | { |
---|
22 | int status; |
---|
23 | int protocol; |
---|
24 | int prioceiling; |
---|
25 | int pshared; |
---|
26 | |
---|
27 | /* protocol */ |
---|
28 | |
---|
29 | status = pthread_mutexattr_getprotocol( attr, &protocol ); |
---|
30 | assert( !status ); |
---|
31 | |
---|
32 | printf( "%smutex protocol is (%d) -- ", msg, protocol ); |
---|
33 | switch ( protocol ) { |
---|
34 | case PTHREAD_PRIO_NONE: |
---|
35 | puts( "PTHREAD_PRIO_NONE" ); |
---|
36 | break; |
---|
37 | case PTHREAD_PRIO_INHERIT: |
---|
38 | puts( "PTHREAD_PRIO_INHERIT" ); |
---|
39 | break; |
---|
40 | case PTHREAD_PRIO_PROTECT: |
---|
41 | puts( "PTHREAD_PRIO_PROTECT" ); |
---|
42 | break; |
---|
43 | default: |
---|
44 | puts( "UNKNOWN" ); |
---|
45 | assert( 0 ); |
---|
46 | break; |
---|
47 | } |
---|
48 | |
---|
49 | /* priority ceiling */ |
---|
50 | |
---|
51 | status = pthread_mutexattr_getprioceiling( attr, &prioceiling ); |
---|
52 | assert( !status ); |
---|
53 | printf( "%smutex priority ceiling is %d\n", msg, prioceiling ); |
---|
54 | |
---|
55 | /* process shared */ |
---|
56 | |
---|
57 | status = pthread_mutexattr_getpshared( attr, &pshared ); |
---|
58 | assert( !status ); |
---|
59 | printf( "%smutex process shared is (%d) -- ", msg, pshared ); |
---|
60 | switch ( pshared ) { |
---|
61 | case PTHREAD_PROCESS_PRIVATE: |
---|
62 | puts( "PTHREAD_PROCESS_PRIVATE" ); |
---|
63 | break; |
---|
64 | case PTHREAD_PROCESS_SHARED: |
---|
65 | puts( "PTHREAD_PROCESS_SHARED" ); |
---|
66 | break; |
---|
67 | default: |
---|
68 | puts( "UNKNOWN" ); |
---|
69 | assert( 0 ); |
---|
70 | break; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | void *POSIX_Init( |
---|
75 | void *argument |
---|
76 | ) |
---|
77 | { |
---|
78 | int status; |
---|
79 | pthread_mutexattr_t attr; |
---|
80 | pthread_mutexattr_t destroyed_attr; |
---|
81 | struct timespec times; |
---|
82 | struct sched_param param; |
---|
83 | int pshared; |
---|
84 | int policy; |
---|
85 | int ceiling; |
---|
86 | int old_ceiling; |
---|
87 | |
---|
88 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
89 | |
---|
90 | /* set the time of day, and print our buffer in multiple ways */ |
---|
91 | |
---|
92 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
93 | |
---|
94 | /* get id of this thread */ |
---|
95 | |
---|
96 | Init_id = pthread_self(); |
---|
97 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
98 | |
---|
99 | /* tes pthread_mutex_attr_init */ |
---|
100 | |
---|
101 | puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" ); |
---|
102 | status = pthread_mutexattr_init( NULL ); |
---|
103 | assert( status == EINVAL ); |
---|
104 | |
---|
105 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
106 | status = pthread_mutexattr_init( &attr ); |
---|
107 | assert( !status ); |
---|
108 | |
---|
109 | Print_mutexattr( "Init: ", &attr ); |
---|
110 | |
---|
111 | /* create an "uninitialized" attribute structure */ |
---|
112 | |
---|
113 | status = pthread_mutexattr_init( &destroyed_attr ); |
---|
114 | assert( !status ); |
---|
115 | |
---|
116 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
117 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
118 | assert( !status ); |
---|
119 | |
---|
120 | puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" ); |
---|
121 | status = pthread_mutexattr_destroy( NULL ); |
---|
122 | assert( status == EINVAL ); |
---|
123 | |
---|
124 | puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" ); |
---|
125 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
126 | assert( status == EINVAL ); |
---|
127 | |
---|
128 | /* error cases for set and get attributes routines */ |
---|
129 | |
---|
130 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" ); |
---|
131 | status = pthread_mutexattr_getpshared( NULL, &pshared ); |
---|
132 | assert( status == EINVAL ); |
---|
133 | |
---|
134 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" ); |
---|
135 | status = pthread_mutexattr_getpshared( &attr, NULL ); |
---|
136 | assert( status == EINVAL ); |
---|
137 | |
---|
138 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" ); |
---|
139 | status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared ); |
---|
140 | assert( status == EINVAL ); |
---|
141 | |
---|
142 | /* change the attributes structure */ |
---|
143 | |
---|
144 | puts( "Init: Changing mutex attributes" ); |
---|
145 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
146 | assert( !status ); |
---|
147 | |
---|
148 | status = pthread_mutexattr_setprioceiling( &attr, 128 ); |
---|
149 | assert( !status ); |
---|
150 | |
---|
151 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
152 | assert( !status ); |
---|
153 | |
---|
154 | Print_mutexattr( "Init: ", &attr ); |
---|
155 | |
---|
156 | puts( "Init: Resetting mutex attributes" ); |
---|
157 | status = pthread_mutexattr_init( &attr ); |
---|
158 | assert( !status ); |
---|
159 | |
---|
160 | /* create a thread */ |
---|
161 | |
---|
162 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
163 | assert( !status ); |
---|
164 | |
---|
165 | /* now try some basic mutex operations */ |
---|
166 | |
---|
167 | empty_line(); |
---|
168 | |
---|
169 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
170 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
171 | if ( status ) |
---|
172 | printf( "status = %d\n", status ); |
---|
173 | assert( !status ); |
---|
174 | |
---|
175 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
176 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
177 | if ( status ) |
---|
178 | printf( "status = %d\n", status ); |
---|
179 | assert( !status ); |
---|
180 | |
---|
181 | puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" ); |
---|
182 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
183 | if ( status != EDEADLK ) |
---|
184 | printf( "status = %d\n", status ); |
---|
185 | assert( status == EDEADLK ); |
---|
186 | |
---|
187 | puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" ); |
---|
188 | status = pthread_mutex_lock( &Mutex_id ); |
---|
189 | if ( status != EDEADLK ) |
---|
190 | printf( "status = %d\n", status ); |
---|
191 | assert( status == EDEADLK ); |
---|
192 | |
---|
193 | puts( "Init: Sleep 1 second" ); |
---|
194 | |
---|
195 | sleep( 1 ); |
---|
196 | |
---|
197 | /* switch to task 1 */ |
---|
198 | |
---|
199 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
200 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
201 | if ( status ) |
---|
202 | printf( "status = %d\n", status ); |
---|
203 | assert( !status ); |
---|
204 | |
---|
205 | puts( "Init: pthread_mutex_unlock - EPERM (not owner)" ); |
---|
206 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
207 | if ( status != EPERM ) |
---|
208 | printf( "status = %d\n", status ); |
---|
209 | assert( status == EPERM ); |
---|
210 | |
---|
211 | times.tv_sec = 0; |
---|
212 | times.tv_nsec = 500000000; |
---|
213 | puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" ); |
---|
214 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
215 | if ( status != EAGAIN ) |
---|
216 | printf( "status = %d\n", status ); |
---|
217 | assert( status == EAGAIN ); |
---|
218 | |
---|
219 | /* switch to idle */ |
---|
220 | |
---|
221 | puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" ); |
---|
222 | |
---|
223 | /* destroy a mutex */ |
---|
224 | |
---|
225 | empty_line(); |
---|
226 | |
---|
227 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
228 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
229 | if ( status ) |
---|
230 | printf( "status = %d\n", status ); |
---|
231 | assert( !status ); |
---|
232 | |
---|
233 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
234 | status = pthread_mutexattr_destroy( &attr ); |
---|
235 | assert( !status ); |
---|
236 | |
---|
237 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
238 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
239 | assert( !status ); |
---|
240 | |
---|
241 | /* destroy a busy mutex */ |
---|
242 | |
---|
243 | empty_line(); |
---|
244 | |
---|
245 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
246 | status = pthread_mutexattr_init( &attr ); |
---|
247 | assert( !status ); |
---|
248 | |
---|
249 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
250 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
251 | assert( !status ); |
---|
252 | |
---|
253 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
254 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
255 | if ( status ) |
---|
256 | printf( "status = %d\n", status ); |
---|
257 | assert( !status ); |
---|
258 | |
---|
259 | puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" ); |
---|
260 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
261 | if ( status != EBUSY ) |
---|
262 | printf( "status = %d\n", status ); |
---|
263 | assert( status == EBUSY ); |
---|
264 | |
---|
265 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
266 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
267 | assert( !status ); |
---|
268 | |
---|
269 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
270 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
271 | assert( !status ); |
---|
272 | |
---|
273 | /* priority inherit mutex */ |
---|
274 | |
---|
275 | empty_line(); |
---|
276 | |
---|
277 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
278 | status = pthread_mutexattr_init( &attr ); |
---|
279 | assert( !status ); |
---|
280 | |
---|
281 | puts( |
---|
282 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)" |
---|
283 | ); |
---|
284 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
285 | assert( !status ); |
---|
286 | |
---|
287 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
288 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
289 | assert( !status ); |
---|
290 | |
---|
291 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
292 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
293 | assert( !status ); |
---|
294 | |
---|
295 | /* create a thread at a lower priority */ |
---|
296 | |
---|
297 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
298 | assert( !status ); |
---|
299 | |
---|
300 | /* set priority of Task2 to highest priority */ |
---|
301 | |
---|
302 | param.sched_priority = 255; |
---|
303 | |
---|
304 | puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" ); |
---|
305 | status = pthread_setschedparam( Task2_id, SCHED_FIFO, ¶m ); |
---|
306 | assert( !status ); |
---|
307 | |
---|
308 | /* switching to Task2 */ |
---|
309 | |
---|
310 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
311 | assert( !status ); |
---|
312 | printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority); |
---|
313 | |
---|
314 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
315 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
316 | assert( !status ); |
---|
317 | |
---|
318 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
319 | status = pthread_mutexattr_destroy( &attr ); |
---|
320 | assert( !status ); |
---|
321 | |
---|
322 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
323 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
324 | assert( !status ); |
---|
325 | |
---|
326 | /* priority ceiling mutex */ |
---|
327 | |
---|
328 | empty_line(); |
---|
329 | |
---|
330 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
331 | status = pthread_mutexattr_init( &attr ); |
---|
332 | assert( !status ); |
---|
333 | |
---|
334 | puts( |
---|
335 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)" |
---|
336 | ); |
---|
337 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); |
---|
338 | assert( !status ); |
---|
339 | |
---|
340 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
341 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
342 | assert( !status ); |
---|
343 | |
---|
344 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
345 | assert( !status ); |
---|
346 | printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling ); |
---|
347 | |
---|
348 | puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" ); |
---|
349 | status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling ); |
---|
350 | assert( !status ); |
---|
351 | printf( |
---|
352 | "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling |
---|
353 | ); |
---|
354 | |
---|
355 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
356 | assert( !status ); |
---|
357 | printf( |
---|
358 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
359 | ); |
---|
360 | |
---|
361 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
362 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
363 | assert( !status ); |
---|
364 | |
---|
365 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
366 | assert( !status ); |
---|
367 | printf( |
---|
368 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
369 | ); |
---|
370 | |
---|
371 | /* create a thread at a higher priority */ |
---|
372 | |
---|
373 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
374 | assert( !status ); |
---|
375 | |
---|
376 | /* set priority of Task3 to highest priority */ |
---|
377 | |
---|
378 | param.sched_priority = 199; |
---|
379 | |
---|
380 | status = pthread_setschedparam( Task3_id, SCHED_FIFO, ¶m ); |
---|
381 | assert( !status ); |
---|
382 | puts( "Init: pthread_setschedparam - set Task3 priority to highest" ); |
---|
383 | |
---|
384 | /* DOES NOT SWITCH to Task3 */ |
---|
385 | |
---|
386 | puts( "Init: Sleep 1 second" ); |
---|
387 | assert( !status ); |
---|
388 | sleep( 1 ); |
---|
389 | |
---|
390 | /* switch to task 3 */ |
---|
391 | |
---|
392 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
393 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
394 | assert( !status ); |
---|
395 | |
---|
396 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
397 | assert( !status ); |
---|
398 | printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling ); |
---|
399 | |
---|
400 | /* set priority of Init to highest priority */ |
---|
401 | |
---|
402 | param.sched_priority = 255; |
---|
403 | |
---|
404 | status = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
405 | assert( !status ); |
---|
406 | puts( "Init: pthread_setschedparam - set Init priority to highest" ); |
---|
407 | |
---|
408 | puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" ); |
---|
409 | status = pthread_mutex_lock( &Mutex2_id ); |
---|
410 | if ( status != EINVAL ) |
---|
411 | printf( "status = %d\n", status ); |
---|
412 | assert( status == EINVAL ); |
---|
413 | |
---|
414 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
415 | exit( 0 ); |
---|
416 | |
---|
417 | return NULL; /* just so the compiler thinks we returned something */ |
---|
418 | } |
---|