xref: /xnu-12377.41.6/libkern/os/atomic.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2019 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef __OS_ATOMIC_H__
30 #define __OS_ATOMIC_H__
31 
32 /*!
33  * @file <os/atomic.h>
34  *
35  * @brief
36  * Small header that helps write code that works with both C11 and C++11,
37  * or pre-C11 type declarations.
38  *
39  * @discussion
40  * The macros below allow to write code like this, that can be put in a header
41  * and will work with both C11 and C++11:
42  *
43  * <code>
44  * struct old_type {
45  *     int atomic_field;
46  * } old_variable;
47  *
48  * os_atomic_std(atomic_fetch_add_explicit)(
49  *     os_cast_to_atomic_pointer(&old_variable), 1,
50  *     os_atomic_std(memory_order_relaxed));
51  * </code>
52  */
53 
54 #include <os/base.h>
55 
56 #ifndef OS_ATOMIC_USES_CXX
57 #ifdef KERNEL
58 #define OS_ATOMIC_USES_CXX 0
59 #elif defined(__cplusplus) && __cplusplus >= 201103L
60 #define OS_ATOMIC_USES_CXX 1
61 #else
62 #define OS_ATOMIC_USES_CXX 0
63 #endif
64 #endif
65 
66 #if OS_ATOMIC_USES_CXX
67 #include <atomic>
68 #define OS_ATOMIC_STD                    std::
69 #define os_atomic_std(op)                std::op
70 #define os_atomic(type)                  std::atomic<type> volatile
71 #define os_cast_to_atomic_pointer(p)     os::cast_to_atomic_pointer(p)
72 #define os_atomic_basetypeof(p)          decltype(os_cast_to_atomic_pointer(p)->load())
73 #define os_cast_to_nonatomic_pointer(p)  os::cast_to_nonatomic_pointer(p)
74 #else /* !OS_ATOMIC_USES_CXX */
75 #include <stdatomic.h>
76 #define OS_ATOMIC_STD
77 #define os_atomic_std(op)                op
78 #define os_atomic(type)                  type volatile _Atomic
79 #if __has_ptrcheck
80 #define os_cast_to_atomic_pointer(p)     (__typeof__(*(p)) volatile _Atomic * __single)(p)
81 #define os_cast_to_nonatomic_pointer(p)                             \
82 	_Pragma("clang diagnostic push")                        \
83 	_Pragma("clang diagnostic ignored \"-Wcast-qual\"")     \
84 	(os_atomic_basetypeof(p) * __single)(p)                 \
85 	_Pragma("clang diagnostic pop")
86 #else /* !__has_ptrcheck */
87 #define os_cast_to_atomic_pointer(p)     (__typeof__(*(p)) volatile _Atomic *)(uintptr_t)(p)
88 #define os_cast_to_nonatomic_pointer(p)  (os_atomic_basetypeof(p) *)(uintptr_t)(p)
89 #endif /* !__has_ptrcheck */
90 #define os_atomic_basetypeof(p)          __typeof__(atomic_load(os_cast_to_atomic_pointer(p)))
91 
92 #endif /* !OS_ATOMIC_USES_CXX */
93 
94 /*!
95  * @group Internal implementation details
96  *
97  * @discussion The functions below are not intended to be used directly.
98  */
99 
100 #if OS_ATOMIC_USES_CXX
101 #include <type_traits>
102 
103 namespace os {
104 template <class T> using remove_volatile_t = typename std::remove_volatile<T>::type;
105 
106 template <class T>
107 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(T * v)108 cast_to_atomic_pointer(T *v)
109 {
110 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
111 }
112 
113 template <class T>
114 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(std::atomic<T> * v)115 cast_to_atomic_pointer(std::atomic<T> *v)
116 {
117 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
118 }
119 
120 template <class T>
121 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(volatile std::atomic<T> * v)122 cast_to_atomic_pointer(volatile std::atomic<T> *v)
123 {
124 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
125 }
126 
127 template <class T>
128 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(T * v)129 cast_to_nonatomic_pointer(T *v)
130 {
131 	return const_cast<remove_volatile_t<T> *>(v);
132 }
133 
134 template <class T>
135 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(std::atomic<T> * v)136 cast_to_nonatomic_pointer(std::atomic<T> *v)
137 {
138 	return reinterpret_cast<remove_volatile_t<T> *>(v);
139 }
140 
141 template <class T>
142 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(volatile std::atomic<T> * v)143 cast_to_nonatomic_pointer(volatile std::atomic<T> *v)
144 {
145 	auto _v = const_cast<std::atomic<T> *>(v);
146 	return reinterpret_cast<remove_volatile_t<T> *>(_v);
147 }
148 };
149 #endif /* OS_ATOMIC_USES_CXX */
150 
151 #endif /* __OS_ATOMIC_H__ */
152