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# else
41/* Other arch specific implementation */
43# endif /* CONFIG_XTENSA */
44#elif defined(CONFIG_ATOMIC_OPERATIONS_BUILTIN)
45/* Default. See this file for the Doxygen reference: */
47#else
48#error "CONFIG_ATOMIC_OPERATIONS_* not defined"
49#endif
50
51/* Portable higher-level utilities: */
52
58
67#define ATOMIC_INIT(i) (i)
68
78#define ATOMIC_PTR_INIT(p) (p)
79
83
84#define ATOMIC_BITS (sizeof(atomic_val_t) * BITS_PER_BYTE)
85#define ATOMIC_MASK(bit) BIT((unsigned long)(bit) & (ATOMIC_BITS - 1U))
86#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS))
87
91
98#define ATOMIC_BITMAP_SIZE(num_bits) (ROUND_UP(num_bits, ATOMIC_BITS) / ATOMIC_BITS)
99
119#define ATOMIC_DEFINE(name, num_bits) \
120 atomic_t name[ATOMIC_BITMAP_SIZE(num_bits)]
121
135static inline bool atomic_test_bit(const atomic_t *target, int bit)
136{
137 atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit));
138
139 return (1 & (val >> (bit & (ATOMIC_BITS - 1)))) != 0;
140}
141
155static inline bool atomic_test_and_clear_bit(atomic_t *target, int bit)
156{
157 atomic_val_t mask = ATOMIC_MASK(bit);
158 atomic_val_t old;
159
160 old = atomic_and(ATOMIC_ELEM(target, bit), ~mask);
161
162 return (old & mask) != 0;
163}
164
178static inline bool atomic_test_and_set_bit(atomic_t *target, int bit)
179{
180 atomic_val_t mask = ATOMIC_MASK(bit);
181 atomic_val_t old;
182
183 old = atomic_or(ATOMIC_ELEM(target, bit), mask);
184
185 return (old & mask) != 0;
186}
187
202static inline bool atomic_test_and_set_bit_to(atomic_t *target, int bit, bool val)
203{
204 atomic_val_t mask = ATOMIC_MASK(bit);
205
206 if (val) {
207 return (atomic_or(ATOMIC_ELEM(target, bit), mask) & mask) == 0;
208 }
209
210 return (atomic_and(ATOMIC_ELEM(target, bit), ~mask) & mask) != 0;
211}
212
224static inline void atomic_clear_bit(atomic_t *target, int bit)
225{
226 atomic_val_t mask = ATOMIC_MASK(bit);
227
228 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
229}
230
242static inline void atomic_set_bit(atomic_t *target, int bit)
243{
244 atomic_val_t mask = ATOMIC_MASK(bit);
245
246 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
247}
248
261static inline void atomic_set_bit_to(atomic_t *target, int bit, bool val)
262{
263 atomic_val_t mask = ATOMIC_MASK(bit);
264
265 if (val) {
266 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
267 } else {
268 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
269 }
270}
271
287bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value);
288
305 atomic_ptr_val_t new_value);
306
320
334
347
360
373
386
401
416
430
444
459
474
489
504
508
509#ifdef __cplusplus
510} /* extern "C" */
511#endif
512
513#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:242
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:135
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition atomic.h:224
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:155
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:202
static bool atomic_test_and_set_bit(atomic_t *target, int bit)
Atomically set a bit and test it.
Definition atomic.h:178
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:261
Misc utilities.
Macros to abstract toolchain specific capabilities.