1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/rtems/semimpl.h> |
---|
20 | #include <rtems/rtems/tasksimpl.h> |
---|
21 | #include <rtems/score/schedulerimpl.h> |
---|
22 | |
---|
23 | static rtems_status_code _Semaphore_Is_scheduler_valid( |
---|
24 | const CORE_ceiling_mutex_Control *the_mutex, |
---|
25 | const Scheduler_Control *scheduler |
---|
26 | ) |
---|
27 | { |
---|
28 | #if defined(RTEMS_SMP) |
---|
29 | if ( scheduler != _CORE_ceiling_mutex_Get_scheduler( the_mutex ) ) { |
---|
30 | return RTEMS_NOT_DEFINED; |
---|
31 | } |
---|
32 | #endif |
---|
33 | |
---|
34 | return RTEMS_SUCCESSFUL; |
---|
35 | } |
---|
36 | |
---|
37 | static rtems_status_code _Semaphore_Set_priority( |
---|
38 | Semaphore_Control *the_semaphore, |
---|
39 | const Scheduler_Control *scheduler, |
---|
40 | rtems_task_priority new_priority, |
---|
41 | rtems_task_priority *old_priority_p, |
---|
42 | Thread_queue_Context *queue_context |
---|
43 | ) |
---|
44 | { |
---|
45 | rtems_status_code sc; |
---|
46 | bool valid; |
---|
47 | Priority_Control core_priority; |
---|
48 | Priority_Control old_priority; |
---|
49 | Per_CPU_Control *cpu_self; |
---|
50 | |
---|
51 | core_priority = _RTEMS_Priority_To_core( scheduler, new_priority, &valid ); |
---|
52 | if ( new_priority != RTEMS_CURRENT_PRIORITY && !valid ) { |
---|
53 | return RTEMS_INVALID_PRIORITY; |
---|
54 | } |
---|
55 | |
---|
56 | _Thread_queue_Context_clear_priority_updates( queue_context ); |
---|
57 | _Thread_queue_Acquire_critical( |
---|
58 | &the_semaphore->Core_control.Wait_queue, |
---|
59 | queue_context |
---|
60 | ); |
---|
61 | |
---|
62 | switch ( the_semaphore->variant ) { |
---|
63 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
64 | sc = _Semaphore_Is_scheduler_valid( |
---|
65 | &the_semaphore->Core_control.Mutex, |
---|
66 | scheduler |
---|
67 | ); |
---|
68 | |
---|
69 | old_priority = _CORE_ceiling_mutex_Get_priority( |
---|
70 | &the_semaphore->Core_control.Mutex |
---|
71 | ); |
---|
72 | |
---|
73 | if ( sc == RTEMS_SUCCESSFUL && new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
74 | _CORE_ceiling_mutex_Set_priority( |
---|
75 | &the_semaphore->Core_control.Mutex, |
---|
76 | core_priority, |
---|
77 | queue_context |
---|
78 | ); |
---|
79 | } |
---|
80 | |
---|
81 | break; |
---|
82 | #if defined(RTEMS_SMP) |
---|
83 | case SEMAPHORE_VARIANT_MRSP: |
---|
84 | old_priority = _MRSP_Get_priority( |
---|
85 | &the_semaphore->Core_control.MRSP, |
---|
86 | scheduler |
---|
87 | ); |
---|
88 | |
---|
89 | if ( new_priority != RTEMS_CURRENT_PRIORITY ) { |
---|
90 | _MRSP_Set_priority( |
---|
91 | &the_semaphore->Core_control.MRSP, |
---|
92 | scheduler, |
---|
93 | core_priority |
---|
94 | ); |
---|
95 | } |
---|
96 | |
---|
97 | sc = RTEMS_SUCCESSFUL; |
---|
98 | break; |
---|
99 | #endif |
---|
100 | default: |
---|
101 | _Assert( |
---|
102 | the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY |
---|
103 | || the_semaphore->variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL |
---|
104 | || the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
105 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
106 | ); |
---|
107 | old_priority = 0; |
---|
108 | sc = RTEMS_NOT_DEFINED; |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | cpu_self = _Thread_Dispatch_disable_critical( |
---|
113 | &queue_context->Lock_context.Lock_context |
---|
114 | ); |
---|
115 | _Thread_queue_Release( |
---|
116 | &the_semaphore->Core_control.Wait_queue, |
---|
117 | queue_context |
---|
118 | ); |
---|
119 | _Thread_Priority_update( queue_context ); |
---|
120 | _Thread_Dispatch_enable( cpu_self ); |
---|
121 | |
---|
122 | *old_priority_p = _RTEMS_Priority_From_core( scheduler, old_priority ); |
---|
123 | return sc; |
---|
124 | } |
---|
125 | |
---|
126 | rtems_status_code rtems_semaphore_set_priority( |
---|
127 | rtems_id semaphore_id, |
---|
128 | rtems_id scheduler_id, |
---|
129 | rtems_task_priority new_priority, |
---|
130 | rtems_task_priority *old_priority |
---|
131 | ) |
---|
132 | { |
---|
133 | const Scheduler_Control *scheduler; |
---|
134 | Semaphore_Control *the_semaphore; |
---|
135 | Thread_queue_Context queue_context; |
---|
136 | |
---|
137 | if ( old_priority == NULL ) { |
---|
138 | return RTEMS_INVALID_ADDRESS; |
---|
139 | } |
---|
140 | |
---|
141 | scheduler = _Scheduler_Get_by_id( scheduler_id ); |
---|
142 | if ( scheduler == NULL ) { |
---|
143 | return RTEMS_INVALID_ID; |
---|
144 | } |
---|
145 | |
---|
146 | the_semaphore = _Semaphore_Get( semaphore_id, &queue_context ); |
---|
147 | |
---|
148 | if ( the_semaphore == NULL ) { |
---|
149 | #if defined(RTEMS_MULTIPROCESSING) |
---|
150 | if ( _Semaphore_MP_Is_remote( semaphore_id ) ) { |
---|
151 | return RTEMS_ILLEGAL_ON_REMOTE_OBJECT; |
---|
152 | } |
---|
153 | #endif |
---|
154 | |
---|
155 | return RTEMS_INVALID_ID; |
---|
156 | } |
---|
157 | |
---|
158 | return _Semaphore_Set_priority( |
---|
159 | the_semaphore, |
---|
160 | scheduler, |
---|
161 | new_priority, |
---|
162 | old_priority, |
---|
163 | &queue_context |
---|
164 | ); |
---|
165 | } |
---|