Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / src / shims / tsd.h
1 /*
2  * Copyright (c) 2008-2009 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_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
37 #else
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;
42 #endif
43
44 #if USE_APPLE_TSD_OPTIMIZATIONS
45 #define SIMULATE_5491082 1
46 #ifndef _PTHREAD_TSD_OFFSET
47 #define _PTHREAD_TSD_OFFSET 0
48 #endif
49
50 static inline void
51 _dispatch_thread_setspecific(unsigned long k, void *v)
52 {
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");
57 #else
58         int res;
59         if (_pthread_has_direct_tsd()) {
60                 res = _pthread_setspecific_direct(k, v);
61         } else {
62                 res = pthread_setspecific(k, v);
63         }
64         dispatch_assert_zero(res);
65 #endif
66 }
67
68 static inline void *
69 _dispatch_thread_getspecific(unsigned long k)
70 {
71 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
72         void *rval;
73         asm("mov %%gs:%1, %0" : "=r" (rval) : "m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)));
74         return rval;
75 #else
76         if (_pthread_has_direct_tsd()) {
77                 return _pthread_getspecific_direct(k);
78         } else {
79                 return pthread_getspecific(k);
80         }
81 #endif
82 }
83
84 #else /* !USE_APPLE_TSD_OPTIMIZATIONS */
85
86 static inline void
87 _dispatch_thread_setspecific(pthread_key_t k, void *v)
88 {
89         int res;
90
91         res = pthread_setspecific(k, v);
92         dispatch_assert_zero(res);
93 }
94
95 static inline void *
96 _dispatch_thread_getspecific(pthread_key_t k)
97 {
98
99         return pthread_getspecific(k);
100 }
101 #endif /* USE_APPLE_TSD_OPTIMIZATIONS */
102
103 #if HAVE_PTHREAD_KEY_INIT_NP
104 static inline void
105 _dispatch_thread_key_init_np(unsigned long k, void (*d)(void *))
106 {
107         dispatch_assert_zero(pthread_key_init_np((int)k, d));
108 }
109 #else
110 static inline void
111 _dispatch_thread_key_create(pthread_key_t *key, void (*destructor)(void *))
112 {
113
114         dispatch_assert_zero(pthread_key_create(key, destructor));
115 }
116 #endif
117
118 #define _dispatch_thread_self (uintptr_t)pthread_self
119
120 #endif /* __DISPATCH_SHIMS_TSD__ */