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, 1990, 1991, 1992, 1993, 1994. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * All rights assigned to U.S. Government, 1994. |
---|
12 | * |
---|
13 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
14 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
15 | * notice must appear in all copies of this file and its derivatives. |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | */ |
---|
19 | |
---|
20 | #include <rtems/system.h> |
---|
21 | #include <rtems/score/isr.h> |
---|
22 | #include <rtems/score/coremutex.h> |
---|
23 | #include <rtems/score/states.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #include <rtems/score/threadq.h> |
---|
26 | |
---|
27 | /*PAGE |
---|
28 | * |
---|
29 | * _CORE_mutex_Initialize |
---|
30 | * |
---|
31 | * This routine initializes a mutex at create time and set the control |
---|
32 | * structure according to the values passed. |
---|
33 | * |
---|
34 | * Input parameters: |
---|
35 | * the_mutex - the mutex control block to initialize |
---|
36 | * the_class - the API class of the object |
---|
37 | * the_mutex_attributes - the mutex attributes specified at create time |
---|
38 | * initial_lock - mutex initial lock or unlocked status |
---|
39 | * proxy_extract_callout - MP specific extract callout |
---|
40 | * |
---|
41 | * Output parameters: NONE |
---|
42 | */ |
---|
43 | |
---|
44 | void _CORE_mutex_Initialize( |
---|
45 | CORE_mutex_Control *the_mutex, |
---|
46 | Objects_Classes the_class, |
---|
47 | CORE_mutex_Attributes *the_mutex_attributes, |
---|
48 | unsigned32 initial_lock, |
---|
49 | Thread_queue_Extract_callout proxy_extract_callout |
---|
50 | ) |
---|
51 | { |
---|
52 | |
---|
53 | /* Add this to the RTEMS environment later ????????? |
---|
54 | rtems_assert( initial_lock == CORE_MUTEX_LOCKED || |
---|
55 | initial_lock == CORE_MUTEX_UNLOCKED ); |
---|
56 | */ |
---|
57 | |
---|
58 | the_mutex->Attributes = *the_mutex_attributes; |
---|
59 | the_mutex->lock = initial_lock; |
---|
60 | |
---|
61 | if ( initial_lock == CORE_MUTEX_LOCKED ) { |
---|
62 | the_mutex->nest_count = 1; |
---|
63 | the_mutex->holder = _Thread_Executing; |
---|
64 | the_mutex->holder_id = _Thread_Executing->Object.id; |
---|
65 | _Thread_Executing->resource_count++; |
---|
66 | } else { |
---|
67 | the_mutex->nest_count = 0; |
---|
68 | the_mutex->holder = NULL; |
---|
69 | the_mutex->holder_id = 0; |
---|
70 | } |
---|
71 | |
---|
72 | _Thread_queue_Initialize( |
---|
73 | &the_mutex->Wait_queue, |
---|
74 | the_class, |
---|
75 | _CORE_mutex_Is_priority( the_mutex_attributes ) ? |
---|
76 | THREAD_QUEUE_DISCIPLINE_PRIORITY : THREAD_QUEUE_DISCIPLINE_FIFO, |
---|
77 | STATES_WAITING_FOR_MUTEX, |
---|
78 | proxy_extract_callout, |
---|
79 | CORE_MUTEX_TIMEOUT |
---|
80 | ); |
---|
81 | } |
---|
82 | |
---|
83 | /*PAGE |
---|
84 | * |
---|
85 | * _CORE_mutex_Seize |
---|
86 | * |
---|
87 | * This routine attempts to allocate a mutex to the calling thread. |
---|
88 | * |
---|
89 | * Input parameters: |
---|
90 | * the_mutex - pointer to mutex control block |
---|
91 | * id - id of object to wait on |
---|
92 | * wait - TRUE if wait is allowed, FALSE otherwise |
---|
93 | * timeout - number of ticks to wait (0 means forever) |
---|
94 | * |
---|
95 | * Output parameters: NONE |
---|
96 | * |
---|
97 | * INTERRUPT LATENCY: |
---|
98 | * available |
---|
99 | * wait |
---|
100 | */ |
---|
101 | |
---|
102 | void _CORE_mutex_Seize( |
---|
103 | CORE_mutex_Control *the_mutex, |
---|
104 | Objects_Id id, |
---|
105 | boolean wait, |
---|
106 | Watchdog_Interval timeout |
---|
107 | ) |
---|
108 | { |
---|
109 | Thread_Control *executing; |
---|
110 | ISR_Level level; |
---|
111 | |
---|
112 | executing = _Thread_Executing; |
---|
113 | executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
114 | _ISR_Disable( level ); |
---|
115 | if ( ! _CORE_mutex_Is_locked( the_mutex ) ) { |
---|
116 | the_mutex->lock = CORE_MUTEX_LOCKED; |
---|
117 | the_mutex->holder = executing; |
---|
118 | the_mutex->holder_id = executing->Object.id; |
---|
119 | the_mutex->nest_count = 1; |
---|
120 | executing->resource_count++; |
---|
121 | _ISR_Enable( level ); |
---|
122 | switch ( the_mutex->Attributes.discipline ) { |
---|
123 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
124 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
125 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
126 | break; |
---|
127 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
128 | _Thread_Change_priority( |
---|
129 | executing, |
---|
130 | the_mutex->Attributes.priority_ceiling |
---|
131 | ); |
---|
132 | break; |
---|
133 | } |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | if ( _Objects_Are_ids_equal( |
---|
138 | _Thread_Executing->Object.id, the_mutex->holder_id ) ) { |
---|
139 | if ( _CORE_mutex_Is_nesting_allowed( &the_mutex->Attributes ) ) |
---|
140 | the_mutex->nest_count++; |
---|
141 | else |
---|
142 | executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; |
---|
143 | |
---|
144 | _ISR_Enable( level ); |
---|
145 | return; |
---|
146 | } |
---|
147 | |
---|
148 | if ( !wait ) { |
---|
149 | _ISR_Enable( level ); |
---|
150 | executing->Wait.return_code = CORE_MUTEX_STATUS_UNSATISFIED_NOWAIT; |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | _Thread_queue_Enter_critical_section( &the_mutex->Wait_queue ); |
---|
155 | executing->Wait.queue = &the_mutex->Wait_queue; |
---|
156 | executing->Wait.id = id; |
---|
157 | _ISR_Enable( level ); |
---|
158 | |
---|
159 | switch ( the_mutex->Attributes.discipline ) { |
---|
160 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
161 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
162 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
163 | break; |
---|
164 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
165 | if ( the_mutex->holder->current_priority > executing->current_priority ) { |
---|
166 | _Thread_Change_priority( |
---|
167 | the_mutex->holder, |
---|
168 | executing->current_priority |
---|
169 | ); |
---|
170 | } |
---|
171 | break; |
---|
172 | } |
---|
173 | |
---|
174 | _Thread_queue_Enqueue( &the_mutex->Wait_queue, timeout ); |
---|
175 | |
---|
176 | if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) { |
---|
177 | switch ( the_mutex->Attributes.discipline ) { |
---|
178 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
179 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
180 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
181 | break; |
---|
182 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
183 | _Thread_Change_priority( |
---|
184 | executing, |
---|
185 | the_mutex->Attributes.priority_ceiling |
---|
186 | ); |
---|
187 | break; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /* |
---|
193 | * _CORE_mutex_Surrender |
---|
194 | * |
---|
195 | * DESCRIPTION: |
---|
196 | * |
---|
197 | * This routine frees a unit to the mutex. If a task was blocked waiting for |
---|
198 | * a unit from this mutex, then that task will be readied and the unit |
---|
199 | * given to that task. Otherwise, the unit will be returned to the mutex. |
---|
200 | * |
---|
201 | * Input parameters: |
---|
202 | * the_mutex - the mutex to be flushed |
---|
203 | * id - id of parent mutex |
---|
204 | * api_mutex_mp_support - api dependent MP support actions |
---|
205 | * |
---|
206 | * Output parameters: |
---|
207 | * CORE_MUTEX_STATUS_SUCCESSFUL - if successful |
---|
208 | * core error code - if unsuccessful |
---|
209 | */ |
---|
210 | |
---|
211 | CORE_mutex_Status _CORE_mutex_Surrender( |
---|
212 | CORE_mutex_Control *the_mutex, |
---|
213 | Objects_Id id, |
---|
214 | CORE_mutex_API_mp_support_callout api_mutex_mp_support |
---|
215 | ) |
---|
216 | { |
---|
217 | Thread_Control *the_thread; |
---|
218 | Thread_Control *executing; |
---|
219 | |
---|
220 | executing = _Thread_Executing; |
---|
221 | |
---|
222 | if ( !_Objects_Are_ids_equal( |
---|
223 | _Thread_Executing->Object.id, the_mutex->holder_id ) ) |
---|
224 | return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE ); |
---|
225 | |
---|
226 | the_mutex->nest_count--; |
---|
227 | |
---|
228 | if ( the_mutex->nest_count != 0 ) |
---|
229 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
230 | |
---|
231 | _Thread_Executing->resource_count--; |
---|
232 | the_mutex->holder = NULL; |
---|
233 | the_mutex->holder_id = 0; |
---|
234 | |
---|
235 | /* |
---|
236 | * Whether or not someone is waiting for the mutex, an |
---|
237 | * inherited priority must be lowered if this is the last |
---|
238 | * mutex (i.e. resource) this task has. |
---|
239 | */ |
---|
240 | |
---|
241 | switch ( the_mutex->Attributes.discipline ) { |
---|
242 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
243 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
244 | break; |
---|
245 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
246 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
247 | if ( executing->resource_count == 0 && |
---|
248 | executing->real_priority != |
---|
249 | executing->current_priority ) { |
---|
250 | _Thread_Change_priority( executing, executing->real_priority ); |
---|
251 | } |
---|
252 | break; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { |
---|
257 | |
---|
258 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
259 | |
---|
260 | the_mutex->holder = NULL; |
---|
261 | the_mutex->holder_id = the_thread->Object.id; |
---|
262 | the_mutex->nest_count = 1; |
---|
263 | |
---|
264 | ( *api_mutex_mp_support)( the_thread, id ); |
---|
265 | |
---|
266 | } else { |
---|
267 | |
---|
268 | the_mutex->holder = the_thread; |
---|
269 | the_mutex->holder_id = the_thread->Object.id; |
---|
270 | the_thread->resource_count++; |
---|
271 | the_mutex->nest_count = 1; |
---|
272 | |
---|
273 | /* |
---|
274 | * No special action for priority inheritance or priority ceiling |
---|
275 | * because the_thread is guaranteed to be the highest priority |
---|
276 | * thread waiting for the mutex. |
---|
277 | */ |
---|
278 | } |
---|
279 | } else |
---|
280 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
281 | |
---|
282 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
283 | } |
---|
284 | |
---|
285 | /*PAGE |
---|
286 | * |
---|
287 | * _CORE_mutex_Flush |
---|
288 | * |
---|
289 | * This function a flushes the mutex's task wait queue. |
---|
290 | * |
---|
291 | * Input parameters: |
---|
292 | * the_mutex - the mutex to be flushed |
---|
293 | * remote_extract_callout - function to invoke remotely |
---|
294 | * status - status to pass to thread |
---|
295 | * |
---|
296 | * Output parameters: NONE |
---|
297 | */ |
---|
298 | |
---|
299 | void _CORE_mutex_Flush( |
---|
300 | CORE_mutex_Control *the_mutex, |
---|
301 | Thread_queue_Flush_callout remote_extract_callout, |
---|
302 | unsigned32 status |
---|
303 | ) |
---|
304 | { |
---|
305 | _Thread_queue_Flush( |
---|
306 | &the_mutex->Wait_queue, |
---|
307 | remote_extract_callout, |
---|
308 | status |
---|
309 | ); |
---|
310 | } |
---|