2 * Copyright (c) 2003-2004, 2008, 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 /* Machine-dependent definitions for pthread internals. */
49 #ifndef _POSIX_PTHREAD_MACHDEP_H
50 #define _POSIX_PTHREAD_MACHDEP_H
54 #include <System/machine/cpu_capabilities.h>
58 #include <TargetConditionals.h>
62 ** Define macros for inline pthread_getspecific() usage.
63 ** We reserve a number of slots for Apple internal use.
64 ** This number can grow dynamically, no need to fix it.
67 /* This header contains pre defined thread specific keys */
68 /* 0 is used for pthread_self */
69 #define _PTHREAD_TSD_SLOT_PTHREAD_SELF 0
70 /* Keys 1- 9 for use by dyld, directly or indirectly */
71 #define _PTHREAD_TSD_SLOT_DYLD_1 1
72 #define _PTHREAD_TSD_SLOT_DYLD_2 2
73 #define _PTHREAD_TSD_SLOT_DYLD_3 3
74 #define _PTHREAD_TSD_RESERVED_SLOT_COUNT 4
75 /* To mirror the usage by dyld for Unwind_SjLj */
76 #define _PTHREAD_TSD_SLOT_DYLD_8 8
78 /* Keys 10 - 29 are for Libc/Libsystem internal ussage */
79 /* used as __pthread_tsd_first + Num */
80 #define __PTK_LIBC_LOCALE_KEY 10
81 #define __PTK_LIBC_TTYNAME_KEY 11
82 #define __PTK_LIBC_LOCALTIME_KEY 12
83 #define __PTK_LIBC_GMTIME_KEY 13
84 #define __PTK_LIBC_GDTOA_BIGINT_KEY 14
85 #define __PTK_LIBC_PARSEFLOAT_KEY 15
86 /* for usage by dyld */
87 #define __PTK_LIBC_DYLD_Unwind_SjLj_Key 18
89 /* Keys 20-25 for libdispactch usage */
90 #define __PTK_LIBDISPATCH_KEY0 20
91 #define __PTK_LIBDISPATCH_KEY1 21
92 #define __PTK_LIBDISPATCH_KEY2 22
93 #define __PTK_LIBDISPATCH_KEY3 23
94 #define __PTK_LIBDISPATCH_KEY4 24
95 #define __PTK_LIBDISPATCH_KEY5 25
97 /* Keys 30-255 for Non Libsystem usage */
99 /* Keys 30-39 for Graphic frameworks usage */
100 #define _PTHREAD_TSD_SLOT_OPENGL 30 /* backwards compat sake */
101 #define __PTK_FRAMEWORK_OPENGL_KEY 30
102 #define __PTK_FRAMEWORK_GRAPHICS_KEY1 31
103 #define __PTK_FRAMEWORK_GRAPHICS_KEY2 32
104 #define __PTK_FRAMEWORK_GRAPHICS_KEY3 33
105 #define __PTK_FRAMEWORK_GRAPHICS_KEY4 34
106 #define __PTK_FRAMEWORK_GRAPHICS_KEY5 35
107 #define __PTK_FRAMEWORK_GRAPHICS_KEY6 36
108 #define __PTK_FRAMEWORK_GRAPHICS_KEY7 37
109 #define __PTK_FRAMEWORK_GRAPHICS_KEY8 38
110 #define __PTK_FRAMEWORK_GRAPHICS_KEY9 39
112 /* Keys 40-49 for Objective-C runtime usage */
113 #define __PTK_FRAMEWORK_OBJC_KEY0 40
114 #define __PTK_FRAMEWORK_OBJC_KEY1 41
115 #define __PTK_FRAMEWORK_OBJC_KEY2 42
116 #define __PTK_FRAMEWORK_OBJC_KEY3 43
117 #define __PTK_FRAMEWORK_OBJC_KEY4 44
118 #define __PTK_FRAMEWORK_OBJC_KEY5 45
119 #define __PTK_FRAMEWORK_OBJC_KEY6 46
120 #define __PTK_FRAMEWORK_OBJC_KEY7 47
121 #define __PTK_FRAMEWORK_OBJC_KEY8 48
122 #define __PTK_FRAMEWORK_OBJC_KEY9 49
124 /* Keys 50-59 for Core Foundation usage */
125 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY0 50
126 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY1 51
127 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY2 52
128 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY3 53
129 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY4 54
130 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY5 55
131 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY6 56
132 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY7 57
133 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY8 58
134 #define __PTK_FRAMEWORK_COREFOUNDATION_KEY9 59
136 /* Keys 60-69 for Foundation usage */
137 #define __PTK_FRAMEWORK_FOUNDATION_KEY0 60
138 #define __PTK_FRAMEWORK_FOUNDATION_KEY1 61
139 #define __PTK_FRAMEWORK_FOUNDATION_KEY2 62
140 #define __PTK_FRAMEWORK_FOUNDATION_KEY3 63
141 #define __PTK_FRAMEWORK_FOUNDATION_KEY4 64
142 #define __PTK_FRAMEWORK_FOUNDATION_KEY5 65
143 #define __PTK_FRAMEWORK_FOUNDATION_KEY6 66
144 #define __PTK_FRAMEWORK_FOUNDATION_KEY7 67
145 #define __PTK_FRAMEWORK_FOUNDATION_KEY8 68
146 #define __PTK_FRAMEWORK_FOUNDATION_KEY9 69
148 /* Keys 70-79 for Core Animation/QuartzCore usage */
149 #define __PTK_FRAMEWORK_QUARTZCORE_KEY0 70
150 #define __PTK_FRAMEWORK_QUARTZCORE_KEY1 71
151 #define __PTK_FRAMEWORK_QUARTZCORE_KEY2 72
152 #define __PTK_FRAMEWORK_QUARTZCORE_KEY3 73
153 #define __PTK_FRAMEWORK_QUARTZCORE_KEY4 74
154 #define __PTK_FRAMEWORK_QUARTZCORE_KEY5 75
155 #define __PTK_FRAMEWORK_QUARTZCORE_KEY6 76
156 #define __PTK_FRAMEWORK_QUARTZCORE_KEY7 77
157 #define __PTK_FRAMEWORK_QUARTZCORE_KEY8 78
158 #define __PTK_FRAMEWORK_QUARTZCORE_KEY9 79
161 /* Keys 80-89 for Garbage Collection */
162 #define __PTK_FRAMEWORK_OLDGC_KEY0 80
163 #define __PTK_FRAMEWORK_OLDGC_KEY1 81
164 #define __PTK_FRAMEWORK_OLDGC_KEY2 82
165 #define __PTK_FRAMEWORK_OLDGC_KEY3 83
166 #define __PTK_FRAMEWORK_OLDGC_KEY4 84
167 #define __PTK_FRAMEWORK_OLDGC_KEY5 85
168 #define __PTK_FRAMEWORK_OLDGC_KEY6 86
169 #define __PTK_FRAMEWORK_OLDGC_KEY7 87
170 #define __PTK_FRAMEWORK_OLDGC_KEY8 88
171 #define __PTK_FRAMEWORK_OLDGC_KEY9 89
173 /* Keys 90-94 for JavaScriptCore Collection */
174 #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0 90
175 #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1 91
176 #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY2 92
177 #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY3 93
178 #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY4 94
179 /* Keys 95 for CoreText */
180 #define __PTK_FRAMEWORK_CORETEXT_KEY0 95
182 /* Keys 110-119 for Garbage Collection */
183 #define __PTK_FRAMEWORK_GC_KEY0 110
184 #define __PTK_FRAMEWORK_GC_KEY1 111
185 #define __PTK_FRAMEWORK_GC_KEY2 112
186 #define __PTK_FRAMEWORK_GC_KEY3 113
187 #define __PTK_FRAMEWORK_GC_KEY4 114
188 #define __PTK_FRAMEWORK_GC_KEY5 115
189 #define __PTK_FRAMEWORK_GC_KEY6 116
190 #define __PTK_FRAMEWORK_GC_KEY7 117
191 #define __PTK_FRAMEWORK_GC_KEY8 118
192 #define __PTK_FRAMEWORK_GC_KEY9 119
195 ** Define macros for inline pthread_getspecific() usage.
196 ** We reserve a number of slots for Apple internal use.
197 ** This number can grow dynamically, no need to fix it.
201 #if defined(__cplusplus)
205 extern void *pthread_getspecific(unsigned long);
206 extern int pthread_setspecific(unsigned long, const void *);
207 /* setup destructor function for static key as it is not created with pthread_key_create() */
208 int pthread_key_init_np(int, void (*)(void *));
210 #if defined(__cplusplus)
214 typedef int pthread_lock_t
;
216 __inline__
static int
217 _pthread_has_direct_tsd(void)
219 #if TARGET_IPHONE_SIMULATOR
220 /* Simulator will use the host implementation, so bypass the macro that is in the target code */
222 #elif defined(__ppc__)
223 int *caps
= (int *)_COMM_PAGE_CPU_CAPABILITIES
;
224 if (*caps
& kFastThreadLocalStorage
) {
234 #if TARGET_IPHONE_SIMULATOR || defined(__ppc__) || defined(__ppc64__) || \
235 (defined(__arm__) && !defined(_ARM_ARCH_7) && defined(_ARM_ARCH_6) && defined(__thumb__))
237 #define _pthread_getspecific_direct(key) pthread_getspecific((key))
238 #define _pthread_setspecific_direct(key, val) pthread_setspecific((key), (val))
242 /* To be used with static constant keys only */
243 __inline__
static void *
244 _pthread_getspecific_direct(unsigned long slot
)
247 #if defined(__i386__) || defined(__x86_64__)
248 __asm__("mov %%gs:%1, %0" : "=r" (ret
) : "m" (*(void **)(slot
* sizeof(void *))));
249 #elif (defined(__arm__) && (defined(_ARM_ARCH_6) || defined(_ARM_ARCH_5)))
250 void **__pthread_tsd
;
251 #if defined(__arm__) && defined(_ARM_ARCH_6)
252 uintptr_t __pthread_tpid
;
253 __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r" (__pthread_tpid
));
254 __pthread_tsd
= (void**)(__pthread_tpid
& ~0x3ul
);
255 #elif defined(__arm__) && defined(_ARM_ARCH_5)
256 register uintptr_t __pthread_tpid
asm ("r9");
257 __pthread_tsd
= (void**)__pthread_tpid
;
259 ret
= __pthread_tsd
[slot
];
261 #error no _pthread_getspecific_direct implementation for this arch
266 /* To be used with static constant keys only */
267 __inline__
static int
268 _pthread_setspecific_direct(unsigned long slot
, void * val
)
270 #if defined(__i386__)
272 __asm__("movl %1,%%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
274 __asm__("movl %1,%%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "ri" (val
));
276 #elif defined(__x86_64__)
277 /* PIC is free and cannot be disabled, even with: gcc -mdynamic-no-pic ... */
278 __asm__("movq %1,%%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
279 #elif (defined(__arm__) && (defined(_ARM_ARCH_6) || defined(_ARM_ARCH_5)))
280 void **__pthread_tsd
;
281 #if defined(__arm__) && defined(_ARM_ARCH_6)
282 uintptr_t __pthread_tpid
;
283 __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r" (__pthread_tpid
));
284 __pthread_tsd
= (void**)(__pthread_tpid
& ~0x3ul
);
285 #elif defined(__arm__) && defined(_ARM_ARCH_5)
286 register uintptr_t __pthread_tpid
asm ("r9");
287 __pthread_tsd
= (void**)__pthread_tpid
;
289 __pthread_tsd
[slot
] = val
;
291 #error no _pthread_setspecific_direct implementation for this arch
298 #define LOCK_INIT(l) ((l) = 0)
299 #define LOCK_INITIALIZER 0
301 #endif /* ! __ASSEMBLER__ */
302 #endif /* _POSIX_PTHREAD_MACHDEP_H */