2 * Copyright (c) 2008-2009 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_KEY_INIT_NP
31 static const unsigned long dispatch_queue_key = __PTK_LIBDISPATCH_KEY0;
32 static const unsigned long dispatch_sema4_key = __PTK_LIBDISPATCH_KEY1;
33 static const unsigned long dispatch_cache_key = __PTK_LIBDISPATCH_KEY2;
34 static const unsigned long dispatch_bcounter_key = __PTK_LIBDISPATCH_KEY3;
35 //__PTK_LIBDISPATCH_KEY4
36 //__PTK_LIBDISPATCH_KEY5
38 extern pthread_key_t dispatch_queue_key;
39 extern pthread_key_t dispatch_sema4_key;
40 extern pthread_key_t dispatch_cache_key;
41 extern pthread_key_t dispatch_bcounter_key;
44 #if USE_APPLE_TSD_OPTIMIZATIONS
45 #define SIMULATE_5491082 1
46 #ifndef _PTHREAD_TSD_OFFSET
47 #define _PTHREAD_TSD_OFFSET 0
51 _dispatch_thread_setspecific(unsigned long k, void *v)
53 #if defined(SIMULATE_5491082) && defined(__i386__)
54 asm("movl %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "ri" (v) : "memory");
55 #elif defined(SIMULATE_5491082) && defined(__x86_64__)
56 asm("movq %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "rn" (v) : "memory");
59 if (_pthread_has_direct_tsd()) {
60 res = _pthread_setspecific_direct(k, v);
62 res = pthread_setspecific(k, v);
64 dispatch_assert_zero(res);
69 _dispatch_thread_getspecific(unsigned long k)
71 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
73 asm("mov %%gs:%1, %0" : "=r" (rval) : "m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)));
76 if (_pthread_has_direct_tsd()) {
77 return _pthread_getspecific_direct(k);
79 return pthread_getspecific(k);
84 #else /* !USE_APPLE_TSD_OPTIMIZATIONS */
87 _dispatch_thread_setspecific(pthread_key_t k, void *v)
91 res = pthread_setspecific(k, v);
92 dispatch_assert_zero(res);
96 _dispatch_thread_getspecific(pthread_key_t k)
99 return pthread_getspecific(k);
101 #endif /* USE_APPLE_TSD_OPTIMIZATIONS */
103 #if HAVE_PTHREAD_KEY_INIT_NP
105 _dispatch_thread_key_init_np(unsigned long k, void (*d)(void *))
107 dispatch_assert_zero(pthread_key_init_np((int)k, d));
111 _dispatch_thread_key_create(pthread_key_t *key, void (*destructor)(void *))
114 dispatch_assert_zero(pthread_key_create(key, destructor));
118 #define _dispatch_thread_self (uintptr_t)pthread_self
120 #endif /* __DISPATCH_SHIMS_TSD__ */