source: rtems/cpukit/score/include/rtems/score/cpustdatomic.h @ cd2c655

4.115
Last change on this file since cd2c655 was cd2c655, checked in by Sebastian Huber <sebastian.huber@…>, on 02/13/14 at 13:28:50

score: Fix warnings, C++ compatibility, fix typos

  • Property mode set to 100644
File size: 8.8 KB
Line 
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 <stdbool.h>
23
24#ifdef __cplusplus
25extern "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 */
38typedef atomic_ulong Atomic_Ulong;
39
40/**
41 * @brief atomic operation unsigned integer the size of a pointer type
42 */
43typedef atomic_uintptr_t Atomic_Pointer;
44
45/**
46 * @brief atomic operation flag type
47 */
48typedef 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 */
55typedef 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/**
72 * @brief atomic data initializer for static initialization.
73 */
74#define CPU_ATOMIC_INITIALIZER_ULONG(value) ATOMIC_VAR_INIT(value)
75#define CPU_ATOMIC_INITIALIZER_PTR(pointer) \
76  ATOMIC_VAR_INIT((uintptr_t) pointer)
77
78#define CPU_ATOMIC_INITIALIZER_FLAG ATOMIC_FLAG_INIT
79
80/**
81 * @brief Initializes an atomic type value into a atomic object.
82 *
83 * @param object an atomic type pointer of object.
84 * @param value a value to be stored into object.
85 */
86static inline void _CPU_atomic_Init_ulong(
87  volatile Atomic_Ulong *object,
88  unsigned long value
89)
90{
91  atomic_init( object, value );
92}
93
94static inline void _CPU_atomic_Init_ptr(
95  volatile Atomic_Pointer *object,
96  void *pointer
97)
98{
99  atomic_init( object, (uintptr_t) pointer );
100}
101
102/**
103 * @brief Atomically load an atomic type value from atomic object.
104 *
105 * @param object an atomic type pointer of object.
106 * @param order a type of Atomic_Order.
107 *
108 * The order shall not be ATOMIC_ORDER_RELEASE.
109 */
110static inline unsigned long _CPU_atomic_Load_ulong(
111  volatile Atomic_Ulong *object,
112  Atomic_Order order
113)
114{
115  return atomic_load_explicit( object, (memory_order) order );
116}
117
118static inline void *_CPU_atomic_Load_ptr(
119  volatile Atomic_Pointer *object,
120  Atomic_Order order
121)
122{
123  return (void *) atomic_load_explicit( object, (memory_order) order );
124}
125
126/**
127 * @brief Atomically store an atomic type value into a atomic object.
128 *
129 * @param object an atomic type pointer of object.
130 * @param value a value to be stored into object.
131 * @param order a type of Atomic_Order.
132 *
133 * The order shall not be ATOMIC_ORDER_ACQUIRE.
134 */
135static inline void _CPU_atomic_Store_ulong(
136  volatile Atomic_Ulong *object,
137  unsigned long value,
138  Atomic_Order order
139)
140{
141  atomic_store_explicit( object, value, (memory_order) order );
142}
143
144static inline void _CPU_atomic_Store_ptr(
145  volatile Atomic_Pointer *object,
146  void *pointer,
147  Atomic_Order order
148)
149{
150  atomic_store_explicit( object, pointer, (memory_order) order );
151}
152
153/**
154 * @brief Atomically load-add-store an atomic type value into object
155 *
156 * @param object a atomic type pointer of object.
157 * @param value a value to be add and store into object.
158 * @param order a type of Atomic_Order.
159 *
160 * @retval a result value before add ops.
161 */
162static inline unsigned long _CPU_atomic_Fetch_add_ulong(
163  volatile Atomic_Ulong *object,
164  unsigned long value,
165  Atomic_Order order
166)
167{
168  return atomic_fetch_add_explicit( object, value, (memory_order) order );
169}
170
171static inline uintptr_t _CPU_atomic_Fetch_add_ptr(
172  volatile Atomic_Pointer *object,
173  uintptr_t value,
174  Atomic_Order order
175)
176{
177  return atomic_fetch_add_explicit( object, value, (memory_order) order );
178}
179
180/**
181 * @brief Atomically load-sub-store an atomic type value into object
182 *
183 * @param object a atomic type pointer of object.
184 * @param value a value to be sub and store into object.
185 * @param order a type of Atomic_Order.
186 *
187 * @retval a result value before sub ops.
188 */
189static inline unsigned long _CPU_atomic_Fetch_sub_ulong(
190  volatile Atomic_Ulong *object,
191  unsigned long value,
192  Atomic_Order order
193)
194{
195  return atomic_fetch_sub_explicit( object, value, (memory_order) order );
196}
197
198static inline uintptr_t _CPU_atomic_Fetch_sub_ptr(
199  volatile Atomic_Pointer *object,
200  uintptr_t value,
201  Atomic_Order order
202)
203{
204  return atomic_fetch_sub_explicit( object, value, (memory_order) order );
205}
206
207/**
208 * @brief Atomically load-or-store an atomic type value into object
209 *
210 * @param object a atomic type pointer of object.
211 * @param value a value to be or and store into object.
212 * @param order a type of Atomic_Order.
213 *
214 * @retval a result value before or ops.
215 */
216static inline unsigned long _CPU_atomic_Fetch_or_ulong(
217  volatile Atomic_Ulong *object,
218  unsigned long value,
219  Atomic_Order order
220)
221{
222  return atomic_fetch_or_explicit( object, value, (memory_order) order );
223}
224
225static inline uintptr_t _CPU_atomic_Fetch_or_ptr(
226  volatile Atomic_Pointer *object,
227  uintptr_t value,
228  Atomic_Order order
229)
230{
231  return atomic_fetch_or_explicit( object, value, (memory_order) order );
232}
233
234/**
235 * @brief Atomically load-and-store an atomic type value into object
236 *
237 * @param object a atomic type pointer of object.
238 * @param value a value to be and and store into object.
239 * @param order a type of Atomic_Order.
240 *
241 * @retval a result value before and ops.
242 */
243static inline unsigned long _CPU_atomic_Fetch_and_ulong(
244  volatile Atomic_Ulong *object,
245  unsigned long value,
246  Atomic_Order order
247)
248{
249  return atomic_fetch_and_explicit( object, value, (memory_order) order );
250}
251
252static inline uintptr_t _CPU_atomic_Fetch_and_ptr(
253  volatile Atomic_Pointer *object,
254  uintptr_t value,
255  Atomic_Order order
256)
257{
258  return atomic_fetch_and_explicit( object, value, (memory_order) order );
259}
260
261/**
262 * @brief Atomically exchange an atomic type value into object
263 *
264 * @param object a atomic type pointer of object.
265 * @param value a value to exchange and and store into object.
266 * @param order a type of Atomic_Order.
267 *
268 * @retval a result value before exchange ops.
269 */
270static inline unsigned long _CPU_atomic_Exchange_ulong(
271 volatile Atomic_Ulong *object,
272 unsigned long value,
273 Atomic_Order order
274)
275{
276  return atomic_exchange_explicit( object, value, (memory_order) order );
277}
278
279static inline void *_CPU_atomic_Exchange_ptr(
280 volatile Atomic_Pointer *object,
281 void *pointer,
282 Atomic_Order order
283)
284{
285  return (void *) atomic_exchange_explicit(
286    object,
287    (uintptr_t) pointer,
288    (memory_order) order
289  );
290}
291
292/**
293 * @brief Atomically compare the value stored at object with a
294 * old_value and if the two values are equal, update the value of a
295 * address with a new_value
296 *
297 * @param object a atomic type pointer of object.
298 * @param old_value pointer of a value.
299 * @param new_value a atomic type value.
300 * @param order_succ a type of Atomic_Order for successful exchange.
301 * @param order_fail a type of Atomic_Order for failed exchange.
302 *
303 * @retval true if the compare exchange successully.
304 * @retval false if the compare exchange failed.
305 */
306static inline bool _CPU_atomic_Compare_exchange_ulong(
307  volatile Atomic_Ulong *object,
308  unsigned long *old_value,
309  unsigned long new_value,
310  Atomic_Order order_succ,
311  Atomic_Order order_fail
312)
313{
314  return atomic_compare_exchange_strong_explicit( object, old_value,
315    new_value, order_succ, order_fail );
316}
317
318static inline bool _CPU_atomic_Compare_exchange_ptr(
319  volatile Atomic_Pointer *object,
320  void **old_pointer,
321  void *new_pointer,
322  Atomic_Order order_succ,
323  Atomic_Order order_fail
324)
325{
326  return atomic_compare_exchange_strong_explicit( object, old_pointer,
327    new_pointer, order_succ, order_fail );
328}
329
330static inline void _CPU_atomic_Flag_clear(
331  volatile Atomic_Flag *object,
332  Atomic_Order order
333)
334{
335  return atomic_flag_clear_explicit( object, (memory_order) order );
336}
337
338static inline bool _CPU_atomic_Flag_test_and_set(
339  volatile Atomic_Flag *object,
340  Atomic_Order order
341)
342{
343  return atomic_flag_test_and_set_explicit( object, (memory_order) order );
344}
345
346#ifdef __cplusplus
347}
348#endif
349
350/**@}*/
351#endif
352/*  end of include file */
Note: See TracBrowser for help on using the repository browser.