1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreISRLocks |
---|
5 | * |
---|
6 | * @brief ISR Locks |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2013 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 | } ISR_lock_Context; |
---|
72 | |
---|
73 | /** |
---|
74 | * @brief Defines an ISR lock. |
---|
75 | * |
---|
76 | * Do not add a ';' after this macro. |
---|
77 | * |
---|
78 | * @param _designator The designator for the interrupt lock. |
---|
79 | */ |
---|
80 | #if defined( RTEMS_SMP ) |
---|
81 | #define ISR_LOCK_DEFINE( _designator ) ISR_lock_Control _designator; |
---|
82 | #else |
---|
83 | #define ISR_LOCK_DEFINE( _designator ) |
---|
84 | #endif |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief Initializer for static initialization of ISR locks. |
---|
88 | * |
---|
89 | * @param _name The name for the interrupt lock. It must be a string. The |
---|
90 | * name is only used if profiling is enabled. |
---|
91 | */ |
---|
92 | #if defined( RTEMS_SMP ) |
---|
93 | #define ISR_LOCK_INITIALIZER( _name ) \ |
---|
94 | { SMP_LOCK_INITIALIZER( _name ) } |
---|
95 | #else |
---|
96 | #define ISR_LOCK_INITIALIZER( _name ) \ |
---|
97 | { } |
---|
98 | #endif |
---|
99 | |
---|
100 | /** |
---|
101 | * @brief Initializes an ISR lock. |
---|
102 | * |
---|
103 | * Concurrent initialization leads to unpredictable results. |
---|
104 | * |
---|
105 | * @param[in] _lock The ISR lock control. |
---|
106 | * @param[in] _name The name for the ISR lock. This name must be a |
---|
107 | * string persistent throughout the life time of this lock. The name is only |
---|
108 | * used if profiling is enabled. |
---|
109 | */ |
---|
110 | #if defined( RTEMS_SMP ) |
---|
111 | #define _ISR_lock_Initialize( _lock, _name ) \ |
---|
112 | _SMP_lock_Initialize( &( _lock )->Lock, _name ) |
---|
113 | #else |
---|
114 | #define _ISR_lock_Initialize( _lock, _name ) |
---|
115 | #endif |
---|
116 | |
---|
117 | /** |
---|
118 | * @brief Destroys an ISR lock. |
---|
119 | * |
---|
120 | * Concurrent destruction leads to unpredictable results. |
---|
121 | * |
---|
122 | * @param[in] _lock The ISR lock control. |
---|
123 | */ |
---|
124 | #if defined( RTEMS_SMP ) |
---|
125 | #define _ISR_lock_Destroy( _lock ) \ |
---|
126 | _SMP_lock_Destroy( &( _lock )->Lock ) |
---|
127 | #else |
---|
128 | #define _ISR_lock_Destroy( _lock ) |
---|
129 | #endif |
---|
130 | |
---|
131 | /** |
---|
132 | * @brief Acquires an ISR lock. |
---|
133 | * |
---|
134 | * Interrupts will be disabled. On SMP configurations this function acquires |
---|
135 | * an SMP lock. |
---|
136 | * |
---|
137 | * This function can be used in thread and interrupt context. |
---|
138 | * |
---|
139 | * @param[in] _lock The ISR lock control. |
---|
140 | * @param[in] context The local ISR lock context for an acquire and release |
---|
141 | * pair. |
---|
142 | * |
---|
143 | * @see _ISR_lock_Release_and_ISR_enable(). |
---|
144 | */ |
---|
145 | #if defined( RTEMS_SMP ) |
---|
146 | #define _ISR_lock_ISR_disable_and_acquire( _lock, _context ) \ |
---|
147 | _SMP_lock_ISR_disable_and_acquire( \ |
---|
148 | &( _lock )->Lock, \ |
---|
149 | &( _context )->Lock_context \ |
---|
150 | ) |
---|
151 | #else |
---|
152 | #define _ISR_lock_ISR_disable_and_acquire( _lock, _context ) \ |
---|
153 | _ISR_Disable( ( _context )->isr_level ) |
---|
154 | #endif |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief Releases an ISR lock. |
---|
158 | * |
---|
159 | * The interrupt status will be restored. On SMP configurations this function |
---|
160 | * releases an SMP lock. |
---|
161 | * |
---|
162 | * This function can be used in thread and interrupt context. |
---|
163 | * |
---|
164 | * @param[in] _lock The ISR lock control. |
---|
165 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
166 | * pair. |
---|
167 | * |
---|
168 | * @see _ISR_lock_ISR_disable_and_acquire(). |
---|
169 | */ |
---|
170 | #if defined( RTEMS_SMP ) |
---|
171 | #define _ISR_lock_Release_and_ISR_enable( _lock, _context ) \ |
---|
172 | _SMP_lock_Release_and_ISR_enable( \ |
---|
173 | &( _lock )->Lock, \ |
---|
174 | &( _context )->Lock_context \ |
---|
175 | ) |
---|
176 | #else |
---|
177 | #define _ISR_lock_Release_and_ISR_enable( _lock, _context ) \ |
---|
178 | _ISR_Enable( ( _context )->isr_level ) |
---|
179 | #endif |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Acquires an ISR lock inside an ISR disabled section. |
---|
183 | * |
---|
184 | * The interrupt status will remain unchanged. On SMP configurations this |
---|
185 | * function acquires an SMP lock. |
---|
186 | * |
---|
187 | * In case the executing context can be interrupted by higher priority |
---|
188 | * interrupts and these interrupts enter the critical section protected by this |
---|
189 | * lock, then the result is unpredictable. |
---|
190 | * |
---|
191 | * @param[in] _lock The ISR lock control. |
---|
192 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
193 | * pair. |
---|
194 | * |
---|
195 | * @see _ISR_lock_Release(). |
---|
196 | */ |
---|
197 | #if defined( RTEMS_SMP ) |
---|
198 | #define _ISR_lock_Acquire( _lock, _context ) \ |
---|
199 | _SMP_lock_Acquire( \ |
---|
200 | &( _lock )->Lock, \ |
---|
201 | &( _context )->Lock_context \ |
---|
202 | ) |
---|
203 | #else |
---|
204 | #define _ISR_lock_Acquire( _lock, _context ) |
---|
205 | #endif |
---|
206 | |
---|
207 | /** |
---|
208 | * @brief Releases an ISR lock inside an ISR disabled section. |
---|
209 | * |
---|
210 | * The interrupt status will remain unchanged. On SMP configurations this |
---|
211 | * function releases an SMP lock. |
---|
212 | * |
---|
213 | * @param[in] _lock The ISR lock control. |
---|
214 | * @param[in] _context The local ISR lock context for an acquire and release |
---|
215 | * pair. |
---|
216 | * |
---|
217 | * @see _ISR_lock_Acquire(). |
---|
218 | */ |
---|
219 | #if defined( RTEMS_SMP ) |
---|
220 | #define _ISR_lock_Release( _lock, _context ) \ |
---|
221 | _SMP_lock_Release( \ |
---|
222 | &( _lock )->Lock, \ |
---|
223 | &( _context )->Lock_context \ |
---|
224 | ) |
---|
225 | #else |
---|
226 | #define _ISR_lock_Release( _lock, _context ) |
---|
227 | #endif |
---|
228 | |
---|
229 | /** @} */ |
---|
230 | |
---|
231 | #ifdef __cplusplus |
---|
232 | } |
---|
233 | #endif |
---|
234 | |
---|
235 | #endif /* _RTEMS_SCORE_ISR_LOCK_H */ |
---|