Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
atomic.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 1997-2015, Wind River Systems, Inc.
3 * Copyright (c) 2021 Intel Corporation
4 * Copyright (c) 2023 Nordic Semiconductor ASA
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
14
15#ifndef ZEPHYR_INCLUDE_SYS_ATOMIC_H_
16#define ZEPHYR_INCLUDE_SYS_ATOMIC_H_
17
18#include <stdbool.h>
19#include <zephyr/toolchain.h>
20#include <stddef.h>
21
22#include <zephyr/sys/atomic_types.h> /* IWYU pragma: export */
23#include <zephyr/types.h>
24#include <zephyr/sys/util.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* Low-level primitives come in several styles: */
31
32#if defined(CONFIG_ATOMIC_OPERATIONS_C)
33/* Generic-but-slow implementation based on kernel locking and syscalls */
34#include <zephyr/sys/atomic_c.h>
35#elif defined(CONFIG_ATOMIC_OPERATIONS_ARCH)
36/* Some architectures need their own implementation */
37# ifdef CONFIG_XTENSA
38/* Not all Xtensa toolchains support GCC-style atomic intrinsics */
39# include <zephyr/arch/xtensa/atomic_xtensa.h>
40# elif defined(CONFIG_ARC)
41/* MWDT ignores the memory-order argument of the atomic builtins */
42# include <zephyr/arch/arc/atomic_arc.h>
43# else
44/* Other arch specific implementation */
46# endif /* CONFIG_XTENSA */
47#elif defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN)
48/* Default. See this file for the Doxygen reference: */
50#else
51#error "CONFIG_ATOMIC_OPERATIONS_* not defined"
52#endif
53
54/* Portable higher-level utilities: */
55
61
70#define ATOMIC_INIT(i) (i)
71
81#define ATOMIC_PTR_INIT(p) (p)
82
86
87#define ATOMIC_BITS (sizeof(atomic_val_t) * BITS_PER_BYTE)
88#define ATOMIC_MASK(bit) BIT((unsigned long)(bit) & (ATOMIC_BITS - 1U))
89#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS))
90
94
101#define ATOMIC_BITMAP_SIZE(num_bits) (ROUND_UP(num_bits, ATOMIC_BITS) / ATOMIC_BITS)
102
122#define ATOMIC_DEFINE(name, num_bits) \
123 atomic_t name[ATOMIC_BITMAP_SIZE(num_bits)]
124
138static inline bool atomic_test_bit(const atomic_t *target, int bit)
139{
140 atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit));
141
142 return (1 & (val >> (bit & (ATOMIC_BITS - 1)))) != 0;
143}
144
158static inline bool atomic_test_and_clear_bit(atomic_t *target, int bit)
159{
160 atomic_val_t mask = ATOMIC_MASK(bit);
161 atomic_val_t old;
162
163 old = atomic_and(ATOMIC_ELEM(target, bit), ~mask);
164
165 return (old & mask) != 0;
166}
167
181static inline bool atomic_test_and_set_bit(atomic_t *target, int bit)
182{
183 atomic_val_t mask = ATOMIC_MASK(bit);
184 atomic_val_t old;
185
186 old = atomic_or(ATOMIC_ELEM(target, bit), mask);
187
188 return (old & mask) != 0;
189}
190
205static inline bool atomic_test_and_set_bit_to(atomic_t *target, int bit, bool val)
206{
207 atomic_val_t mask = ATOMIC_MASK(bit);
208
209 if (val) {
210 return (atomic_or(ATOMIC_ELEM(target, bit), mask) & mask) == 0;
211 }
212
213 return (atomic_and(ATOMIC_ELEM(target, bit), ~mask) & mask) != 0;
214}
215
227static inline void atomic_clear_bit(atomic_t *target, int bit)
228{
229 atomic_val_t mask = ATOMIC_MASK(bit);
230
231 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
232}
233
245static inline void atomic_set_bit(atomic_t *target, int bit)
246{
247 atomic_val_t mask = ATOMIC_MASK(bit);
248
249 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
250}
251
264static inline void atomic_set_bit_to(atomic_t *target, int bit, bool val)
265{
266 atomic_val_t mask = ATOMIC_MASK(bit);
267
268 if (val) {
269 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
270 } else {
271 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
272 }
273}
274
290bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value);
291
308 atomic_ptr_val_t new_value);
309
323
337
350
363
376
389
404
419
433
447
462
477
492
507
511
512#ifdef __cplusplus
513} /* extern "C" */
514#endif
515
516#endif /* ZEPHYR_INCLUDE_SYS_ATOMIC_H_ */
Architecture-specific atomic operation declarations.
Compiler-builtin implementation of the atomic operations API.
Interrupt-locking C implementation of the atomic operations API.
Atomic type definitions for the atomic operations API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
atomic_val_t atomic_or(atomic_t *target, atomic_val_t value)
Atomic bitwise inclusive OR.
static void atomic_set_bit(atomic_t *target, int bit)
Atomically set a bit.
Definition atomic.h:245
atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value)
Atomic bitwise exclusive OR (XOR).
static bool atomic_test_bit(const atomic_t *target, int bit)
Atomically get and test a bit.
Definition atomic.h:138
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition atomic.h:227
atomic_ptr_val_t atomic_ptr_get(const atomic_ptr_t *target)
Atomic get a pointer value.
atomic_t atomic_val_t
Value type for atomic integer variables.
Definition atomic_types.h:38
atomic_val_t atomic_get(const atomic_t *target)
Atomic get.
atomic_ptr_t atomic_ptr_val_t
Value type for atomic pointer variables.
Definition atomic_types.h:53
atomic_ptr_val_t atomic_ptr_set(atomic_ptr_t *target, atomic_ptr_val_t value)
Atomic get-and-set for pointer values.
atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value)
Atomic bitwise NAND.
atomic_val_t atomic_and(atomic_t *target, atomic_val_t value)
Atomic bitwise AND.
atomic_val_t atomic_add(atomic_t *target, atomic_val_t value)
Atomic addition.
static bool atomic_test_and_clear_bit(atomic_t *target, int bit)
Atomically clear a bit and test it.
Definition atomic.h:158
atomic_ptr_val_t atomic_ptr_clear(atomic_ptr_t *target)
Atomic clear of a pointer value.
atomic_val_t atomic_set(atomic_t *target, atomic_val_t value)
Atomic get-and-set.
static bool atomic_test_and_set_bit_to(atomic_t *target, int bit, bool val)
Atomically set a bit to a given value and report if it was changed.
Definition atomic.h:205
static bool atomic_test_and_set_bit(atomic_t *target, int bit)
Atomically set a bit and test it.
Definition atomic.h:181
atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value)
Atomic subtraction.
atomic_val_t atomic_clear(atomic_t *target)
Atomic clear.
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.
atomic_val_t atomic_inc(atomic_t *target)
Atomic increment.
bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value)
Atomic compare-and-set.
atomic_val_t atomic_dec(atomic_t *target)
Atomic decrement.
void * atomic_ptr_t
Atomic pointer variable.
Definition atomic_types.h:46
static void atomic_set_bit_to(atomic_t *target, int bit, bool val)
Atomically set a bit to a given value.
Definition atomic.h:264
Misc utilities.
Macros to abstract toolchain specific capabilities.