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 | struct timespec times; |
---|
81 | struct sched_param param; |
---|
82 | int policy; |
---|
83 | int ceiling; |
---|
84 | int old_ceiling; |
---|
85 | |
---|
86 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
87 | |
---|
88 | /* set the time of day, and print our buffer in multiple ways */ |
---|
89 | |
---|
90 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
91 | |
---|
92 | /* get id of this thread */ |
---|
93 | |
---|
94 | Init_id = pthread_self(); |
---|
95 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
96 | |
---|
97 | /* basic checkout of mutex attributes */ |
---|
98 | |
---|
99 | puts( "Init: Initializing mutex attributes" ); |
---|
100 | status = pthread_mutexattr_init( &attr ); |
---|
101 | assert( !status ); |
---|
102 | |
---|
103 | Print_mutexattr( "Init: ", &attr ); |
---|
104 | |
---|
105 | puts( "Init: Changing mutex attributes" ); |
---|
106 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
107 | assert( !status ); |
---|
108 | |
---|
109 | status = pthread_mutexattr_setprioceiling( &attr, 128 ); |
---|
110 | assert( !status ); |
---|
111 | |
---|
112 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
113 | assert( !status ); |
---|
114 | |
---|
115 | Print_mutexattr( "Init: ", &attr ); |
---|
116 | |
---|
117 | puts( "Init: Resetting mutex attributes" ); |
---|
118 | status = pthread_mutexattr_init( &attr ); |
---|
119 | assert( !status ); |
---|
120 | |
---|
121 | /* create a thread */ |
---|
122 | |
---|
123 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
124 | assert( !status ); |
---|
125 | |
---|
126 | /* now try some basic mutex operations */ |
---|
127 | |
---|
128 | empty_line(); |
---|
129 | |
---|
130 | puts( "Init: Creating a mutex" ); |
---|
131 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
132 | if ( status ) |
---|
133 | printf( "status = %d\n", status ); |
---|
134 | assert( !status ); |
---|
135 | |
---|
136 | puts( "Init: pthread_mutex_trylock successfully" ); |
---|
137 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
138 | if ( status ) |
---|
139 | printf( "status = %d\n", status ); |
---|
140 | assert( !status ); |
---|
141 | |
---|
142 | puts( "Init: pthread_mutex_trylock already locked" ); |
---|
143 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
144 | if ( status != EDEADLK ) |
---|
145 | printf( "status = %d\n", status ); |
---|
146 | assert( status == EDEADLK ); |
---|
147 | |
---|
148 | puts( "Init: pthread_mutex_lock already locked" ); |
---|
149 | status = pthread_mutex_lock( &Mutex_id ); |
---|
150 | if ( status != EDEADLK ) |
---|
151 | printf( "status = %d\n", status ); |
---|
152 | assert( status == EDEADLK ); |
---|
153 | |
---|
154 | puts( "Init: Sleep 1 second" ); |
---|
155 | |
---|
156 | sleep( 1 ); |
---|
157 | |
---|
158 | /* switch to task 1 */ |
---|
159 | |
---|
160 | puts( "Init: pthread_mutex_unlock successfully" ); |
---|
161 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
162 | if ( status ) |
---|
163 | printf( "status = %d\n", status ); |
---|
164 | assert( !status ); |
---|
165 | |
---|
166 | puts( "Init: pthread_mutex_unlock not owner" ); |
---|
167 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
168 | if ( status != EPERM ) |
---|
169 | printf( "status = %d\n", status ); |
---|
170 | assert( status == EPERM ); |
---|
171 | |
---|
172 | times.tv_sec = 0; |
---|
173 | times.tv_nsec = 500000000; |
---|
174 | puts( "Init: pthread_mutex_timedlock time out in 1/2 second" ); |
---|
175 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
176 | if ( status != EAGAIN ) |
---|
177 | printf( "status = %d\n", status ); |
---|
178 | assert( status == EAGAIN ); |
---|
179 | |
---|
180 | /* switch to idle */ |
---|
181 | |
---|
182 | puts( "Init: correctly timed out waiting for mutex" ); |
---|
183 | |
---|
184 | /* destroy a mutex */ |
---|
185 | |
---|
186 | empty_line(); |
---|
187 | |
---|
188 | puts( "Init: Creating a mutex" ); |
---|
189 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
190 | if ( status ) |
---|
191 | printf( "status = %d\n", status ); |
---|
192 | assert( !status ); |
---|
193 | |
---|
194 | puts( "Init: pthread_mutexattr_destroy" ); |
---|
195 | status = pthread_mutexattr_destroy( &attr ); |
---|
196 | assert( !status ); |
---|
197 | |
---|
198 | puts( "Init: pthread_mutex_destroy" ); |
---|
199 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
200 | assert( !status ); |
---|
201 | |
---|
202 | /* destroy a busy mutex */ |
---|
203 | |
---|
204 | empty_line(); |
---|
205 | |
---|
206 | puts( "Init: Initializing mutex attributes" ); |
---|
207 | status = pthread_mutexattr_init( &attr ); |
---|
208 | assert( !status ); |
---|
209 | |
---|
210 | puts( "Init: Creating a mutex" ); |
---|
211 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
212 | assert( !status ); |
---|
213 | |
---|
214 | puts( "Init: pthread_mutex_trylock successfully" ); |
---|
215 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
216 | if ( status ) |
---|
217 | printf( "status = %d\n", status ); |
---|
218 | assert( !status ); |
---|
219 | |
---|
220 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
221 | if ( status != EBUSY ) |
---|
222 | printf( "status = %d\n", status ); |
---|
223 | assert( status == EBUSY ); |
---|
224 | puts( "Init: pthread_mutex_destroy - EBUSY" ); |
---|
225 | |
---|
226 | puts( "Init: pthread_mutex_unlock successfully" ); |
---|
227 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
228 | assert( !status ); |
---|
229 | |
---|
230 | puts( "Init: pthread_mutex_destroy" ); |
---|
231 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
232 | assert( !status ); |
---|
233 | |
---|
234 | /* priority inherit mutex */ |
---|
235 | |
---|
236 | empty_line(); |
---|
237 | |
---|
238 | puts( "Init: Initializing mutex attributes" ); |
---|
239 | status = pthread_mutexattr_init( &attr ); |
---|
240 | assert( !status ); |
---|
241 | |
---|
242 | puts( "Init: Setting PTHREAD_PRIO_INHERIT attribute" ); |
---|
243 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
244 | assert( !status ); |
---|
245 | |
---|
246 | puts( "Init: Creating a mutex" ); |
---|
247 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
248 | assert( !status ); |
---|
249 | |
---|
250 | puts( "Init: pthread_mutex_trylock successfully" ); |
---|
251 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
252 | assert( !status ); |
---|
253 | |
---|
254 | /* create a thread at a lower priority */ |
---|
255 | |
---|
256 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
257 | assert( !status ); |
---|
258 | |
---|
259 | /* set priority of Task2 to highest priority */ |
---|
260 | |
---|
261 | param.sched_priority = 255; |
---|
262 | |
---|
263 | puts( "Init: Setting Task2 priority to highest" ); |
---|
264 | status = pthread_setschedparam( Task2_id, SCHED_FIFO, ¶m ); |
---|
265 | assert( !status ); |
---|
266 | |
---|
267 | /* switching to Task2 */ |
---|
268 | |
---|
269 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
270 | assert( !status ); |
---|
271 | printf( "Init: pthread_getschedparam priority = %d\n", param.sched_priority ); |
---|
272 | |
---|
273 | puts( "Init: pthread_mutex_unlock successfully" ); |
---|
274 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
275 | assert( !status ); |
---|
276 | |
---|
277 | puts( "Init: pthread_mutexattr_destroy" ); |
---|
278 | status = pthread_mutexattr_destroy( &attr ); |
---|
279 | assert( !status ); |
---|
280 | |
---|
281 | puts( "Init: pthread_mutex_destroy" ); |
---|
282 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
283 | assert( !status ); |
---|
284 | |
---|
285 | /* priority ceiling mutex */ |
---|
286 | |
---|
287 | empty_line(); |
---|
288 | |
---|
289 | puts( "Init: Initializing mutex attributes" ); |
---|
290 | status = pthread_mutexattr_init( &attr ); |
---|
291 | assert( !status ); |
---|
292 | |
---|
293 | puts( "Init: Setting PTHREAD_PRIO_PROTECT attribute" ); |
---|
294 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); |
---|
295 | assert( !status ); |
---|
296 | |
---|
297 | puts( "Init: Creating a mutex" ); |
---|
298 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
299 | assert( !status ); |
---|
300 | |
---|
301 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
302 | assert( !status ); |
---|
303 | printf( "Init: priority ceiling = %d\n", ceiling ); |
---|
304 | |
---|
305 | status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling ); |
---|
306 | assert( !status ); |
---|
307 | printf( "Init: Set ceiling = 200 old priority ceiling = %d\n",old_ceiling ); |
---|
308 | |
---|
309 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
310 | assert( !status ); |
---|
311 | printf( "Init: pthread_getschedparam priority = %d\n", param.sched_priority ); |
---|
312 | |
---|
313 | puts( "Init: pthread_mutex_trylock successfully" ); |
---|
314 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
315 | assert( !status ); |
---|
316 | |
---|
317 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
318 | assert( !status ); |
---|
319 | printf( "Init: pthread_getschedparam priority = %d\n", param.sched_priority ); |
---|
320 | |
---|
321 | /* create a thread at a higher priority */ |
---|
322 | |
---|
323 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
324 | assert( !status ); |
---|
325 | |
---|
326 | /* set priority of Task3 to highest priority */ |
---|
327 | |
---|
328 | param.sched_priority = 199; |
---|
329 | |
---|
330 | status = pthread_setschedparam( Task3_id, SCHED_FIFO, ¶m ); |
---|
331 | assert( !status ); |
---|
332 | puts( "Init: Set Task3 priority to highest" ); |
---|
333 | |
---|
334 | /* DOES NOT SWITCH to Task3 */ |
---|
335 | |
---|
336 | puts( "Init: Sleep 1 second" ); |
---|
337 | assert( !status ); |
---|
338 | sleep( 1 ); |
---|
339 | |
---|
340 | /* switch to task 3 */ |
---|
341 | |
---|
342 | puts( "Init: pthread_mutex_unlock successfully" ); |
---|
343 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
344 | assert( !status ); |
---|
345 | |
---|
346 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
347 | assert( !status ); |
---|
348 | printf( "Init: priority ceiling = %d\n", ceiling ); |
---|
349 | |
---|
350 | /* set priority of Init to highest priority */ |
---|
351 | |
---|
352 | param.sched_priority = 255; |
---|
353 | |
---|
354 | status = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
355 | assert( !status ); |
---|
356 | puts( "Init: Set Init priority to highest" ); |
---|
357 | |
---|
358 | status = pthread_mutex_lock( &Mutex2_id ); |
---|
359 | if ( status != EDEADLK ) |
---|
360 | printf( "status = %d\n", status ); |
---|
361 | assert( status == EINVAL ); |
---|
362 | puts( "Init: pthread_mutex_lock EINVAL (priority ceiling violation)" ); |
---|
363 | |
---|
364 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
365 | exit( 0 ); |
---|
366 | |
---|
367 | return NULL; /* just so the compiler thinks we returned something */ |
---|
368 | } |
---|