1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2009. |
---|
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.rtems.com/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <sched.h> |
---|
15 | |
---|
16 | #define CONFIGURE_INIT |
---|
17 | #include "system.h" |
---|
18 | #include <errno.h> |
---|
19 | |
---|
20 | #include <rtems/score/todimpl.h> |
---|
21 | |
---|
22 | #define MUTEX_BAD_ID 0xfffffffe |
---|
23 | |
---|
24 | void Print_mutexattr( |
---|
25 | char *msg, |
---|
26 | pthread_mutexattr_t *attr |
---|
27 | ); |
---|
28 | |
---|
29 | void calculate_abstimeout( |
---|
30 | struct timespec *times, |
---|
31 | uint32_t seconds, |
---|
32 | uint32_t nanoseconds |
---|
33 | ); |
---|
34 | |
---|
35 | void Print_mutexattr( |
---|
36 | char *msg, |
---|
37 | pthread_mutexattr_t *attr |
---|
38 | ) |
---|
39 | { |
---|
40 | int status; |
---|
41 | int protocol; |
---|
42 | int prioceiling; |
---|
43 | int pshared; |
---|
44 | |
---|
45 | /* protocol */ |
---|
46 | |
---|
47 | status = pthread_mutexattr_getprotocol( attr, &protocol ); |
---|
48 | rtems_test_assert( !status ); |
---|
49 | |
---|
50 | printf( "%smutex protocol is (%d) -- ", msg, protocol ); |
---|
51 | switch ( protocol ) { |
---|
52 | case PTHREAD_PRIO_NONE: |
---|
53 | puts( "PTHREAD_PRIO_NONE" ); |
---|
54 | break; |
---|
55 | case PTHREAD_PRIO_INHERIT: |
---|
56 | puts( "PTHREAD_PRIO_INHERIT" ); |
---|
57 | break; |
---|
58 | case PTHREAD_PRIO_PROTECT: |
---|
59 | puts( "PTHREAD_PRIO_PROTECT" ); |
---|
60 | break; |
---|
61 | default: |
---|
62 | puts( "UNKNOWN" ); |
---|
63 | rtems_test_assert( 0 ); |
---|
64 | break; |
---|
65 | } |
---|
66 | |
---|
67 | /* priority ceiling */ |
---|
68 | |
---|
69 | status = pthread_mutexattr_getprioceiling( attr, &prioceiling ); |
---|
70 | rtems_test_assert( !status ); |
---|
71 | printf( "%smutex priority ceiling is %d\n", msg, prioceiling ); |
---|
72 | |
---|
73 | /* process shared */ |
---|
74 | |
---|
75 | status = pthread_mutexattr_getpshared( attr, &pshared ); |
---|
76 | rtems_test_assert( !status ); |
---|
77 | printf( "%smutex process shared is (%d) -- ", msg, pshared ); |
---|
78 | switch ( pshared ) { |
---|
79 | case PTHREAD_PROCESS_PRIVATE: |
---|
80 | puts( "PTHREAD_PROCESS_PRIVATE" ); |
---|
81 | break; |
---|
82 | case PTHREAD_PROCESS_SHARED: |
---|
83 | puts( "PTHREAD_PROCESS_SHARED" ); |
---|
84 | break; |
---|
85 | default: |
---|
86 | puts( "UNKNOWN" ); |
---|
87 | rtems_test_assert( 0 ); |
---|
88 | break; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | void calculate_abstimeout( |
---|
93 | struct timespec *times, |
---|
94 | uint32_t seconds, |
---|
95 | uint32_t nanoseconds |
---|
96 | ) |
---|
97 | { |
---|
98 | struct timeval tv1; |
---|
99 | struct timezone tz1; |
---|
100 | |
---|
101 | gettimeofday( &tv1, &tz1 ); |
---|
102 | |
---|
103 | times->tv_sec = seconds + tv1.tv_sec; |
---|
104 | times->tv_nsec = nanoseconds + (tv1.tv_usec * 1000); |
---|
105 | |
---|
106 | while ( times->tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) { |
---|
107 | times->tv_sec++; |
---|
108 | times->tv_nsec -= TOD_NANOSECONDS_PER_SECOND; |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | void *POSIX_Init( |
---|
114 | void *argument |
---|
115 | ) |
---|
116 | { |
---|
117 | int status; |
---|
118 | pthread_mutexattr_t attr; |
---|
119 | pthread_mutexattr_t destroyed_attr; |
---|
120 | struct timespec times; |
---|
121 | struct sched_param param; |
---|
122 | int pshared; |
---|
123 | int policy; |
---|
124 | int protocol; |
---|
125 | int ceiling; |
---|
126 | int old_ceiling; |
---|
127 | int priority; |
---|
128 | |
---|
129 | rtems_test_assert( MUTEX_BAD_ID != PTHREAD_MUTEX_INITIALIZER ); |
---|
130 | Mutex_bad_id = MUTEX_BAD_ID; |
---|
131 | |
---|
132 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
133 | |
---|
134 | /* set the time of day, and print our buffer in multiple ways */ |
---|
135 | |
---|
136 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
137 | |
---|
138 | /* get id of this thread */ |
---|
139 | |
---|
140 | Init_id = pthread_self(); |
---|
141 | printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id ); |
---|
142 | |
---|
143 | /* test pthread_mutex_attr_init */ |
---|
144 | |
---|
145 | puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" ); |
---|
146 | status = pthread_mutexattr_init( NULL ); |
---|
147 | rtems_test_assert( status == EINVAL ); |
---|
148 | |
---|
149 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
150 | status = pthread_mutexattr_init( &attr ); |
---|
151 | rtems_test_assert( !status ); |
---|
152 | |
---|
153 | Print_mutexattr( "Init: ", &attr ); |
---|
154 | |
---|
155 | /* create an "uninitialized" attribute structure */ |
---|
156 | |
---|
157 | status = pthread_mutexattr_init( &destroyed_attr ); |
---|
158 | rtems_test_assert( !status ); |
---|
159 | |
---|
160 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
161 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
162 | rtems_test_assert( !status ); |
---|
163 | |
---|
164 | puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" ); |
---|
165 | status = pthread_mutexattr_destroy( NULL ); |
---|
166 | rtems_test_assert( status == EINVAL ); |
---|
167 | |
---|
168 | puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" ); |
---|
169 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
170 | rtems_test_assert( status == EINVAL ); |
---|
171 | |
---|
172 | /* error cases for set and get pshared attribute */ |
---|
173 | |
---|
174 | empty_line(); |
---|
175 | |
---|
176 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" ); |
---|
177 | status = pthread_mutexattr_getpshared( NULL, &pshared ); |
---|
178 | rtems_test_assert( status == EINVAL ); |
---|
179 | |
---|
180 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" ); |
---|
181 | status = pthread_mutexattr_getpshared( &attr, NULL ); |
---|
182 | rtems_test_assert( status == EINVAL ); |
---|
183 | |
---|
184 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" ); |
---|
185 | status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared ); |
---|
186 | rtems_test_assert( status == EINVAL ); |
---|
187 | |
---|
188 | pshared = PTHREAD_PROCESS_PRIVATE; |
---|
189 | puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" ); |
---|
190 | status = pthread_mutexattr_setpshared( NULL, pshared ); |
---|
191 | rtems_test_assert( status == EINVAL ); |
---|
192 | |
---|
193 | pshared = PTHREAD_PROCESS_PRIVATE; |
---|
194 | puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" ); |
---|
195 | status = pthread_mutexattr_setpshared( &destroyed_attr, pshared ); |
---|
196 | rtems_test_assert( status == EINVAL ); |
---|
197 | |
---|
198 | /* error cases for set and get protocol attribute */ |
---|
199 | |
---|
200 | empty_line(); |
---|
201 | |
---|
202 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" ); |
---|
203 | status = pthread_mutexattr_getprotocol( NULL, &protocol ); |
---|
204 | rtems_test_assert( status == EINVAL ); |
---|
205 | |
---|
206 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" ); |
---|
207 | status = pthread_mutexattr_getprotocol( &attr, NULL ); |
---|
208 | rtems_test_assert( status == EINVAL ); |
---|
209 | |
---|
210 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" ); |
---|
211 | status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol ); |
---|
212 | rtems_test_assert( status == EINVAL ); |
---|
213 | |
---|
214 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" ); |
---|
215 | status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE ); |
---|
216 | rtems_test_assert( status == EINVAL ); |
---|
217 | |
---|
218 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" ); |
---|
219 | status = pthread_mutexattr_setprotocol( &attr, -1 ); |
---|
220 | rtems_test_assert( status == EINVAL ); |
---|
221 | |
---|
222 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" ); |
---|
223 | status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 ); |
---|
224 | rtems_test_assert( status == EINVAL ); |
---|
225 | |
---|
226 | /* error cases for set and get prioceiling attribute */ |
---|
227 | |
---|
228 | empty_line(); |
---|
229 | |
---|
230 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" ); |
---|
231 | status = pthread_mutexattr_getprioceiling( NULL, &ceiling ); |
---|
232 | rtems_test_assert( status == EINVAL ); |
---|
233 | |
---|
234 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" ); |
---|
235 | status = pthread_mutexattr_getprioceiling( &attr, NULL ); |
---|
236 | rtems_test_assert( status == EINVAL ); |
---|
237 | |
---|
238 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" ); |
---|
239 | status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling ); |
---|
240 | rtems_test_assert( status == EINVAL ); |
---|
241 | |
---|
242 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" ); |
---|
243 | status = pthread_mutexattr_setprioceiling( NULL, 128 ); |
---|
244 | rtems_test_assert( status == EINVAL ); |
---|
245 | |
---|
246 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (invalid priority)" ); |
---|
247 | status = pthread_mutexattr_setprioceiling( &attr, 512 ); |
---|
248 | if ( status != EINVAL ) |
---|
249 | printf( "status = %d\n", status ); |
---|
250 | rtems_test_assert( status == EINVAL ); |
---|
251 | |
---|
252 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" ); |
---|
253 | status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 ); |
---|
254 | rtems_test_assert( status == EINVAL ); |
---|
255 | |
---|
256 | /* create a thread */ |
---|
257 | |
---|
258 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
259 | rtems_test_assert( !status ); |
---|
260 | |
---|
261 | /* now try some basic mutex operations */ |
---|
262 | |
---|
263 | empty_line(); |
---|
264 | |
---|
265 | puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" ); |
---|
266 | status = pthread_mutex_init( NULL, &attr ); |
---|
267 | rtems_test_assert( status == EINVAL ); |
---|
268 | |
---|
269 | puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" ); |
---|
270 | status = pthread_mutex_init( &Mutex_id, &destroyed_attr ); |
---|
271 | rtems_test_assert( status == EINVAL ); |
---|
272 | |
---|
273 | /* must get around error checks in attribute set routines */ |
---|
274 | attr.protocol = -1; |
---|
275 | |
---|
276 | puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" ); |
---|
277 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
278 | rtems_test_assert( status == EINVAL ); |
---|
279 | |
---|
280 | /* must get around error checks in attribute set routines */ |
---|
281 | attr.protocol = PTHREAD_PRIO_INHERIT; |
---|
282 | attr.prio_ceiling = -1; |
---|
283 | |
---|
284 | puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" ); |
---|
285 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
286 | rtems_test_assert( status == EINVAL ); |
---|
287 | |
---|
288 | /* must get around various error checks before checking bad scope */ |
---|
289 | puts( "Init: Resetting mutex attributes" ); |
---|
290 | status = pthread_mutexattr_init( &attr ); |
---|
291 | rtems_test_assert( !status ); |
---|
292 | |
---|
293 | puts( "Init: pthread_mutex_init - ENOSYS (process wide scope)" ); |
---|
294 | attr.process_shared = PTHREAD_PROCESS_SHARED; |
---|
295 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
296 | rtems_test_assert( status == ENOSYS ); |
---|
297 | |
---|
298 | puts( "Init: pthread_mutex_init - EINVAL (invalid scope)" ); |
---|
299 | attr.process_shared = -1; |
---|
300 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
301 | rtems_test_assert( status == EINVAL ); |
---|
302 | |
---|
303 | /* bad kind */ |
---|
304 | status = pthread_mutexattr_init( &attr ); |
---|
305 | rtems_test_assert( !status ); |
---|
306 | |
---|
307 | puts( "Init: pthread_mutex_init - EINVAL (invalid type)" ); |
---|
308 | attr.type = -1; |
---|
309 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
310 | rtems_test_assert( status == EINVAL ); |
---|
311 | |
---|
312 | /* now set up for a success pthread_mutex_init */ |
---|
313 | |
---|
314 | puts( "Init: Resetting mutex attributes" ); |
---|
315 | status = pthread_mutexattr_init( &attr ); |
---|
316 | rtems_test_assert( !status ); |
---|
317 | |
---|
318 | puts( "Init: Changing mutex attributes" ); |
---|
319 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
320 | rtems_test_assert( !status ); |
---|
321 | |
---|
322 | status = pthread_mutexattr_setprioceiling( |
---|
323 | &attr, |
---|
324 | (sched_get_priority_max(SCHED_FIFO) / 2) + 1 |
---|
325 | ); |
---|
326 | rtems_test_assert( !status ); |
---|
327 | |
---|
328 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
329 | rtems_test_assert( !status ); |
---|
330 | |
---|
331 | Print_mutexattr( "Init: ", &attr ); |
---|
332 | |
---|
333 | puts( "Init: Resetting mutex attributes" ); |
---|
334 | status = pthread_mutexattr_init( &attr ); |
---|
335 | rtems_test_assert( !status ); |
---|
336 | |
---|
337 | /* |
---|
338 | * Set the protocol to priority ceiling so the owner check happens |
---|
339 | * and the EPERM test (later) will work. |
---|
340 | */ |
---|
341 | |
---|
342 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
343 | rtems_test_assert( !status ); |
---|
344 | |
---|
345 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
346 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
347 | if ( status ) |
---|
348 | printf( "status = %d\n", status ); |
---|
349 | rtems_test_assert( !status ); |
---|
350 | |
---|
351 | /* |
---|
352 | * This is not required to be an error and when it is, there are |
---|
353 | * behavioral conflicts with other implementations. |
---|
354 | */ |
---|
355 | puts( "Init: pthread_mutex_init - EBUSY (reinitialize an existing mutex) - skipped" ); |
---|
356 | |
---|
357 | #if 0 |
---|
358 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
359 | if ( !status ) |
---|
360 | printf( "status = %d\n", status ); |
---|
361 | rtems_test_assert( status == EBUSY ); |
---|
362 | #endif |
---|
363 | |
---|
364 | puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" ); |
---|
365 | status = pthread_mutex_trylock( &Mutex_bad_id ); |
---|
366 | if ( status != EINVAL ) |
---|
367 | printf( "status = %d\n", status ); |
---|
368 | rtems_test_assert( status == EINVAL ); |
---|
369 | |
---|
370 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
371 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
372 | if ( status ) |
---|
373 | printf( "status = %d\n", status ); |
---|
374 | rtems_test_assert( !status ); |
---|
375 | |
---|
376 | puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" ); |
---|
377 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
378 | if ( status != EDEADLK ) |
---|
379 | printf( "status = %d\n", status ); |
---|
380 | rtems_test_assert( status == EDEADLK ); |
---|
381 | |
---|
382 | puts( "Init: pthread_mutex_lock - EINVAL (NULL id)" ); |
---|
383 | status = pthread_mutex_lock( NULL ); |
---|
384 | if ( status != EINVAL ) |
---|
385 | printf( "status = %d\n", status ); |
---|
386 | rtems_test_assert( status == EINVAL ); |
---|
387 | |
---|
388 | puts( "Init: pthread_mutex_unlock - EINVAL (NULL id)" ); |
---|
389 | status = pthread_mutex_unlock( NULL ); |
---|
390 | if ( status != EINVAL ) |
---|
391 | printf( "status = %d\n", status ); |
---|
392 | rtems_test_assert( status == EINVAL ); |
---|
393 | |
---|
394 | puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" ); |
---|
395 | status = pthread_mutex_lock( &Mutex_id ); |
---|
396 | if ( status != EDEADLK ) |
---|
397 | printf( "status = %d\n", status ); |
---|
398 | rtems_test_assert( status == EDEADLK ); |
---|
399 | |
---|
400 | puts( "Init: Sleep 1 second" ); |
---|
401 | |
---|
402 | sleep( 1 ); |
---|
403 | |
---|
404 | /* switch to task 1 */ |
---|
405 | |
---|
406 | puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" ); |
---|
407 | status = pthread_mutex_unlock( &Mutex_bad_id ); |
---|
408 | if ( status != EINVAL ) |
---|
409 | printf( "status = %d\n", status ); |
---|
410 | rtems_test_assert( status == EINVAL ); |
---|
411 | |
---|
412 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
413 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
414 | if ( status ) |
---|
415 | printf( "status = %d\n", status ); |
---|
416 | rtems_test_assert( !status ); |
---|
417 | |
---|
418 | puts( "Init: pthread_mutex_unlock - EPERM (not owner)" ); |
---|
419 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
420 | if ( status != EPERM ) |
---|
421 | printf( "status = %d\n", status ); |
---|
422 | rtems_test_assert( status == EPERM ); |
---|
423 | |
---|
424 | puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" ); |
---|
425 | calculate_abstimeout( ×, 0, (TOD_NANOSECONDS_PER_SECOND / 2) ); |
---|
426 | |
---|
427 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
428 | if ( status != ETIMEDOUT ) |
---|
429 | printf( "status = %d\n", status ); |
---|
430 | rtems_test_assert( status == ETIMEDOUT ); |
---|
431 | |
---|
432 | puts( "Init: pthread_mutex_timedlock - time out in the past" ); |
---|
433 | calculate_abstimeout( ×, -1, (TOD_NANOSECONDS_PER_SECOND / 2) ); |
---|
434 | |
---|
435 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
436 | if ( status != ETIMEDOUT ) |
---|
437 | printf( "status = %d\n", status ); |
---|
438 | rtems_test_assert( status == ETIMEDOUT ); |
---|
439 | |
---|
440 | /* switch to idle */ |
---|
441 | |
---|
442 | puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" ); |
---|
443 | |
---|
444 | /* destroy a mutex */ |
---|
445 | |
---|
446 | empty_line(); |
---|
447 | |
---|
448 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
449 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
450 | if ( status ) |
---|
451 | printf( "status = %d\n", status ); |
---|
452 | rtems_test_assert( !status ); |
---|
453 | |
---|
454 | puts( "Init: pthread_mutex_init - EAGAIN (too many)" ); |
---|
455 | status = pthread_mutex_init( &Mutex3_id, &attr ); |
---|
456 | rtems_test_assert( status == EAGAIN ); |
---|
457 | |
---|
458 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
459 | status = pthread_mutexattr_destroy( &attr ); |
---|
460 | rtems_test_assert( !status ); |
---|
461 | |
---|
462 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
463 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
464 | rtems_test_assert( !status ); |
---|
465 | |
---|
466 | puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" ); |
---|
467 | status = pthread_mutex_destroy( &Mutex_bad_id ); |
---|
468 | rtems_test_assert( status == EINVAL ); |
---|
469 | |
---|
470 | /* destroy a busy mutex */ |
---|
471 | |
---|
472 | empty_line(); |
---|
473 | |
---|
474 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
475 | status = pthread_mutexattr_init( &attr ); |
---|
476 | rtems_test_assert( !status ); |
---|
477 | |
---|
478 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
479 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
480 | rtems_test_assert( !status ); |
---|
481 | |
---|
482 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
483 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
484 | if ( status ) |
---|
485 | printf( "status = %d\n", status ); |
---|
486 | rtems_test_assert( !status ); |
---|
487 | |
---|
488 | puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" ); |
---|
489 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
490 | if ( status != EBUSY ) |
---|
491 | printf( "status = %d\n", status ); |
---|
492 | rtems_test_assert( status == EBUSY ); |
---|
493 | |
---|
494 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
495 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
496 | rtems_test_assert( !status ); |
---|
497 | |
---|
498 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
499 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
500 | rtems_test_assert( !status ); |
---|
501 | |
---|
502 | /* priority inherit mutex */ |
---|
503 | |
---|
504 | empty_line(); |
---|
505 | |
---|
506 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
507 | status = pthread_mutexattr_init( &attr ); |
---|
508 | rtems_test_assert( !status ); |
---|
509 | |
---|
510 | puts( |
---|
511 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)" |
---|
512 | ); |
---|
513 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
514 | rtems_test_assert( !status ); |
---|
515 | |
---|
516 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
517 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
518 | rtems_test_assert( !status ); |
---|
519 | |
---|
520 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
521 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
522 | rtems_test_assert( !status ); |
---|
523 | |
---|
524 | /* create a thread at a lower priority */ |
---|
525 | |
---|
526 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
527 | rtems_test_assert( !status ); |
---|
528 | |
---|
529 | /* set priority of Task2 to highest priority */ |
---|
530 | |
---|
531 | param.sched_priority = sched_get_priority_max( SCHED_FIFO ); |
---|
532 | |
---|
533 | puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" ); |
---|
534 | status = pthread_setschedparam( Task2_id, SCHED_FIFO, ¶m ); |
---|
535 | rtems_test_assert( !status ); |
---|
536 | |
---|
537 | /* switching to Task2 */ |
---|
538 | |
---|
539 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
540 | rtems_test_assert( !status ); |
---|
541 | printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority); |
---|
542 | |
---|
543 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
544 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
545 | rtems_test_assert( !status ); |
---|
546 | |
---|
547 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
548 | status = pthread_mutexattr_destroy( &attr ); |
---|
549 | rtems_test_assert( !status ); |
---|
550 | |
---|
551 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
552 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
553 | rtems_test_assert( !status ); |
---|
554 | |
---|
555 | /* priority ceiling mutex */ |
---|
556 | |
---|
557 | empty_line(); |
---|
558 | |
---|
559 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
560 | status = pthread_mutexattr_init( &attr ); |
---|
561 | rtems_test_assert( !status ); |
---|
562 | |
---|
563 | puts( |
---|
564 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)" |
---|
565 | ); |
---|
566 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); |
---|
567 | rtems_test_assert( !status ); |
---|
568 | |
---|
569 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
570 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
571 | rtems_test_assert( !status ); |
---|
572 | |
---|
573 | puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" ); |
---|
574 | status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling ); |
---|
575 | rtems_test_assert( status == EINVAL ); |
---|
576 | |
---|
577 | puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" ); |
---|
578 | status = pthread_mutex_getprioceiling( &Mutex2_id, NULL ); |
---|
579 | rtems_test_assert( status == EINVAL ); |
---|
580 | |
---|
581 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
582 | rtems_test_assert( !status ); |
---|
583 | printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling ); |
---|
584 | |
---|
585 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" ); |
---|
586 | status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling ); |
---|
587 | rtems_test_assert( status == EINVAL ); |
---|
588 | |
---|
589 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" ); |
---|
590 | status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling ); |
---|
591 | rtems_test_assert( status == EINVAL ); |
---|
592 | |
---|
593 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" ); |
---|
594 | status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL ); |
---|
595 | rtems_test_assert( status == EINVAL ); |
---|
596 | |
---|
597 | /* normal cases of set priority ceiling */ |
---|
598 | |
---|
599 | priority = sched_get_priority_max( SCHED_FIFO ); |
---|
600 | priority = (priority == 254) ? 200 : 13; |
---|
601 | |
---|
602 | printf( "Init: pthread_mutex_setprioceiling - new ceiling = %d\n", priority ); |
---|
603 | status = pthread_mutex_setprioceiling( &Mutex2_id, priority, &old_ceiling ); |
---|
604 | rtems_test_assert( !status ); |
---|
605 | printf( |
---|
606 | "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling |
---|
607 | ); |
---|
608 | |
---|
609 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
610 | rtems_test_assert( !status ); |
---|
611 | printf( |
---|
612 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
613 | ); |
---|
614 | |
---|
615 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
616 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
617 | rtems_test_assert( !status ); |
---|
618 | |
---|
619 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
620 | rtems_test_assert( !status ); |
---|
621 | printf( |
---|
622 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
623 | ); |
---|
624 | |
---|
625 | /* create a thread at a higher priority */ |
---|
626 | |
---|
627 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
628 | rtems_test_assert( !status ); |
---|
629 | |
---|
630 | /* set priority of Task3 to highest priority */ |
---|
631 | |
---|
632 | param.sched_priority = --priority; |
---|
633 | |
---|
634 | status = pthread_setschedparam( Task3_id, SCHED_FIFO, ¶m ); |
---|
635 | rtems_test_assert( !status ); |
---|
636 | puts( "Init: pthread_setschedparam - set Task3 priority to highest" ); |
---|
637 | |
---|
638 | /* DOES NOT SWITCH to Task3 */ |
---|
639 | |
---|
640 | puts( "Init: Sleep 1 second" ); |
---|
641 | rtems_test_assert( !status ); |
---|
642 | sleep( 1 ); |
---|
643 | |
---|
644 | /* switch to task 3 */ |
---|
645 | |
---|
646 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
647 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
648 | rtems_test_assert( !status ); |
---|
649 | |
---|
650 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
651 | rtems_test_assert( !status ); |
---|
652 | printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling ); |
---|
653 | |
---|
654 | /* set priority of Init to highest priority */ |
---|
655 | |
---|
656 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); |
---|
657 | |
---|
658 | status = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
659 | rtems_test_assert( !status ); |
---|
660 | puts( "Init: pthread_setschedparam - set Init priority to highest" ); |
---|
661 | |
---|
662 | puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" ); |
---|
663 | status = pthread_mutex_lock( &Mutex2_id ); |
---|
664 | if ( status != EINVAL ) |
---|
665 | printf( "status = %d\n", status ); |
---|
666 | rtems_test_assert( status == EINVAL ); |
---|
667 | |
---|
668 | /* mutexinit.c: Initialising recursive mutex */ |
---|
669 | |
---|
670 | puts( "Init: Recursive Mutex" ); |
---|
671 | |
---|
672 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
673 | if( status ) |
---|
674 | printf( "status mutex destroy:%d\n", status ); |
---|
675 | |
---|
676 | status = pthread_mutexattr_init( &attr ); |
---|
677 | if( status ) |
---|
678 | printf( "status mutexattr:%d\n", status ); |
---|
679 | |
---|
680 | attr.recursive=true; |
---|
681 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
682 | if ( status ) |
---|
683 | printf( "status recursive mutex :%d\n", status ); |
---|
684 | rtems_test_assert( !status ); |
---|
685 | |
---|
686 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
687 | rtems_test_exit( 0 ); |
---|
688 | |
---|
689 | return NULL; /* just so the compiler thinks we returned something */ |
---|
690 | } |
---|