1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreISRLocks |
---|
5 | * |
---|
6 | * @brief ISR Locks |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013-2015 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef _RTEMS_SCORE_ISR_LOCK_H |
---|
24 | #define _RTEMS_SCORE_ISR_LOCK_H |
---|
25 | |
---|
26 | #include <rtems/score/isrlevel.h> |
---|
27 | #include <rtems/score/smplock.h> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif |
---|
32 | |
---|
33 | /** |
---|
34 | * @defgroup ScoreISRLocks ISR Locks |
---|
35 | * |
---|
36 | * @ingroup ScoreISR |
---|
37 | * |
---|
38 | * @brief Low-level lock to protect critical sections accessed by threads and |
---|
39 | * interrupt service routines. |
---|
40 | * |
---|
41 | * On single processor configurations the ISR locks degrade to simple ISR |
---|
42 | * disable/enable sequences. No additional storage or objects are required. |
---|
43 | * |
---|
44 | * This synchronization primitive is supported on SMP configurations. Here SMP |
---|
45 | * locks are used. |
---|
46 | * |
---|
47 | * @{ |
---|
48 | */ |
---|
49 | |
---|
50 | /** |
---|
51 | * @brief ISR lock control. |
---|
52 | * |
---|
53 | * @warning Empty structures are implementation-defined in C. GCC gives them a |
---|
54 | * size of zero. In C++ empty structures have a non-zero size. |
---|
55 | */ |
---|
56 | typedef struct { |
---|
57 | #if defined( RTEMS_SMP ) |
---|
58 | SMP_lock_Control Lock; |
---|
59 | #endif |
---|
60 | } ISR_lock_Control; |
---|
61 | |
---|
62 | /** |
---|
63 | * @brief Local ISR lock context for acquire and release pairs. |
---|
64 | */ |
---|
65 | typedef struct { |
---|
66 | #if defined( RTEMS_SMP ) |
---|
67 | SMP_lock_Context Lock_context; |
---|
68 | #else |
---|
69 | ISR_Level isr_level; |
---|
70 | #endif |
---|
71 | #if defined( RTEMS_PROFILING ) |
---|
72 | /** |
---|
73 | * @brief The last interrupt disable instant in CPU counter ticks. |
---|
74 | */ |
---|
75 | CPU_Counter_ticks ISR_disable_instant; |
---|
76 | #endif |
---|
77 | } ISR_lock_Context; |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief Defines an ISR lock member. |
---|
81 | * |
---|
82 | * Do not add a ';' after this macro. |
---|
83 | * |
---|
84 | * @param _designator The designator for the interrupt lock. |
---|
85 | */ |
---|
86 | #if defined( RTEMS_SMP ) |
---|
87 | #define ISR_LOCK_MEMBER( _designator ) ISR_lock_Control _designator; |
---|
88 | #else |
---|
89 | #define ISR_LOCK_MEMBER( _designator ) |
---|
90 | #endif |
---|
91 | |
---|
92 | /** |
---|
93 | * @brief Declares an ISR lock variable. |
---|
94 | * |
---|
95 | * Do not add a ';' after this macro. |
---|
96 | * |
---|
97 | * @param _qualifier The qualifier for the interrupt lock, e.g. extern. |
---|
98 | * @param _designator The designator for the interrupt lock. |
---|
99 | */ |
---|
100 | #if defined( RTEMS_SMP ) |
---|
101 | #define ISR_LOCK_DECLARE( _qualifier, _designator ) \ |
---|
102 | _qualifier ISR_lock_Control _designator; |
---|
103 | #else |
---|
104 | #define ISR_LOCK_DECLARE( _qualifier, _designator ) |
---|
105 | #endif |
---|
106 | |
---|
107 | /** |
---|
108 | * @brief Defines an ISR lock variable. |
---|
109 | * |
---|
110 | * Do not add a ';' after this macro. |
---|
111 | * |
---|
112 | * @param _qualifier The qualifier for the interrupt lock, e.g. static. |
---|
113 | * @param _designator The designator for the interrupt lock. |
---|
114 | * @param _name The name for the interrupt lock. It must be a string. The |
---|
115 | * name is only used if profiling is enabled. |
---|
116 | */ |
---|
117 | #if defined( RTEMS_SMP ) |
---|
118 | #define ISR_LOCK_DEFINE( _qualifier, _designator, _name ) \ |
---|
119 | _qualifier ISR_lock_Control _designator = { SMP_LOCK_INITIALIZER( _name ) }; |
---|
120 | #else |
---|
121 | #define ISR_LOCK_DEFINE( _qualifier, _designator, _name ) |
---|
122 | #endif |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief Defines an ISR lock variable reference. |
---|
126 | * |
---|
127 | * Do not add a ';' after this macro. |
---|
128 | * |
---|
129 | * @param _designator The designator for the interrupt lock reference. |
---|
130 | * @param _target The target for the interrupt lock reference. |
---|
131 | */ |
---|
132 | #if defined( RTEMS_SMP ) |
---|
133 | #define ISR_LOCK_REFERENCE( _designator, _target ) \ |
---|
134 | ISR_lock_Control *_designator = _target; |
---|
135 | #else |
---|
136 | #define ISR_LOCK_REFERENCE( _designator, _target ) |
---|
137 | #endif |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief Initializer for static initialization of ISR locks. |
---|
141 | * |
---|
142 | * @param _name The name for the interrupt lock. It must be a string. The |
---|
143 | * name is only used if profiling is enabled. |
---|
144 | */ |
---|
145 | #if defined( RTEMS_SMP ) |
---|
146 | #define ISR_LOCK_INITIALIZER( _name ) \ |
---|
147 | { SMP_LOCK_INITIALIZER( _name ) } |
---|
148 | #else |
---|
149 | #define ISR_LOCK_INITIALIZER( _name ) \ |
---|
150 | { } |
---|
151 | #endif |
---|
152 | |
---|
153 | /** |
---|
154 | * @brief Sets the ISR level in the ISR lock context. |
---|
155 | * |
---|
156 | * @param[in] context The ISR lock context. |
---|
157 | * @param[in] level The ISR level. |
---|
158 | */ |
---|
159 | RTEMS_INLINE_ROUTINE void _ISR_lock_Context_set_level( |
---|
160 | ISR_lock_Context *context, |
---|
161 | ISR_Level level |
---|
162 | ) |
---|
163 | { |
---|
164 | #if defined( RTEMS_SMP ) |
---|
165 | context->Lock_context.isr_level = level; |
---|
166 | #else |
---|
167 | context->isr_level = level; |
---|
168 | #endif |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief Initializes an ISR lock. |
---|
173 | * |
---|
174 | * Concurrent initialization leads to unpredictable results. |
---|
175 | * |
---|
176 | * @param[in] _lock The ISR lock control. |
---|
177 | * @param[in] _name The name for the ISR lock. This name must be a |
---|
178 | * string persistent throughout the life time of this lock. The name is only |
---|
179 | * used if profiling is enabled. |
---|
180 | */ |
---|
181 | #if defined( RTEMS_SMP ) |
---|
182 | #define _ISR_lock_Initialize( _lock, _name ) \ |
---|
183 | _SMP_lock_Initialize( &( _lock )->Lock, _name ) |
---|
184 | #else |
---|
185 | #define _ISR_lock_Initialize( _lock, _name ) |
---|
186 | #endif |
---|
187 | |
---|
188 | /** |
---|
189 | * @brief Destroys an ISR lock. |
---|
190 | * |
---|
191 | * Concurrent destruction leads to unpredictable results. |
---|
192 | * |
---|
193 | * @param[in] _lock The ISR lock control. |
---|
194 | */ |
---|
195 | #if defined( RTEMS_SMP ) |
---|
196 | #define _ISR_lock_Destroy( _lock ) \ |
---|
197 | _SMP_lock_Destroy( &( _lock )->Lock ) |
---|
198 | #else |
---|
199 | #define _ISR_lock_Destroy( _lock ) |
---|
200 | #endif |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Acquires an ISR lock. |
---|
204 | * |
---|
205 | * Interrupts will be disabled. On SMP configurations this function acquires |
---|
206 | * an SMP lock. |
---|
207 | * |
---|
208 | * This function can be used in thread and interrupt context. |
---|
209 | * |
---|
210 | * @param[in] _lock The ISR lock control. |
---|
211 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
212 | * pair. |
---|
213 | * |
---|
214 | * @see _ISR_lock_Release_and_ISR_enable(). |
---|
215 | */ |
---|
216 | #if defined( RTEMS_SMP ) |
---|
217 | #define _ISR_lock_ISR_disable_and_acquire( _lock, _context ) \ |
---|
218 | _SMP_lock_ISR_disable_and_acquire( \ |
---|
219 | &( _lock )->Lock, \ |
---|
220 | &( _context )->Lock_context \ |
---|
221 | ) |
---|
222 | #else |
---|
223 | #define _ISR_lock_ISR_disable_and_acquire( _lock, _context ) \ |
---|
224 | _ISR_Local_disable( ( _context )->isr_level ) |
---|
225 | #endif |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief Releases an ISR lock. |
---|
229 | * |
---|
230 | * The interrupt status will be restored. On SMP configurations this function |
---|
231 | * releases an SMP lock. |
---|
232 | * |
---|
233 | * This function can be used in thread and interrupt context. |
---|
234 | * |
---|
235 | * @param[in] _lock The ISR lock control. |
---|
236 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
237 | * pair. |
---|
238 | * |
---|
239 | * @see _ISR_lock_ISR_disable_and_acquire(). |
---|
240 | */ |
---|
241 | #if defined( RTEMS_SMP ) |
---|
242 | #define _ISR_lock_Release_and_ISR_enable( _lock, _context ) \ |
---|
243 | _SMP_lock_Release_and_ISR_enable( \ |
---|
244 | &( _lock )->Lock, \ |
---|
245 | &( _context )->Lock_context \ |
---|
246 | ) |
---|
247 | #else |
---|
248 | #define _ISR_lock_Release_and_ISR_enable( _lock, _context ) \ |
---|
249 | _ISR_Local_enable( ( _context )->isr_level ) |
---|
250 | #endif |
---|
251 | |
---|
252 | /** |
---|
253 | * @brief Acquires an ISR lock inside an ISR disabled section. |
---|
254 | * |
---|
255 | * The interrupt status will remain unchanged. On SMP configurations this |
---|
256 | * function acquires an SMP lock. |
---|
257 | * |
---|
258 | * In case the executing context can be interrupted by higher priority |
---|
259 | * interrupts and these interrupts enter the critical section protected by this |
---|
260 | * lock, then the result is unpredictable. |
---|
261 | * |
---|
262 | * @param[in] _lock The ISR lock control. |
---|
263 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
264 | * pair. |
---|
265 | * |
---|
266 | * @see _ISR_lock_Release(). |
---|
267 | */ |
---|
268 | #if defined( RTEMS_SMP ) |
---|
269 | #define _ISR_lock_Acquire( _lock, _context ) \ |
---|
270 | _SMP_lock_Acquire( \ |
---|
271 | &( _lock )->Lock, \ |
---|
272 | &( _context )->Lock_context \ |
---|
273 | ) |
---|
274 | #else |
---|
275 | #define _ISR_lock_Acquire( _lock, _context ) \ |
---|
276 | (void) _context; |
---|
277 | #endif |
---|
278 | |
---|
279 | /** |
---|
280 | * @brief Releases an ISR lock inside an ISR disabled section. |
---|
281 | * |
---|
282 | * The interrupt status will remain unchanged. On SMP configurations this |
---|
283 | * function releases an SMP lock. |
---|
284 | * |
---|
285 | * @param[in] _lock The ISR lock control. |
---|
286 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
287 | * pair. |
---|
288 | * |
---|
289 | * @see _ISR_lock_Acquire(). |
---|
290 | */ |
---|
291 | #if defined( RTEMS_SMP ) |
---|
292 | #define _ISR_lock_Release( _lock, _context ) \ |
---|
293 | _SMP_lock_Release( \ |
---|
294 | &( _lock )->Lock, \ |
---|
295 | &( _context )->Lock_context \ |
---|
296 | ) |
---|
297 | #else |
---|
298 | #define _ISR_lock_Release( _lock, _context ) \ |
---|
299 | (void) _context; |
---|
300 | #endif |
---|
301 | |
---|
302 | /** |
---|
303 | * @brief Acquires an ISR lock inside an ISR disabled section (inline). |
---|
304 | * |
---|
305 | * @see _ISR_lock_Acquire(). |
---|
306 | */ |
---|
307 | #if defined( RTEMS_SMP ) |
---|
308 | #define _ISR_lock_Acquire_inline( _lock, _context ) \ |
---|
309 | _SMP_lock_Acquire_inline( \ |
---|
310 | &( _lock )->Lock, \ |
---|
311 | &( _context )->Lock_context \ |
---|
312 | ) |
---|
313 | #else |
---|
314 | #define _ISR_lock_Acquire_inline( _lock, _context ) \ |
---|
315 | (void) _context; |
---|
316 | #endif |
---|
317 | |
---|
318 | /** |
---|
319 | * @brief Releases an ISR lock inside an ISR disabled section (inline). |
---|
320 | * |
---|
321 | * @see _ISR_lock_Release(). |
---|
322 | */ |
---|
323 | #if defined( RTEMS_SMP ) |
---|
324 | #define _ISR_lock_Release_inline( _lock, _context ) \ |
---|
325 | _SMP_lock_Release_inline( \ |
---|
326 | &( _lock )->Lock, \ |
---|
327 | &( _context )->Lock_context \ |
---|
328 | ) |
---|
329 | #else |
---|
330 | #define _ISR_lock_Release_inline( _lock, _context ) \ |
---|
331 | (void) _context; |
---|
332 | #endif |
---|
333 | |
---|
334 | #if defined( RTEMS_DEBUG ) |
---|
335 | /** |
---|
336 | * @brief Returns true, if the ISR lock is owned by the current processor, |
---|
337 | * otherwise false. |
---|
338 | * |
---|
339 | * On uni-processor configurations, this function returns true, if interrupts |
---|
340 | * are disabled, otherwise false. |
---|
341 | * |
---|
342 | * @param[in] _lock The ISR lock control. |
---|
343 | */ |
---|
344 | #if defined( RTEMS_SMP ) |
---|
345 | #define _ISR_lock_Is_owner( _lock ) \ |
---|
346 | _SMP_lock_Is_owner( &( _lock )->Lock ) |
---|
347 | #else |
---|
348 | #define _ISR_lock_Is_owner( _lock ) \ |
---|
349 | ( _ISR_Get_level() != 0 ) |
---|
350 | #endif |
---|
351 | #endif |
---|
352 | |
---|
353 | /** |
---|
354 | * @brief Flashes an ISR lock. |
---|
355 | * |
---|
356 | * On uni-processor configurations this a simple _ISR_Local_flash(). On SMP |
---|
357 | * configurations this function releases an SMP lock, restores the interrupt |
---|
358 | * status, then disables interrupts and acquires the SMP lock again. |
---|
359 | * |
---|
360 | * This function can be used in thread and interrupt context. |
---|
361 | * |
---|
362 | * @param[in] _lock The ISR lock control. |
---|
363 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
364 | * pair. |
---|
365 | * |
---|
366 | * @see _ISR_lock_ISR_disable_and_acquire() and |
---|
367 | * _ISR_lock_Release_and_ISR_enable(). |
---|
368 | */ |
---|
369 | #if defined( RTEMS_SMP ) |
---|
370 | #define _ISR_lock_Flash( _lock, _context ) \ |
---|
371 | _SMP_lock_Release_and_ISR_enable( \ |
---|
372 | &( _lock )->Lock, \ |
---|
373 | &( _context )->Lock_context \ |
---|
374 | ); \ |
---|
375 | _SMP_lock_ISR_disable_and_acquire( \ |
---|
376 | &( _lock )->Lock, \ |
---|
377 | &( _context )->Lock_context \ |
---|
378 | ) |
---|
379 | #else |
---|
380 | #define _ISR_lock_Flash( _lock, _context ) \ |
---|
381 | _ISR_Local_flash( ( _context )->isr_level ) |
---|
382 | #endif |
---|
383 | |
---|
384 | #if defined( RTEMS_PROFILING ) |
---|
385 | #define _ISR_lock_ISR_disable_profile( _context ) \ |
---|
386 | ( _context )->ISR_disable_instant = _CPU_Counter_read(); |
---|
387 | #else |
---|
388 | #define _ISR_lock_ISR_disable_profile( _context ) |
---|
389 | #endif |
---|
390 | |
---|
391 | /** |
---|
392 | * @brief Disables interrupts and saves the previous interrupt state in the ISR |
---|
393 | * lock context. |
---|
394 | * |
---|
395 | * This function can be used in thread and interrupt context. |
---|
396 | * |
---|
397 | * @param[in] _context The local ISR lock context to store the interrupt state. |
---|
398 | * |
---|
399 | * @see _ISR_lock_ISR_enable(). |
---|
400 | */ |
---|
401 | #if defined( RTEMS_SMP ) |
---|
402 | #define _ISR_lock_ISR_disable( _context ) \ |
---|
403 | do { \ |
---|
404 | _ISR_Local_disable( ( _context )->Lock_context.isr_level ); \ |
---|
405 | _ISR_lock_ISR_disable_profile( _context ) \ |
---|
406 | } while ( 0 ) |
---|
407 | #else |
---|
408 | #define _ISR_lock_ISR_disable( _context ) \ |
---|
409 | do { \ |
---|
410 | _ISR_Local_disable( ( _context )->isr_level ); \ |
---|
411 | _ISR_lock_ISR_disable_profile( _context ) \ |
---|
412 | } while ( 0 ) |
---|
413 | #endif |
---|
414 | |
---|
415 | /** |
---|
416 | * @brief Restores the saved interrupt state of the ISR lock context. |
---|
417 | * |
---|
418 | * This function can be used in thread and interrupt context. |
---|
419 | * |
---|
420 | * @param[in] _context The local ISR lock context containing the saved |
---|
421 | * interrupt state. |
---|
422 | * |
---|
423 | * @see _ISR_lock_ISR_disable(). |
---|
424 | */ |
---|
425 | #if defined( RTEMS_SMP ) |
---|
426 | #define _ISR_lock_ISR_enable( _context ) \ |
---|
427 | _ISR_Local_enable( ( _context )->Lock_context.isr_level ) |
---|
428 | #else |
---|
429 | #define _ISR_lock_ISR_enable( _context ) \ |
---|
430 | _ISR_Local_enable( ( _context )->isr_level ) |
---|
431 | #endif |
---|
432 | |
---|
433 | /** @} */ |
---|
434 | |
---|
435 | #ifdef __cplusplus |
---|
436 | } |
---|
437 | #endif |
---|
438 | |
---|
439 | #endif /* _RTEMS_SCORE_ISR_LOCK_H */ |
---|