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