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.org/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 <stdbool.h> |
---|
23 | #include <stdint.h> |
---|
24 | |
---|
25 | #ifdef __cplusplus |
---|
26 | extern "C" { |
---|
27 | #endif |
---|
28 | |
---|
29 | /** |
---|
30 | * @defgroup RTEMS general stdatomic data type and implementation. |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | /**@{*/ |
---|
35 | |
---|
36 | /** |
---|
37 | * @brief atomic operation unsigned integer type |
---|
38 | */ |
---|
39 | typedef atomic_uint Atomic_Uint; |
---|
40 | |
---|
41 | /** |
---|
42 | * @brief atomic operation unsigned long integer type |
---|
43 | */ |
---|
44 | typedef atomic_ulong Atomic_Ulong; |
---|
45 | |
---|
46 | /** |
---|
47 | * @brief atomic operation unsigned integer the size of a pointer type |
---|
48 | */ |
---|
49 | typedef atomic_uintptr_t Atomic_Pointer; |
---|
50 | |
---|
51 | /** |
---|
52 | * @brief atomic operation flag type |
---|
53 | */ |
---|
54 | typedef atomic_flag Atomic_Flag; |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief the enumeration Atomic_Memory_barrier specifies the detailed regular |
---|
58 | * memory synchronization operations used in the atomic operation API |
---|
59 | * definitions. |
---|
60 | */ |
---|
61 | typedef enum { |
---|
62 | /** no operation orders memory. */ |
---|
63 | ATOMIC_ORDER_RELAXED = memory_order_relaxed, |
---|
64 | /** a load operation performs an acquire operation on the affected memory |
---|
65 | * location. This flag guarantees that the effects of load operation are |
---|
66 | * completed before the effects of any later data accesses. |
---|
67 | */ |
---|
68 | ATOMIC_ORDER_ACQUIRE = memory_order_acquire, |
---|
69 | /** a store operation performs a release operation on the affected memory |
---|
70 | * location. This flag guarantee that all effects of all previous data |
---|
71 | * accesses are completed before the store operation takes place. |
---|
72 | */ |
---|
73 | ATOMIC_ORDER_RELEASE = memory_order_release |
---|
74 | } Atomic_Order; |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * @brief atomic data initializer for static initialization. |
---|
79 | */ |
---|
80 | #define CPU_ATOMIC_INITIALIZER_UINT(value) ATOMIC_VAR_INIT(value) |
---|
81 | #define CPU_ATOMIC_INITIALIZER_ULONG(value) ATOMIC_VAR_INIT(value) |
---|
82 | #define CPU_ATOMIC_INITIALIZER_PTR(pointer) \ |
---|
83 | ATOMIC_VAR_INIT((uintptr_t) pointer) |
---|
84 | |
---|
85 | #define CPU_ATOMIC_INITIALIZER_FLAG ATOMIC_FLAG_INIT |
---|
86 | |
---|
87 | static inline void _CPU_atomic_Fence( |
---|
88 | Atomic_Order order |
---|
89 | ) |
---|
90 | { |
---|
91 | atomic_thread_fence( (memory_order) order ); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * @brief Initializes 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 | */ |
---|
100 | static inline void _CPU_atomic_Init_uint( |
---|
101 | volatile Atomic_Uint *object, |
---|
102 | unsigned int value |
---|
103 | ) |
---|
104 | { |
---|
105 | atomic_init( object, value ); |
---|
106 | } |
---|
107 | |
---|
108 | static inline void _CPU_atomic_Init_ulong( |
---|
109 | volatile Atomic_Ulong *object, |
---|
110 | unsigned long value |
---|
111 | ) |
---|
112 | { |
---|
113 | atomic_init( object, value ); |
---|
114 | } |
---|
115 | |
---|
116 | static inline void _CPU_atomic_Init_ptr( |
---|
117 | volatile Atomic_Pointer *object, |
---|
118 | void *pointer |
---|
119 | ) |
---|
120 | { |
---|
121 | atomic_init( object, (uintptr_t) pointer ); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief Atomically load an atomic type value from atomic object. |
---|
126 | * |
---|
127 | * @param object an atomic type pointer of object. |
---|
128 | * @param order a type of Atomic_Order. |
---|
129 | * |
---|
130 | * The order shall not be ATOMIC_ORDER_RELEASE. |
---|
131 | */ |
---|
132 | static inline unsigned int _CPU_atomic_Load_uint( |
---|
133 | volatile Atomic_Uint *object, |
---|
134 | Atomic_Order order |
---|
135 | ) |
---|
136 | { |
---|
137 | return atomic_load_explicit( object, (memory_order) order ); |
---|
138 | } |
---|
139 | |
---|
140 | static inline unsigned long _CPU_atomic_Load_ulong( |
---|
141 | volatile Atomic_Ulong *object, |
---|
142 | Atomic_Order order |
---|
143 | ) |
---|
144 | { |
---|
145 | return atomic_load_explicit( object, (memory_order) order ); |
---|
146 | } |
---|
147 | |
---|
148 | static inline void *_CPU_atomic_Load_ptr( |
---|
149 | volatile Atomic_Pointer *object, |
---|
150 | Atomic_Order order |
---|
151 | ) |
---|
152 | { |
---|
153 | return (void *) atomic_load_explicit( object, (memory_order) order ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief Atomically store an atomic type value into a atomic object. |
---|
158 | * |
---|
159 | * @param object an atomic type pointer of object. |
---|
160 | * @param value a value to be stored into object. |
---|
161 | * @param order a type of Atomic_Order. |
---|
162 | * |
---|
163 | * The order shall not be ATOMIC_ORDER_ACQUIRE. |
---|
164 | */ |
---|
165 | static inline void _CPU_atomic_Store_uint( |
---|
166 | volatile Atomic_Uint *object, |
---|
167 | unsigned int value, |
---|
168 | Atomic_Order order |
---|
169 | ) |
---|
170 | { |
---|
171 | atomic_store_explicit( object, value, (memory_order) order ); |
---|
172 | } |
---|
173 | |
---|
174 | static inline void _CPU_atomic_Store_ulong( |
---|
175 | volatile Atomic_Ulong *object, |
---|
176 | unsigned long value, |
---|
177 | Atomic_Order order |
---|
178 | ) |
---|
179 | { |
---|
180 | atomic_store_explicit( object, value, (memory_order) order ); |
---|
181 | } |
---|
182 | |
---|
183 | static inline void _CPU_atomic_Store_ptr( |
---|
184 | volatile Atomic_Pointer *object, |
---|
185 | void *pointer, |
---|
186 | Atomic_Order order |
---|
187 | ) |
---|
188 | { |
---|
189 | atomic_store_explicit( object, pointer, (memory_order) order ); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief Atomically load-add-store an atomic type value into object |
---|
194 | * |
---|
195 | * @param object a atomic type pointer of object. |
---|
196 | * @param value a value to be add and store into object. |
---|
197 | * @param order a type of Atomic_Order. |
---|
198 | * |
---|
199 | * @retval a result value before add ops. |
---|
200 | */ |
---|
201 | static inline unsigned int _CPU_atomic_Fetch_add_uint( |
---|
202 | volatile Atomic_Uint *object, |
---|
203 | unsigned int value, |
---|
204 | Atomic_Order order |
---|
205 | ) |
---|
206 | { |
---|
207 | return atomic_fetch_add_explicit( object, value, (memory_order) order ); |
---|
208 | } |
---|
209 | |
---|
210 | static inline unsigned long _CPU_atomic_Fetch_add_ulong( |
---|
211 | volatile Atomic_Ulong *object, |
---|
212 | unsigned long value, |
---|
213 | Atomic_Order order |
---|
214 | ) |
---|
215 | { |
---|
216 | return atomic_fetch_add_explicit( object, value, (memory_order) order ); |
---|
217 | } |
---|
218 | |
---|
219 | static inline uintptr_t _CPU_atomic_Fetch_add_ptr( |
---|
220 | volatile Atomic_Pointer *object, |
---|
221 | uintptr_t value, |
---|
222 | Atomic_Order order |
---|
223 | ) |
---|
224 | { |
---|
225 | return atomic_fetch_add_explicit( object, value, (memory_order) order ); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * @brief Atomically load-sub-store an atomic type value into object |
---|
230 | * |
---|
231 | * @param object a atomic type pointer of object. |
---|
232 | * @param value a value to be sub and store into object. |
---|
233 | * @param order a type of Atomic_Order. |
---|
234 | * |
---|
235 | * @retval a result value before sub ops. |
---|
236 | */ |
---|
237 | static inline unsigned int _CPU_atomic_Fetch_sub_uint( |
---|
238 | volatile Atomic_Uint *object, |
---|
239 | unsigned int value, |
---|
240 | Atomic_Order order |
---|
241 | ) |
---|
242 | { |
---|
243 | return atomic_fetch_sub_explicit( object, value, (memory_order) order ); |
---|
244 | } |
---|
245 | |
---|
246 | static inline unsigned long _CPU_atomic_Fetch_sub_ulong( |
---|
247 | volatile Atomic_Ulong *object, |
---|
248 | unsigned long value, |
---|
249 | Atomic_Order order |
---|
250 | ) |
---|
251 | { |
---|
252 | return atomic_fetch_sub_explicit( object, value, (memory_order) order ); |
---|
253 | } |
---|
254 | |
---|
255 | static inline uintptr_t _CPU_atomic_Fetch_sub_ptr( |
---|
256 | volatile Atomic_Pointer *object, |
---|
257 | uintptr_t value, |
---|
258 | Atomic_Order order |
---|
259 | ) |
---|
260 | { |
---|
261 | return atomic_fetch_sub_explicit( object, value, (memory_order) order ); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * @brief Atomically load-or-store an atomic type value into object |
---|
266 | * |
---|
267 | * @param object a atomic type pointer of object. |
---|
268 | * @param value a value to be or and store into object. |
---|
269 | * @param order a type of Atomic_Order. |
---|
270 | * |
---|
271 | * @retval a result value before or ops. |
---|
272 | */ |
---|
273 | static inline unsigned int _CPU_atomic_Fetch_or_uint( |
---|
274 | volatile Atomic_Uint *object, |
---|
275 | unsigned int value, |
---|
276 | Atomic_Order order |
---|
277 | ) |
---|
278 | { |
---|
279 | return atomic_fetch_or_explicit( object, value, (memory_order) order ); |
---|
280 | } |
---|
281 | |
---|
282 | static inline unsigned long _CPU_atomic_Fetch_or_ulong( |
---|
283 | volatile Atomic_Ulong *object, |
---|
284 | unsigned long value, |
---|
285 | Atomic_Order order |
---|
286 | ) |
---|
287 | { |
---|
288 | return atomic_fetch_or_explicit( object, value, (memory_order) order ); |
---|
289 | } |
---|
290 | |
---|
291 | static inline uintptr_t _CPU_atomic_Fetch_or_ptr( |
---|
292 | volatile Atomic_Pointer *object, |
---|
293 | uintptr_t value, |
---|
294 | Atomic_Order order |
---|
295 | ) |
---|
296 | { |
---|
297 | return atomic_fetch_or_explicit( object, value, (memory_order) order ); |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * @brief Atomically load-and-store an atomic type value into object |
---|
302 | * |
---|
303 | * @param object a atomic type pointer of object. |
---|
304 | * @param value a value to be and and store into object. |
---|
305 | * @param order a type of Atomic_Order. |
---|
306 | * |
---|
307 | * @retval a result value before and ops. |
---|
308 | */ |
---|
309 | static inline unsigned int _CPU_atomic_Fetch_and_uint( |
---|
310 | volatile Atomic_Uint *object, |
---|
311 | unsigned int value, |
---|
312 | Atomic_Order order |
---|
313 | ) |
---|
314 | { |
---|
315 | return atomic_fetch_and_explicit( object, value, (memory_order) order ); |
---|
316 | } |
---|
317 | |
---|
318 | static inline unsigned long _CPU_atomic_Fetch_and_ulong( |
---|
319 | volatile Atomic_Ulong *object, |
---|
320 | unsigned long value, |
---|
321 | Atomic_Order order |
---|
322 | ) |
---|
323 | { |
---|
324 | return atomic_fetch_and_explicit( object, value, (memory_order) order ); |
---|
325 | } |
---|
326 | |
---|
327 | static inline uintptr_t _CPU_atomic_Fetch_and_ptr( |
---|
328 | volatile Atomic_Pointer *object, |
---|
329 | uintptr_t value, |
---|
330 | Atomic_Order order |
---|
331 | ) |
---|
332 | { |
---|
333 | return atomic_fetch_and_explicit( object, value, (memory_order) order ); |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * @brief Atomically exchange an atomic type value into object |
---|
338 | * |
---|
339 | * @param object a atomic type pointer of object. |
---|
340 | * @param value a value to exchange and and store into object. |
---|
341 | * @param order a type of Atomic_Order. |
---|
342 | * |
---|
343 | * @retval a result value before exchange ops. |
---|
344 | */ |
---|
345 | static inline unsigned int _CPU_atomic_Exchange_uint( |
---|
346 | volatile Atomic_Uint *object, |
---|
347 | unsigned int value, |
---|
348 | Atomic_Order order |
---|
349 | ) |
---|
350 | { |
---|
351 | return atomic_exchange_explicit( object, value, (memory_order) order ); |
---|
352 | } |
---|
353 | |
---|
354 | static inline unsigned long _CPU_atomic_Exchange_ulong( |
---|
355 | volatile Atomic_Ulong *object, |
---|
356 | unsigned long value, |
---|
357 | Atomic_Order order |
---|
358 | ) |
---|
359 | { |
---|
360 | return atomic_exchange_explicit( object, value, (memory_order) order ); |
---|
361 | } |
---|
362 | |
---|
363 | static inline void *_CPU_atomic_Exchange_ptr( |
---|
364 | volatile Atomic_Pointer *object, |
---|
365 | void *pointer, |
---|
366 | Atomic_Order order |
---|
367 | ) |
---|
368 | { |
---|
369 | return (void *) atomic_exchange_explicit( |
---|
370 | object, |
---|
371 | (uintptr_t) pointer, |
---|
372 | (memory_order) order |
---|
373 | ); |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * @brief Atomically compare the value stored at object with a |
---|
378 | * old_value and if the two values are equal, update the value of a |
---|
379 | * address with a new_value |
---|
380 | * |
---|
381 | * @param object a atomic type pointer of object. |
---|
382 | * @param old_value pointer of a value. |
---|
383 | * @param new_value a atomic type value. |
---|
384 | * @param order_succ a type of Atomic_Order for successful exchange. |
---|
385 | * @param order_fail a type of Atomic_Order for failed exchange. |
---|
386 | * |
---|
387 | * @retval true if the compare exchange successully. |
---|
388 | * @retval false if the compare exchange failed. |
---|
389 | */ |
---|
390 | static inline bool _CPU_atomic_Compare_exchange_uint( |
---|
391 | volatile Atomic_Uint *object, |
---|
392 | unsigned int *old_value, |
---|
393 | unsigned int new_value, |
---|
394 | Atomic_Order order_succ, |
---|
395 | Atomic_Order order_fail |
---|
396 | ) |
---|
397 | { |
---|
398 | return atomic_compare_exchange_strong_explicit( object, old_value, |
---|
399 | new_value, order_succ, order_fail ); |
---|
400 | } |
---|
401 | |
---|
402 | static inline bool _CPU_atomic_Compare_exchange_ulong( |
---|
403 | volatile Atomic_Ulong *object, |
---|
404 | unsigned long *old_value, |
---|
405 | unsigned long new_value, |
---|
406 | Atomic_Order order_succ, |
---|
407 | Atomic_Order order_fail |
---|
408 | ) |
---|
409 | { |
---|
410 | return atomic_compare_exchange_strong_explicit( object, old_value, |
---|
411 | new_value, order_succ, order_fail ); |
---|
412 | } |
---|
413 | |
---|
414 | static inline bool _CPU_atomic_Compare_exchange_ptr( |
---|
415 | volatile Atomic_Pointer *object, |
---|
416 | void **old_pointer, |
---|
417 | void *new_pointer, |
---|
418 | Atomic_Order order_succ, |
---|
419 | Atomic_Order order_fail |
---|
420 | ) |
---|
421 | { |
---|
422 | return atomic_compare_exchange_strong_explicit( object, old_pointer, |
---|
423 | new_pointer, order_succ, order_fail ); |
---|
424 | } |
---|
425 | |
---|
426 | static inline void _CPU_atomic_Flag_clear( |
---|
427 | volatile Atomic_Flag *object, |
---|
428 | Atomic_Order order |
---|
429 | ) |
---|
430 | { |
---|
431 | return atomic_flag_clear_explicit( object, (memory_order) order ); |
---|
432 | } |
---|
433 | |
---|
434 | static inline bool _CPU_atomic_Flag_test_and_set( |
---|
435 | volatile Atomic_Flag *object, |
---|
436 | Atomic_Order order |
---|
437 | ) |
---|
438 | { |
---|
439 | return atomic_flag_test_and_set_explicit( object, (memory_order) order ); |
---|
440 | } |
---|
441 | |
---|
442 | #ifdef __cplusplus |
---|
443 | } |
---|
444 | #endif |
---|
445 | |
---|
446 | /**@}*/ |
---|
447 | #endif |
---|
448 | /* end of include file */ |
---|