tsd_private.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2003-2013 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. #ifndef __PTHREAD_TSD_H__
  47. #define __PTHREAD_TSD_H__
  48. #ifndef __ASSEMBLER__
  49. #include <System/machine/cpu_capabilities.h>
  50. #include <sys/cdefs.h>
  51. #include <TargetConditionals.h>
  52. #include <Availability.h>
  53. #include <os/tsd.h>
  54. #include <pthread/spinlock_private.h>
  55. #ifndef __TSD_MACH_THREAD_SELF
  56. #define __TSD_MACH_THREAD_SELF 3
  57. #endif
  58. #ifndef __TSD_THREAD_QOS_CLASS
  59. #define __TSD_THREAD_QOS_CLASS 4
  60. #endif
  61. #ifndef __TSD_RETURN_TO_KERNEL
  62. #define __TSD_RETURN_TO_KERNEL 5
  63. #endif
  64. #ifndef __TSD_PTR_MUNGE
  65. #define __TSD_PTR_MUNGE 7
  66. #endif
  67. #ifndef __TSD_MACH_SPECIAL_REPLY
  68. #define __TSD_MACH_SPECIAL_REPLY 8
  69. #endif
  70. /* Constant TSD slots for inline pthread_getspecific() usage. */
  71. /* Keys 0 - 9 are for Libsyscall/libplatform usage */
  72. #define _PTHREAD_TSD_SLOT_PTHREAD_SELF __TSD_THREAD_SELF
  73. #define _PTHREAD_TSD_SLOT_ERRNO __TSD_ERRNO
  74. #define _PTHREAD_TSD_SLOT_MIG_REPLY __TSD_MIG_REPLY
  75. #define _PTHREAD_TSD_SLOT_MACH_THREAD_SELF __TSD_MACH_THREAD_SELF
  76. #define _PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS __TSD_THREAD_QOS_CLASS
  77. #define _PTHREAD_TSD_SLOT_RETURN_TO_KERNEL __TSD_RETURN_TO_KERNEL
  78. #define _PTHREAD_TSD_SLOT_PTR_MUNGE __TSD_PTR_MUNGE
  79. #define _PTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY __TSD_MACH_SPECIAL_REPLY
  80. //#define _PTHREAD_TSD_SLOT_SEMAPHORE_CACHE __TSD_SEMAPHORE_CACHE
  81. /*
  82. * Windows 64-bit ABI bakes %gs relative accesses into its code in the same
  83. * range as our TSD keys. To allow some limited interoperability for code
  84. * targeting that ABI, we leave slots 6 and 11 unused.
  85. *
  86. * The Go runtime on x86_64 also uses this because their ABI doesn't reserve a
  87. * register for the TSD base. They were previously using an arbitrarily chosen
  88. * dynamic key and relying on being able to get it at runtime, but switched to
  89. * this slot to avoid issues with that approach. It's assumed that Go and
  90. * Windows code won't run in the same address space.
  91. */
  92. //#define _PTHREAD_TSD_SLOT_RESERVED_WIN64 6
  93. #define _PTHREAD_TSD_RESERVED_SLOT_COUNT _PTHREAD_TSD_RESERVED_SLOT_COUNT
  94. /* Keys 10 - 29 are for Libc/Libsystem internal usage */
  95. /* used as __pthread_tsd_first + Num */
  96. #define __PTK_LIBC_LOCALE_KEY 10
  97. //#define __PTK_LIBC_RESERVED_WIN64 11
  98. #define __PTK_LIBC_LOCALTIME_KEY 12
  99. #define __PTK_LIBC_GMTIME_KEY 13
  100. #define __PTK_LIBC_GDTOA_BIGINT_KEY 14
  101. #define __PTK_LIBC_PARSEFLOAT_KEY 15
  102. #define __PTK_LIBC_TTYNAME_KEY 16
  103. /* for usage by dyld */
  104. #define __PTK_LIBC_DYLD_Unwind_SjLj_Key 18
  105. /* Keys 20-29 for libdispatch usage */
  106. #define __PTK_LIBDISPATCH_KEY0 20
  107. #define __PTK_LIBDISPATCH_KEY1 21
  108. #define __PTK_LIBDISPATCH_KEY2 22
  109. #define __PTK_LIBDISPATCH_KEY3 23
  110. #define __PTK_LIBDISPATCH_KEY4 24
  111. #define __PTK_LIBDISPATCH_KEY5 25
  112. #define __PTK_LIBDISPATCH_KEY6 26
  113. #define __PTK_LIBDISPATCH_KEY7 27
  114. #define __PTK_LIBDISPATCH_KEY8 28
  115. #define __PTK_LIBDISPATCH_KEY9 29
  116. /* Keys 30-255 for Non Libsystem usage */
  117. /* Keys 30-39 for Graphic frameworks usage */
  118. #define _PTHREAD_TSD_SLOT_OPENGL 30 /* backwards compat sake */
  119. #define __PTK_FRAMEWORK_OPENGL_KEY 30
  120. #define __PTK_FRAMEWORK_GRAPHICS_KEY1 31
  121. #define __PTK_FRAMEWORK_GRAPHICS_KEY2 32
  122. #define __PTK_FRAMEWORK_GRAPHICS_KEY3 33
  123. #define __PTK_FRAMEWORK_GRAPHICS_KEY4 34
  124. #define __PTK_FRAMEWORK_GRAPHICS_KEY5 35
  125. #define __PTK_FRAMEWORK_GRAPHICS_KEY6 36
  126. #define __PTK_FRAMEWORK_GRAPHICS_KEY7 37
  127. #define __PTK_FRAMEWORK_GRAPHICS_KEY8 38
  128. #define __PTK_FRAMEWORK_GRAPHICS_KEY9 39
  129. /* Keys 40-49 for Objective-C runtime usage */
  130. #define __PTK_FRAMEWORK_OBJC_KEY0 40
  131. #define __PTK_FRAMEWORK_OBJC_KEY1 41
  132. #define __PTK_FRAMEWORK_OBJC_KEY2 42
  133. #define __PTK_FRAMEWORK_OBJC_KEY3 43
  134. #define __PTK_FRAMEWORK_OBJC_KEY4 44
  135. #define __PTK_FRAMEWORK_OBJC_KEY5 45
  136. #define __PTK_FRAMEWORK_OBJC_KEY6 46
  137. #define __PTK_FRAMEWORK_OBJC_KEY7 47
  138. #define __PTK_FRAMEWORK_OBJC_KEY8 48
  139. #define __PTK_FRAMEWORK_OBJC_KEY9 49
  140. /* Keys 50-59 for Core Foundation usage */
  141. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY0 50
  142. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY1 51
  143. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY2 52
  144. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY3 53
  145. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY4 54
  146. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY5 55
  147. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY6 56
  148. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY7 57
  149. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY8 58
  150. #define __PTK_FRAMEWORK_COREFOUNDATION_KEY9 59
  151. /* Keys 60-69 for Foundation usage */
  152. #define __PTK_FRAMEWORK_FOUNDATION_KEY0 60
  153. #define __PTK_FRAMEWORK_FOUNDATION_KEY1 61
  154. #define __PTK_FRAMEWORK_FOUNDATION_KEY2 62
  155. #define __PTK_FRAMEWORK_FOUNDATION_KEY3 63
  156. #define __PTK_FRAMEWORK_FOUNDATION_KEY4 64
  157. #define __PTK_FRAMEWORK_FOUNDATION_KEY5 65
  158. #define __PTK_FRAMEWORK_FOUNDATION_KEY6 66
  159. #define __PTK_FRAMEWORK_FOUNDATION_KEY7 67
  160. #define __PTK_FRAMEWORK_FOUNDATION_KEY8 68
  161. #define __PTK_FRAMEWORK_FOUNDATION_KEY9 69
  162. /* Keys 70-79 for Core Animation/QuartzCore usage */
  163. #define __PTK_FRAMEWORK_QUARTZCORE_KEY0 70
  164. #define __PTK_FRAMEWORK_QUARTZCORE_KEY1 71
  165. #define __PTK_FRAMEWORK_QUARTZCORE_KEY2 72
  166. #define __PTK_FRAMEWORK_QUARTZCORE_KEY3 73
  167. #define __PTK_FRAMEWORK_QUARTZCORE_KEY4 74
  168. #define __PTK_FRAMEWORK_QUARTZCORE_KEY5 75
  169. #define __PTK_FRAMEWORK_QUARTZCORE_KEY6 76
  170. #define __PTK_FRAMEWORK_QUARTZCORE_KEY7 77
  171. #define __PTK_FRAMEWORK_QUARTZCORE_KEY8 78
  172. #define __PTK_FRAMEWORK_QUARTZCORE_KEY9 79
  173. /* Keys 80-89 for CoreData */
  174. #define __PTK_FRAMEWORK_COREDATA_KEY0 80
  175. #define __PTK_FRAMEWORK_COREDATA_KEY1 81
  176. #define __PTK_FRAMEWORK_COREDATA_KEY2 82
  177. #define __PTK_FRAMEWORK_COREDATA_KEY3 83
  178. #define __PTK_FRAMEWORK_COREDATA_KEY4 84
  179. #define __PTK_FRAMEWORK_COREDATA_KEY5 85
  180. #define __PTK_FRAMEWORK_COREDATA_KEY6 86
  181. #define __PTK_FRAMEWORK_COREDATA_KEY7 87
  182. #define __PTK_FRAMEWORK_COREDATA_KEY8 88
  183. #define __PTK_FRAMEWORK_COREDATA_KEY9 89
  184. /* Keys 90-94 for JavaScriptCore Collection */
  185. #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY0 90
  186. #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY1 91
  187. #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY2 92
  188. #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY3 93
  189. #define __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY4 94
  190. /* Keys 95 for CoreText */
  191. #define __PTK_FRAMEWORK_CORETEXT_KEY0 95
  192. /* Keys 100-109 are for the Swift runtime */
  193. #define __PTK_FRAMEWORK_SWIFT_KEY0 100
  194. #define __PTK_FRAMEWORK_SWIFT_KEY1 101
  195. #define __PTK_FRAMEWORK_SWIFT_KEY2 102
  196. #define __PTK_FRAMEWORK_SWIFT_KEY3 103
  197. #define __PTK_FRAMEWORK_SWIFT_KEY4 104
  198. #define __PTK_FRAMEWORK_SWIFT_KEY5 105
  199. #define __PTK_FRAMEWORK_SWIFT_KEY6 106
  200. #define __PTK_FRAMEWORK_SWIFT_KEY7 107
  201. #define __PTK_FRAMEWORK_SWIFT_KEY8 108
  202. #define __PTK_FRAMEWORK_SWIFT_KEY9 109
  203. /* Keys 190 - 194 are for the use of PerfUtils */
  204. #define __PTK_PERF_UTILS_KEY0 190
  205. #define __PTK_PERF_UTILS_KEY1 191
  206. #define __PTK_PERF_UTILS_KEY2 192
  207. #define __PTK_PERF_UTILS_KEY3 193
  208. #define __PTK_PERF_UTILS_KEY4 194
  209. /* Keys 210 - 229 are for libSystem usage within the iOS Simulator */
  210. /* They are offset from their corresponding libSystem keys by 200 */
  211. #define __PTK_LIBC_SIM_LOCALE_KEY 210
  212. #define __PTK_LIBC_SIM_TTYNAME_KEY 211
  213. #define __PTK_LIBC_SIM_LOCALTIME_KEY 212
  214. #define __PTK_LIBC_SIM_GMTIME_KEY 213
  215. #define __PTK_LIBC_SIM_GDTOA_BIGINT_KEY 214
  216. #define __PTK_LIBC_SIM_PARSEFLOAT_KEY 215
  217. __BEGIN_DECLS
  218. extern void *pthread_getspecific(unsigned long);
  219. extern int pthread_setspecific(unsigned long, const void *);
  220. /* setup destructor function for static key as it is not created with pthread_key_create() */
  221. extern int pthread_key_init_np(int, void (*)(void *));
  222. __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  223. extern int _pthread_setspecific_static(unsigned long, void *);
  224. #if PTHREAD_LAYOUT_SPI
  225. /* SPI intended for CoreSymbolication only */
  226. __API_AVAILABLE(macos(10.10), ios(8.0))
  227. extern const struct pthread_layout_offsets_s {
  228. // always add new fields at the end
  229. const uint16_t plo_version;
  230. // either of the next two fields may be 0; use whichever is set
  231. // bytes from pthread_t to base of tsd
  232. const uint16_t plo_pthread_tsd_base_offset;
  233. // bytes from pthread_t to a pointer to base of tsd
  234. const uint16_t plo_pthread_tsd_base_address_offset;
  235. const uint16_t plo_pthread_tsd_entry_size;
  236. } pthread_layout_offsets;
  237. #endif // PTHREAD_LAYOUT_SPI
  238. __header_always_inline int
  239. _pthread_has_direct_tsd(void)
  240. {
  241. #if TARGET_IPHONE_SIMULATOR
  242. return 0;
  243. #else
  244. return 1;
  245. #endif
  246. }
  247. /* To be used with static constant keys only */
  248. __header_always_inline void *
  249. _pthread_getspecific_direct(unsigned long slot)
  250. {
  251. #if TARGET_IPHONE_SIMULATOR
  252. return pthread_getspecific(slot);
  253. #else
  254. return _os_tsd_get_direct(slot);
  255. #endif
  256. }
  257. /* To be used with static constant keys only, assumes destructor is
  258. * already setup (with pthread_key_init_np) */
  259. __header_always_inline int
  260. _pthread_setspecific_direct(unsigned long slot, void * val)
  261. {
  262. #if TARGET_IPHONE_SIMULATOR
  263. return _pthread_setspecific_static(slot, val);
  264. #else
  265. return _os_tsd_set_direct(slot, val);
  266. #endif
  267. }
  268. __END_DECLS
  269. #endif /* ! __ASSEMBLER__ */
  270. #endif /* __PTHREAD_TSD_H__ */