Zephyr API Documentation  3.6.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
9#ifndef ZEPHYR_INCLUDE_SYS_ATOMIC_H_
10#define ZEPHYR_INCLUDE_SYS_ATOMIC_H_
11
12#include <stdbool.h>
13#include <zephyr/toolchain.h>
14#include <stddef.h>
15
16#include <zephyr/sys/atomic_types.h> /* IWYU pragma: export */
17#include <zephyr/types.h>
18#include <zephyr/sys/util.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/* Low-level primitives come in several styles: */
25
26#if defined(CONFIG_ATOMIC_OPERATIONS_C)
27/* Generic-but-slow implementation based on kernel locking and syscalls */
28#include <zephyr/sys/atomic_c.h>
29#elif defined(CONFIG_ATOMIC_OPERATIONS_ARCH)
30/* Some architectures need their own implementation */
31# ifdef CONFIG_XTENSA
32/* Not all Xtensa toolchains support GCC-style atomic intrinsics */
34# else
35/* Other arch specific implementation */
37# endif /* CONFIG_XTENSA */
38#else
39/* Default. See this file for the Doxygen reference: */
41#endif
42
43/* Portable higher-level utilities: */
44
59#define ATOMIC_INIT(i) (i)
60
70#define ATOMIC_PTR_INIT(p) (p)
71
76#define ATOMIC_BITS (sizeof(atomic_val_t) * 8)
77#define ATOMIC_MASK(bit) BIT((unsigned long)(bit) & (ATOMIC_BITS - 1U))
78#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS))
79
90#define ATOMIC_BITMAP_SIZE(num_bits) (ROUND_UP(num_bits, ATOMIC_BITS) / ATOMIC_BITS)
91
111#define ATOMIC_DEFINE(name, num_bits) \
112 atomic_t name[ATOMIC_BITMAP_SIZE(num_bits)]
113
127static inline bool atomic_test_bit(const atomic_t *target, int bit)
128{
129 atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit));
130
131 return (1 & (val >> (bit & (ATOMIC_BITS - 1)))) != 0;
132}
133
147static inline bool atomic_test_and_clear_bit(atomic_t *target, int bit)
148{
149 atomic_val_t mask = ATOMIC_MASK(bit);
150 atomic_val_t old;
151
152 old = atomic_and(ATOMIC_ELEM(target, bit), ~mask);
153
154 return (old & mask) != 0;
155}
156
170static inline bool atomic_test_and_set_bit(atomic_t *target, int bit)
171{
172 atomic_val_t mask = ATOMIC_MASK(bit);
173 atomic_val_t old;
174
175 old = atomic_or(ATOMIC_ELEM(target, bit), mask);
176
177 return (old & mask) != 0;
178}
179
191static inline void atomic_clear_bit(atomic_t *target, int bit)
192{
193 atomic_val_t mask = ATOMIC_MASK(bit);
194
195 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
196}
197
209static inline void atomic_set_bit(atomic_t *target, int bit)
210{
211 atomic_val_t mask = ATOMIC_MASK(bit);
212
213 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
214}
215
228static inline void atomic_set_bit_to(atomic_t *target, int bit, bool val)
229{
230 atomic_val_t mask = ATOMIC_MASK(bit);
231
232 if (val) {
233 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
234 } else {
235 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);
236 }
237}
238
254bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value);
255
272 atomic_ptr_val_t new_value);
273
287
301
314
327
340
353
368
383
397
411
426
441
456
471
476#ifdef __cplusplus
477} /* extern "C" */
478#endif
479
480#endif /* ZEPHYR_INCLUDE_SYS_ATOMIC_H_ */
long atomic_t
Definition: atomic_types.h:15
atomic_t atomic_val_t
Definition: atomic_types.h:16
atomic_ptr_t atomic_ptr_val_t
Definition: atomic_types.h:18
void * atomic_ptr_t
Definition: atomic_types.h:17
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:209
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 test a bit.
Definition: atomic.h:127
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition: atomic.h:191
atomic_ptr_val_t atomic_ptr_get(const atomic_ptr_t *target)
Atomic get a pointer value.
atomic_val_t atomic_get(const atomic_t *target)
Atomic get.
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 test and clear a bit.
Definition: atomic.h:147
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(atomic_t *target, int bit)
Atomically set a bit.
Definition: atomic.h:170
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.
static void atomic_set_bit_to(atomic_t *target, int bit, bool val)
Atomically set a bit to a given value.
Definition: atomic.h:228
Macros to abstract toolchain specific capabilities.
Misc utilities.