]>
git.saurik.com Git - apple/libdispatch.git/blob - src/os_shims.h
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_OS_SHIMS__
28 #define __DISPATCH_OS_SHIMS__
31 #include <pthread_machdep.h>
32 #include <pthread_workqueue.h>
34 __private_extern__
const char *__crashreporter_info__
;
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
44 #define SIMULATE_5491082 1
45 #ifndef _PTHREAD_TSD_OFFSET
46 #define _PTHREAD_TSD_OFFSET 0
50 _dispatch_thread_setspecific(unsigned long k
, void *v
)
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");
58 if (_pthread_has_direct_tsd()) {
59 res
= _pthread_setspecific_direct(k
, v
);
61 res
= pthread_setspecific(k
, v
);
63 dispatch_assert_zero(res
);
68 _dispatch_thread_getspecific(unsigned long k
)
70 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
72 asm("mov %%gs:%1, %0" : "=r" (rval
) : "m" (*(void **)(k
* sizeof(void *) + _PTHREAD_TSD_OFFSET
)));
75 if (_pthread_has_direct_tsd()) {
76 return _pthread_getspecific_direct(k
);
78 return pthread_getspecific(k
);
84 _dispatch_thread_key_init_np(unsigned long k
, void (*d
)(void *))
86 dispatch_assert_zero(pthread_key_init_np((int)k
, d
));
89 #define _dispatch_thread_self pthread_self
94 #if defined(SIMULATE_5491082) && (defined(__i386__) || defined(__x86_64__))
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")
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")
108 _dispatch_workitem_inc(void)
110 unsigned long cnt
= (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key
);
111 _dispatch_thread_setspecific(dispatch_bcounter_key
, (void *)++cnt
);
114 _dispatch_workitem_dec(void)
116 unsigned long cnt
= (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key
);
117 _dispatch_thread_setspecific(dispatch_bcounter_key
, (void *)--cnt
);
121 // C99 doesn't define flsll() or ffsll()
123 #define flsll(x) flsl(x)
125 static inline unsigned int
130 #ifdef __BIG_ENDIAN__
131 unsigned int hi
, low
;
133 unsigned int low
, hi
;
140 if (_bucket
.words
.hi
) {
141 return fls(_bucket
.words
.hi
) + 32;
143 return fls(_bucket
.words
.low
);
148 #define _dispatch_workitem_inc()
149 #define _dispatch_workitem_dec()
150 #endif // DISPATCH_PERF_MON