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__
32 #include "shims/linux_stubs.h"
36 #include "shims/android_stubs.h"
39 #include "shims/hw_config.h"
40 #include "shims/priority.h"
42 #if HAVE_PTHREAD_WORKQUEUES
43 #if __has_include(<pthread/workqueue_private.h>)
44 #include <pthread/workqueue_private.h>
46 #include <pthread_workqueue.h>
48 #ifndef WORKQ_FEATURE_MAINTENANCE
49 #define WORKQ_FEATURE_MAINTENANCE 0x10
51 #endif // HAVE_PTHREAD_WORKQUEUES
53 #if DISPATCH_USE_INTERNAL_WORKQUEUE
54 #include "event/workqueue_internal.h"
58 #include <pthread_np.h>
61 #if __has_include(<pthread/private.h>)
62 #include <pthread/private.h>
65 #if !HAVE_DECL_FD_COPY
66 #define FD_COPY(f, t) (void)(*(t) = *(f))
70 #define bzero(ptr,len) memset((ptr), 0, (len))
71 #define snprintf _snprintf
73 inline size_t strlcpy(char *dst
, const char *src
, size_t size
) {
74 int res
= strlen(dst
) + strlen(src
) + 1;
82 #endif // TARGET_OS_WIN32
84 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
86 _pthread_workqueue_override_start_direct(mach_port_t thread
,
87 pthread_priority_t priority
)
89 (void)thread
; (void)priority
;
92 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
94 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
96 _pthread_workqueue_override_start_direct_check_owner(mach_port_t thread
,
97 pthread_priority_t priority
, mach_port_t
*ulock_addr
)
100 return _pthread_workqueue_override_start_direct(thread
, priority
);
102 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150319
104 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
106 _pthread_override_qos_class_start_direct(mach_port_t thread
,
107 pthread_priority_t priority
)
109 (void)thread
; (void)priority
;
114 _pthread_override_qos_class_end_direct(mach_port_t thread
)
119 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
121 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
123 _pthread_qos_override_start_direct(mach_port_t thread
,
124 pthread_priority_t priority
, void *resource
)
127 return _pthread_override_qos_class_start_direct(thread
, priority
);
131 _pthread_qos_override_end_direct(mach_port_t thread
, void *resource
)
134 return _pthread_override_qos_class_end_direct(thread
);
136 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20150325
138 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20160427
139 #define _PTHREAD_SET_SELF_WQ_KEVENT_UNBIND 0
142 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20160427
144 _pthread_workqueue_should_narrow(pthread_priority_t priority
)
151 #if HAVE_PTHREAD_QOS_H && __has_include(<pthread/qos_private.h>) && \
152 defined(PTHREAD_MAX_PARALLELISM_PHYSICAL) && \
153 DISPATCH_HAVE_HW_CONFIG_COMMPAGE && \
154 DISPATCH_MIN_REQUIRED_OSX_AT_LEAST(109900)
155 #define DISPATCH_USE_PTHREAD_QOS_MAX_PARALLELISM 1
156 #define DISPATCH_MAX_PARALLELISM_PHYSICAL PTHREAD_MAX_PARALLELISM_PHYSICAL
158 #define DISPATCH_MAX_PARALLELISM_PHYSICAL 0x1
160 #define DISPATCH_MAX_PARALLELISM_ACTIVE 0x2
161 _Static_assert(!(DISPATCH_MAX_PARALLELISM_PHYSICAL
&
162 DISPATCH_MAX_PARALLELISM_ACTIVE
), "Overlapping parallelism flags");
164 DISPATCH_ALWAYS_INLINE
165 static inline uint32_t
166 _dispatch_qos_max_parallelism(dispatch_qos_t qos
, unsigned long flags
)
172 #if DISPATCH_USE_PTHREAD_QOS_MAX_PARALLELISM
173 r
= pthread_qos_max_parallelism(_dispatch_qos_to_qos_class(qos
),
174 flags
& PTHREAD_MAX_PARALLELISM_PHYSICAL
);
180 p
= (flags
& DISPATCH_MAX_PARALLELISM_PHYSICAL
) ?
181 dispatch_hw_config(physical_cpus
) :
182 dispatch_hw_config(logical_cpus
);
184 if (flags
& DISPATCH_MAX_PARALLELISM_ACTIVE
) {
185 uint32_t active_cpus
= dispatch_hw_config(active_cpus
);
186 if ((flags
& DISPATCH_MAX_PARALLELISM_PHYSICAL
) &&
187 active_cpus
< dispatch_hw_config(logical_cpus
)) {
188 active_cpus
/= dispatch_hw_config(logical_cpus
) /
189 dispatch_hw_config(physical_cpus
);
191 if (active_cpus
< p
) p
= active_cpus
;
196 #if !HAVE_NORETURN_BUILTIN_TRAP
198 * XXXRW: Work-around for possible clang bug in which __builtin_trap() is not
199 * marked noreturn, leading to a build error as dispatch_main() *is* marked
200 * noreturn. Mask by marking __builtin_trap() as noreturn locally.
203 void __builtin_trap(void);
206 #if DISPATCH_HW_CONFIG_UP
207 #define OS_ATOMIC_UP 1
209 #define OS_ATOMIC_UP 0
213 #ifndef __OS_INTERNAL_ATOMIC__
214 #include "shims/atomic.h"
216 #define DISPATCH_ATOMIC64_ALIGN __attribute__((aligned(8)))
218 #include "shims/atomic_sfb.h"
219 #include "shims/tsd.h"
220 #include "shims/yield.h"
221 #include "shims/lock.h"
223 #include "shims/perfmon.h"
225 #include "shims/getprogname.h"
226 #include "shims/time.h"
228 #if __has_include(<os/overflow.h>)
229 #include <os/overflow.h>
230 #elif __has_builtin(__builtin_add_overflow)
231 #define os_add_overflow(a, b, c) __builtin_add_overflow(a, b, c)
232 #define os_sub_overflow(a, b, c) __builtin_sub_overflow(a, b, c)
233 #define os_mul_overflow(a, b, c) __builtin_mul_overflow(a, b, c)
235 #error unsupported compiler
238 #ifndef os_mul_and_add_overflow
239 #define os_mul_and_add_overflow(a, x, b, res) __extension__({ \
240 __typeof(*(res)) _tmp; \
242 _s = os_mul_overflow((a), (x), &_tmp); \
243 _t = os_add_overflow((b), _tmp, (res)); \
249 #if __has_feature(c_static_assert)
250 #define __dispatch_is_array(x) \
251 _Static_assert(!__builtin_types_compatible_p(typeof((x)[0]) *, typeof(x)), \
252 #x " isn't an array")
254 ({ __dispatch_is_array(x); sizeof(x) / sizeof((x)[0]); })
256 #define countof(x) (sizeof(x) / sizeof(x[0]))
259 DISPATCH_ALWAYS_INLINE
261 _dispatch_mempcpy(void *ptr
, const void *data
, size_t len
)
263 memcpy(ptr
, data
, len
);
264 return (char *)ptr
+ len
;
266 #define _dispatch_memappend(ptr, e) \
267 _dispatch_mempcpy(ptr, e, sizeof(*(e)))
270 // Clear the stack before calling long-running thread-handler functions that
271 // never return (and don't take arguments), to facilitate leak detection and
272 // provide cleaner backtraces. <rdar://problem/9050566>
273 #define _dispatch_clear_stack(s) do { \
274 void *a[(s)/sizeof(void*) ? (s)/sizeof(void*) : 1]; \
275 a[0] = pthread_get_stackaddr_np(pthread_self()); \
276 void* volatile const p = (void*)&a[1]; /* <rdar://32604885> */ \
277 bzero((void*)p, (size_t)(a[0] - (void*)&a[1])); \
280 #define _dispatch_clear_stack(s)