summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/cpustdatomic.h
blob: 222ad78852f338d225e2b196bbd428fdddea9d43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
 * @file  rtems/score/cpustdatomic.h
 * 
 * This include file defines the generic data struct and implementation
 * based on stdatomic.h for all the support architectures. You should not
 * include this header file directly, because it will be used by atomic.h
 * which should be included by score components
 */

/*
 * COPYRIGHT (c) 2013 Deng Hengyi.
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_GENERAL_STDATOMIC_CPU_H_
#define _RTEMS_SCORE_GENERAL_STDATOMIC_CPU_H_

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @defgroup RTEMS general stdatomic data type and implementation.
 *
 */

/**@{*/

/**
 * @brief atomic operation unsigned integer type
 */
typedef atomic_uint Atomic_Uint;

/**
 * @brief atomic operation unsigned long integer type
 */
typedef atomic_ulong Atomic_Ulong;

/**
 * @brief atomic operation unsigned integer the size of a pointer type
 */
typedef atomic_uintptr_t Atomic_Pointer;

/**
 * @brief atomic operation flag type
 */
typedef atomic_flag Atomic_Flag;

/**
 * @brief the enumeration Atomic_Memory_barrier specifies the detailed regular
 * memory synchronization operations used in the atomic operation API
 * definitions.
 */
typedef enum {
  /** no operation orders memory. */
  ATOMIC_ORDER_RELAXED = memory_order_relaxed,
  /** a load operation performs an acquire operation on the affected memory
  * location. This flag guarantees that the effects of load operation are
  * completed before the effects of any later data accesses.
  */
  ATOMIC_ORDER_ACQUIRE = memory_order_acquire,
  /** a store operation performs a release operation on the affected memory
  * location. This flag guarantee that all effects of all previous data
  * accesses are completed before the store operation takes place.
  */
  ATOMIC_ORDER_RELEASE = memory_order_release
} Atomic_Order;


/**
 * @brief atomic data initializer for static initialization.
 */
#define CPU_ATOMIC_INITIALIZER_UINT(value) ATOMIC_VAR_INIT(value)
#define CPU_ATOMIC_INITIALIZER_ULONG(value) ATOMIC_VAR_INIT(value)
#define CPU_ATOMIC_INITIALIZER_PTR(pointer) \
  ATOMIC_VAR_INIT((uintptr_t) pointer)

#define CPU_ATOMIC_INITIALIZER_FLAG ATOMIC_FLAG_INIT

static inline void _CPU_atomic_Fence(
  Atomic_Order order
)
{
  atomic_thread_fence( (memory_order) order );
}

/**
 * @brief Initializes an atomic type value into a atomic object.
 *
 * @param object an atomic type pointer of object.
 * @param value a value to be stored into object.
 */
static inline void _CPU_atomic_Init_uint(
  volatile Atomic_Uint *object,
  unsigned int value
)
{
  atomic_init( object, value );
}

static inline void _CPU_atomic_Init_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value
)
{
  atomic_init( object, value );
}

static inline void _CPU_atomic_Init_ptr(
  volatile Atomic_Pointer *object,
  void *pointer
)
{
  atomic_init( object, (uintptr_t) pointer );
}

/**
 * @brief Atomically load an atomic type value from atomic object.
 *
 * @param object an atomic type pointer of object.
 * @param order a type of Atomic_Order. 
 * 
 * The order shall not be ATOMIC_ORDER_RELEASE.
 */
static inline unsigned int _CPU_atomic_Load_uint(
  volatile Atomic_Uint *object,
  Atomic_Order order
)
{
  return atomic_load_explicit( object, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Load_ulong(
  volatile Atomic_Ulong *object,
  Atomic_Order order
)
{
  return atomic_load_explicit( object, (memory_order) order );
}

static inline void *_CPU_atomic_Load_ptr(
  volatile Atomic_Pointer *object,
  Atomic_Order order
)
{
  return (void *) atomic_load_explicit( object, (memory_order) order );
}

/**
 * @brief Atomically store an atomic type value into a atomic object.
 *
 * @param object an atomic type pointer of object.
 * @param value a value to be stored into object.
 * @param order a type of Atomic_Order. 
 * 
 * The order shall not be ATOMIC_ORDER_ACQUIRE.
 */
static inline void _CPU_atomic_Store_uint(
  volatile Atomic_Uint *object,
  unsigned int value,
  Atomic_Order order
)
{
  atomic_store_explicit( object, value, (memory_order) order );
}

static inline void _CPU_atomic_Store_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value,
  Atomic_Order order
)
{
  atomic_store_explicit( object, value, (memory_order) order );
}

static inline void _CPU_atomic_Store_ptr(
  volatile Atomic_Pointer *object,
  void *pointer,
  Atomic_Order order
)
{
  atomic_store_explicit( object, pointer, (memory_order) order );
}

/**
 * @brief Atomically load-add-store an atomic type value into object
 *
 * @param object a atomic type pointer of object.
 * @param value a value to be add and store into object.
 * @param order a type of Atomic_Order. 
 * 
 * @retval a result value before add ops.
 */
static inline unsigned int _CPU_atomic_Fetch_add_uint(
  volatile Atomic_Uint *object,
  unsigned int value,
  Atomic_Order order
)
{
  return atomic_fetch_add_explicit( object, value, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Fetch_add_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value,
  Atomic_Order order
)
{
  return atomic_fetch_add_explicit( object, value, (memory_order) order );
}

static inline uintptr_t _CPU_atomic_Fetch_add_ptr(
  volatile Atomic_Pointer *object,
  uintptr_t value,
  Atomic_Order order
)
{
  return atomic_fetch_add_explicit( object, value, (memory_order) order );
}

/**
 * @brief Atomically load-sub-store an atomic type value into object
 *
 * @param object a atomic type pointer of object.
 * @param value a value to be sub and store into object.
 * @param order a type of Atomic_Order. 
 * 
 * @retval a result value before sub ops.
 */
static inline unsigned int _CPU_atomic_Fetch_sub_uint(
  volatile Atomic_Uint *object,
  unsigned int value,
  Atomic_Order order
)
{
  return atomic_fetch_sub_explicit( object, value, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Fetch_sub_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value,
  Atomic_Order order
)
{
  return atomic_fetch_sub_explicit( object, value, (memory_order) order );
}

static inline uintptr_t _CPU_atomic_Fetch_sub_ptr(
  volatile Atomic_Pointer *object,
  uintptr_t value,
  Atomic_Order order
)
{
  return atomic_fetch_sub_explicit( object, value, (memory_order) order );
}

/**
 * @brief Atomically load-or-store an atomic type value into object
 *
 * @param object a atomic type pointer of object.
 * @param value a value to be or and store into object.
 * @param order a type of Atomic_Order. 
 * 
 * @retval a result value before or ops.
 */
static inline unsigned int _CPU_atomic_Fetch_or_uint(
  volatile Atomic_Uint *object,
  unsigned int value,
  Atomic_Order order
)
{
  return atomic_fetch_or_explicit( object, value, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Fetch_or_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value,
  Atomic_Order order
)
{
  return atomic_fetch_or_explicit( object, value, (memory_order) order );
}

static inline uintptr_t _CPU_atomic_Fetch_or_ptr(
  volatile Atomic_Pointer *object,
  uintptr_t value,
  Atomic_Order order
)
{
  return atomic_fetch_or_explicit( object, value, (memory_order) order );
}

/**
 * @brief Atomically load-and-store an atomic type value into object
 *
 * @param object a atomic type pointer of object.
 * @param value a value to be and and store into object.
 * @param order a type of Atomic_Order. 
 * 
 * @retval a result value before and ops.
 */
static inline unsigned int _CPU_atomic_Fetch_and_uint(
  volatile Atomic_Uint *object,
  unsigned int value,
  Atomic_Order order
)
{
  return atomic_fetch_and_explicit( object, value, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Fetch_and_ulong(
  volatile Atomic_Ulong *object,
  unsigned long value,
  Atomic_Order order
)
{
  return atomic_fetch_and_explicit( object, value, (memory_order) order );
}

static inline uintptr_t _CPU_atomic_Fetch_and_ptr(
  volatile Atomic_Pointer *object,
  uintptr_t value,
  Atomic_Order order
)
{
  return atomic_fetch_and_explicit( object, value, (memory_order) order );
}

/**
 * @brief Atomically exchange an atomic type value into object
 *
 * @param object a atomic type pointer of object.
 * @param value a value to exchange and and store into object.
 * @param order a type of Atomic_Order. 
 * 
 * @retval a result value before exchange ops.
 */
static inline unsigned int _CPU_atomic_Exchange_uint(
 volatile Atomic_Uint *object,
 unsigned int value,
 Atomic_Order order
)
{
  return atomic_exchange_explicit( object, value, (memory_order) order );
}

static inline unsigned long _CPU_atomic_Exchange_ulong(
 volatile Atomic_Ulong *object,
 unsigned long value,
 Atomic_Order order
)
{
  return atomic_exchange_explicit( object, value, (memory_order) order );
}

static inline void *_CPU_atomic_Exchange_ptr(
 volatile Atomic_Pointer *object,
 void *pointer,
 Atomic_Order order
)
{
  return (void *) atomic_exchange_explicit(
    object,
    (uintptr_t) pointer,
    (memory_order) order
  );
}

/**
 * @brief Atomically compare the value stored at object with a
 * old_value and if the two values are equal, update the value of a
 * address with a new_value
 *
 * @param object a atomic type pointer of object.
 * @param old_value pointer of a value.
 * @param new_value a atomic type value.
 * @param order_succ a type of Atomic_Order for successful exchange. 
 * @param order_fail a type of Atomic_Order for failed exchange.
 * 
 * @retval true if the compare exchange successully.
 * @retval false if the compare exchange failed.
 */
static inline bool _CPU_atomic_Compare_exchange_uint(
  volatile Atomic_Uint *object,
  unsigned int *old_value,
  unsigned int new_value,
  Atomic_Order order_succ,
  Atomic_Order order_fail
)
{
  return atomic_compare_exchange_strong_explicit( object, old_value,
    new_value, order_succ, order_fail );
}

static inline bool _CPU_atomic_Compare_exchange_ulong(
  volatile Atomic_Ulong *object,
  unsigned long *old_value,
  unsigned long new_value,
  Atomic_Order order_succ,
  Atomic_Order order_fail
)
{
  return atomic_compare_exchange_strong_explicit( object, old_value,
    new_value, order_succ, order_fail );
}

static inline bool _CPU_atomic_Compare_exchange_ptr(
  volatile Atomic_Pointer *object,
  void **old_pointer,
  void *new_pointer,
  Atomic_Order order_succ,
  Atomic_Order order_fail
)
{
  return atomic_compare_exchange_strong_explicit( object, old_pointer,
    new_pointer, order_succ, order_fail );
}

static inline void _CPU_atomic_Flag_clear(
  volatile Atomic_Flag *object,
  Atomic_Order order
)
{
  return atomic_flag_clear_explicit( object, (memory_order) order );
}

static inline bool _CPU_atomic_Flag_test_and_set(
  volatile Atomic_Flag *object,
  Atomic_Order order
)
{
  return atomic_flag_test_and_set_explicit( object, (memory_order) order );
}

#ifdef __cplusplus
}
#endif

/**@}*/
#endif
/*  end of include file */