]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims.h
libdispatch-442.1.4.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 #ifndef QOS_CLASS_LEGACY
38 #define _DISPATCH_QOS_CLASS_DEFAULT QOS_CLASS_LEGACY
39 #else
40 #define _DISPATCH_QOS_CLASS_DEFAULT QOS_CLASS_DEFAULT
41 #endif
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
56 #endif
57 #ifndef _PTHREAD_PRIORITY_ENFORCE_FLAG
58 #define _PTHREAD_PRIORITY_ENFORCE_FLAG 0x10000000
59 #endif
60 #ifndef _PTHREAD_PRIORITY_OVERRIDE_FLAG
61 #define _PTHREAD_PRIORITY_OVERRIDE_FLAG 0x08000000
62 #endif
63 #ifndef _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG
64 #define _PTHREAD_PRIORITY_DEFAULTQUEUE_FLAG 0x04000000
65 #endif
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
77 enum {
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,
85 };
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>
90 #else
91 #include <pthread_workqueue.h>
92 #endif
93 #ifndef WORKQ_FEATURE_MAINTENANCE
94 #define WORKQ_FEATURE_MAINTENANCE 0x10
95 #endif
96 #endif // HAVE_PTHREAD_WORKQUEUES
97
98 #if HAVE_PTHREAD_NP_H
99 #include <pthread_np.h>
100 #endif
101
102 #if !HAVE_DECL_FD_COPY
103 #define FD_COPY(f, t) (void)(*(t) = *(f))
104 #endif
105
106 #if TARGET_OS_WIN32
107 #define bzero(ptr,len) memset((ptr), 0, (len))
108 #define snprintf _snprintf
109
110 inline size_t strlcpy(char *dst, const char *src, size_t size) {
111 int res = strlen(dst) + strlen(src) + 1;
112 if (size > 0) {
113 size_t n = size - 1;
114 strncpy(dst, src, n);
115 dst[n] = 0;
116 }
117 return res;
118 }
119 #endif // TARGET_OS_WIN32
120
121 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
122 static inline int
123 _pthread_workqueue_override_start_direct(mach_port_t thread,
124 pthread_priority_t priority)
125 {
126 (void)thread; (void)priority;
127 return 0;
128 }
129 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140716
130
131 #if PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
132 static inline int
133 _pthread_override_qos_class_start_direct(pthread_t thread,
134 pthread_priority_t priority)
135 {
136 (void)thread; (void)priority;
137 return 0;
138 }
139
140 static inline int
141 _pthread_override_qos_class_end_direct(mach_port_t thread)
142 {
143 (void)thread;
144 return 0;
145 }
146 #endif // PTHREAD_WORKQUEUE_SPI_VERSION < 20140707
147
148 #if !HAVE_NORETURN_BUILTIN_TRAP
149 /*
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.
153 */
154 DISPATCH_NORETURN
155 void __builtin_trap(void);
156 #endif
157
158 #if DISPATCH_HW_CONFIG_UP
159 #define DISPATCH_ATOMIC_UP 1
160 #endif
161
162 #include "shims/atomic.h"
163 #include "shims/atomic_sfb.h"
164 #include "shims/tsd.h"
165 #include "shims/yield.h"
166
167 #include "shims/hw_config.h"
168 #include "shims/perfmon.h"
169
170 #include "shims/getprogname.h"
171 #include "shims/time.h"
172
173 #ifdef __APPLE__
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])); \
181 } while (0)
182 #else
183 #define _dispatch_clear_stack(s)
184 #endif
185
186 #endif