Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
atomic_builtin.h
Go to the documentation of this file.
1/* atomic operations */
2
3/*
4 * Copyright (c) 1997-2015, Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef ZEPHYR_INCLUDE_SYS_ATOMIC_BUILTIN_H_
10#define ZEPHYR_INCLUDE_SYS_ATOMIC_BUILTIN_H_
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16/* Included from <atomic.h> */
17
40static inline bool atomic_cas(atomic_t *target, atomic_val_t old_value,
41 atomic_val_t new_value)
42{
43 return __atomic_compare_exchange_n(target, &old_value, new_value,
44 0, __ATOMIC_SEQ_CST,
45 __ATOMIC_SEQ_CST);
46}
47
64static inline bool atomic_ptr_cas(atomic_ptr_t *target, atomic_ptr_val_t old_value,
65 atomic_ptr_val_t new_value)
66{
67 return __atomic_compare_exchange_n(target, &old_value, new_value,
68 0, __ATOMIC_SEQ_CST,
69 __ATOMIC_SEQ_CST);
70}
71
86static inline atomic_val_t atomic_add(atomic_t *target, atomic_val_t value)
87{
88 return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST);
89}
90
105static inline atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value)
106{
107 return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST);
108}
109
123static inline atomic_val_t atomic_inc(atomic_t *target)
124{
125 return atomic_add(target, 1);
126}
127
141static inline atomic_val_t atomic_dec(atomic_t *target)
142{
143 return atomic_sub(target, 1);
144}
145
159static inline atomic_val_t atomic_get(const atomic_t *target)
160{
161 return __atomic_load_n(target, __ATOMIC_SEQ_CST);
162}
163
177static inline atomic_ptr_val_t atomic_ptr_get(const atomic_ptr_t *target)
178{
179 return __atomic_load_n(target, __ATOMIC_SEQ_CST);
180}
181
197static inline atomic_val_t atomic_set(atomic_t *target, atomic_val_t value)
198{
199 /* This builtin, as described by Intel, is not a traditional
200 * test-and-set operation, but rather an atomic exchange operation. It
201 * writes value into *ptr, and returns the previous contents of *ptr.
202 */
203 return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST);
204}
205
222{
223 return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST);
224}
225
240static inline atomic_val_t atomic_clear(atomic_t *target)
241{
242 return atomic_set(target, 0);
243}
244
260{
261 return atomic_ptr_set(target, NULL);
262}
263
279static inline atomic_val_t atomic_or(atomic_t *target, atomic_val_t value)
280{
281 return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST);
282}
283
299static inline atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value)
300{
301 return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST);
302}
303
319static inline atomic_val_t atomic_and(atomic_t *target, atomic_val_t value)
320{
321 return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST);
322}
323
339static inline atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value)
340{
341 return __atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST);
342}
343
347#ifdef __cplusplus
348}
349#endif
350
351#endif /* ZEPHYR_INCLUDE_SYS_ATOMIC_BUILTIN_H_ */
long atomic_t
Definition: atomic.h:22
atomic_t atomic_val_t
Definition: atomic.h:23
atomic_ptr_t atomic_ptr_val_t
Definition: atomic.h:25
void * atomic_ptr_t
Definition: atomic.h:24
static atomic_val_t atomic_dec(atomic_t *target)
Atomic decrement.
Definition: atomic_builtin.h:141
static atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value)
Atomic subtraction.
Definition: atomic_builtin.h:105
static atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value)
Atomic bitwise exclusive OR (XOR).
Definition: atomic_builtin.h:299
static bool atomic_ptr_cas(atomic_ptr_t *target, atomic_ptr_val_t old_value, atomic_ptr_val_t new_value)
Atomic compare-and-set with pointer values.
Definition: atomic_builtin.h:64
static atomic_val_t atomic_clear(atomic_t *target)
Atomic clear.
Definition: atomic_builtin.h:240
static atomic_val_t atomic_set(atomic_t *target, atomic_val_t value)
Atomic get-and-set.
Definition: atomic_builtin.h:197
static atomic_val_t atomic_inc(atomic_t *target)
Atomic increment.
Definition: atomic_builtin.h:123
static bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value)
Atomic compare-and-set.
Definition: atomic_builtin.h:40
static atomic_ptr_val_t atomic_ptr_clear(atomic_ptr_t *target)
Atomic clear of a pointer value.
Definition: atomic_builtin.h:259
static atomic_ptr_val_t atomic_ptr_get(const atomic_ptr_t *target)
Atomic get a pointer value.
Definition: atomic_builtin.h:177
static atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value)
Atomic bitwise NAND.
Definition: atomic_builtin.h:339
static atomic_ptr_val_t atomic_ptr_set(atomic_ptr_t *target, atomic_ptr_val_t value)
Atomic get-and-set for pointer values.
Definition: atomic_builtin.h:221
static atomic_val_t atomic_or(atomic_t *target, atomic_val_t value)
Atomic bitwise inclusive OR.
Definition: atomic_builtin.h:279
static atomic_val_t atomic_and(atomic_t *target, atomic_val_t value)
Atomic bitwise AND.
Definition: atomic_builtin.h:319
static atomic_val_t atomic_get(const atomic_t *target)
Atomic get.
Definition: atomic_builtin.h:159
static atomic_val_t atomic_add(atomic_t *target, atomic_val_t value)
Atomic addition.
Definition: atomic_builtin.h:86