aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/arch/default/odp/api/abi/atomic_generic.h
blob: b2029777cf2a0fe6b2d445708ab7e8ef09196ec6 (plain)
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2021 ARM Limited
 * Copyright (c) 2021-2022 Nokia
 */

#ifndef ODP_API_ABI_ATOMIC_GENERIC_H_
#define ODP_API_ABI_ATOMIC_GENERIC_H_

#include <odp/api/atomic.h>

static inline void _odp_atomic_add_u32(odp_atomic_u32_t *atom, uint32_t val)
{
	(void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_sub_u32(odp_atomic_u32_t *atom, uint32_t val)
{
	(void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_inc_u32(odp_atomic_u32_t *atom)
{
	(void)__atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_dec_u32(odp_atomic_u32_t *atom)
{
	(void)__atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_max_u32(odp_atomic_u32_t *atom, uint32_t new_val)
{
	uint32_t old_val;

	old_val = __atomic_load_n(&atom->v, __ATOMIC_RELAXED);

	while (new_val > old_val) {
		if (__atomic_compare_exchange_n(&atom->v, &old_val, new_val, 0 /* strong */,
						__ATOMIC_RELAXED, __ATOMIC_RELAXED))
			break;
	}
}

static inline void _odp_atomic_min_u32(odp_atomic_u32_t *atom, uint32_t new_val)
{
	uint32_t old_val;

	old_val = __atomic_load_n(&atom->v, __ATOMIC_RELAXED);

	while (new_val < old_val) {
		if (__atomic_compare_exchange_n(&atom->v, &old_val, new_val, 0 /* strong */,
						__ATOMIC_RELAXED, __ATOMIC_RELAXED))
			break;
	}
}

static inline void _odp_atomic_add_rel_u32(odp_atomic_u32_t *atom, uint32_t val)
{
	(void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELEASE);
}

static inline void _odp_atomic_sub_rel_u32(odp_atomic_u32_t *atom, uint32_t val)
{
	(void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELEASE);
}

static inline void _odp_atomic_add_u64(odp_atomic_u64_t *atom, uint64_t val)
{
	(void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_sub_u64(odp_atomic_u64_t *atom, uint64_t val)
{
	(void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_inc_u64(odp_atomic_u64_t *atom)
{
	(void)__atomic_fetch_add(&atom->v, 1, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_dec_u64(odp_atomic_u64_t *atom)
{
	(void)__atomic_fetch_sub(&atom->v, 1, __ATOMIC_RELAXED);
}

static inline void _odp_atomic_max_u64(odp_atomic_u64_t *atom, uint64_t new_val)
{
	uint64_t old_val;

	old_val = __atomic_load_n(&atom->v, __ATOMIC_RELAXED);

	while (new_val > old_val) {
		if (__atomic_compare_exchange_n(&atom->v, &old_val, new_val, 0 /* strong */,
						__ATOMIC_RELAXED, __ATOMIC_RELAXED))
			break;
	}
}

static inline void _odp_atomic_min_u64(odp_atomic_u64_t *atom, uint64_t new_val)
{
	uint64_t old_val;

	old_val = __atomic_load_n(&atom->v, __ATOMIC_RELAXED);

	while (new_val < old_val) {
		if (__atomic_compare_exchange_n(&atom->v, &old_val, new_val, 0 /* strong */,
						__ATOMIC_RELAXED, __ATOMIC_RELAXED))
			break;
	}
}

#ifndef ODP_ATOMIC_U64_LOCK
static inline void _odp_atomic_add_rel_u64(odp_atomic_u64_t *atom, uint64_t val)
{
	(void)__atomic_fetch_add(&atom->v, val, __ATOMIC_RELEASE);
}

static inline void _odp_atomic_sub_rel_u64(odp_atomic_u64_t *atom, uint64_t val)
{
	(void)__atomic_fetch_sub(&atom->v, val, __ATOMIC_RELEASE);
}
#endif

#ifdef __SIZEOF_INT128__

static inline void _odp_atomic_init_u128(odp_atomic_u128_t *atom, odp_u128_t val)
{
	atom->v = val;
}

static inline odp_u128_t _odp_atomic_load_u128(odp_atomic_u128_t *atom)
{
	union {
		odp_u128_t val;
		__int128_t i;
	} u;

	u.i = __atomic_load_n((__int128_t *)&atom->v, __ATOMIC_RELAXED);
	return u.val;
}

static inline void _odp_atomic_store_u128(odp_atomic_u128_t *atom, odp_u128_t val)
{
	__atomic_store_n((__int128_t *)&atom->v, *(__int128_t *)&val, __ATOMIC_RELAXED);
}

static inline int _odp_atomic_cas_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
				       odp_u128_t new_val)
{
	return __atomic_compare_exchange_n((__int128_t *)&atom->v, (__int128_t *)old_val,
					   *(__int128_t *)&new_val, 0 /* strong */,
					   __ATOMIC_RELAXED, __ATOMIC_RELAXED);
}

static inline int _odp_atomic_cas_acq_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					   odp_u128_t new_val)
{
	return __atomic_compare_exchange_n((__int128_t *)&atom->v, (__int128_t *)old_val,
					   *(__int128_t *)&new_val, 0 /* strong */,
					   __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
}

static inline int _odp_atomic_cas_rel_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					   odp_u128_t new_val)
{
	return __atomic_compare_exchange_n((__int128_t *)&atom->v, (__int128_t *)old_val,
					   *(__int128_t *)&new_val, 0 /* strong */,
					   __ATOMIC_RELEASE, __ATOMIC_RELAXED);
}

static inline int _odp_atomic_cas_acq_rel_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					       odp_u128_t new_val)
{
	return __atomic_compare_exchange_n((__int128_t *)&atom->v, (__int128_t *)old_val,
					   *(__int128_t *)&new_val, 0 /* strong */,
					   __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
}

#else /* Lock-based implementation */

/**
 * @internal
 * 128 bit store operation expression for the ATOMIC_OP macro
 */
#define ATOMIC_STORE_OP_128(new_val) \
({ \
	(_atom)->v = (new_val); \
})

/**
 * @internal
 * 128 bit CAS operation expression for the ATOMIC_OP macro
 */
#define ATOMIC_CAS_OP_128(ret_ptr, old_val, new_val) \
__extension__ ({ \
	int *_ret_ptr = ret_ptr; \
	odp_u128_t *_cas_old = old_val; \
	odp_u128_t _cas_new = new_val; \
	if (((_atom)->v.u64[0] == (_cas_old)->u64[0]) && \
	    ((_atom)->v.u64[1] == (_cas_old)->u64[1])) { \
		(_atom)->v = (_cas_new); \
		*(_ret_ptr) = 1; \
	} else { \
		*(_ret_ptr) = 0; \
	} \
})

/**
 * @internal
 * Helper macro for lock-based atomic operations on 128-bit integers
 * @param[in,out] atom Pointer to the 128-bit atomic variable
 * @param expr Expression used update the variable.
 * @return The old value of the variable.
 */
#define ATOMIC_OP_128(atom, expr) \
__extension__ ({ \
	odp_u128_t _old_val; \
	odp_atomic_u128_t *_atom = atom; \
	/* Loop while lock is already taken, stop when lock becomes clear */ \
	while (__atomic_test_and_set(&(_atom)->lock, __ATOMIC_ACQUIRE)) \
		(void)0; \
	_old_val = (_atom)->v; \
	(expr); /* Perform whatever update is desired */ \
	__atomic_clear(&(_atom)->lock, __ATOMIC_RELEASE); \
	_old_val; /* Return old value */ \
})

static inline void _odp_atomic_init_u128(odp_atomic_u128_t *atom, odp_u128_t val)
{
	atom->v.u64[0] = val.u64[0];
	atom->v.u64[1] = val.u64[1];
	atom->lock = 0;
}

static inline odp_u128_t _odp_atomic_load_u128(odp_atomic_u128_t *atom)
{
	return ATOMIC_OP_128(atom, (void)0);
}

static inline void _odp_atomic_store_u128(odp_atomic_u128_t *atom, odp_u128_t val)
{
	ATOMIC_OP_128(atom, ATOMIC_STORE_OP_128(val));
}

static inline int _odp_atomic_cas_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
				       odp_u128_t new_val)
{
	int ret;

	*old_val = ATOMIC_OP_128(atom, ATOMIC_CAS_OP_128(&ret, old_val, new_val));
	return ret;
}

static inline int _odp_atomic_cas_acq_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					   odp_u128_t new_val)
{
	return _odp_atomic_cas_u128(atom, old_val, new_val);
}

static inline int _odp_atomic_cas_rel_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					   odp_u128_t new_val)
{
	return _odp_atomic_cas_u128(atom, old_val, new_val);
}

static inline int _odp_atomic_cas_acq_rel_u128(odp_atomic_u128_t *atom, odp_u128_t *old_val,
					       odp_u128_t new_val)
{
	return _odp_atomic_cas_u128(atom, old_val, new_val);
}
#endif

#endif