1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Atomic operations usable in machine independent code */
3 #ifndef _LINUX_ATOMIC_H
4 #define _LINUX_ATOMIC_H
5 #include <linux/types.h>
7 #include <asm/atomic.h>
8 #include <asm/barrier.h>
11 * Relaxed variants of xchg, cmpxchg and some atomic operations.
13 * We support four variants:
15 * - Fully ordered: The default implementation, no suffix required.
16 * - Acquire: Provides ACQUIRE semantics, _acquire suffix.
17 * - Release: Provides RELEASE semantics, _release suffix.
18 * - Relaxed: No ordering guarantees, _relaxed suffix.
20 * For compound atomics performing both a load and a store, ACQUIRE
21 * semantics apply only to the load and RELEASE semantics only to the
22 * store portion of the operation. Note that a failed cmpxchg_acquire
23 * does -not- imply any memory ordering constraints.
25 * See Documentation/memory-barriers.txt for ACQUIRE/RELEASE definitions.
29 * The idea here is to build acquire/release variants by adding explicit
30 * barriers on top of the relaxed variant. In the case where the relaxed
31 * variant is already fully ordered, no additional barriers are needed.
33 * If an architecture overrides __atomic_acquire_fence() it will probably
34 * want to define smp_mb__after_spinlock().
36 #ifndef __atomic_acquire_fence
37 #define __atomic_acquire_fence smp_mb__after_atomic
40 #ifndef __atomic_release_fence
41 #define __atomic_release_fence smp_mb__before_atomic
44 #ifndef __atomic_pre_full_fence
45 #define __atomic_pre_full_fence smp_mb__before_atomic
48 #ifndef __atomic_post_full_fence
49 #define __atomic_post_full_fence smp_mb__after_atomic
52 #define __atomic_op_acquire(op, args...) \
54 typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \
55 __atomic_acquire_fence(); \
59 #define __atomic_op_release(op, args...) \
61 __atomic_release_fence(); \
65 #define __atomic_op_fence(op, args...) \
67 typeof(op##_relaxed(args)) __ret; \
68 __atomic_pre_full_fence(); \
69 __ret = op##_relaxed(args); \
70 __atomic_post_full_fence(); \
74 #include <linux/atomic-fallback.h>
76 #include <asm-generic/atomic-long.h>
78 #endif /* _LINUX_ATOMIC_H */