]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims/tsd.h
libdispatch-339.1.9.tar.gz
[apple/libdispatch.git] / src / shims / tsd.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_SHIMS_TSD__
28 #define __DISPATCH_SHIMS_TSD__
29
30 #if HAVE_PTHREAD_MACHDEP_H
31 #include <pthread_machdep.h>
32 #endif
33
34 #define DISPATCH_TSD_INLINE DISPATCH_ALWAYS_INLINE_NDEBUG
35
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 #endif
40
41 #if DISPATCH_USE_DIRECT_TSD
42 static const unsigned long dispatch_queue_key = __PTK_LIBDISPATCH_KEY0;
43 #if DISPATCH_USE_OS_SEMAPHORE_CACHE
44 static const unsigned long dispatch_sema4_key = __TSD_SEMAPHORE_CACHE;
45 #else
46 static const unsigned long dispatch_sema4_key = __PTK_LIBDISPATCH_KEY1;
47 #endif
48 static const unsigned long dispatch_cache_key = __PTK_LIBDISPATCH_KEY2;
49 static const unsigned long dispatch_io_key = __PTK_LIBDISPATCH_KEY3;
50 static const unsigned long dispatch_apply_key = __PTK_LIBDISPATCH_KEY4;
51 #if DISPATCH_INTROSPECTION
52 static const unsigned long dispatch_introspection_key = __PTK_LIBDISPATCH_KEY5;
53 #elif DISPATCH_PERF_MON
54 static const unsigned long dispatch_bcounter_key = __PTK_LIBDISPATCH_KEY5;
55 #endif
56
57 DISPATCH_TSD_INLINE
58 static inline void
59 _dispatch_thread_key_create(const unsigned long *k, void (*d)(void *))
60 {
61 dispatch_assert_zero(pthread_key_init_np((int)*k, d));
62 }
63 #else
64 extern pthread_key_t dispatch_queue_key;
65 #if DISPATCH_USE_OS_SEMAPHORE_CACHE
66 #error "Invalid DISPATCH_USE_OS_SEMAPHORE_CACHE configuration"
67 #else
68 extern pthread_key_t dispatch_sema4_key;
69 #endif
70 extern pthread_key_t dispatch_cache_key;
71 extern pthread_key_t dispatch_io_key;
72 extern pthread_key_t dispatch_apply_key;
73 #if DISPATCH_INTROSPECTION
74 extern pthread_key_t dispatch_introspection_key;
75 #elif DISPATCH_PERF_MON
76 extern pthread_key_t dispatch_bcounter_key;
77 #endif
78
79
80 DISPATCH_TSD_INLINE
81 static inline void
82 _dispatch_thread_key_create(pthread_key_t *k, void (*d)(void *))
83 {
84 dispatch_assert_zero(pthread_key_create(k, d));
85 }
86 #endif
87
88 #if DISPATCH_USE_TSD_BASE && !DISPATCH_DEBUG
89 #else // DISPATCH_USE_TSD_BASE
90 DISPATCH_TSD_INLINE
91 static inline void
92 _dispatch_thread_setspecific(pthread_key_t k, void *v)
93 {
94 #if DISPATCH_USE_DIRECT_TSD
95 if (_pthread_has_direct_tsd()) {
96 (void)_pthread_setspecific_direct(k, v);
97 return;
98 }
99 #endif
100 dispatch_assert_zero(pthread_setspecific(k, v));
101 }
102
103 DISPATCH_TSD_INLINE
104 static inline void *
105 _dispatch_thread_getspecific(pthread_key_t k)
106 {
107 #if DISPATCH_USE_DIRECT_TSD
108 if (_pthread_has_direct_tsd()) {
109 return _pthread_getspecific_direct(k);
110 }
111 #endif
112 return pthread_getspecific(k);
113 }
114 #endif // DISPATCH_USE_TSD_BASE
115
116 #if TARGET_OS_WIN32
117 #define _dispatch_thread_self() ((uintptr_t)GetCurrentThreadId())
118 #else
119 #if DISPATCH_USE_DIRECT_TSD
120 #define _dispatch_thread_self() ((uintptr_t)_dispatch_thread_getspecific( \
121 _PTHREAD_TSD_SLOT_PTHREAD_SELF))
122 #else
123 #define _dispatch_thread_self() ((uintptr_t)pthread_self())
124 #endif
125 #endif
126
127 DISPATCH_TSD_INLINE DISPATCH_CONST
128 static inline unsigned int
129 _dispatch_cpu_number(void)
130 {