]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims.h
libdispatch-703.30.5.tar.gz
[apple/libdispatch.git] / src / shims.h
1 /*
2 * Copyright (c) 2008-2013 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_OS_SHIMS__
28 #define __DISPATCH_OS_SHIMS__
29
30 #include <pthread.h>
31 #if HAVE_PTHREAD_QOS_H && __has_include(<pthread/qos.h>)
32 #include <pthread/qos.h>
33 #if __has_include(<pthread/qos_private.h>)
34 #include <pthread/qos_private.h>
35 #define _DISPATCH_QOS_CLASS_USER_INTERACTIVE QOS_CLASS_USER_INTERACTIVE
36 #define _DISPATCH_QOS_CLASS_USER_INITIATED QOS_CLASS_USER_INITIATED
37 #define _DISPATCH_QOS_CLASS_DEFAULT QOS_CLASS_DEFAULT
38 #define _DISPATCH_QOS_CLASS_UTILITY QOS_CLASS_UTILITY
39 #define _DISPATCH_QOS_CLASS_BACKGROUND QOS_CLASS_BACKGROUND
40 #define _DISPATCH_QOS_CLASS_UNSPECIFIED QOS_CLASS_UNSPECIFIED
41 #else // pthread/qos_private.h
42 typedef unsigned long pthread_priority_t;
43 #endif // pthread/qos_private.h
44 #if __has_include(<sys/qos_private.h>)
45 #include <sys/qos_private.h>
46 #define _DISPATCH_QOS_CLASS_MAINTENANCE QOS_CLASS_MAINTENANCE
47 #else // sys/qos_private.h
48 #define _DISPATCH_QOS_CLASS_MAINTENANCE 0x05
49 #endif // sys/qos_private.h
50 #ifndef _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
51 #define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG 0x80000000
52 #endif
53 #ifndef _PTHREAD_PRIORITY_INHERIT_FLAG
54 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
55 #endif
56 #ifndef _PTHREAD_PRIORITY_ROOTQUEUE_FLAG
57 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
58 #endif
59 #ifndef _PTHREAD_PRIORITY_SCHED_PRI_FLAG
60 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
61 #endif
62 #ifndef _PTHREAD_PRIORITY_ENFORCE_FLAG
63 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
64 #endif
65 #ifndef _PTHREAD_PRIORITY_OVERRIDE_FLAG
66 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
67 #endif
68 #ifndef _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG
69 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
70 #endif
71 #ifndef _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
72 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
73 #endif
74 #ifndef _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG
75 #define _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG 0x01000000
76 #endif
77
78 #else // HAVE_PTHREAD_QOS_H
79 typedef unsigned int qos_class_t;
80 typedef unsigned long pthread_priority_t;
81 #define QOS_MIN_RELATIVE_PRIORITY (-15)
82 #define _PTHREAD_PRIORITY_FLAGS_MASK (~0xffffff)
83 #define _PTHREAD_PRIORITY_QOS_CLASS_MASK 0x00ffff00
84 #define _PTHREAD_PRIORITY_QOS_CLASS_SHIFT (8ull)
85 #define _PTHREAD_PRIORITY_PRIORITY_MASK 0x000000ff
86 #define _PTHREAD_PRIORITY_OVERCOMMIT_FLAG 0x80000000
87 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
88 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
89 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
90 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
91 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
92 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
93 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
94 #endif // HAVE_PTHREAD_QOS_H
95
96 #ifdef __linux__
97 #include "shims/linux_stubs.h"
98 #endif
99
100 typedef uint32_t dispatch_priority_t;
101 #define DISPATCH_SATURATED_OVERRIDE ((dispatch_priority_t)UINT32_MAX)
102
103 #ifndef _DISPATCH_QOS_CLASS_USER_INTERACTIVE
104 enum {
105 _DISPATCH_QOS_CLASS_USER_INTERACTIVE = 0x21,
106 _DISPATCH_QOS_CLASS_USER_INITIATED = 0x19,
107 _DISPATCH_QOS_CLASS_DEFAULT = 0x15,
108 _DISPATCH_QOS_CLASS_UTILITY = 0x11,
109 _DISPATCH_QOS_CLASS_BACKGROUND = 0x09,
110 _DISPATCH_QOS_CLASS_MAINTENANCE = 0x05,
111 _DISPATCH_QOS_CLASS_UNSPECIFIED = 0x00,
112 };
113 #endif // _DISPATCH_QOS_CLASS_USER_INTERACTIVE
114 #if HAVE_PTHREAD_WORKQUEUES
115 #if __has_include(<pthread/workqueue_private.h>)
116 #include <pthread/workqueue_private.h>
117 #else
118 #include <pthread_workqueue.h>
119 #endif
120 #ifndef WORKQ_FEATURE_MAINTENANCE
121 #define WORKQ_FEATURE_MAINTENANCE 0x10
122 #endif
123 #endif // HAVE_PTHREAD_WORKQUEUES
124
125 #if HAVE_PTHREAD_NP_H
126 #include <pthread_np.h>
127 #endif
128
129 #if __has_include(<pthread/private.h>)
130 #include <pthread/private.h>
131 #endif
132
133 #if !HAVE_DECL_FD_COPY
134 #define FD_COPY(f, t) (void)(*(t) = *(f))
135 #endif
136
137 #if TARGET_OS_WIN32
138 #define bzero(ptr,len) memset((ptr), 0, (len))
139 #define snprintf _snprintf
140
141 inline size_t strlcpy(char *dst, const char *src, size_t size) {
142 int res = strlen(dst) + strlen(src) + 1;
143 if (size > 0) {
144 size_t n = size - 1;
145 strncpy(dst, src, n);
146 dst[n] = 0;
147 }
148 return res;
149 }
150 #endif // TARGET_OS_WIN32
151
152 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
153 static inline int
154 _pthread_workqueue_override_start_direct(mach_port_t thread,
155 pthread_priority_t priority)
156 {
157 (void)thread; (void)priority;
158 return 0;
159 }
160 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
161
162 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
163 static inline int
164 _pthread_workqueue_override_start_direct_check_owner(mach_port_t thread,
165 pthread_priority_t priority, mach_port_t *ulock_addr)
166 {
167 (void)ulock_addr;
168 return _pthread_workqueue_override_start_direct(thread, priority);
169 }
170 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
171
172 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
173 static inline int
174 _pthread_override_qos_class_start_direct(mach_port_t thread,
175 pthread_priority_t priority)
176 {
177 (void)thread; (void)priority;
178 return 0;
179 }
180
181 static inline int
182 _pthread_override_qos_class_end_direct(mach_port_t thread)
183 {
184 (void)thread;
185 return 0;
186 }
187 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
188
189 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
190 static inline int
191 _pthread_qos_override_start_direct(mach_port_t thread,
192 pthread_priority_t priority, void *resource)
193 {
194 (void)resource;
195 return _pthread_override_qos_class_start_direct(thread, priority);
196 }
197
198 static inline int
199 _pthread_qos_override_end_direct(mach_port_t thread, void *resource)
200 {
201 (void)resource;
202 return _pthread_override_qos_class_end_direct(thread);
203 }
204 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
205
206 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20160427
207 #define _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND 0
208 #endif
209
210 #if !HAVE_NORETURN_BUILTIN_TRAP
211 /*
212 * XXXRW: Work-around for possible clang bug in which __builtin_trap() is not
213 * marked noreturn, leading to a build error as dispatch_main() *is* marked
214 * noreturn. Mask by marking __builtin_trap() as noreturn locally.
215 */
216 DISPATCH_NORETURN
217 void __builtin_trap(void);
218 #endif
219
220 #if DISPATCH_HW_CONFIG_UP
221 #define OS_ATOMIC_UP 1
222 #else
223 #define OS_ATOMIC_UP 0
224 #endif
225
226
227 #ifndef __OS_INTERNAL_ATOMIC__
228 #include "shims/atomic.h"
229 #endif
230 #include "shims/atomic_sfb.h"
231 #include "shims/tsd.h"
232 #include "shims/yield.h"
233 #include "shims/lock.h"
234
235 #include "shims/hw_config.h"
236 #include "shims/perfmon.h"
237
238 #include "shims/getprogname.h"
239 #include "shims/time.h"
240
241 #if __has_include(<os/overflow.h>)
242 #include <os/overflow.h>
243 #elif __has_builtin(__builtin_add_overflow)
244 #define os_add_overflow(a, b, c) __builtin_add_overflow(a, b, c)
245 #define os_sub_overflow(a, b, c) __builtin_sub_overflow(a, b, c)
246 #define os_mul_overflow(a, b, c) __builtin_mul_overflow(a, b, c)
247 #else
248 #error unsupported compiler
249 #endif
250
251 #ifndef os_mul_and_add_overflow
252 #define os_mul_and_add_overflow(a, x, b, res) __extension__({ \
253 __typeof(*(res)) _tmp; \
254 bool _s, _t; \
255 _s = os_mul_overflow((a), (x), &_tmp); \
256 _t = os_add_overflow((b), _tmp, (res)); \
257 _s | _t; \
258 })
259 #endif
260
261
262 #if __has_feature(c_static_assert)
263 #define __dispatch_is_array(x) \
264 _Static_assert(!__builtin_types_compatible_p(typeof((x)[0]) *, typeof(x)), \
265 #x " isn't an array")
266 #define countof(x) \
267 ({ __dispatch_is_array(x); sizeof(x) / sizeof((x)[0]); })
268 #else
269 #define countof(x) (sizeof(x) / sizeof(x[0]))
270 #endif
271
272 DISPATCH_ALWAYS_INLINE
273 static inline void *
274 _dispatch_mempcpy(void *ptr, const void *data, size_t len)
275 {
276 memcpy(ptr, data, len);
277 return (char *)ptr + len;
278 }
279 #define _dispatch_memappend(ptr, e) \
280 _dispatch_mempcpy(ptr, e, sizeof(*(e)))
281
282 #ifdef __APPLE__
283 // Clear the stack before calling long-running thread-handler functions that
284 // never return (and don't take arguments), to facilitate leak detection and
285 // provide cleaner backtraces. <rdar://problem/9050566>
286 #define _dispatch_clear_stack(s) do { \
287 void *a[(s)/sizeof(void*) ? (s)/sizeof(void*) : 1]; \
288 a[0] = pthread_get_stackaddr_np(pthread_self()); \
289 bzero((void*)&a[1], (size_t)(a[0] - (void*)&a[1])); \
290 } while (0)
291 #else
292 #define _dispatch_clear_stack(s)
293 #endif
294
295 #endif