]>
git.saurik.com Git - apple/xnu.git/blob - libkern/os/atomic_private.h
96ab5bb1430ad52d7610675dd40df0b7bbd9840e
2 * Copyright (c) 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 __OS_ATOMIC_PRIVATE_H__
30 #define __OS_ATOMIC_PRIVATE_H__
33 * @file <os/atomic_private.h>
36 * This file defines nicer (terser and safer) wrappers for C11's <stdatomic.h>.
39 * @see xnu.git::doc/atomics.md which provides more extensive documentation
42 * Note that some of the macros defined in this file may be overridden by
43 * architecture specific headers.
45 * All the os_atomic* functions take an operation ordering argument that can be:
46 * - C11 memory orders: relaxed, acquire, release, acq_rel or seq_cst which
47 * imply a memory fence on SMP machines, and always carry the matching
48 * compiler barrier semantics.
50 * - the os_atomic-specific `dependency` memory ordering that is used to
51 * document intent to a carry a data or address dependency.
52 * See doc/atomics.md for more information.
54 * - a compiler barrier: compiler_acquire, compiler_release, compiler_acq_rel
55 * without a corresponding memory fence.
58 #include <os/atomic.h>
61 * @group <os/atomic_private.h> tunables.
66 * @c OS_ATOMIC_CONFIG_* macros provide tunables for clients.
70 * @macro OS_ATOMIC_CONFIG_SMP
73 * Whether this is used on an SMP system, defaults to 1.
75 #ifndef OS_ATOMIC_CONFIG_SMP
76 #define OS_ATOMIC_CONFIG_SMP 1
77 #endif // OS_ATOMIC_CONFIG_SMP
80 * @macro OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
83 * Hide interfaces that can lead to starvation on certain hardware/build
87 * The following ABIs are currently supported by os_atomic:
88 * - i386 and x86_64: Intel atomics
89 * - armv7: load/store exclusive
90 * - armv8: load/store exclusive
91 * - armv8.1: armv8.1 style atomics
93 * On armv8 hardware with asymmetric cores, using load/store exclusive based
94 * atomics can lead to starvation in very hot code or non-preemptible context,
95 * and code that is sensitive to such must not use these interfaces.
97 * When OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY is set, any os_atomic_* interface
98 * that may cause starvation will be made unavailable to avoid accidental use.
101 * - XNU: builds per SoC, already safe
102 * - Kexts: default to avoid starvable interfaces by default
103 * - User: default to allow starvable interfaces by default
105 * Note: at this time, on Apple supported platforms, the only configuration
106 * that is affected by this would be for the "arm64" slices.
108 * Intel, armv7 variants, and the arm64e slice always are unaffected.
110 #ifndef OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
111 #if XNU_KERNEL_PRIVATE
112 #define OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY 0
114 #define OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY 1
116 #define OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY 0
118 #endif // OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
121 * @macro OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY
124 * Expose the os_atomic-specific fake `dependency` memory ordering.
127 * The dependency ordering can be used to try to "repair" C11's consume ordering
128 * and should be limited to extremely complex algorithms where every cycle counts.
130 * Due to the inherent risks (no compiler support) for this feature, it is
131 * reserved for expert and very domain-specific code only and is off by default.
135 #ifndef OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY
136 #if XNU_KERNEL_PRIVATE
137 #define OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY 1
139 #define OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY 0
141 #endif // OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY
146 * @group <os/atomic_private.h> features (arch specific).
151 * The @c OS_ATOMIC_USE_* and @c OS_ATOMIC_HAS_* defines expose some
152 * specificities of <os/atomic_private.h> implementation that are relevant to
153 * certain clients and can be used to conditionalize code.
157 * @const OS_ATOMIC_HAS_LLSC
160 * Whether the platform has LL/SC features.
163 * When set, the os_atomic_*_exclusive() macros are defined.
165 #if defined(__i386__) || defined(__x86_64__)
166 #define OS_ATOMIC_HAS_LLSC 0
167 #elif defined(__arm__) || defined(__arm64__)
168 #define OS_ATOMIC_HAS_LLSC 1
170 #error unsupported architecture
174 * @const OS_ATOMIC_USE_LLSC
177 * Whether os_atomic* use LL/SC internally.
180 * OS_ATOMIC_USE_LLSC implies OS_ATOMIC_HAS_LLSC.
182 #if defined(__arm64__) && defined(__ARM_ARCH_8_2__)
183 #define OS_ATOMIC_USE_LLSC 0
185 #define OS_ATOMIC_USE_LLSC OS_ATOMIC_HAS_LLSC
189 * @const OS_ATOMIC_HAS_STARVATION_FREE_RMW
192 * Whether os_atomic* Read-Modify-Write operations are starvation free
193 * in the current configuration.
195 #define OS_ATOMIC_HAS_STARVATION_FREE_RMW (!OS_ATOMIC_USE_LLSC)
199 #include "atomic_private_impl.h" // Internal implementation details
202 * @function os_compiler_barrier
205 * Provide a compiler barrier according to the specified ordering.
208 * An optional ordering among `acquire`, `release` or `acq_rel` which defaults
209 * to `acq_rel` when not specified.
210 * These are equivalent to the `compiler_acquire`, `compiler_release` and
211 * `compiler_acq_rel` orderings taken by the os_atomic* functions
213 #undef os_compiler_barrier
214 #define os_compiler_barrier(b...) \
215 os_atomic_std(atomic_signal_fence)(_os_compiler_barrier_##b)
218 * @function os_atomic_thread_fence
221 * Memory fence which is elided in non-SMP mode, but always carries the
222 * corresponding compiler barrier.
225 * The ordering for this fence.
227 #define os_atomic_thread_fence(m) ({ \
228 os_atomic_std(atomic_thread_fence)(_os_atomic_mo_##m##_smp); \
229 os_atomic_std(atomic_signal_fence)(_os_atomic_mo_##m); \
233 * @function os_atomic_init
236 * Wrapper for C11 atomic_init()
239 * This initialization is not performed atomically, and so must only be used as
240 * part of object initialization before the object is made visible to other
244 * A pointer to an atomic variable.
247 * The value to initialize the variable with.
250 * The value loaded from @a p.
252 #define os_atomic_init(p, v) \
253 os_atomic_std(atomic_init)(os_cast_to_atomic_pointer(p), v)
256 * @function os_atomic_load_is_plain, os_atomic_store_is_plain
259 * Return whether a relaxed atomic load (resp. store) to an atomic variable
260 * is implemented as a single plain load (resp. store) instruction.
263 * Non-relaxed loads/stores may involve additional memory fence instructions
264 * or more complex atomic instructions.
266 * This is a construct that can safely be used in static asserts.
268 * This doesn't check for alignment and it is assumed that `p` is
272 * A pointer to an atomic variable.
275 * True when relaxed atomic loads (resp. stores) compile to a plain load
276 * (resp. store) instruction, false otherwise.
278 #define os_atomic_load_is_plain(p) (sizeof(*(p)) <= sizeof(void *))
279 #define os_atomic_store_is_plain(p) os_atomic_load_is_plain(p)
282 * @function os_atomic_load
285 * Wrapper for C11 atomic_load_explicit(), guaranteed to compile to a single
286 * plain load instruction (when @a m is `relaxed`).
289 * A pointer to an atomic variable.
292 * The ordering to use.
295 * The value loaded from @a p.
297 #define os_atomic_load(p, m) ({ \
298 _Static_assert(os_atomic_load_is_plain(p), "Load is wide"); \
299 _os_compiler_barrier_before_atomic(m); \
300 __auto_type _r = os_atomic_std(atomic_load_explicit)( \
301 os_cast_to_atomic_pointer(p), _os_atomic_mo_##m##_smp); \
302 _os_compiler_barrier_after_atomic(m); \
307 * @function os_atomic_store
310 * Wrapper for C11 atomic_store_explicit(), guaranteed to compile to a single
311 * plain store instruction (when @a m is `relaxed`).
314 * A pointer to an atomic variable.
317 * The value to store.
320 * The ordering to use.
323 * The value stored at @a p.
325 #define os_atomic_store(p, v, m) ({ \
326 _Static_assert(os_atomic_store_is_plain(p), "Store is wide"); \
327 __auto_type _v = (v); \
328 _os_compiler_barrier_before_atomic(m); \
329 os_atomic_std(atomic_store_explicit)(os_cast_to_atomic_pointer(p), _v, \
330 _os_atomic_mo_##m##_smp); \
331 _os_compiler_barrier_after_atomic(m); \
335 #if OS_ATOMIC_HAS_STARVATION_FREE_RMW || !OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
338 * @function os_atomic_load_wide
341 * Wrapper for C11 atomic_load_explicit(), which may be implemented by a
342 * compare-exchange loop for double-wide variables.
345 * A pointer to an atomic variable.
348 * The ordering to use.
351 * The value loaded from @a p.
353 #define os_atomic_load_wide(p, m) ({ \
354 _os_compiler_barrier_before_atomic(m); \
355 __auto_type _r = os_atomic_std(atomic_load_explicit)( \
356 os_cast_to_atomic_pointer(p), _os_atomic_mo_##m##_smp); \
357 _os_compiler_barrier_after_atomic(m); \
362 * @function os_atomic_store_wide
365 * Wrapper for C11 atomic_store_explicit(), which may be implemented by a
366 * compare-exchange loop for double-wide variables.
369 * A pointer to an atomic variable.
372 * The value to store.
375 * The ordering to use.
378 * The value stored at @a p.
380 #define os_atomic_store_wide(p, v, m) ({ \
381 __auto_type _v = (v); \
382 _os_compiler_barrier_before_atomic(m); \
383 os_atomic_std(atomic_store_explicit)(os_cast_to_atomic_pointer(p), _v, \
384 _os_atomic_mo_##m##_smp); \
385 _os_compiler_barrier_after_atomic(m); \
390 * @function os_atomic_add, os_atomic_add_orig
393 * Wrappers for C11 atomic_fetch_add_explicit().
396 * A pointer to an atomic variable.
402 * The ordering to use.
405 * os_atomic_add_orig returns the value of the variable before the atomic add,
406 * os_atomic_add returns the value of the variable after the atomic add.
408 #define os_atomic_add_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_add)
409 #define os_atomic_add(p, v, m) _os_atomic_c11_op(p, v, m, fetch_add, +)
412 * @function os_atomic_inc, os_atomic_inc_orig
415 * Perform an atomic increment.
418 * A pointer to an atomic variable.
421 * The ordering to use.
424 * os_atomic_inc_orig returns the value of the variable before the atomic increment,
425 * os_atomic_inc returns the value of the variable after the atomic increment.
427 #define os_atomic_inc_orig(p, m) _os_atomic_c11_op_orig(p, 1, m, fetch_add)
428 #define os_atomic_inc(p, m) _os_atomic_c11_op(p, 1, m, fetch_add, +)
431 * @function os_atomic_sub, os_atomic_sub_orig
434 * Wrappers for C11 atomic_fetch_sub_explicit().
437 * A pointer to an atomic variable.
440 * The value to subtract.
443 * The ordering to use.
446 * os_atomic_sub_orig returns the value of the variable before the atomic subtract,
447 * os_atomic_sub returns the value of the variable after the atomic subtract.
449 #define os_atomic_sub_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_sub)
450 #define os_atomic_sub(p, v, m) _os_atomic_c11_op(p, v, m, fetch_sub, -)
453 * @function os_atomic_dec, os_atomic_dec_orig
456 * Perform an atomic decrement.
459 * A pointer to an atomic variable.
462 * The ordering to use.
465 * os_atomic_dec_orig returns the value of the variable before the atomic decrement,
466 * os_atomic_dec returns the value of the variable after the atomic decrement.
468 #define os_atomic_dec_orig(p, m) _os_atomic_c11_op_orig(p, 1, m, fetch_sub)
469 #define os_atomic_dec(p, m) _os_atomic_c11_op(p, 1, m, fetch_sub, -)
472 * @function os_atomic_and, os_atomic_and_orig
475 * Wrappers for C11 atomic_fetch_and_explicit().
478 * A pointer to an atomic variable.
484 * The ordering to use.
487 * os_atomic_and_orig returns the value of the variable before the atomic and,
488 * os_atomic_and returns the value of the variable after the atomic and.
490 #define os_atomic_and_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_and)
491 #define os_atomic_and(p, v, m) _os_atomic_c11_op(p, v, m, fetch_and, &)
494 * @function os_atomic_andnot, os_atomic_andnot_orig
497 * Wrappers for C11 atomic_fetch_and_explicit(p, ~value).
500 * A pointer to an atomic variable.
503 * The value whose complement to and.
506 * The ordering to use.
509 * os_atomic_andnot_orig returns the value of the variable before the atomic andnot,
510 * os_atomic_andnot returns the value of the variable after the atomic andnot.
512 #define os_atomic_andnot_orig(p, v, m) _os_atomic_c11_op_orig(p, (typeof(v))~(v), m, fetch_and)
513 #define os_atomic_andnot(p, v, m) _os_atomic_c11_op(p, (typeof(v))~(v), m, fetch_and, &)
516 * @function os_atomic_or, os_atomic_or_orig
519 * Wrappers for C11 atomic_fetch_or_explicit().
522 * A pointer to an atomic variable.
528 * The ordering to use.
531 * os_atomic_or_orig returns the value of the variable before the atomic or,
532 * os_atomic_or returns the value of the variable after the atomic or.
534 #define os_atomic_or_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_or)
535 #define os_atomic_or(p, v, m) _os_atomic_c11_op(p, v, m, fetch_or, |)
538 * @function os_atomic_xor, os_atomic_xor_orig
541 * Wrappers for C11 atomic_fetch_xor_explicit().
544 * A pointer to an atomic variable.
550 * The ordering to use.
553 * os_atomic_xor_orig returns the value of the variable before the atomic xor,
554 * os_atomic_xor returns the value of the variable after the atomic xor.
556 #define os_atomic_xor_orig(p, v, m) _os_atomic_c11_op_orig(p, v, m, fetch_xor)
557 #define os_atomic_xor(p, v, m) _os_atomic_c11_op(p, v, m, fetch_xor, ^)
560 * @function os_atomic_min, os_atomic_min_orig
563 * Wrappers for Clang's __atomic_fetch_min()
566 * A pointer to an atomic variable.
569 * The value to minimize.
572 * The ordering to use.
575 * os_atomic_min_orig returns the value of the variable before the atomic min,
576 * os_atomic_min returns the value of the variable after the atomic min.
578 #define os_atomic_min_orig(p, v, m) _os_atomic_clang_op_orig(p, v, m, fetch_min)
579 #define os_atomic_min(p, v, m) _os_atomic_clang_op(p, v, m, fetch_min, MIN)
582 * @function os_atomic_max, os_atomic_max_orig
585 * Wrappers for Clang's __atomic_fetch_max()
588 * A pointer to an atomic variable.
591 * The value to maximize.
594 * The ordering to use.
597 * os_atomic_max_orig returns the value of the variable before the atomic max,
598 * os_atomic_max returns the value of the variable after the atomic max.
600 #define os_atomic_max_orig(p, v, m) _os_atomic_clang_op_orig(p, v, m, fetch_max)
601 #define os_atomic_max(p, v, m) _os_atomic_clang_op(p, v, m, fetch_max, MAX)
604 * @function os_atomic_xchg
607 * Wrapper for C11 atomic_exchange_explicit().
610 * A pointer to an atomic variable.
613 * The value to exchange with.
616 * The ordering to use.
619 * The value of the variable before the exchange.
621 #define os_atomic_xchg(p, v, m) _os_atomic_c11_op_orig(p, v, m, exchange)
624 * @function os_atomic_cmpxchg
627 * Wrapper for C11 atomic_compare_exchange_strong_explicit().
630 * Loops around os_atomic_cmpxchg() may want to consider using the
631 * os_atomic_rmw_loop() construct instead to take advantage of the C11 weak
632 * compare-exchange operation.
635 * A pointer to an atomic variable.
638 * The value expected in the atomic variable.
641 * The value to store if the atomic variable has the expected value @a e.
644 * The ordering to use in case of success.
645 * The ordering in case of failure is always `relaxed`.
648 * 0 if the compare-exchange failed.
649 * 1 if the compare-exchange succeeded.
651 #define os_atomic_cmpxchg(p, e, v, m) ({ \
652 os_atomic_basetypeof(p) _r = (e); int _b; \
653 _os_compiler_barrier_before_atomic(m); \
654 _b = os_atomic_std(atomic_compare_exchange_strong_explicit)( \
655 os_cast_to_atomic_pointer(p), &_r, \
656 _os_atomic_value_cast(p, v), \
657 _os_atomic_mo_##m##_smp, _os_atomic_mo_relaxed); \
658 _os_compiler_barrier_after_atomic(m); \
663 * @function os_atomic_cmpxchgv
666 * Wrapper for C11 atomic_compare_exchange_strong_explicit().
669 * Loops around os_atomic_cmpxchgv() may want to consider using the
670 * os_atomic_rmw_loop() construct instead to take advantage of the C11 weak
671 * compare-exchange operation.
674 * A pointer to an atomic variable.
677 * The value expected in the atomic variable.
680 * The value to store if the atomic variable has the expected value @a e.
683 * A pointer to a location that is filled with the value that was present in
684 * the atomic variable before the compare-exchange (whether successful or not).
685 * This can be used to redrive compare-exchange loops.
688 * The ordering to use in case of success.
689 * The ordering in case of failure is always `relaxed`.
692 * 0 if the compare-exchange failed.
693 * 1 if the compare-exchange succeeded.
695 #define os_atomic_cmpxchgv(p, e, v, g, m) ({ \
696 os_atomic_basetypeof(p) _r = (e); int _b; \
697 _os_compiler_barrier_before_atomic(m); \
698 _b = os_atomic_std(atomic_compare_exchange_strong_explicit)( \
699 os_cast_to_atomic_pointer(p), &_r, \
700 _os_atomic_value_cast(p, v), \
701 _os_atomic_mo_##m##_smp, _os_atomic_mo_relaxed); \
702 _os_compiler_barrier_after_atomic(m); \
707 * @function os_atomic_rmw_loop
710 * Advanced read-modify-write construct to wrap compare-exchange loops.
713 * A pointer to an atomic variable to be modified.
716 * The name of the variable that will contain the original value of the atomic
717 * variable (reloaded every iteration of the loop).
720 * The name of the variable that will contain the new value to compare-exchange
721 * the atomic variable to (typically computed from @a ov every iteration of the
725 * The ordering to use in case of success.
726 * The ordering in case of failure is always `relaxed`.
729 * Code block that validates the value of @p ov and computes the new value of
730 * @p nv that the atomic variable will be compare-exchanged to in an iteration
733 * The loop can be aborted using os_atomic_rmw_loop_give_up(), e.g. when the
734 * value of @p ov is found to be "invalid" for the ovarall operation.
735 * `continue` cannot be used in this context.
737 * No stores to memory should be performed within the code block as it may cause
738 * LL/SC transactions used to implement compare-exchange to fail persistently.
741 * 0 if the loop was aborted with os_atomic_rmw_loop_give_up().
742 * 1 if the loop completed.
744 #define os_atomic_rmw_loop(p, ov, nv, m, ...) ({ \
746 __auto_type _p = os_cast_to_nonatomic_pointer(p); \
747 _os_compiler_barrier_before_atomic(m); \
751 _result = os_atomic_std(atomic_compare_exchange_weak_explicit)( \
752 os_cast_to_atomic_pointer(_p), &ov, nv, \
753 _os_atomic_mo_##m##_smp, _os_atomic_mo_relaxed); \
754 } while (__builtin_expect(!_result, 0)); \
755 _os_compiler_barrier_after_atomic(m); \
760 * @function os_atomic_rmw_loop_give_up
763 * Abort an os_atomic_rmw_loop() loop.
766 * Optional code block to execute before the `break` out of the loop. May
767 * further alter the control flow (e.g. using `return`, `goto`, ...).
769 #define os_atomic_rmw_loop_give_up(...) ({ __VA_ARGS__; break; })
771 #else // !OS_ATOMIC_HAS_STARVATION_FREE_RMW && OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
773 #define _os_atomic_error_is_starvable(name) \
774 _Static_assert(0, #name " is not starvation-free and isn't available in this configuration")
775 #define os_atomic_load_wide(p, m) _os_atomic_error_is_starvable(os_atomic_load_wide)
776 #define os_atomic_store_wide(p, v, m) _os_atomic_error_is_starvable(os_atomic_store_wide)
777 #define os_atomic_add_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_add_orig)
778 #define os_atomic_add(p, v, m) _os_atomic_error_is_starvable(os_atomic_add)
779 #define os_atomic_inc_orig(p, m) _os_atomic_error_is_starvable(os_atomic_inc_orig)
780 #define os_atomic_inc(p, m) _os_atomic_error_is_starvable(os_atomic_inc)
781 #define os_atomic_sub_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_sub_orig)
782 #define os_atomic_sub(p, v, m) _os_atomic_error_is_starvable(os_atomic_sub)
783 #define os_atomic_dec_orig(p, m) _os_atomic_error_is_starvable(os_atomic_dec_orig)
784 #define os_atomic_dec(p, m) _os_atomic_error_is_starvable(os_atomic_dec)
785 #define os_atomic_and_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_and_orig)
786 #define os_atomic_and(p, v, m) _os_atomic_error_is_starvable(os_atomic_and)
787 #define os_atomic_andnot_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_andnot_orig)
788 #define os_atomic_andnot(p, v, m) _os_atomic_error_is_starvable(os_atomic_andnot)
789 #define os_atomic_or_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_or_orig)
790 #define os_atomic_or(p, v, m) _os_atomic_error_is_starvable(os_atomic_or)
791 #define os_atomic_xor_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_xor_orig)
792 #define os_atomic_xor(p, v, m) _os_atomic_error_is_starvable(os_atomic_xor)
793 #define os_atomic_min_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_min_orig)
794 #define os_atomic_min(p, v, m) _os_atomic_error_is_starvable(os_atomic_min)
795 #define os_atomic_max_orig(p, v, m) _os_atomic_error_is_starvable(os_atomic_max_orig)
796 #define os_atomic_max(p, v, m) _os_atomic_error_is_starvable(os_atomic_max)
797 #define os_atomic_xchg(p, v, m) _os_atomic_error_is_starvable(os_atomic_xchg)
798 #define os_atomic_cmpxchg(p, e, v, m) _os_atomic_error_is_starvable(os_atomic_cmpxchg)
799 #define os_atomic_cmpxchgv(p, e, v, g, m) _os_atomic_error_is_starvable(os_atomic_cmpxchgv)
800 #define os_atomic_rmw_loop(p, ov, nv, m, ...) _os_atomic_error_is_starvable(os_atomic_rmw_loop)
801 #define os_atomic_rmw_loop_give_up(...) _os_atomic_error_is_starvable(os_atomic_rmw_loop_give_up)
803 #endif // !OS_ATOMIC_HAS_STARVATION_FREE_RMW && OS_ATOMIC_CONFIG_STARVATION_FREE_ONLY
805 #if OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY
808 * @typedef os_atomic_dependency_t
811 * Type for dependency tokens that can be derived from loads with dependency
812 * and injected into various expressions.
815 * The implementation of atomic dependencies makes painstakingly sure that the
816 * compiler doesn't know that os_atomic_dependency_t::__opaque_zero is always 0.
818 * Users of os_atomic_dependency_t MUST NOT test its value (even with an
819 * assert), as doing so would allow the compiler to reason about the value and
820 * elide its use to inject hardware dependencies (thwarting the entire purpose
823 typedef struct { unsigned long __opaque_zero
; } os_atomic_dependency_t
;
826 * @const OS_ATOMIC_DEPENDENCY_NONE
829 * A value to pass to functions that can carry dependencies, to indicate that
830 * no dependency should be carried.
832 #define OS_ATOMIC_DEPENDENCY_NONE \
833 ((os_atomic_dependency_t){ 0UL })
836 * @function os_atomic_make_dependency
839 * Create a dependency token that can be injected into expressions to force a
840 * hardware dependency.
843 * This function is only useful for cases where the dependency needs to be used
846 * os_atomic_load_with_dependency_on() and os_atomic_inject_dependency() are
847 * otherwise capable of automatically creating dependency tokens.
851 * - an os_atomic_load(..., dependency),
852 * - an os_atomic_inject_dependency(),
853 * - an os_atomic_load_with_dependency_on().
855 * Note that due to implementation limitations, the type of @p v must be
856 * register-sized, if necessary an explicit cast is required.
859 * An os_atomic_dependency_t token that can be used to prolongate dependency
862 * The token value is always 0, but the compiler must never be able to reason
863 * about that fact (c.f. os_atomic_dependency_t)
865 #define os_atomic_make_dependency(v) \
866 ((void)(v), OS_ATOMIC_DEPENDENCY_NONE)
869 * @function os_atomic_inject_dependency
872 * Inject a hardware dependency resulting from a `dependency` load into a
876 * A pointer to inject the dependency into.
879 * - a dependency token returned from os_atomic_make_dependency(),
881 * - OS_ATOMIC_DEPENDENCY_NONE, which turns this operation into a no-op,
883 * - any value accepted by os_atomic_make_dependency().
886 * A value equal to @a p but that prolongates the dependency chain rooted at
889 #define os_atomic_inject_dependency(p, e) \
890 ((typeof(*(p)) *)((p) + _os_atomic_auto_dependency(e).__opaque_zero))
893 * @function os_atomic_load_with_dependency_on
896 * Load that prolongates the dependency chain rooted at `v`.
899 * This is shorthand for:
902 * os_atomic_load(os_atomic_inject_dependency(p, e), dependency)
906 * A pointer to an atomic variable.
909 * - a dependency token returned from os_atomic_make_dependency(),
911 * - OS_ATOMIC_DEPENDENCY_NONE, which turns this operation into a no-op,
913 * - any value accepted by os_atomic_make_dependency().
916 * The value loaded from @a p.
918 #define os_atomic_load_with_dependency_on(p, e) \
919 os_atomic_load(os_atomic_inject_dependency(p, e), dependency)
921 #endif // OS_ATOMIC_CONFIG_MEMORY_ORDER_DEPENDENCY
923 #include "atomic_private_arch.h" // Per architecture overrides
925 #endif /* __OS_ATOMIC_PRIVATE_H__ */