xref: /xnu-8792.61.2/libkern/os/atomic.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
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 add_volatile_t = typename std::add_volatile<T>::type;
95 template <class T> using remove_volatile_t = typename std::remove_volatile<T>::type;
96 
97 template <class T>
98 inline add_volatile_t<std::atomic<remove_volatile_t<T> > > *
cast_to_atomic_pointer(T * v)99 cast_to_atomic_pointer(T *v)
100 {
101 	return reinterpret_cast<add_volatile_t<std::atomic<remove_volatile_t<T> > > *>(v);
102 }
103 
104 template <class T>
105 inline add_volatile_t<std::atomic<remove_volatile_t<T> > > *
cast_to_atomic_pointer(std::atomic<T> * v)106 cast_to_atomic_pointer(std::atomic<T> *v)
107 {
108 	return reinterpret_cast<add_volatile_t<std::atomic<remove_volatile_t<T> > > *>(v);
109 }
110 
111 template <class T>
112 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(T * v)113 cast_to_nonatomic_pointer(T *v)
114 {
115 	return const_cast<remove_volatile_t<T> *>(v);
116 }
117 
118 template <class T>
119 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(std::atomic<T> * v)120 cast_to_nonatomic_pointer(std::atomic<T> *v)
121 {
122 	return reinterpret_cast<remove_volatile_t<T> *>(v);
123 }
124 
125 template <class T>
126 inline remove_volatile_t<T> *
cast_to_nonatomic_pointer(volatile std::atomic<T> * v)127 cast_to_nonatomic_pointer(volatile std::atomic<T> *v)
128 {
129 	auto _v = const_cast<std::atomic<T> *>(v);
130 	return reinterpret_cast<remove_volatile_t<T> *>(_v);
131 }
132 };
133 #endif /* OS_ATOMIC_USES_CXX */
134 
135 #endif /* __OS_ATOMIC_H__ */
136