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
93namespace os {
94template <class T> using remove_volatile_t = typename std::remove_volatile<T>::type;
95
96template <class T>
97inline volatile std::atomic<remove_volatile_t<T> > *
98cast_to_atomic_pointer(T *v)
99{
100 return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
101}
102
103template <class T>
104inline volatile std::atomic<remove_volatile_t<T> > *
105cast_to_atomic_pointer(std::atomic<T> *v)
106{
107 return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
108}
109
110template <class T>
111inline volatile std::atomic<remove_volatile_t<T> > *
112cast_to_atomic_pointer(volatile std::atomic<T> *v)
113{
114 return reinterpret_cast<volatile std::atomic<remove_volatile_t<T> > *>(v);
115}
116
117template <class T>
118inline remove_volatile_t<T> *
119cast_to_nonatomic_pointer(T *v)
120{
121 return const_cast<remove_volatile_t<T> *>(v);
122}
123
124template <class T>
125inline remove_volatile_t<T> *
126cast_to_nonatomic_pointer(std::atomic<T> *v)
127{
128 return reinterpret_cast<remove_volatile_t<T> *>(v);
129}
130
131template <class T>
132inline remove_volatile_t<T> *
133cast_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