]> git.saurik.com Git - cycript.git/blame - include/pthread_machdep.h
Support casting any pointer to a function type.
[cycript.git] / include / pthread_machdep.h
CommitLineData
7c6c5b0a
JF
1/*
2 * Copyright (c) 2003-2004, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
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.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
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.
42 */
43/*
44 * MkLinux
45 */
46
47/* Machine-dependent definitions for pthread internals. */
48
49#ifndef _POSIX_PTHREAD_MACHDEP_H
50#define _POSIX_PTHREAD_MACHDEP_H
51
7c6c5b0a
JF
52#ifndef __ASSEMBLER__
53
54#include <System/machine/cpu_capabilities.h>
55#ifdef __arm__
56#include <arm/arch.h>
57#endif
3615a2f7 58#include <TargetConditionals.h>
7c6c5b0a
JF
59
60/*
61** Define macros for inline pthread_getspecific() usage.
62** We reserve a number of slots for Apple internal use.
63** This number can grow dynamically, no need to fix it.
64*/
65
66/* This header contains pre defined thread specific keys */
67/* 0 is used for pthread_self */
68#define _PTHREAD_TSD_SLOT_PTHREAD_SELF 0
69/* Keys 1- 9 for use by dyld, directly or indirectly */
70#define _PTHREAD_TSD_SLOT_DYLD_1 1
71#define _PTHREAD_TSD_SLOT_DYLD_2 2
72#define _PTHREAD_TSD_SLOT_DYLD_3 3
73#define _PTHREAD_TSD_RESERVED_SLOT_COUNT 4
3615a2f7
JF
74/* To mirror the usage by dyld for Unwind_SjLj */
75#define _PTHREAD_TSD_SLOT_DYLD_8 8
7c6c5b0a
JF
76
77/* Keys 10 - 29 are for Libc/Libsystem internal ussage */
78/* used as __pthread_tsd_first + Num */
79#define __PTK_LIBC_LOCALE_KEY 10
80#define __PTK_LIBC_TTYNAME_KEY 11
81#define __PTK_LIBC_LOCALTIME_KEY 12
82#define __PTK_LIBC_GMTIME_KEY 13
83#define __PTK_LIBC_GDTOA_BIGINT_KEY 14
84#define __PTK_LIBC_PARSEFLOAT_KEY 15
3615a2f7
JF
85/* for usage by dyld */
86#define __PTK_LIBC_DYLD_Unwind_SjLj_Key 18
7c6c5b0a
JF
87
88/* Keys 20-25 for libdispactch usage */
89#define __PTK_LIBDISPATCH_KEY0 20
90#define __PTK_LIBDISPATCH_KEY1 21
91#define __PTK_LIBDISPATCH_KEY2 22
92#define __PTK_LIBDISPATCH_KEY3 23
93#define __PTK_LIBDISPATCH_KEY4 24
94#define __PTK_LIBDISPATCH_KEY5 25
95
96/* Keys 30-255 for Non Libsystem usage */
97
98/* Keys 30-39 for Graphic frameworks usage */
99#define _PTHREAD_TSD_SLOT_OPENGL 30 /* backwards compat sake */
100#define __PTK_FRAMEWORK_OPENGL_KEY 30
101#define __PTK_FRAMEWORK_GRAPHICS_KEY1 31
102#define __PTK_FRAMEWORK_GRAPHICS_KEY2 32
103#define __PTK_FRAMEWORK_GRAPHICS_KEY3 33
104#define __PTK_FRAMEWORK_GRAPHICS_KEY4 34
105#define __PTK_FRAMEWORK_GRAPHICS_KEY5 35
106#define __PTK_FRAMEWORK_GRAPHICS_KEY6 36
107#define __PTK_FRAMEWORK_GRAPHICS_KEY7 37
108#define __PTK_FRAMEWORK_GRAPHICS_KEY8 38
109#define __PTK_FRAMEWORK_GRAPHICS_KEY9 39
110
111/* Keys 40-49 for Objective-C runtime usage */
112#define __PTK_FRAMEWORK_OBJC_KEY0 40
113#define __PTK_FRAMEWORK_OBJC_KEY1 41
114#define __PTK_FRAMEWORK_OBJC_KEY2 42
115#define __PTK_FRAMEWORK_OBJC_KEY3 43
116#define __PTK_FRAMEWORK_OBJC_KEY4 44
117#define __PTK_FRAMEWORK_OBJC_KEY5 45
118#define __PTK_FRAMEWORK_OBJC_KEY6 46
119#define __PTK_FRAMEWORK_OBJC_KEY7 47
120#define __PTK_FRAMEWORK_OBJC_KEY8 48
121#define __PTK_FRAMEWORK_OBJC_KEY9 49
122
123/* Keys 50-59 for Core Foundation usage */
124#define __PTK_FRAMEWORK_COREFOUNDATION_KEY0 50
125#define __PTK_FRAMEWORK_COREFOUNDATION_KEY1 51
126#define __PTK_FRAMEWORK_COREFOUNDATION_KEY2 52
127#define __PTK_FRAMEWORK_COREFOUNDATION_KEY3 53
128#define __PTK_FRAMEWORK_COREFOUNDATION_KEY4 54
129#define __PTK_FRAMEWORK_COREFOUNDATION_KEY5 55
130#define __PTK_FRAMEWORK_COREFOUNDATION_KEY6 56
131#define __PTK_FRAMEWORK_COREFOUNDATION_KEY7 57
132#define __PTK_FRAMEWORK_COREFOUNDATION_KEY8 58
133#define __PTK_FRAMEWORK_COREFOUNDATION_KEY9 59
134
135/* Keys 60-69 for Foundation usage */
136#define __PTK_FRAMEWORK_FOUNDATION_KEY0 60
137#define __PTK_FRAMEWORK_FOUNDATION_KEY1 61
138#define __PTK_FRAMEWORK_FOUNDATION_KEY2 62
139#define __PTK_FRAMEWORK_FOUNDATION_KEY3 63
140#define __PTK_FRAMEWORK_FOUNDATION_KEY4 64
141#define __PTK_FRAMEWORK_FOUNDATION_KEY5 65
142#define __PTK_FRAMEWORK_FOUNDATION_KEY6 66
143#define __PTK_FRAMEWORK_FOUNDATION_KEY7 67
144#define __PTK_FRAMEWORK_FOUNDATION_KEY8 68
145#define __PTK_FRAMEWORK_FOUNDATION_KEY9 69
146
147/* Keys 70-79 for Core Animation/QuartzCore usage */
148#define __PTK_FRAMEWORK_QUARTZCORE_KEY0 70
149#define __PTK_FRAMEWORK_QUARTZCORE_KEY1 71
150#define __PTK_FRAMEWORK_QUARTZCORE_KEY2 72
151#define __PTK_FRAMEWORK_QUARTZCORE_KEY3 73
152#define __PTK_FRAMEWORK_QUARTZCORE_KEY4 74
153#define __PTK_FRAMEWORK_QUARTZCORE_KEY5 75
154#define __PTK_FRAMEWORK_QUARTZCORE_KEY6 76
155#define __PTK_FRAMEWORK_QUARTZCORE_KEY7 77
156#define __PTK_FRAMEWORK_QUARTZCORE_KEY8 78
157#define __PTK_FRAMEWORK_QUARTZCORE_KEY9 79
158
159
160/* Keys 80-89 for Garbage Collection */
3615a2f7
JF
161#define __PTK_FRAMEWORK_OLDGC_KEY0 80
162#define __PTK_FRAMEWORK_OLDGC_KEY1 81
163#define __PTK_FRAMEWORK_OLDGC_KEY2 82
164#define __PTK_FRAMEWORK_OLDGC_KEY3 83
165#define __PTK_FRAMEWORK_OLDGC_KEY4 84
166#define __PTK_FRAMEWORK_OLDGC_KEY5 85
167#define __PTK_FRAMEWORK_OLDGC_KEY6 86
168#define __PTK_FRAMEWORK_OLDGC_KEY7 87
169#define __PTK_FRAMEWORK_OLDGC_KEY8 88
170#define __PTK_FRAMEWORK_OLDGC_KEY9 89
171
172/* Keys 90-94 for JavaScriptCore Collection */
173#define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0 90
174#define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1 91
175#define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY2 92
176#define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY3 93
177#define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY4 94
7c6c5b0a 178
3615a2f7
JF
179/* Keys 110-119 for Garbage Collection */
180#define __PTK_FRAMEWORK_GC_KEY0 110
181#define __PTK_FRAMEWORK_GC_KEY1 111
182#define __PTK_FRAMEWORK_GC_KEY2 112
183#define __PTK_FRAMEWORK_GC_KEY3 113
184#define __PTK_FRAMEWORK_GC_KEY4 114
185#define __PTK_FRAMEWORK_GC_KEY5 115
186#define __PTK_FRAMEWORK_GC_KEY6 116
187#define __PTK_FRAMEWORK_GC_KEY7 117
188#define __PTK_FRAMEWORK_GC_KEY8 118
189#define __PTK_FRAMEWORK_GC_KEY9 119
7c6c5b0a
JF
190
191/*
192** Define macros for inline pthread_getspecific() usage.
193** We reserve a number of slots for Apple internal use.
194** This number can grow dynamically, no need to fix it.
195*/
196
197
198#if defined(__cplusplus)
199extern "C" {
200#endif
201
202extern void *pthread_getspecific(unsigned long);
203/* setup destructor function for static key as it is not created with pthread_key_create() */
204int pthread_key_init_np(int, void (*)(void *));
205
206#if defined(__cplusplus)
207}
208#endif
209
210typedef int pthread_lock_t;
211
3615a2f7
JF
212#if TARGET_IPHONE_SIMULATOR
213
214/* Similator will use the host implementation, so bypass the macro that is in the target code */
215
216inline static int
217_pthread_has_direct_tsd(void)
218{
219 return 0;
220}
221
222#define _pthread_getspecific_direct(key) pthread_getspecific(key)
223#define _pthread_setspecific_direct(key, val) pthread_setspecific(key, val)
224
225#else /* TARGET_IPHONE_SIMULATOR */
226
7c6c5b0a
JF
227inline static int
228_pthread_has_direct_tsd(void)
229{
230#if defined(__ppc__)
231 int *caps = (int *)_COMM_PAGE_CPU_CAPABILITIES;
232 if (*caps & kFastThreadLocalStorage) {
233 return 1;
234 } else {
235 return 0;
236 }
7c6c5b0a
JF
237#else
238 return 1;
239#endif
240}
241
242/* To be used with static constant keys only */
243inline static void *
244_pthread_getspecific_direct(unsigned long slot)
245{
246 void *ret;
3615a2f7 247
7c6c5b0a 248#if defined(__i386__) || defined(__x86_64__)
3615a2f7
JF
249 asm("mov %%gs:%1, %0" : "=r" (ret) : "m" (*(void **)(slot * sizeof(void *))));
250#elif defined(__ppc64__) || defined(__ppc__)
251 ret = pthread_getspecific(slot);
252#elif defined(__arm__) && defined(_ARM_ARCH_6) && !defined(_ARM_ARCH_7) && defined(__thumb__) && !defined(__OPTIMIZE__)
253 ret = pthread_getspecific(slot);
7c6c5b0a 254#elif defined(__arm__) && defined(_ARM_ARCH_6)
3615a2f7
JF
255 void **__pthread_tsd;
256 __asm__ (
257 "mrc p15, 0, %0, c13, c0, 3\n"
258 "bic %0, %0, #3\n"
259 : "=r"(__pthread_tsd));
260 ret = __pthread_tsd[slot];
7c6c5b0a
JF
261#elif defined(__arm__) && !defined(_ARM_ARCH_6)
262 register void **__pthread_tsd asm ("r9");
3615a2f7 263 ret = __pthread_tsd[slot];
7c6c5b0a
JF
264#else
265#error no pthread_getspecific_direct implementation for this arch
266#endif
267 return ret;
268}
269
3615a2f7
JF
270#if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
271/* To be used with static constant keys only */
272inline static int
273_pthread_setspecific_direct(unsigned long slot, void * val)
274{
275
276#if defined(__i386__)
277#if defined(__PIC__)
278 asm("movl %1,%%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
279#else
280 asm("movl %1,%%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "ri" (val));
281#endif
282#elif defined(__x86_64__)
283 /* PIC is free and cannot be disabled, even with: gcc -mdynamic-no-pic ... */
284 asm("movq %1,%%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
285#elif defined(__arm__) && defined(_ARM_ARCH_6)
286 void **__pthread_tsd;
287 __asm__ (
288 "mrc p15, 0, %0, c13, c0, 3\n"
289 "bic %0, %0, #3\n"
290 : "=r"(__pthread_tsd));
291 __pthread_tsd[slot] = val;
292#elif defined(__arm__) && !defined(_ARM_ARCH_6)
293 register void **__pthread_tsd asm ("r9");
294 __pthread_tsd[slot] = val;
295#endif
296 return(0);
297}
298#elif defined(__ppc__) || defined(__ppc64__)
7c6c5b0a
JF
299/* To be used with static constant keys only */
300#define _pthread_setspecific_direct(key, val) pthread_setspecific(key, val)
3615a2f7
JF
301#else
302#error no pthread_setspecific_direct implementation for this arch
303#endif
304
305#endif /* TARGET_IPHONE_SIMULATOR */
7c6c5b0a
JF
306
307#define LOCK_INIT(l) ((l) = 0)
308#define LOCK_INITIALIZER 0
309
310#endif /* ! __ASSEMBLER__ */
311#endif /* _POSIX_PTHREAD_MACHDEP_H */