]> git.saurik.com Git - apple/libpthread.git/blob - private/pthread/private.h
libpthread-454.100.8.tar.gz
[apple/libpthread.git] / private / pthread / private.h
1 /*
2 * Copyright (c) 2012 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 #ifndef __PTHREAD_PRIVATE_H__
25 #define __PTHREAD_PRIVATE_H__
26
27 #include <sys/cdefs.h>
28 #include <Availability.h>
29 #include <pthread/tsd_private.h>
30
31 __API_AVAILABLE(macos(10.9), ios(7.0))
32 pthread_t pthread_main_thread_np(void);
33
34 struct _libpthread_functions {
35 unsigned long version;
36 void (*exit)(int); // added with version=1
37 void *(*malloc)(size_t); // added with version=2
38 void (*free)(void *); // added with version=2
39 };
40
41 /*!
42 * @function pthread_chdir_np
43 *
44 * @abstract
45 * Sets the per-thread current working directory.
46 *
47 * @discussion
48 * This will set the per-thread current working directory to the provided path.
49 * If this is used on a workqueue (dispatch) thread, it MUST be unset with
50 * pthread_fchdir_np(-1) before returning.
51 *
52 * posix_spawn_file_actions_addchdir_np is a better approach if this call would
53 * only be used to spawn a new process with a given working directory.
54 *
55 * @param path
56 * The path of the new working directory.
57 *
58 * @result
59 * 0 upon success, -1 upon error and errno is set.
60 */
61 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
62 int pthread_chdir_np(const char *path);
63
64 /*!
65 * @function pthread_fchdir_np
66 *
67 * @abstract
68 * Sets the per-thread current working directory.
69 *
70 * @discussion
71 * This will set the per-thread current working directory to the provided
72 * directory fd. If this is used on a workqueue (dispatch) thread, it MUST be
73 * unset with pthread_fchdir_np(-1) before returning.
74 *
75 * posix_spawn_file_actions_addfchdir_np is a better approach if this call would
76 * only be used to spawn a new process with a given working directory.
77 *
78 * @param fd
79 * A file descriptor to the new working directory. Pass -1 to unset the
80 * per-thread working directory.
81 *
82 * @result
83 * 0 upon success, -1 upon error and errno is set.
84 */
85 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
86 int pthread_fchdir_np(int fd);
87
88 __API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
89 int pthread_attr_setcpupercent_np(pthread_attr_t * __restrict, int, unsigned long);
90
91 __API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0))
92 int pthread_current_stack_contains_np(const void *, size_t);
93
94 /*!
95 * @function pthread_self_is_exiting_np
96 *
97 * @abstract
98 * Returns whether the current thread is exiting.
99 *
100 * @discussion
101 * This can be useful for certain introspection tools to know that malloc/free
102 * is called from the TSD destruction codepath.
103 *
104 * @result
105 * 0 if the thread is not exiting
106 * 1 if the thread is exiting
107 */
108 __API_AVAILABLE(macos(10.16), ios(14.0), tvos(14.0), watchos(7.0))
109 int pthread_self_is_exiting_np(void);
110
111 #ifdef __LP64__
112 #define _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET -8
113 #define _PTHREAD_STRUCT_DIRECT_TSD_OFFSET 224
114 #else
115 #define _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET -16
116 #define _PTHREAD_STRUCT_DIRECT_TSD_OFFSET 176
117 #endif
118
119 #if !TARGET_OS_SIMULATOR
120 #if defined(__i386__)
121 #define _pthread_direct_tsd_relative_access(type, offset) \
122 (type *)((char *)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF) + \
123 _PTHREAD_STRUCT_DIRECT_TSD_OFFSET + _PTHREAD_STRUCT_DIRECT_##offset##_OFFSET)
124 #elif defined(OS_GS_RELATIVE)
125 #define _pthread_direct_tsd_relative_access(type, offset) \
126 (type OS_GS_RELATIVE *)(_PTHREAD_STRUCT_DIRECT_##offset##_OFFSET)
127 #elif defined(_os_tsd_get_base)
128 #define _pthread_direct_tsd_relative_access(type, offset) \
129 (type *)((char *)_os_tsd_get_base() + _PTHREAD_STRUCT_DIRECT_##offset##_OFFSET)
130 #else
131 #error "unknown configuration"
132 #endif
133 #endif // !TARGET_OS_SIMULATOR
134
135 /* N.B. DO NOT USE UNLESS YOU ARE REBUILT AS PART OF AN OS TRAIN WORLDBUILD */
136 __header_always_inline __pure2 uint64_t
137 _pthread_threadid_self_np_direct(void)
138 {
139 #ifdef _pthread_direct_tsd_relative_access
140 return *_pthread_direct_tsd_relative_access(uint64_t, THREADID);
141 #else
142 uint64_t threadid = 0;
143 pthread_threadid_np(NULL, &threadid);
144 return threadid;
145 #endif
146 }
147
148 __header_always_inline __pure2 pthread_t
149 _pthread_self_direct(void)
150 {
151 #if TARGET_OS_SIMULATOR || defined(__i386__) || defined(__x86_64__)
152 return (pthread_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF);
153 #elif defined(__arm__) || defined(__arm64__)
154 uintptr_t tsd_base = (uintptr_t)_os_tsd_get_base();
155 return (pthread_t)(tsd_base - _PTHREAD_STRUCT_DIRECT_TSD_OFFSET);
156 #else
157 #error unsupported architecture
158 #endif
159 }
160
161 __header_always_inline __pure2 mach_port_t
162 _pthread_mach_thread_self_direct(void)
163 {
164 return (mach_port_t)(uintptr_t)
165 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF);
166 }
167
168 __header_always_inline int *
169 _pthread_errno_address_direct(void)
170 {
171 return (int *)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_ERRNO);
172 }
173
174 /* get the thread specific errno value */
175 __header_always_inline int
176 _pthread_get_errno_direct(void)
177 {
178 return *_pthread_errno_address_direct();
179 }
180
181 /* set the thread specific errno value */
182 __header_always_inline void
183 _pthread_set_errno_direct(int value)
184 {
185 *_pthread_errno_address_direct() = value;
186 }
187
188 #endif // __PTHREAD_PRIVATE_H__