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_SHIMS_TSD__
28 #define __DISPATCH_SHIMS_TSD__
30 #if HAVE_PTHREAD_MACHDEP_H
31 #include <pthread_machdep.h>
34 #define DISPATCH_TSD_INLINE DISPATCH_ALWAYS_INLINE_NDEBUG
36 #if USE_APPLE_TSD_OPTIMIZATIONS && HAVE_PTHREAD_KEY_INIT_NP && \
37 !defined(DISPATCH_USE_DIRECT_TSD)
38 #define DISPATCH_USE_DIRECT_TSD 1
39 #if __has_include(<os/tsd.h>)
44 #if DISPATCH_USE_DIRECT_TSD
45 static const unsigned long dispatch_queue_key
= __PTK_LIBDISPATCH_KEY0
;
46 #if DISPATCH_USE_OS_SEMAPHORE_CACHE
47 static const unsigned long dispatch_sema4_key
= __TSD_SEMAPHORE_CACHE
;
49 static const unsigned long dispatch_sema4_key
= __PTK_LIBDISPATCH_KEY1
;
51 static const unsigned long dispatch_cache_key
= __PTK_LIBDISPATCH_KEY2
;
52 static const unsigned long dispatch_io_key
= __PTK_LIBDISPATCH_KEY3
;
53 static const unsigned long dispatch_apply_key
= __PTK_LIBDISPATCH_KEY4
;
54 #if DISPATCH_INTROSPECTION
55 static const unsigned long dispatch_introspection_key
= __PTK_LIBDISPATCH_KEY5
;
56 #elif DISPATCH_PERF_MON
57 static const unsigned long dispatch_bcounter_key
= __PTK_LIBDISPATCH_KEY5
;
62 _dispatch_thread_key_create(const unsigned long *k
, void (*d
)(void *))
64 dispatch_assert_zero(pthread_key_init_np((int)*k
, d
));
67 extern pthread_key_t dispatch_queue_key
;
68 #if DISPATCH_USE_OS_SEMAPHORE_CACHE
69 #error "Invalid DISPATCH_USE_OS_SEMAPHORE_CACHE configuration"
71 extern pthread_key_t dispatch_sema4_key
;
73 extern pthread_key_t dispatch_cache_key
;
74 extern pthread_key_t dispatch_io_key
;
75 extern pthread_key_t dispatch_apply_key
;
76 #if DISPATCH_INTROSPECTION
77 extern pthread_key_t dispatch_introspection_key
;
78 #elif DISPATCH_PERF_MON
79 extern pthread_key_t dispatch_bcounter_key
;
85 _dispatch_thread_key_create(pthread_key_t
*k
, void (*d
)(void *))
87 dispatch_assert_zero(pthread_key_create(k
, d
));
91 #if DISPATCH_USE_TSD_BASE && !DISPATCH_DEBUG
92 #else // DISPATCH_USE_TSD_BASE
95 _dispatch_thread_setspecific(pthread_key_t k
, void *v
)
97 #if DISPATCH_USE_DIRECT_TSD
98 if (_pthread_has_direct_tsd()) {
99 (void)_pthread_setspecific_direct(k
, v
);
103 dispatch_assert_zero(pthread_setspecific(k
, v
));
108 _dispatch_thread_getspecific(pthread_key_t k
)
110 #if DISPATCH_USE_DIRECT_TSD
111 if (_pthread_has_direct_tsd()) {
112 return _pthread_getspecific_direct(k
);
115 return pthread_getspecific(k
);
117 #endif // DISPATCH_USE_TSD_BASE
120 #define _dispatch_thread_self() ((uintptr_t)GetCurrentThreadId())
122 #if DISPATCH_USE_DIRECT_TSD
123 #define _dispatch_thread_self() ((uintptr_t)_dispatch_thread_getspecific( \
124 _PTHREAD_TSD_SLOT_PTHREAD_SELF))
126 #define _dispatch_thread_self() ((uintptr_t)pthread_self())
130 DISPATCH_TSD_INLINE DISPATCH_CONST
131 static inline unsigned int
132 _dispatch_cpu_number(void)
134 #if TARGET_IPHONE_SIMULATOR && IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED < 1090
136 #elif __has_include(<os/tsd.h>)
137 return _os_cpu_number();
138 #elif defined(__x86_64__) || defined(__i386__)
139 struct { uintptr_t p1
, p2
; } p
;
140 __asm__("sidt %[p]" : [p
] "=&m" (p
));
141 return (unsigned int)(p
.p1
& 0xfff);
143 // Not yet implemented.
148 #undef DISPATCH_TSD_INLINE