xref: /xnu-10002.61.3/libkern/os/atomic.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
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 #define os_cast_to_atomic_pointer(p)     (__typeof__(*(p)) volatile _Atomic *)(uintptr_t)(p)
80 #define os_atomic_basetypeof(p)          __typeof__(atomic_load(os_cast_to_atomic_pointer(p)))
81 #define os_cast_to_nonatomic_pointer(p)  (os_atomic_basetypeof(p) *)(uintptr_t)(p)
82 #endif /* !OS_ATOMIC_USES_CXX */
83 
84 /*!
85  * @group Internal implementation details
86  *
87  * @discussion The functions below are not intended to be used directly.
88  */
89 
90 #if OS_ATOMIC_USES_CXX
91 #include <type_traits>
92 
93 namespace os {
94 template <class T> using remove_volatile_t = typename std::remove_volatile<T>::type;
95 
96 template <class T>
97 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(T * v)98 cast_to_atomic_pointer(T *v)
99 {
100 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
101 }
102 
103 template <class T>
104 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(std::atomic<T> * v)105 cast_to_atomic_pointer(std::atomic<T> *v)
106 {
107 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
108 }
109 
110 template <class T>
111 inline volatile std::atomic<remove_volatile_t<T> > *
cast_to_atomic_pointer(volatile std::atomic<T> * v)112 cast_to_atomic_pointer(volatile std::atomic<T> *v)
113 {
114 	return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
115 }
116 
117 template <class T>
118 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(T * v)119 cast_to_nonatomic_pointer(T *v)
120 {
121 	return const_cast<remove_volatile_t<T> *>(v);
122 }
123 
124 template <class T>
125 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(std::atomic<T> * v)126 cast_to_nonatomic_pointer(std::atomic<T> *v)
127 {
128 	return reinterpret_cast<remove_volatile_t<T> *>(v);
129 }
130 
131 template <class T>
132 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(volatile std::atomic<T> * v)133 cast_to_nonatomic_pointer(volatile std::atomic<T> *v)
134 {
135 	auto _v = const_cast<std::atomic<T> *>(v);
136 	return reinterpret_cast<remove_volatile_t<T> *>(_v);
137 }
138 };
139 #endif /* OS_ATOMIC_USES_CXX */
140 
141 #endif /* __OS_ATOMIC_H__ */
142