2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
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.
27 #ifndef __DISPATCH_OS_SHIMS__
28 #define __DISPATCH_OS_SHIMS__
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
53 #ifndef _PTHREAD_PRIORITY_INHERIT_FLAG
54 #define _PTHREAD_PRIORITY_INHERIT_FLAG 0x40000000
56 #ifndef _PTHREAD_PRIORITY_ROOTQUEUE_FLAG
57 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
59 #ifndef _PTHREAD_PRIORITY_SCHED_PRI_FLAG
60 #define _PTHREAD_PRIORITY_SCHED_PRI_FLAG 0x20000000
62 #ifndef _PTHREAD_PRIORITY_ENFORCE_FLAG
63 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
65 #ifndef _PTHREAD_PRIORITY_OVERRIDE_FLAG
66 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
68 #ifndef _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG
69 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
71 #ifndef _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG
72 #define _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG 0x02000000
74 #ifndef _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG
75 #define _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG 0x01000000
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
97 #include "shims/linux_stubs.h"
100 typedef uint32_t dispatch_priority_t
;
101 #define DISPATCH_SATURATED_OVERRIDE ((dispatch_priority_t)UINT32_MAX)
103 #ifndef _DISPATCH_QOS_CLASS_USER_INTERACTIVE
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,
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>
118 #include <pthread_workqueue.h>
120 #ifndef WORKQ_FEATURE_MAINTENANCE
121 #define WORKQ_FEATURE_MAINTENANCE 0x10
123 #endif // HAVE_PTHREAD_WORKQUEUES
125 #if HAVE_PTHREAD_NP_H
126 #include <pthread_np.h>
129 #if __has_include(<pthread/private.h>)
130 #include <pthread/private.h>
133 #if !HAVE_DECL_FD_COPY
134 #define FD_COPY(f, t) (void)(*(t) = *(f))
138 #define bzero(ptr,len) memset((ptr), 0, (len))
139 #define snprintf _snprintf
141 inline size_t strlcpy(char *dst
, const char *src
, size_t size
) {
142 int res
= strlen(dst
) + strlen(src
) + 1;
145 strncpy(dst
, src
, n
);
150 #endif // TARGET_OS_WIN32
152 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
154 _pthread_workqueue_override_start_direct(mach_port_t thread
,
155 pthread_priority_t priority
)
157 (void)thread
; (void)priority
;
160 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
162 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
164 _pthread_workqueue_override_start_direct_check_owner(mach_port_t thread
,
165 pthread_priority_t priority
, mach_port_t
*ulock_addr
)
168 return _pthread_workqueue_override_start_direct(thread
, priority
);
170 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
172 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
174 _pthread_override_qos_class_start_direct(mach_port_t thread
,
175 pthread_priority_t priority
)
177 (void)thread
; (void)priority
;
182 _pthread_override_qos_class_end_direct(mach_port_t thread
)
187 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
189 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
191 _pthread_qos_override_start_direct(mach_port_t thread
,
192 pthread_priority_t priority
, void *resource
)
195 return _pthread_override_qos_class_start_direct(thread
, priority
);
199 _pthread_qos_override_end_direct(mach_port_t thread
, void *resource
)
202 return _pthread_override_qos_class_end_direct(thread
);
204 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
206 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20160427
207 #define _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND 0
210 #if !HAVE_NORETURN_BUILTIN_TRAP
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.
217 void __builtin_trap(void);
220 #if DISPATCH_HW_CONFIG_UP
221 #define OS_ATOMIC_UP 1
223 #define OS_ATOMIC_UP 0
227 #ifndef __OS_INTERNAL_ATOMIC__
228 #include "shims/atomic.h"
230 #include "shims/atomic_sfb.h"
231 #include "shims/tsd.h"
232 #include "shims/yield.h"
233 #include "shims/lock.h"
235 #include "shims/hw_config.h"
236 #include "shims/perfmon.h"
238 #include "shims/getprogname.h"
239 #include "shims/time.h"
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)
248 #error unsupported compiler
251 #ifndef os_mul_and_add_overflow
252 #define os_mul_and_add_overflow(a, x, b, res) __extension__({ \
253 __typeof(*(res)) _tmp; \
255 _s = os_mul_overflow((a), (x), &_tmp); \
256 _t = os_add_overflow((b), _tmp, (res)); \
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")
267 ({ __dispatch_is_array(x); sizeof(x) / sizeof((x)[0]); })
269 #define countof(x) (sizeof(x) / sizeof(x[0]))
272 DISPATCH_ALWAYS_INLINE
274 _dispatch_mempcpy(void *ptr
, const void *data
, size_t len
)
276 memcpy(ptr
, data
, len
);
277 return (char *)ptr
+ len
;
279 #define _dispatch_memappend(ptr, e) \
280 _dispatch_mempcpy(ptr, e, sizeof(*(e)))
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])); \
292 #define _dispatch_clear_stack(s)