libmetal
lib
compiler
gcc
atomic.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved.
3
*
4
* SPDX-License-Identifier: BSD-3-Clause
5
*/
6
7
/*
8
* @file gcc/atomic.h
9
* @brief GCC specific atomic primitives for libmetal.
10
*/
11
12
#ifndef __METAL_GCC_ATOMIC__H__
13
#define __METAL_GCC_ATOMIC__H__
14
15
#ifdef __cplusplus
16
extern
"C"
{
17
#endif
18
19
typedef
int
atomic_flag
;
20
typedef
char
atomic_char
;
21
typedef
unsigned
char
atomic_uchar
;
22
typedef
short
atomic_short
;
23
typedef
unsigned
short
atomic_ushort
;
24
typedef
int
atomic_int
;
25
typedef
unsigned
int
atomic_uint
;
26
typedef
long
atomic_long
;
27
typedef
unsigned
long
atomic_ulong
;
28
typedef
long
long
atomic_llong
;
29
typedef
unsigned
long
long
atomic_ullong
;
30
31
#define ATOMIC_FLAG_INIT 0
32
#define ATOMIC_VAR_INIT(VAL) (VAL)
33
34
typedef
enum
{
35
memory_order_relaxed
,
36
memory_order_consume
,
37
memory_order_acquire
,
38
memory_order_release
,
39
memory_order_acq_rel
,
40
memory_order_seq_cst
,
41
}
memory_order
;
42
43
#define atomic_flag_test_and_set(FLAG) \
44
__sync_lock_test_and_set((FLAG), 1)
45
#define atomic_flag_test_and_set_explicit(FLAG, MO) \
46
atomic_flag_test_and_set(FLAG)
47
#define atomic_flag_clear(FLAG) \
48
__sync_lock_release((FLAG))
49
#define atomic_flag_clear_explicit(FLAG, MO) \
50
atomic_flag_clear(FLAG)
51
#define atomic_init(OBJ, VAL) \
52
do { *(OBJ) = (VAL); } while (0)
53
#define atomic_is_lock_free(OBJ) \
54
(sizeof(*(OBJ)) <= sizeof(long))
55
#define atomic_store(OBJ, VAL) \
56
do { *(OBJ) = (VAL); __sync_synchronize(); } while (0)
57
#define atomic_store_explicit(OBJ, VAL, MO) \
58
atomic_store((OBJ), (VAL))
59
#define atomic_load(OBJ) \
60
({ __sync_synchronize(); *(OBJ); })
61
#define atomic_load_explicit(OBJ, MO) \
62
atomic_load(OBJ)
63
#define atomic_exchange(OBJ, DES) \
64
({ \
65
typeof(OBJ) obj = (OBJ); \
66
typeof(*obj) des = (DES); \
67
typeof(*obj) expval; \
68
typeof(*obj) oldval = atomic_load(obj); \
69
do { \
70
expval = oldval; \
71
oldval = __sync_val_compare_and_swap( \
72
obj, expval, des); \
73
} while (oldval != expval); \
74
oldval; \
75
})
76
#define atomic_exchange_explicit(OBJ, DES, MO) \
77
atomic_exchange((OBJ), (DES))
78
#define atomic_compare_exchange_strong(OBJ, EXP, DES) \
79
({ \
80
typeof(OBJ) obj = (OBJ); \
81
typeof(EXP) exp = (EXP); \
82
typeof(*obj) expval = *exp; \
83
typeof(*obj) oldval = __sync_val_compare_and_swap( \
84
obj, expval, (DES)); \
85
*exp = oldval; \
86
oldval == expval; \
87
})
88
#define atomic_compare_exchange_strong_explicit(OBJ, EXP, DES, MO) \
89
atomic_compare_exchange_strong((OBJ), (EXP), (DES))
90
#define atomic_compare_exchange_weak(OBJ, EXP, DES) \
91
atomic_compare_exchange_strong((OBJ), (EXP), (DES))
92
#define atomic_compare_exchange_weak_explicit(OBJ, EXP, DES, MO) \
93
atomic_compare_exchange_weak((OBJ), (EXP), (DES))
94
#define atomic_fetch_add(OBJ, VAL) \
95
__sync_fetch_and_add((OBJ), (VAL))
96
#define atomic_fetch_add_explicit(OBJ, VAL, MO) \
97
atomic_fetch_add((OBJ), (VAL))
98
#define atomic_fetch_sub(OBJ, VAL) \
99
__sync_fetch_and_sub((OBJ), (VAL))
100
#define atomic_fetch_sub_explicit(OBJ, VAL, MO) \
101
atomic_fetch_sub((OBJ), (VAL))
102
#define atomic_fetch_or(OBJ, VAL) \
103
__sync_fetch_and_or((OBJ), (VAL))
104
#define atomic_fetch_or_explicit(OBJ, VAL, MO) \
105
atomic_fetch_or((OBJ), (VAL))
106
#define atomic_fetch_xor(OBJ, VAL) \
107
__sync_fetch_and_xor((OBJ), (VAL))
108
#define atomic_fetch_xor_explicit(OBJ, VAL, MO) \
109
atomic_fetch_xor((OBJ), (VAL))
110
#define atomic_fetch_and(OBJ, VAL) \
111
__sync_fetch_and_and((OBJ), (VAL))
112
#define atomic_fetch_and_explicit(OBJ, VAL, MO) \
113
atomic_fetch_and((OBJ), (VAL))
114
#define atomic_thread_fence(MO) \
115
__sync_synchronize()
116
#define atomic_signal_fence(MO) \
117
__sync_synchronize()
118
119
#ifdef __cplusplus
120
}
121
#endif
122
123
#endif
/* __METAL_GCC_ATOMIC__H__ */
atomic_flag
int atomic_flag
Definition:
atomic.h:19
atomic_uchar
unsigned char atomic_uchar
Definition:
atomic.h:21
atomic_ushort
unsigned short atomic_ushort
Definition:
atomic.h:23
atomic_uint
unsigned int atomic_uint
Definition:
atomic.h:25
atomic_long
long atomic_long
Definition:
atomic.h:26
memory_order_seq_cst
Definition:
atomic.h:40
memory_order_acquire
Definition:
atomic.h:37
atomic_short
short atomic_short
Definition:
atomic.h:22
atomic_llong
long long atomic_llong
Definition:
atomic.h:28
memory_order_acq_rel
Definition:
atomic.h:39
memory_order_consume
Definition:
atomic.h:36
atomic_ulong
unsigned long atomic_ulong
Definition:
atomic.h:27
atomic_ullong
unsigned long long atomic_ullong
Definition:
atomic.h:29
memory_order_relaxed
Definition:
atomic.h:35
memory_order
memory_order
Definition:
atomic.h:34
atomic_int
int atomic_int
Definition:
atomic.h:24
memory_order_release
Definition:
atomic.h:38
atomic_char
char atomic_char
Definition:
atomic.h:20
Generated on Mon Jul 9 2018 00:00:00 for libmetal by
1.8.16