source: rtems/cpukit/score/include/rtems/score/cpustdatomic.h @ 945853b7

4.115
Last change on this file since 945853b7 was 945853b7, checked in by Sebastian Huber <sebastian.huber@…>, on Feb 13, 2014 at 2:38:52 PM

score: Add Atomic_Uint

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