1 | /* |
---|
2 | * Mutex Handler |
---|
3 | * |
---|
4 | * DESCRIPTION: |
---|
5 | * |
---|
6 | * This package is the implementation of the Mutex Handler. |
---|
7 | * This handler provides synchronization and mutual exclusion capabilities. |
---|
8 | * |
---|
9 | * COPYRIGHT (c) 1989-2006. |
---|
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.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/isr.h> |
---|
23 | #include <rtems/score/coremutex.h> |
---|
24 | #include <rtems/score/states.h> |
---|
25 | #include <rtems/score/thread.h> |
---|
26 | #include <rtems/score/threadq.h> |
---|
27 | |
---|
28 | #ifdef __RTEMS_STRICT_ORDER_MUTEX__ |
---|
29 | static inline void _CORE_mutex_Push_priority( |
---|
30 | CORE_mutex_Control *mutex, |
---|
31 | Thread_Control *thread |
---|
32 | ) |
---|
33 | { |
---|
34 | _Chain_Prepend_unprotected( |
---|
35 | &thread->lock_mutex, |
---|
36 | &mutex->queue.lock_queue |
---|
37 | ); |
---|
38 | mutex->queue.priority_before = thread->current_priority; |
---|
39 | } |
---|
40 | |
---|
41 | static inline CORE_mutex_Status _CORE_mutex_Pop_priority( |
---|
42 | CORE_mutex_Control *mutex, |
---|
43 | Thread_Control *holder |
---|
44 | ) |
---|
45 | { |
---|
46 | /* |
---|
47 | * Check whether the holder release the mutex in LIFO order if not return |
---|
48 | * error code. |
---|
49 | */ |
---|
50 | if ( _Chain_First( holder->lock_mutex ) != &mutex->queue.lock_queue ) { |
---|
51 | mutex->nest_count++; |
---|
52 | |
---|
53 | return CORE_MUTEX_RELEASE_NOT_ORDER; |
---|
54 | } |
---|
55 | |
---|
56 | /* |
---|
57 | * This pops the first node from the list. |
---|
58 | */ |
---|
59 | _Chain_Get_first_unprotected( &holder->lock_mutex ); |
---|
60 | |
---|
61 | if ( mutex->queue.priority_before != holder->current_priority ) |
---|
62 | _Thread_Change_priority( holder, mutex->queue.priority_before, true ); |
---|
63 | |
---|
64 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
65 | } |
---|
66 | #else |
---|
67 | #define _CORE_mutex_Push_priority( mutex, thread ) ((void) 0) |
---|
68 | |
---|
69 | #define _CORE_mutex_Pop_priority( mutex, thread ) \ |
---|
70 | CORE_MUTEX_STATUS_SUCCESSFUL |
---|
71 | #endif |
---|
72 | |
---|
73 | /* |
---|
74 | * _CORE_mutex_Surrender |
---|
75 | * |
---|
76 | * DESCRIPTION: |
---|
77 | * |
---|
78 | * This routine frees a unit to the mutex. If a task was blocked waiting for |
---|
79 | * a unit from this mutex, then that task will be readied and the unit |
---|
80 | * given to that task. Otherwise, the unit will be returned to the mutex. |
---|
81 | * |
---|
82 | * Input parameters: |
---|
83 | * the_mutex - the mutex to be flushed |
---|
84 | * id - id of parent mutex |
---|
85 | * api_mutex_mp_support - api dependent MP support actions |
---|
86 | * |
---|
87 | * Output parameters: |
---|
88 | * CORE_MUTEX_STATUS_SUCCESSFUL - if successful |
---|
89 | * core error code - if unsuccessful |
---|
90 | */ |
---|
91 | |
---|
92 | CORE_mutex_Status _CORE_mutex_Surrender( |
---|
93 | CORE_mutex_Control *the_mutex, |
---|
94 | #if defined(RTEMS_MULTIPROCESSING) |
---|
95 | Objects_Id id, |
---|
96 | CORE_mutex_API_mp_support_callout api_mutex_mp_support |
---|
97 | #else |
---|
98 | Objects_Id id __attribute__((unused)), |
---|
99 | CORE_mutex_API_mp_support_callout api_mutex_mp_support __attribute__((unused)) |
---|
100 | #endif |
---|
101 | ) |
---|
102 | { |
---|
103 | Thread_Control *the_thread; |
---|
104 | Thread_Control *holder; |
---|
105 | |
---|
106 | holder = the_mutex->holder; |
---|
107 | |
---|
108 | /* |
---|
109 | * The following code allows a thread (or ISR) other than the thread |
---|
110 | * which acquired the mutex to release that mutex. This is only |
---|
111 | * allowed when the mutex in quetion is FIFO or simple Priority |
---|
112 | * discipline. But Priority Ceiling or Priority Inheritance mutexes |
---|
113 | * must be released by the thread which acquired them. |
---|
114 | */ |
---|
115 | |
---|
116 | if ( the_mutex->Attributes.only_owner_release ) { |
---|
117 | if ( !_Thread_Is_executing( holder ) ) |
---|
118 | return CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE; |
---|
119 | } |
---|
120 | |
---|
121 | /* XXX already unlocked -- not right status */ |
---|
122 | |
---|
123 | if ( !the_mutex->nest_count ) |
---|
124 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
125 | |
---|
126 | the_mutex->nest_count--; |
---|
127 | |
---|
128 | if ( the_mutex->nest_count != 0 ) { |
---|
129 | /* |
---|
130 | * All error checking is on the locking side, so if the lock was |
---|
131 | * allowed to acquired multiple times, then we should just deal with |
---|
132 | * that. The RTEMS_DEBUG is just a validation. |
---|
133 | */ |
---|
134 | #if defined(RTEMS_DEBUG) |
---|
135 | switch ( the_mutex->Attributes.lock_nesting_behavior ) { |
---|
136 | case CORE_MUTEX_NESTING_ACQUIRES: |
---|
137 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
138 | #if defined(RTEMS_POSIX_API) |
---|
139 | case CORE_MUTEX_NESTING_IS_ERROR: |
---|
140 | /* should never occur */ |
---|
141 | return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; |
---|
142 | #endif |
---|
143 | case CORE_MUTEX_NESTING_BLOCKS: |
---|
144 | /* Currently no API exercises this behavior. */ |
---|
145 | break; |
---|
146 | } |
---|
147 | #else |
---|
148 | /* must be CORE_MUTEX_NESTING_ACQUIRES or we wouldn't be here */ |
---|
149 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
150 | #endif |
---|
151 | } |
---|
152 | |
---|
153 | /* |
---|
154 | * Formally release the mutex before possibly transferring it to a |
---|
155 | * blocked thread. |
---|
156 | */ |
---|
157 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
158 | _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { |
---|
159 | CORE_mutex_Status pop_status = |
---|
160 | _CORE_mutex_Pop_priority( the_mutex, holder ); |
---|
161 | |
---|
162 | if ( pop_status != CORE_MUTEX_STATUS_SUCCESSFUL ) |
---|
163 | return pop_status; |
---|
164 | |
---|
165 | holder->resource_count--; |
---|
166 | |
---|
167 | /* |
---|
168 | * Whether or not someone is waiting for the mutex, an |
---|
169 | * inherited priority must be lowered if this is the last |
---|
170 | * mutex (i.e. resource) this task has. |
---|
171 | */ |
---|
172 | if ( holder->resource_count == 0 && |
---|
173 | holder->real_priority != holder->current_priority ) { |
---|
174 | _Thread_Change_priority( holder, holder->real_priority, true ); |
---|
175 | } |
---|
176 | } |
---|
177 | the_mutex->holder = NULL; |
---|
178 | the_mutex->holder_id = 0; |
---|
179 | |
---|
180 | /* |
---|
181 | * Now we check if another thread was waiting for this mutex. If so, |
---|
182 | * transfer the mutex to that thread. |
---|
183 | */ |
---|
184 | if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { |
---|
185 | |
---|
186 | #if defined(RTEMS_MULTIPROCESSING) |
---|
187 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
188 | |
---|
189 | the_mutex->holder = NULL; |
---|
190 | the_mutex->holder_id = the_thread->Object.id; |
---|
191 | the_mutex->nest_count = 1; |
---|
192 | |
---|
193 | ( *api_mutex_mp_support)( the_thread, id ); |
---|
194 | |
---|
195 | } else |
---|
196 | #endif |
---|
197 | { |
---|
198 | |
---|
199 | the_mutex->holder = the_thread; |
---|
200 | the_mutex->holder_id = the_thread->Object.id; |
---|
201 | the_mutex->nest_count = 1; |
---|
202 | |
---|
203 | switch ( the_mutex->Attributes.discipline ) { |
---|
204 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
205 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
206 | break; |
---|
207 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
208 | _CORE_mutex_Push_priority( the_mutex, the_thread ); |
---|
209 | the_thread->resource_count++; |
---|
210 | break; |
---|
211 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
212 | _CORE_mutex_Push_priority( the_mutex, the_thread ); |
---|
213 | the_thread->resource_count++; |
---|
214 | if (the_mutex->Attributes.priority_ceiling < |
---|
215 | the_thread->current_priority){ |
---|
216 | _Thread_Change_priority( |
---|
217 | the_thread, |
---|
218 | the_mutex->Attributes.priority_ceiling, |
---|
219 | false |
---|
220 | ); |
---|
221 | } |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | } else |
---|
226 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
227 | |
---|
228 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
229 | } |
---|