]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/machine/atomic.h
2 * Copyright (c) 2015-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _MACHINE_ATOMIC_H
30 #define _MACHINE_ATOMIC_H
33 * Internal implementation details are in a separate header
35 #include <machine/atomic_impl.h>
38 * @file <machine/atomic.h>
41 * This file defines nicer (terser and safer) wrappers for C11's <stdatomic.h>.
44 * @see xnu.git::doc/atomics.md which provides more extensive documentation
47 * Note that some of the macros defined in this file may be overridden by
48 * architecture specific headers.
50 * All the os_atomic* functions take an operation ordering argument that can be:
51 * - C11 memory orders: relaxed, acquire, release, acq_rel or seq_cst which
52 * imply a memory fence on SMP machines, and always carry the matching
53 * compiler barrier semantics.
55 * - the os_atomic-specific `dependency` memory ordering that is used to
56 * document intent to a carry a data or address dependency.
57 * See doc/atomics.md for more information.
59 * - a compiler barrier: compiler_acquire, compiler_release, compiler_acq_rel
60 * without a corresponding memory fence.
64 * @function os_compiler_barrier
67 * Provide a compiler barrier according to the specified ordering.
70 * An optional ordering among `acquire`, `release` or `acq_rel` which defaults
71 * to `acq_rel` when not specified.
72 * These are equivalent to the `compiler_acquire`, `compiler_release` and
73 * `compiler_acq_rel` orderings taken by the os_atomic* functions
75 #define os_compiler_barrier(b...) \
76 atomic_signal_fence(_os_compiler_barrier_##b)
79 * @function os_atomic_thread_fence
82 * Memory fence which is elided in non-SMP mode, but always carries the
83 * corresponding compiler barrier.
86 * The ordering for this fence.
88 #define os_atomic_thread_fence(m) ({ \
89 atomic_thread_fence(memory_order_##m##_smp); \
90 atomic_signal_fence(memory_order_##m); \
94 * @function os_atomic_init
97 * Wrapper for C11 atomic_init()
100 * This initialization is not performed atomically, and so must only be used as
101 * part of object initialization before the object is made visible to other
105 * A pointer to an atomic variable.
108 * The value to initialize the variable with.
111 * The value loaded from @a p.
113 #define os_atomic_init(p, v) \
114 atomic_init(_os_atomic_c11_atomic(p), v)
117 * @function os_atomic_load_is_plain, os_atomic_store_is_plain
120 * Return whether a relaxed atomic load (resp. store) to an atomic variable
121 * is implemented as a single plain load (resp. store) instruction.
124 * Non-relaxed loads/stores may involve additional memory fence instructions
125 * or more complex atomic instructions.
127 * This is a construct that can safely be used in static asserts.
130 * A pointer to an atomic variable.
133 * True when relaxed atomic loads (resp. stores) compile to a plain load
134 * (resp. store) instruction, false otherwise.
136 #define os_atomic_load_is_plain(p) (sizeof(*(p)) <= sizeof(void *))
137 #define os_atomic_store_is_plain(p) os_atomic_load_is_plain(p)
140 * @function os_atomic_load
143 * Wrapper for C11 atomic_load_explicit(), guaranteed to compile to a single
144 * plain load instruction (when @a m is `relaxed`).
147 * A pointer to an atomic variable.
150 * The ordering to use.
153 * The value loaded from @a p.
155 #define os_atomic_load(p, m) ({ \
156 _Static_assert(os_atomic_load_is_plain(p), "Load is wide"); \
157 _os_atomic_basetypeof(p) _r; \
158 _os_compiler_barrier_before_atomic(m); \
159 _r = atomic_load_explicit(_os_atomic_c11_atomic(p), \
160 memory_order_##m##_smp); \
161 _os_compiler_barrier_after_atomic(m); \
166 * @function os_atomic_load_wide
169 * Wrapper for C11 atomic_load_explicit(), which may be implemented by a
170 * compare-exchange loop for double-wide variables.
173 * A pointer to an atomic variable.
176 * The ordering to use.
179 * The value loaded from @a p.
181 #define os_atomic_load_wide(p, m) ({ \
182 _os_atomic_basetypeof(p) _r; \
183 _os_compiler_barrier_before_atomic(m); \
184 _r = atomic_load_explicit(_os_atomic_c11_atomic(p), \
185 memory_order_##m##_smp); \
186 _os_compiler_barrier_after_atomic(m); \
191 * @function os_atomic_store
194 * Wrapper for C11 atomic_store_explicit(), guaranteed to compile to a single
195 * plain store instruction (when @a m is `relaxed`).
198 * A pointer to an atomic variable.
201 * The value to store.
204 * The ordering to use.
207 * The value stored at @a p.
209 #define os_atomic_store(p, v, m) ({ \
210 _Static_assert(os_atomic_store_is_plain(p), "Store is wide"); \
211 _os_atomic_basetypeof(p) _v = (v); \
212 _os_compiler_barrier_before_atomic(m); \
213 atomic_store_explicit(_os_atomic_c11_atomic(p), _v, \
214 memory_order_##m##_smp); \
215 _os_compiler_barrier_after_atomic(m); \
220 * @function os_atomic_store_wide
223 * Wrapper for C11 atomic_store_explicit(), which may be implemented by a
224 * compare-exchange loop for double-wide variables.
227 * A pointer to an atomic variable.
230 * The value to store.
233 * The ordering to use.
236 * The value stored at @a p.
238 #define os_atomic_store_wide(p, v, m) ({ \
239 _os_atomic_basetypeof(p) _v = (v); \
240 _os_compiler_barrier_before_atomic(m); \
241 atomic_store_explicit(_os_atomic_c11_atomic(p), _v, \
242 memory_order_##m##_smp); \
243 _os_compiler_barrier_after_atomic(m); \
248 * @function os_atomic_add, os_atomic_add_orig
251 * Wrappers for C11 atomic_fetch_add_explicit().
254 * A pointer to an atomic variable.
260 * The ordering to use.
263 * os_atomic_add_orig returns the value of the variable before the atomic add,
264 * os_atomic_add returns the value of the variable after the atomic add.
266 #define os_atomic_add_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_add)
267 #define os_atomic_add(p, v, m) _os_atomic_c11_op(p, v, m, fetch_add, +)
270 * @function os_atomic_inc, os_atomic_inc_orig
273 * Perform an atomic increment.
276 * A pointer to an atomic variable.
279 * The ordering to use.
282 * os_atomic_inc_orig returns the value of the variable before the atomic increment,
283 * os_atomic_inc returns the value of the variable after the atomic increment.
285 #define os_atomic_inc_orig(p, m) _os_atomic_c11_op_orig(p, 1, m, fetch_add)
286 #define os_atomic_inc(p, m) _os_atomic_c11_op(p, 1, m, fetch_add, +)
289 * @function os_atomic_sub, os_atomic_sub_orig
292 * Wrappers for C11 atomic_fetch_sub_explicit().
295 * A pointer to an atomic variable.
298 * The value to subtract.
301 * The ordering to use.
304 * os_atomic_sub_orig returns the value of the variable before the atomic subtract,
305 * os_atomic_sub returns the value of the variable after the atomic subtract.
307 #define os_atomic_sub_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_sub)
308 #define os_atomic_sub(p, v, m) _os_atomic_c11_op(p, v, m, fetch_sub, -)
311 * @function os_atomic_dec, os_atomic_dec_orig
314 * Perform an atomic decrement.
317 * A pointer to an atomic variable.
320 * The ordering to use.
323 * os_atomic_dec_orig returns the value of the variable before the atomic decrement,
324 * os_atomic_dec returns the value of the variable after the atomic decrement.
326 #define os_atomic_dec_orig(p, m) _os_atomic_c11_op_orig(p, 1, m, fetch_sub)
327 #define os_atomic_dec(p, m) _os_atomic_c11_op(p, 1, m, fetch_sub, -)
330 * @function os_atomic_and, os_atomic_and_orig
333 * Wrappers for C11 atomic_fetch_and_explicit().
336 * A pointer to an atomic variable.
342 * The ordering to use.
345 * os_atomic_and_orig returns the value of the variable before the atomic and,
346 * os_atomic_and returns the value of the variable after the atomic and.
348 #define os_atomic_and_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_and)
349 #define os_atomic_and(p, v, m) _os_atomic_c11_op(p, v, m, fetch_and, &)
352 * @function os_atomic_andnot, os_atomic_andnot_orig
355 * Wrappers for C11 atomic_fetch_and_explicit(p, ~value).
358 * A pointer to an atomic variable.
361 * The value whose complement to and.
364 * The ordering to use.
367 * os_atomic_andnot_orig returns the value of the variable before the atomic andnot,
368 * os_atomic_andnot returns the value of the variable after the atomic andnot.
370 #define os_atomic_andnot_orig(p, v, m) _os_atomic_c11_op_orig(p, ~(v), m, fetch_and)
371 #define os_atomic_andnot(p, v, m) _os_atomic_c11_op(p, ~(v), m, fetch_and, &)
374 * @function os_atomic_or, os_atomic_or_orig
377 * Wrappers for C11 atomic_fetch_or_explicit().
380 * A pointer to an atomic variable.
386 * The ordering to use.
389 * os_atomic_or_orig returns the value of the variable before the atomic or,
390 * os_atomic_or returns the value of the variable after the atomic or.
392 #define os_atomic_or_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_or)
393 #define os_atomic_or(p, v, m) _os_atomic_c11_op(p, v, m, fetch_or, |)
396 * @function os_atomic_xor, os_atomic_xor_orig
399 * Wrappers for C11 atomic_fetch_xor_explicit().
402 * A pointer to an atomic variable.
408 * The ordering to use.
411 * os_atomic_xor_orig returns the value of the variable before the atomic xor,
412 * os_atomic_xor returns the value of the variable after the atomic xor.
414 #define os_atomic_xor_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_xor)
415 #define os_atomic_xor(p, v, m) _os_atomic_c11_op(p, v, m, fetch_xor, ^)
418 * @function os_atomic_min, os_atomic_min_orig
421 * Wrappers for Clang's __atomic_fetch_min()
424 * A pointer to an atomic variable.
427 * The value to minimize.
430 * The ordering to use.
433 * os_atomic_min_orig returns the value of the variable before the atomic min,
434 * os_atomic_min returns the value of the variable after the atomic min.
436 #define os_atomic_min_orig(p, v, m) _os_atomic_clang_op_orig(p, v, m, fetch_min)
437 #define os_atomic_min(p, v, m) _os_atomic_clang_op(p, v, m, fetch_min, MIN)
440 * @function os_atomic_max, os_atomic_max_orig
443 * Wrappers for Clang's __atomic_fetch_max()
446 * A pointer to an atomic variable.
449 * The value to maximize.
452 * The ordering to use.
455 * os_atomic_max_orig returns the value of the variable before the atomic max,
456 * os_atomic_max returns the value of the variable after the atomic max.
458 #define os_atomic_max_orig(p, v, m) _os_atomic_clang_op_orig(p, v, m, fetch_max)
459 #define os_atomic_max(p, v, m) _os_atomic_clang_op(p, v, m, fetch_max, MAX)
462 * @function os_atomic_xchg
465 * Wrapper for C11 atomic_exchange_explicit().
468 * A pointer to an atomic variable.
471 * The value to exchange with.
474 * The ordering to use.
477 * The value of the variable before the exchange.
479 #define os_atomic_xchg(p, v, m) _os_atomic_c11_op_orig(p, v, m, exchange)
482 * @function os_atomic_cmpxchg
485 * Wrapper for C11 atomic_compare_exchange_strong_explicit().
488 * Loops around os_atomic_cmpxchg() may want to consider using the
489 * os_atomic_rmw_loop() construct instead to take advantage of the C11 weak
490 * compare-exchange operation.
493 * A pointer to an atomic variable.
496 * The value expected in the atomic variable.
499 * The value to store if the atomic variable has the expected value @a e.
502 * The ordering to use in case of success.
503 * The ordering in case of failure is always `relaxed`.
506 * 0 if the compare-exchange failed.
507 * 1 if the compare-exchange succeeded.
509 #define os_atomic_cmpxchg(p, e, v, m) ({ \
510 _os_atomic_basetypeof(p) _r = (e); int _b; \
511 _os_compiler_barrier_before_atomic(m); \
512 _b = atomic_compare_exchange_strong_explicit(_os_atomic_c11_atomic(p), \
513 &_r, v, memory_order_##m##_smp, memory_order_relaxed); \
514 _os_compiler_barrier_after_atomic(m); \
519 * @function os_atomic_cmpxchgv
522 * Wrapper for C11 atomic_compare_exchange_strong_explicit().
525 * Loops around os_atomic_cmpxchgv() may want to consider using the
526 * os_atomic_rmw_loop() construct instead to take advantage of the C11 weak
527 * compare-exchange operation.
530 * A pointer to an atomic variable.
533 * The value expected in the atomic variable.
536 * The value to store if the atomic variable has the expected value @a e.
539 * A pointer to a location that is filled with the value that was present in
540 * the atomic variable before the compare-exchange (whether successful or not).
541 * This can be used to redrive compare-exchange loops.
544 * The ordering to use in case of success.
545 * The ordering in case of failure is always `relaxed`.
548 * 0 if the compare-exchange failed.
549 * 1 if the compare-exchange succeeded.
551 #define os_atomic_cmpxchgv(p, e, v, g, m) ({ \
552 _os_atomic_basetypeof(p) _r = (e); int _b; \
553 _os_compiler_barrier_before_atomic(m); \
554 _b = atomic_compare_exchange_strong_explicit(_os_atomic_c11_atomic(p), \
555 &_r, v, memory_order_##m##_smp, memory_order_relaxed); \
556 _os_compiler_barrier_after_atomic(m); \
561 * @function os_atomic_rmw_loop
564 * Advanced read-modify-write construct to wrap compare-exchange loops.
567 * A pointer to an atomic variable to be modified.
570 * The name of the variable that will contain the original value of the atomic
571 * variable (reloaded every iteration of the loop).
574 * The name of the variable that will contain the new value to compare-exchange
575 * the atomic variable to (typically computed from @a ov every iteration of the
579 * The ordering to use in case of success.
580 * The ordering in case of failure is always `relaxed`.
583 * Code block that validates the value of @p ov and computes the new value of
584 * @p nv that the atomic variable will be compare-exchanged to in an iteration
587 * The loop can be aborted using os_atomic_rmw_loop_give_up(), e.g. when the
588 * value of @p ov is found to be "invalid" for the ovarall operation.
589 * `continue` cannot be used in this context.
591 * No stores to memory should be performed within the code block as it may cause
592 * LL/SC transactions used to implement compare-exchange to fail persistently.
595 * 0 if the loop was aborted with os_atomic_rmw_loop_give_up().
596 * 1 if the loop completed.
598 #define os_atomic_rmw_loop(p, ov, nv, m, ...) ({ \
600 typeof(p) _p = (p); \
601 _os_compiler_barrier_before_atomic(m); \
602 ov = atomic_load_explicit(_os_atomic_c11_atomic(_p), \
603 memory_order_relaxed); \
606 _result = atomic_compare_exchange_weak_explicit( \
607 _os_atomic_c11_atomic(_p), &ov, nv, \
608 memory_order_##m##_smp, memory_order_relaxed); \
609 } while (__builtin_expect(!_result, 0)); \
610 _os_compiler_barrier_after_atomic(m); \
615 * @function os_atomic_rmw_loop_give_up
618 * Abort an os_atomic_rmw_loop() loop.
621 * Optional code block to execute before the `break` out of the loop. May
622 * further alter the control flow (e.g. using `return`, `goto`, ...).
624 #define os_atomic_rmw_loop_give_up(...) ({ __VA_ARGS__; break; })
627 * @typedef os_atomic_dependency_t
630 * Type for dependency tokens that can be derived from loads with dependency
631 * and injected into various expressions.
634 * The implementation of atomic dependencies makes painstakingly sure that the
635 * compiler doesn't know that os_atomic_dependency_t::__opaque_zero is always 0.
637 * Users of os_atomic_dependency_t MUST NOT test its value (even with an
638 * assert), as doing so would allow the compiler to reason about the value and
639 * elide its use to inject hardware dependencies (thwarting the entire purpose
642 typedef struct { unsigned long __opaque_zero
; } os_atomic_dependency_t
;
645 * @const OS_ATOMIC_DEPENDENCY_NONE
648 * A value to pass to functions that can carry dependencies, to indicate that
649 * no dependency should be carried.
651 #define OS_ATOMIC_DEPENDENCY_NONE \
652 ((os_atomic_dependency_t){ 0UL })
655 * @function os_atomic_make_dependency
658 * Create a dependency token that can be injected into expressions to force a
659 * hardware dependency.
662 * This function is only useful for cases where the dependency needs to be used
665 * os_atomic_load_with_dependency_on() and os_atomic_inject_dependency() are
666 * otherwise capable of automatically creating dependency tokens.
670 * - an os_atomic_load(..., dependency),
671 * - an os_atomic_inject_dependency(),
672 * - an os_atomic_load_with_dependency_on().
674 * Note that due to implementation limitations, the type of @p v must be
675 * register-sized, if necessary an explicit cast is required.
678 * An os_atomic_dependency_t token that can be used to prolongate dependency
681 * The token value is always 0, but the compiler must never be able to reason
682 * about that fact (c.f. os_atomic_dependency_t)
684 #define os_atomic_make_dependency(v) \
685 ((void)(v), OS_ATOMIC_DEPENDENCY_NONE)
688 * @function os_atomic_inject_dependency
691 * Inject a hardware dependency resulting from a `dependency` load into a
695 * A pointer to inject the dependency into.
698 * - a dependency token returned from os_atomic_make_dependency(),
700 * - OS_ATOMIC_DEPENDENCY_NONE, which turns this operation into a no-op,
702 * - any value accepted by os_atomic_make_dependency().
705 * A value equal to @a p but that prolongates the dependency chain rooted at
708 #define os_atomic_inject_dependency(p, e) \
709 ((typeof(*(p)) *)((p) + _os_atomic_auto_dependency(e).__opaque_zero))
712 * @function os_atomic_load_with_dependency_on
715 * Load that prolongates the dependency chain rooted at `v`.
718 * This is shorthand for:
721 * os_atomic_load(os_atomic_inject_dependency(p, e), dependency)
725 * A pointer to an atomic variable.
728 * - a dependency token returned from os_atomic_make_dependency(),
730 * - OS_ATOMIC_DEPENDENCY_NONE, which turns this operation into a no-op,
732 * - any value accepted by os_atomic_make_dependency().
735 * The value loaded from @a p.
737 #define os_atomic_load_with_dependency_on(p, e) \
738 os_atomic_load(os_atomic_inject_dependency(p, e), dependency)
741 * @const OS_ATOMIC_HAS_LLSC
744 * Whether the platform has LL/SC features.
747 * When set, the os_atomic_*_exclusive() macros are defined.
749 #define OS_ATOMIC_HAS_LLSC 0
752 * @const OS_ATOMIC_USE_LLSC
755 * Whether os_atomic* use LL/SC internally.
758 * OS_ATOMIC_USE_LLSC implies OS_ATOMIC_HAS_LLSC.
760 #define OS_ATOMIC_USE_LLSC 0
762 #if defined (__x86_64__)
763 #include "i386/atomic.h"
764 #elif defined (__arm__) || defined (__arm64__)
765 #include "arm/atomic.h"
767 #error architecture not supported
770 #endif /* _MACHINE_ATOMIC_H */