]>
git.saurik.com Git - apple/xnu.git/blob - libkern/os/atomic.h
2 * Copyright (c) 2019 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_H__
30 #define __OS_ATOMIC_H__
36 * Small header that helps write code that works with both C11 and C++11,
37 * or pre-C11 type declarations.
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:
48 * os_atomic_std(atomic_fetch_add_explicit)(
49 * os_cast_to_atomic_pointer(&old_variable), 1,
50 * os_atomic_std(memory_order_relaxed));
56 #ifndef OS_ATOMIC_USES_CXX
58 #define OS_ATOMIC_USES_CXX 0
59 #elif defined(__cplusplus) && __cplusplus >= 201103L
60 #define OS_ATOMIC_USES_CXX 1
62 #define OS_ATOMIC_USES_CXX 0
66 #if OS_ATOMIC_USES_CXX
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>
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 */
85 * @group Internal implementation details
87 * @discussion The functions below are not intended to be used directly.
90 #if OS_ATOMIC_USES_CXX
91 #include <type_traits>
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
;
98 inline add_volatile_t
<std::atomic
<remove_volatile_t
<T
> > > *
99 cast_to_atomic_pointer(T
*v
)
101 return reinterpret_cast<add_volatile_t
<std::atomic
<remove_volatile_t
<T
> > > *>(v
);
105 inline add_volatile_t
<std::atomic
<remove_volatile_t
<T
> > > *
106 cast_to_atomic_pointer(std::atomic
<T
> *v
)
108 return reinterpret_cast<add_volatile_t
<std::atomic
<remove_volatile_t
<T
> > > *>(v
);
112 inline remove_volatile_t
<T
> *
113 cast_to_nonatomic_pointer(T
*v
)
115 return const_cast<remove_volatile_t
<T
> *>(v
);
119 inline remove_volatile_t
<T
> *
120 cast_to_nonatomic_pointer(std::atomic
<T
> *v
)
122 return reinterpret_cast<remove_volatile_t
<T
> *>(v
);
126 inline remove_volatile_t
<T
> *
127 cast_to_nonatomic_pointer(volatile std::atomic
<T
> *v
)
129 auto _v
= const_cast<std::atomic
<T
> *>(v
);
130 return reinterpret_cast<remove_volatile_t
<T
> *>(_v
);
133 #endif /* OS_ATOMIC_USES_CXX */
135 #endif /* __OS_ATOMIC_H__ */