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 #ifndef QOS_CLASS_LEGACY
38 #define _DISPATCH_QOS_CLASS_DEFAULT QOS_CLASS_LEGACY
40 #define _DISPATCH_QOS_CLASS_DEFAULT QOS_CLASS_DEFAULT
42 #define _DISPATCH_QOS_CLASS_UTILITY QOS_CLASS_UTILITY
43 #define _DISPATCH_QOS_CLASS_BACKGROUND QOS_CLASS_BACKGROUND
44 #define _DISPATCH_QOS_CLASS_UNSPECIFIED QOS_CLASS_UNSPECIFIED
45 #else // pthread/qos_private.h
46 typedef unsigned long pthread_priority_t
;
47 #endif // pthread/qos_private.h
48 #if __has_include(<sys/qos_private.h>)
49 #include <sys/qos_private.h>
50 #define _DISPATCH_QOS_CLASS_MAINTENANCE QOS_CLASS_MAINTENANCE
51 #else // sys/qos_private.h
52 #define _DISPATCH_QOS_CLASS_MAINTENANCE 0x05
53 #endif // sys/qos_private.h
54 #ifndef _PTHREAD_PRIORITY_ROOTQUEUE_FLAG
55 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
57 #ifndef _PTHREAD_PRIORITY_ENFORCE_FLAG
58 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
60 #ifndef _PTHREAD_PRIORITY_OVERRIDE_FLAG
61 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
63 #ifndef _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG
64 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
66 #else // HAVE_PTHREAD_QOS_H
67 typedef unsigned int qos_class_t
;
68 typedef unsigned long pthread_priority_t
;
69 #define QOS_MIN_RELATIVE_PRIORITY (-15)
70 #define _PTHREAD_PRIORITY_QOS_CLASS_MASK 0x00ffff00
71 #define _PTHREAD_PRIORITY_ROOTQUEUE_FLAG 0x20000000
72 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
73 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
74 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
75 #endif // HAVE_PTHREAD_QOS_H
76 #ifndef _DISPATCH_QOS_CLASS_USER_INTERACTIVE
78 _DISPATCH_QOS_CLASS_USER_INTERACTIVE
= 0x21,
79 _DISPATCH_QOS_CLASS_USER_INITIATED
= 0x19,
80 _DISPATCH_QOS_CLASS_DEFAULT
= 0x15,
81 _DISPATCH_QOS_CLASS_UTILITY
= 0x11,
82 _DISPATCH_QOS_CLASS_BACKGROUND
= 0x09,
83 _DISPATCH_QOS_CLASS_MAINTENANCE
= 0x05,
84 _DISPATCH_QOS_CLASS_UNSPECIFIED
= 0x00,
86 #endif // _DISPATCH_QOS_CLASS_USER_INTERACTIVE
87 #if HAVE_PTHREAD_WORKQUEUES
88 #if __has_include(<pthread/workqueue_private.h>)
89 #include <pthread/workqueue_private.h>
91 #include <pthread_workqueue.h>
93 #ifndef WORKQ_FEATURE_MAINTENANCE
94 #define WORKQ_FEATURE_MAINTENANCE 0x10
96 #endif // HAVE_PTHREAD_WORKQUEUES
99 #include <pthread_np.h>
102 #if !HAVE_DECL_FD_COPY
103 #define FD_COPY(f, t) (void)(*(t) = *(f))
107 #define bzero(ptr,len) memset((ptr), 0, (len))
108 #define snprintf _snprintf
110 inline size_t strlcpy(char *dst
, const char *src
, size_t size
) {
111 int res
= strlen(dst
) + strlen(src
) + 1;
114 strncpy(dst
, src
, n
);
119 #endif // TARGET_OS_WIN32
121 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
123 _pthread_workqueue_override_start_direct(mach_port_t thread
,
124 pthread_priority_t priority
)
126 (void)thread
; (void)priority
;
129 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
131 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
133 _pthread_override_qos_class_start_direct(pthread_t thread
,
134 pthread_priority_t priority
)
136 (void)thread
; (void)priority
;
141 _pthread_override_qos_class_end_direct(mach_port_t thread
)
146 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
148 #if !HAVE_NORETURN_BUILTIN_TRAP
150 * XXXRW: Work-around for possible clang bug in which __builtin_trap() is not
151 * marked noreturn, leading to a build error as dispatch_main() *is* marked
152 * noreturn. Mask by marking __builtin_trap() as noreturn locally.
155 void __builtin_trap(void);
158 #if DISPATCH_HW_CONFIG_UP
159 #define DISPATCH_ATOMIC_UP 1
162 #include "shims/atomic.h"
163 #include "shims/atomic_sfb.h"
164 #include "shims/tsd.h"
165 #include "shims/yield.h"
167 #include "shims/hw_config.h"
168 #include "shims/perfmon.h"
170 #include "shims/getprogname.h"
171 #include "shims/time.h"
174 // Clear the stack before calling long-running thread-handler functions that
175 // never return (and don't take arguments), to facilitate leak detection and
176 // provide cleaner backtraces. <rdar://problem/9050566>
177 #define _dispatch_clear_stack(s) do { \
178 void *a[(s)/sizeof(void*) ? (s)/sizeof(void*) : 1]; \
179 a[0] = pthread_get_stackaddr_np(pthread_self()); \
180 bzero((void*)&a[1], (size_t)(a[0] - (void*)&a[1])); \
183 #define _dispatch_clear_stack(s)