]> git.saurik.com Git - apple/libdispatch.git/blob - src/os_shims.h
libdispatch-84.5.tar.gz
[apple/libdispatch.git] / src / os_shims.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_OS_SHIMS__
28 #define __DISPATCH_OS_SHIMS__
29
30 #include <pthread.h>
31 #include <pthread_machdep.h>
32 #include <pthread_workqueue.h>
33
34 __private_extern__ const char *__crashreporter_info__;
35
36 static const unsigned long dispatch_queue_key = __PTK_LIBDISPATCH_KEY0;
37 static const unsigned long dispatch_sema4_key = __PTK_LIBDISPATCH_KEY1;
38 static const unsigned long dispatch_cache_key = __PTK_LIBDISPATCH_KEY2;
39 static const unsigned long dispatch_bcounter_key = __PTK_LIBDISPATCH_KEY3;
40 //__PTK_LIBDISPATCH_KEY4
41 //__PTK_LIBDISPATCH_KEY5
42
43
44 #define SIMULATE_5491082 1
45 #ifndef _PTHREAD_TSD_OFFSET
46 #define _PTHREAD_TSD_OFFSET 0
47 #endif
48
49 static inline void
50 _dispatch_thread_setspecific(unsigned long k, void *v)
51 {
52 #if defined(SIMULATE_5491082) && defined(__i386__)
53 asm("movl %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "ri" (v) : "memory");
54 #elif defined(SIMULATE_5491082) && defined(__x86_64__)
55 asm("movq %1, %%gs:%0" : "=m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)) : "rn" (v) : "memory");
56 #else
57 int res;
58 if (_pthread_has_direct_tsd()) {
59 res = _pthread_setspecific_direct(k, v);
60 } else {
61 res = pthread_setspecific(k, v);
62 }
63 dispatch_assert_zero(res);
64 #endif
65 }
66
67 static inline void *
68 _dispatch_thread_getspecific(unsigned long k)
69 {
70 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
71 void *rval;
72 asm("mov %%gs:%1, %0" : "=r" (rval) : "m" (*(void **)(k * sizeof(void *) + _PTHREAD_TSD_OFFSET)));
73 return rval;
74 #else
75 if (_pthread_has_direct_tsd()) {
76 return _pthread_getspecific_direct(k);
77 } else {
78 return pthread_getspecific(k);
79 }
80 #endif
81 }
82
83 static inline void
84 _dispatch_thread_key_init_np(unsigned long k, void (*d)(void *))
85 {
86 dispatch_assert_zero(pthread_key_init_np((int)k, d));
87 }
88
89 #define _dispatch_thread_self pthread_self
90
91
92 #if DISPATCH_PERF_MON
93
94 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
95 #ifdef __LP64__
96 #define _dispatch_workitem_inc() asm("incq %%gs:%0" : "+m" \
97 (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc")
98 #define _dispatch_workitem_dec() asm("decq %%gs:%0" : "+m" \
99 (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc")
100 #else
101 #define _dispatch_workitem_inc() asm("incl %%gs:%0" : "+m" \
102 (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc")
103 #define _dispatch_workitem_dec() asm("decl %%gs:%0" : "+m" \
104 (*(void **)(dispatch_bcounter_key * sizeof(void *) + _PTHREAD_TSD_OFFSET)) :: "cc")
105 #endif
106 #else
107 static inline void
108 _dispatch_workitem_inc(void)
109 {
110 unsigned long cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key);
111 _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)++cnt);
112 }
113 static inline void
114 _dispatch_workitem_dec(void)
115 {
116 unsigned long cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key);
117 _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)--cnt);
118 }
119 #endif
120
121 // C99 doesn't define flsll() or ffsll()
122 #ifdef __LP64__
123 #define flsll(x) flsl(x)
124 #else
125 static inline unsigned int
126 flsll(uint64_t val)
127 {
128 union {
129 struct {
130 #ifdef __BIG_ENDIAN__
131 unsigned int hi, low;
132 #else
133 unsigned int low, hi;
134 #endif
135 } words;
136 uint64_t word;
137 } _bucket = {
138 .word = val,
139 };
140 if (_bucket.words.hi) {
141 return fls(_bucket.words.hi) + 32;
142 }
143 return fls(_bucket.words.low);
144 }
145 #endif
146
147 #else
148 #define _dispatch_workitem_inc()
149 #define _dispatch_workitem_dec()
150 #endif // DISPATCH_PERF_MON
151
152 #endif