]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims/atomic.h
libdispatch-703.1.4.tar.gz
[apple/libdispatch.git] / src / shims / atomic.h
1 /*
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_SHIMS_ATOMIC__
28 #define __DISPATCH_SHIMS_ATOMIC__
29
30 #if !__has_extension(c_atomic) || \
31 !__has_extension(c_generic_selections) || \
32 !__has_include(<stdatomic.h>)
33 #error libdispatch requires C11 with <stdatomic.h> and generic selections
34 #endif
35
36 #include <stdatomic.h>
37
38 #define memory_order_ordered memory_order_seq_cst
39
40 #define _os_atomic_basetypeof(p) \
41 typeof(*_Generic((p), \
42 char*: (char*)(p), \
43 volatile char*: (char*)(p), \
44 signed char*: (signed char*)(p), \
45 volatile signed char*: (signed char*)(p), \
46 unsigned char*: (unsigned char*)(p), \
47 volatile unsigned char*: (unsigned char*)(p), \
48 short*: (short*)(p), \
49 volatile short*: (short*)(p), \
50 unsigned short*: (unsigned short*)(p), \
51 volatile unsigned short*: (unsigned short*)(p), \
52 int*: (int*)(p), \
53 volatile int*: (int*)(p), \
54 unsigned int*: (unsigned int*)(p), \
55 volatile unsigned int*: (unsigned int*)(p), \
56 long*: (long*)(p), \
57 volatile long*: (long*)(p), \
58 unsigned long*: (unsigned long*)(p), \
59 volatile unsigned long*: (unsigned long*)(p), \
60 long long*: (long long*)(p), \
61 volatile long long*: (long long*)(p), \
62 unsigned long long*: (unsigned long long*)(p), \
63 volatile unsigned long long*: (unsigned long long*)(p), \
64 const void**: (const void**)(p), \
65 const void*volatile*: (const void**)(p), \
66 default: (void**)(p)))
67
68 #define _os_atomic_c11_atomic(p) \
69 _Generic((p), \
70 char*: (_Atomic(char)*)(p), \
71 volatile char*: (volatile _Atomic(char)*)(p), \
72 signed char*: (_Atomic(signed char)*)(p), \
73 volatile signed char*: (volatile _Atomic(signed char)*)(p), \
74 unsigned char*: (_Atomic(unsigned char)*)(p), \
75 volatile unsigned char*: (volatile _Atomic(unsigned char)*)(p), \
76 short*: (_Atomic(short)*)(p), \
77 volatile short*: (volatile _Atomic(short)*)(p), \
78 unsigned short*: (_Atomic(unsigned short)*)(p), \
79 volatile unsigned short*: (volatile _Atomic(unsigned short)*)(p), \
80 int*: (_Atomic(int)*)(p), \
81 volatile int*: (volatile _Atomic(int)*)(p), \
82 unsigned int*: (_Atomic(unsigned int)*)(p), \
83 volatile unsigned int*: (volatile _Atomic(unsigned int)*)(p), \
84 long*: (_Atomic(long)*)(p), \
85 volatile long*: (volatile _Atomic(long)*)(p), \
86 unsigned long*: (_Atomic(unsigned long)*)(p), \
87 volatile unsigned long*: (volatile _Atomic(unsigned long)*)(p), \
88 long long*: (_Atomic(long long)*)(p), \
89 volatile long long*: (volatile _Atomic(long long)*)(p), \
90 unsigned long long*: (_Atomic(unsigned long long)*)(p), \
91 volatile unsigned long long*: \
92 (volatile _Atomic(unsigned long long)*)(p), \
93 const void**: (_Atomic(const void*)*)(p), \
94 const void*volatile*: (volatile _Atomic(const void*)*)(p), \
95 default: (volatile _Atomic(void*)*)(p))
96
97 #define os_atomic_thread_fence(m) atomic_thread_fence(memory_order_##m)
98 // see comment in dispatch_once.c
99 #define os_atomic_maximally_synchronizing_barrier() \
100 atomic_thread_fence(memory_order_seq_cst)
101
102 #define os_atomic_load(p, m) \
103 ({ _os_atomic_basetypeof(p) _r = \
104 atomic_load_explicit(_os_atomic_c11_atomic(p), \
105 memory_order_##m); (typeof(*(p)))_r; })
106 #define os_atomic_store(p, v, m) \
107 ({ _os_atomic_basetypeof(p) _v = (v); \
108 atomic_store_explicit(_os_atomic_c11_atomic(p), _v, \
109 memory_order_##m); })
110 #define os_atomic_xchg(p, v, m) \
111 ({ _os_atomic_basetypeof(p) _v = (v), _r = \
112 atomic_exchange_explicit(_os_atomic_c11_atomic(p), _v, \
113 memory_order_##m); (typeof(*(p)))_r; })
114 #define os_atomic_cmpxchg(p, e, v, m) \
115 ({ _os_atomic_basetypeof(p) _v = (v), _r = (e); \
116 atomic_compare_exchange_strong_explicit(_os_atomic_c11_atomic(p), \
117 &_r, _v, memory_order_##m, \
118 memory_order_relaxed); })
119 #define os_atomic_cmpxchgv(p, e, v, g, m) \
120 ({ _os_atomic_basetypeof(p) _v = (v), _r = (e); _Bool _b = \
121 atomic_compare_exchange_strong_explicit(_os_atomic_c11_atomic(p), \
122 &_r, _v, memory_order_##m, \
123 memory_order_relaxed); *(g) = (typeof(*(p)))_r; _b; })
124 #define os_atomic_cmpxchgvw(p, e, v, g, m) \
125 ({ _os_atomic_basetypeof(p) _v = (v), _r = (e); _Bool _b = \
126 atomic_compare_exchange_weak_explicit(_os_atomic_c11_atomic(p), \
127 &_r, _v, memory_order_##m, \
128 memory_order_relaxed); *(g) = (typeof(*(p)))_r; _b; })
129
130 #define _os_atomic_c11_op(p, v, m, o, op) \
131 ({ _os_atomic_basetypeof(p) _v = (v), _r = \
132 atomic_fetch_##o##_explicit(_os_atomic_c11_atomic(p), _v, \
133 memory_order_##m); (typeof(*(p)))(_r op _v); })
134 #define _os_atomic_c11_op_orig(p, v, m, o, op) \
135 ({ _os_atomic_basetypeof(p) _v = (v), _r = \
136 atomic_fetch_##o##_explicit(_os_atomic_c11_atomic(p), _v, \
137 memory_order_##m); (typeof(*(p)))_r; })
138 #define os_atomic_add(p, v, m) \
139 _os_atomic_c11_op((p), (v), m, add, +)
140 #define os_atomic_add_orig(p, v, m) \
141 _os_atomic_c11_op_orig((p), (v), m, add, +)
142 #define os_atomic_sub(p, v, m) \
143 _os_atomic_c11_op((p), (v), m, sub, -)
144 #define os_atomic_sub_orig(p, v, m) \
145 _os_atomic_c11_op_orig((p), (v), m, sub, -)
146 #define os_atomic_and(p, v, m) \
147 _os_atomic_c11_op((p), (v), m, and, &)
148 #define os_atomic_and_orig(p, v, m) \
149 _os_atomic_c11_op_orig((p), (v), m, and, &)
150 #define os_atomic_or(p, v, m) \
151 _os_atomic_c11_op((p), (v), m, or, |)
152 #define os_atomic_or_orig(p, v, m) \
153 _os_atomic_c11_op_orig((p), (v), m, or, |)
154 #define os_atomic_xor(p, v, m) \
155 _os_atomic_c11_op((p), (v), m, xor, ^)
156 #define os_atomic_xor_orig(p, v, m) \
157 _os_atomic_c11_op_orig((p), (v), m, xor, ^)
158
159 #define os_atomic_rmw_loop(p, ov, nv, m, ...) ({ \
160 bool _result = false; \
161 typeof(p) _p = (p); \
162 ov = os_atomic_load(_p, relaxed); \
163 do { \
164 __VA_ARGS__; \
165 _result = os_atomic_cmpxchgvw(_p, ov, nv, &ov, m); \
166 } while (os_unlikely(!_result)); \
167 _result; \
168 })
169 #define os_atomic_rmw_loop2o(p, f, ov, nv, m, ...) \
170 os_atomic_rmw_loop(&(p)->f, ov, nv, m, __VA_ARGS__)
171 #define os_atomic_rmw_loop_give_up_with_fence(m, expr) \
172 ({ os_atomic_thread_fence(m); expr; __builtin_unreachable(); })
173 #define os_atomic_rmw_loop_give_up(expr) \
174 os_atomic_rmw_loop_give_up_with_fence(relaxed, expr)
175
176 #define os_atomic_load2o(p, f, m) \
177 os_atomic_load(&(p)->f, m)
178 #define os_atomic_store2o(p, f, v, m) \
179 os_atomic_store(&(p)->f, (v), m)
180 #define os_atomic_xchg2o(p, f, v, m) \
181 os_atomic_xchg(&(p)->f, (v), m)
182 #define os_atomic_cmpxchg2o(p, f, e, v, m) \
183 os_atomic_cmpxchg(&(p)->f, (e), (v), m)
184 #define os_atomic_cmpxchgv2o(p, f, e, v, g, m) \
185 os_atomic_cmpxchgv(&(p)->f, (e), (v), (g), m)
186 #define os_atomic_cmpxchgvw2o(p, f, e, v, g, m) \
187 os_atomic_cmpxchgvw(&(p)->f, (e), (v), (g), m)
188 #define os_atomic_add2o(p, f, v, m) \
189 os_atomic_add(&(p)->f, (v), m)
190 #define os_atomic_add_orig2o(p, f, v, m) \
191 os_atomic_add_orig(&(p)->f, (v), m)
192 #define os_atomic_sub2o(p, f, v, m) \
193 os_atomic_sub(&(p)->f, (v), m)
194 #define os_atomic_sub_orig2o(p, f, v, m) \
195 os_atomic_sub_orig(&(p)->f, (v), m)
196 #define os_atomic_and2o(p, f, v, m) \
197 os_atomic_and(&(p)->f, (v), m)
198 #define os_atomic_and_orig2o(p, f, v, m) \
199 os_atomic_and_orig(&(p)->f, (v), m)
200 #define os_atomic_or2o(p, f, v, m) \
201 os_atomic_or(&(p)->f, (v), m)
202 #define os_atomic_or_orig2o(p, f, v, m) \
203 os_atomic_or_orig(&(p)->f, (v), m)
204 #define os_atomic_xor2o(p, f, v, m) \
205 os_atomic_xor(&(p)->f, (v), m)
206 #define os_atomic_xor_orig2o(p, f, v, m) \
207 os_atomic_xor_orig(&(p)->f, (v), m)
208
209 #define os_atomic_inc(p, m) \
210 os_atomic_add((p), 1, m)
211 #define os_atomic_inc_orig(p, m) \
212 os_atomic_add_orig((p), 1, m)
213 #define os_atomic_inc2o(p, f, m) \
214 os_atomic_add2o(p, f, 1, m)
215 #define os_atomic_inc_orig2o(p, f, m) \
216 os_atomic_add_orig2o(p, f, 1, m)
217 #define os_atomic_dec(p, m) \
218 os_atomic_sub((p), 1, m)
219 #define os_atomic_dec_orig(p, m) \
220 os_atomic_sub_orig((p), 1, m)
221 #define os_atomic_dec2o(p, f, m) \
222 os_atomic_sub2o(p, f, 1, m)
223 #define os_atomic_dec_orig2o(p, f, m) \
224 os_atomic_sub_orig2o(p, f, 1, m)
225
226 #if defined(__x86_64__) || defined(__i386__)
227 #undef os_atomic_maximally_synchronizing_barrier
228 #ifdef __LP64__
229 #define os_atomic_maximally_synchronizing_barrier() \
230 ({ unsigned long _clbr; __asm__ __volatile__( \
231 "cpuid" \
232 : "=a" (_clbr) : "0" (0) : "rbx", "rcx", "rdx", "cc", "memory"); })
233 #else
234 #ifdef __llvm__
235 #define os_atomic_maximally_synchronizing_barrier() \
236 ({ unsigned long _clbr; __asm__ __volatile__( \
237 "cpuid" \
238 : "=a" (_clbr) : "0" (0) : "ebx", "ecx", "edx", "cc", "memory"); })
239 #else // gcc does not allow inline i386 asm to clobber ebx
240 #define os_atomic_maximally_synchronizing_barrier() \
241 ({ unsigned long _clbr; __asm__ __volatile__( \
242 "pushl %%ebx\n\t" \
243 "cpuid\n\t" \
244 "popl %%ebx" \
245 : "=a" (_clbr) : "0" (0) : "ecx", "edx", "cc", "memory"); })
246 #endif
247 #endif
248 #endif // defined(__x86_64__) || defined(__i386__)
249
250 #endif // __DISPATCH_SHIMS_ATOMIC__