1 | /** |
---|
2 | * @file rtems/score/cpustdatomic.h |
---|
3 | * |
---|
4 | * This include file defines the generic data struct and implementation |
---|
5 | * based on stdatomic.h for all the support architectures. You should not |
---|
6 | * include this header file directly, because it will be used by atomic.h |
---|
7 | * which should be included by score components |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 2013 Deng Hengyi. |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_GENERAL_STDATOMIC_CPU_H_ |
---|
19 | #define _RTEMS_SCORE_GENERAL_STDATOMIC_CPU_H_ |
---|
20 | |
---|
21 | #include <stdatomic.h> |
---|
22 | #include <rtems/score/types.h> |
---|
23 | |
---|
24 | #ifdef __cplusplus |
---|
25 | extern "C" { |
---|
26 | #endif |
---|
27 | |
---|
28 | /** |
---|
29 | * @defgroup RTEMS general stdatomic data type and implementation. |
---|
30 | * |
---|
31 | */ |
---|
32 | |
---|
33 | /**@{*/ |
---|
34 | |
---|
35 | /** |
---|
36 | * @brief atomic operation unsigned integer type |
---|
37 | */ |
---|
38 | typedef atomic_uint_fast32_t Atomic_Uint; |
---|
39 | |
---|
40 | /** |
---|
41 | * @brief atomic operation unsigned integer the size of a pointer type |
---|
42 | */ |
---|
43 | typedef atomic_uintptr_t Atomic_Pointer; |
---|
44 | |
---|
45 | /** |
---|
46 | * @brief atomic operation flag type |
---|
47 | */ |
---|
48 | typedef atomic_flag Atomic_Flag; |
---|
49 | |
---|
50 | /** |
---|
51 | * @brief the enumeration Atomic_Memory_barrier specifies the detailed regular |
---|
52 | * memory synchronization operations used in the atomic operation API |
---|
53 | * definitions. |
---|
54 | */ |
---|
55 | typedef enum { |
---|
56 | /** no operation orders memory. */ |
---|
57 | ATOMIC_ORDER_RELAXED = memory_order_relaxed, |
---|
58 | /** a load operation performs an acquire operation on the affected memory |
---|
59 | * location. This flag guarantees that the effects of load operation are |
---|
60 | * completed before the effects of any later data accesses. |
---|
61 | */ |
---|
62 | ATOMIC_ORDER_ACQUIRE = memory_order_acquire, |
---|
63 | /** a store operation performs a release operation on the affected memory |
---|
64 | * location. This flag guarantee that all effects of all previous data |
---|
65 | * accesses are completed before the store operation takes place. |
---|
66 | */ |
---|
67 | ATOMIC_ORDER_RELEASE = memory_order_release |
---|
68 | } Atomic_Order; |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief Atomically load an atomic type value from atomic object. |
---|
72 | * |
---|
73 | * @param object an atomic type pointer of object. |
---|
74 | * @param order a type of Atomic_Order. |
---|
75 | * |
---|
76 | * The order shall not be ATOMIC_ORDER_RELEASE. |
---|
77 | */ |
---|
78 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Load_uint( |
---|
79 | volatile Atomic_Uint *object, |
---|
80 | Atomic_Order order |
---|
81 | ) |
---|
82 | { |
---|
83 | return atomic_load_explicit( object, order ); |
---|
84 | } |
---|
85 | |
---|
86 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Load_ptr( |
---|
87 | volatile Atomic_Pointer *object, |
---|
88 | Atomic_Order order |
---|
89 | ) |
---|
90 | { |
---|
91 | return atomic_load_explicit( object, order ); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * @brief Atomically store an atomic type value into a atomic object. |
---|
96 | * |
---|
97 | * @param object an atomic type pointer of object. |
---|
98 | * @param value a value to be stored into object. |
---|
99 | * @param order a type of Atomic_Order. |
---|
100 | * |
---|
101 | * The order shall not be ATOMIC_ORDER_ACQUIRE. |
---|
102 | */ |
---|
103 | RTEMS_INLINE_ROUTINE void _CPU_atomic_Store_uint( |
---|
104 | volatile Atomic_Uint *object, |
---|
105 | uint_fast32_t value, |
---|
106 | Atomic_Order order |
---|
107 | ) |
---|
108 | { |
---|
109 | atomic_store_explicit( object, value, order ); |
---|
110 | } |
---|
111 | |
---|
112 | RTEMS_INLINE_ROUTINE void _CPU_atomic_Store_ptr( |
---|
113 | volatile Atomic_Pointer *object, |
---|
114 | uintptr_t value, |
---|
115 | Atomic_Order order |
---|
116 | ) |
---|
117 | { |
---|
118 | atomic_store_explicit( object, value, order ); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @brief Atomically load-add-store an atomic type value into object |
---|
123 | * |
---|
124 | * @param object a atomic type pointer of object. |
---|
125 | * @param value a value to be add and store into object. |
---|
126 | * @param order a type of Atomic_Order. |
---|
127 | * |
---|
128 | * @retval a result value after add ops. |
---|
129 | */ |
---|
130 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Fetch_add_uint( |
---|
131 | volatile Atomic_Uint *object, |
---|
132 | uint_fast32_t value, |
---|
133 | Atomic_Order order |
---|
134 | ) |
---|
135 | { |
---|
136 | return atomic_fetch_add_explicit( object, value, order ); |
---|
137 | } |
---|
138 | |
---|
139 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Fetch_add_ptr( |
---|
140 | volatile Atomic_Pointer *object, |
---|
141 | uintptr_t value, |
---|
142 | Atomic_Order order |
---|
143 | ) |
---|
144 | { |
---|
145 | return atomic_fetch_add_explicit( object, value, order ); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * @brief Atomically load-sub-store an atomic type value into object |
---|
150 | * |
---|
151 | * @param object a atomic type pointer of object. |
---|
152 | * @param value a value to be sub and store into object. |
---|
153 | * @param order a type of Atomic_Order. |
---|
154 | * |
---|
155 | * @retval a result value after sub ops. |
---|
156 | */ |
---|
157 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Fetch_sub_uint( |
---|
158 | volatile Atomic_Uint *object, |
---|
159 | uint_fast32_t value, |
---|
160 | Atomic_Order order |
---|
161 | ) |
---|
162 | { |
---|
163 | return atomic_fetch_sub_explicit( object, value, order ); |
---|
164 | } |
---|
165 | |
---|
166 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Fetch_sub_ptr( |
---|
167 | volatile Atomic_Pointer *object, |
---|
168 | uintptr_t value, |
---|
169 | Atomic_Order order |
---|
170 | ) |
---|
171 | { |
---|
172 | return atomic_fetch_sub_explicit( object, value, order ); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * @brief Atomically load-or-store an atomic type value into object |
---|
177 | * |
---|
178 | * @param object a atomic type pointer of object. |
---|
179 | * @param value a value to be or and store into object. |
---|
180 | * @param order a type of Atomic_Order. |
---|
181 | * |
---|
182 | * @retval a result value after or ops. |
---|
183 | */ |
---|
184 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Fetch_or_uint( |
---|
185 | volatile Atomic_Uint *object, |
---|
186 | uint_fast32_t value, |
---|
187 | Atomic_Order order |
---|
188 | ) |
---|
189 | { |
---|
190 | return atomic_fetch_or_explicit( object, value, order ); |
---|
191 | } |
---|
192 | |
---|
193 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Fetch_or_ptr( |
---|
194 | volatile Atomic_Pointer *object, |
---|
195 | uintptr_t value, |
---|
196 | Atomic_Order order |
---|
197 | ) |
---|
198 | { |
---|
199 | return atomic_fetch_or_explicit( object, value, order ); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Atomically load-and-store an atomic type value into object |
---|
204 | * |
---|
205 | * @param object a atomic type pointer of object. |
---|
206 | * @param value a value to be and and store into object. |
---|
207 | * @param order a type of Atomic_Order. |
---|
208 | * |
---|
209 | * @retval a result value after and ops. |
---|
210 | */ |
---|
211 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Fetch_and_uint( |
---|
212 | volatile Atomic_Uint *object, |
---|
213 | uint_fast32_t value, |
---|
214 | Atomic_Order order |
---|
215 | ) |
---|
216 | { |
---|
217 | return atomic_fetch_and_explicit( object, value, order ); |
---|
218 | } |
---|
219 | |
---|
220 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Fetch_and_ptr( |
---|
221 | volatile Atomic_Pointer *object, |
---|
222 | uintptr_t value, |
---|
223 | Atomic_Order order |
---|
224 | ) |
---|
225 | { |
---|
226 | return atomic_fetch_and_explicit( object, value, order ); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * @brief Atomically exchange an atomic type value into object |
---|
231 | * |
---|
232 | * @param object a atomic type pointer of object. |
---|
233 | * @param value a value to exchange and and store into object. |
---|
234 | * @param order a type of Atomic_Order. |
---|
235 | * |
---|
236 | * @retval a result value after exchange ops. |
---|
237 | */ |
---|
238 | RTEMS_INLINE_ROUTINE uint_fast32_t _CPU_atomic_Exchange_uint( |
---|
239 | volatile Atomic_Uint *object, |
---|
240 | uint_fast32_t value, |
---|
241 | Atomic_Order order |
---|
242 | ) |
---|
243 | { |
---|
244 | return atomic_exchange_explicit( object, value, order ); |
---|
245 | } |
---|
246 | |
---|
247 | RTEMS_INLINE_ROUTINE uintptr_t _CPU_atomic_Exchange_ptr( |
---|
248 | volatile Atomic_Pointer *object, |
---|
249 | uintptr_t value, |
---|
250 | Atomic_Order order |
---|
251 | ) |
---|
252 | { |
---|
253 | return atomic_exchange_explicit( object, value, order ); |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * @brief Atomically compare the value stored at object with a |
---|
258 | * old_value and if the two values are equal, update the value of a |
---|
259 | * address with a new_value |
---|
260 | * |
---|
261 | * @param object a atomic type pointer of object. |
---|
262 | * @param old_value pointer of a value. |
---|
263 | * @param new_value a atomic type value. |
---|
264 | * @param order_succ a type of Atomic_Order for successful exchange. |
---|
265 | * @param order_fail a type of Atomic_Order for failed exchange. |
---|
266 | * |
---|
267 | * @retval true if the compare exchange successully. |
---|
268 | * @retval false if the compare exchange failed. |
---|
269 | */ |
---|
270 | RTEMS_INLINE_ROUTINE bool _CPU_atomic_Compare_exchange_uint( |
---|
271 | volatile Atomic_Uint *object, |
---|
272 | uint_fast32_t *old_value, |
---|
273 | uint_fast32_t new_value, |
---|
274 | Atomic_Order order_succ, |
---|
275 | Atomic_Order order_fail |
---|
276 | ) |
---|
277 | { |
---|
278 | return atomic_compare_exchange_strong_explicit( object, old_value, |
---|
279 | new_value, order_succ, order_fail ); |
---|
280 | } |
---|
281 | |
---|
282 | RTEMS_INLINE_ROUTINE bool _CPU_atomic_Compare_exchange_ptr( |
---|
283 | volatile Atomic_Pointer *object, |
---|
284 | uintptr_t *old_value, |
---|
285 | uintptr_t new_value, |
---|
286 | Atomic_Order order_succ, |
---|
287 | Atomic_Order order_fail |
---|
288 | ) |
---|
289 | { |
---|
290 | return atomic_compare_exchange_strong_explicit( object, old_value, |
---|
291 | new_value, order_succ, order_fail ); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * @brief Atomically clear the value of an atomic flag type object. |
---|
296 | * |
---|
297 | * @param[in, out] object an atomic flag type pointer of object. |
---|
298 | * @param order a type of Atomic_Order. |
---|
299 | * |
---|
300 | */ |
---|
301 | RTEMS_INLINE_ROUTINE void _CPU_atomic_Clear_flag( |
---|
302 | volatile Atomic_Flag *object, |
---|
303 | Atomic_Order order |
---|
304 | ) |
---|
305 | { |
---|
306 | return atomic_flag_clear_explicit( object, order ); |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * @brief Atomically test and clear the value of an atomic flag type object. |
---|
311 | * |
---|
312 | * @param[in, out] object an atomic flag type pointer of object. |
---|
313 | * @param order a type of Atomic_Order. |
---|
314 | * |
---|
315 | * @retval true if the test and set successully. |
---|
316 | * @retval false if the test and set failed. |
---|
317 | */ |
---|
318 | RTEMS_INLINE_ROUTINE bool _CPU_atomic_Test_set_flag( |
---|
319 | volatile Atomic_Flag *object, |
---|
320 | Atomic_Order order |
---|
321 | ) |
---|
322 | { |
---|
323 | return atomic_flag_test_and_set_explicit( object, order ); |
---|
324 | } |
---|
325 | |
---|
326 | #ifdef __cplusplus |
---|
327 | } |
---|
328 | #endif |
---|
329 | |
---|
330 | /**@}*/ |
---|
331 | #endif |
---|
332 | /* end of include file */ |
---|