Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
atomic_arc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 GlobalFoundries Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13
14#ifndef ZEPHYR_INCLUDE_ARCH_ARC_ATOMIC_ARC_H_
15#define ZEPHYR_INCLUDE_ARCH_ARC_ATOMIC_ARC_H_
16
17#ifndef __CCAC__
18/* ARC GNU GCC brackets every __ATOMIC_SEQ_CST operation with dmb 3 by itself,
19 * so it keeps using <zephyr/sys/atomic_builtin.h>.
20 */
21#error "The ARC atomic backend is a MWDT-only workaround"
22#endif
23
24#include <stdbool.h>
25#include <stddef.h>
26#include <zephyr/toolchain.h>
28#include <zephyr/sys/barrier.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/* Included from <atomic.h> */
35
37
38/* MWDT lowers every __atomic_* operation to a barrier-free llock/scond library
39 * routine (__Sync_*_4) and drops the memory-order argument on the way, so the
40 * atomicity half of __ATOMIC_SEQ_CST is honoured but the ordering half is not.
41 * Supply the missing ordering here.
42 */
43#define Z_ARC_SEQ_CST(expr) \
44 ({ \
45 __typeof__(expr) _z_arc_ret; \
46 barrier_dmem_fence_full(); \
47 _z_arc_ret = (expr); \
48 barrier_dmem_fence_full(); \
49 _z_arc_ret; \
50 })
51
52static ALWAYS_INLINE bool atomic_cas(atomic_t *target, atomic_val_t old_value,
53 atomic_val_t new_value)
54{
55 return Z_ARC_SEQ_CST(__atomic_compare_exchange_n(target, &old_value,
56 new_value, 0,
57 __ATOMIC_SEQ_CST,
58 __ATOMIC_SEQ_CST));
59}
60
61static ALWAYS_INLINE bool atomic_ptr_cas(atomic_ptr_t *target,
62 atomic_ptr_val_t old_value,
63 atomic_ptr_val_t new_value)
64{
65 return Z_ARC_SEQ_CST(__atomic_compare_exchange_n(target, &old_value,
66 new_value, 0,
67 __ATOMIC_SEQ_CST,
68 __ATOMIC_SEQ_CST));
69}
70
72{
73 return Z_ARC_SEQ_CST(__atomic_fetch_add(target, value, __ATOMIC_SEQ_CST));
74}
75
77{
78 return Z_ARC_SEQ_CST(__atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST));
79}
80
82{
83 return atomic_add(target, 1);
84}
85
87{
88 return atomic_sub(target, 1);
89}
90
92{
93 return Z_ARC_SEQ_CST(__atomic_load_n(target, __ATOMIC_SEQ_CST));
94}
95
97{
98 return Z_ARC_SEQ_CST(__atomic_load_n(target, __ATOMIC_SEQ_CST));
99}
100
102{
103 return Z_ARC_SEQ_CST(__atomic_exchange_n(target, value, __ATOMIC_SEQ_CST));
104}
105
107 atomic_ptr_val_t value)
108{
109 return Z_ARC_SEQ_CST(__atomic_exchange_n(target, value, __ATOMIC_SEQ_CST));
110}
111
113{
114 return atomic_set(target, 0);
115}
116
118{
119 return atomic_ptr_set(target, NULL);
120}
121
123{
124 return Z_ARC_SEQ_CST(__atomic_fetch_or(target, value, __ATOMIC_SEQ_CST));
125}
126
128{
129 return Z_ARC_SEQ_CST(__atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST));
130}
131
133{
134 return Z_ARC_SEQ_CST(__atomic_fetch_and(target, value, __ATOMIC_SEQ_CST));
135}
136
138{
139 return Z_ARC_SEQ_CST(__atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST));
140}
141
143
144#ifdef __cplusplus
145}
146#endif
147
148#endif /* ZEPHYR_INCLUDE_ARCH_ARC_ATOMIC_ARC_H_ */
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.
atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value)
Atomic bitwise exclusive OR (XOR).
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.
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.
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
#define NULL
Definition iar_missing_defs.h:20
#define ALWAYS_INLINE
Definition common.h:180
Memory barrier operations.
Macros to abstract toolchain specific capabilities.