1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Changes the Priority Ceiling of a Mutex and Releases it |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/posix/muteximpl.h> |
---|
22 | #include <rtems/posix/priorityimpl.h> |
---|
23 | |
---|
24 | /* |
---|
25 | * 13.6.2 Change the Priority Ceiling of a Mutex, P1003.1c/Draft 10, p. 131 |
---|
26 | */ |
---|
27 | |
---|
28 | int pthread_mutex_setprioceiling( |
---|
29 | pthread_mutex_t *mutex, |
---|
30 | int prioceiling, |
---|
31 | int *old_ceiling |
---|
32 | ) |
---|
33 | { |
---|
34 | POSIX_Mutex_Control *the_mutex; |
---|
35 | int error; |
---|
36 | int unlock_error; |
---|
37 | |
---|
38 | if ( old_ceiling == NULL ) { |
---|
39 | return EINVAL; |
---|
40 | } |
---|
41 | |
---|
42 | /* |
---|
43 | * Must acquire the mutex before we can change it's ceiling. |
---|
44 | * POSIX says block until we acquire it. |
---|
45 | */ |
---|
46 | error = pthread_mutex_lock( mutex ); |
---|
47 | if ( error != 0 ) { |
---|
48 | _Assert( error == EINVAL ); |
---|
49 | return EINVAL; |
---|
50 | } |
---|
51 | |
---|
52 | the_mutex = _POSIX_Mutex_Get_no_protection( mutex ); |
---|
53 | _Assert( the_mutex != NULL ); |
---|
54 | |
---|
55 | if ( the_mutex->protocol == POSIX_MUTEX_PRIORITY_CEILING ) { |
---|
56 | const Scheduler_Control *scheduler; |
---|
57 | bool valid; |
---|
58 | Priority_Control new_priority; |
---|
59 | Priority_Control old_priority; |
---|
60 | |
---|
61 | scheduler = _CORE_ceiling_mutex_Get_scheduler( &the_mutex->Mutex ); |
---|
62 | old_priority = _CORE_ceiling_mutex_Get_priority( &the_mutex->Mutex ); |
---|
63 | *old_ceiling = _POSIX_Priority_From_core( scheduler, old_priority ); |
---|
64 | |
---|
65 | new_priority = _POSIX_Priority_To_core( scheduler, prioceiling, &valid ); |
---|
66 | if ( valid ) { |
---|
67 | Thread_queue_Context queue_context; |
---|
68 | Per_CPU_Control *cpu_self; |
---|
69 | |
---|
70 | _CORE_ceiling_mutex_Set_priority( |
---|
71 | &the_mutex->Mutex, |
---|
72 | new_priority, |
---|
73 | &queue_context |
---|
74 | ); |
---|
75 | cpu_self = _Thread_Dispatch_disable(); |
---|
76 | _Thread_Priority_update( &queue_context ); |
---|
77 | _Thread_Dispatch_enable( cpu_self ); |
---|
78 | error = 0; |
---|
79 | } else { |
---|
80 | error = EINVAL; |
---|
81 | } |
---|
82 | } else { |
---|
83 | *old_ceiling = 0; |
---|
84 | error = 0; |
---|
85 | } |
---|
86 | |
---|
87 | unlock_error = pthread_mutex_unlock( mutex ); |
---|
88 | _Assert( unlock_error == 0 ); |
---|
89 | (void) unlock_error; |
---|
90 | return error; |
---|
91 | } |
---|