2 * Copyright (c) 2012 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 #ifndef __PTHREAD_PRIVATE_H__
25 #define __PTHREAD_PRIVATE_H__
27 #include <sys/cdefs.h>
28 #include <Availability.h>
29 #include <pthread/tsd_private.h>
31 __API_AVAILABLE(macos(10.9), ios(7.0))
32 pthread_t
pthread_main_thread_np(void);
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
42 * @function pthread_chdir_np
45 * Sets the per-thread current working directory.
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.
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.
56 * The path of the new working directory.
59 * 0 upon success, -1 upon error and errno is set.
61 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
62 int pthread_chdir_np(const char *path
);
65 * @function pthread_fchdir_np
68 * Sets the per-thread current working directory.
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.
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.
79 * A file descriptor to the new working directory. Pass -1 to unset the
80 * per-thread working directory.
83 * 0 upon success, -1 upon error and errno is set.
85 __API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
86 int pthread_fchdir_np(int fd
);
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);
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);
95 * @function pthread_self_is_exiting_np
98 * Returns whether the current thread is exiting.
101 * This can be useful for certain introspection tools to know that malloc/free
102 * is called from the TSD destruction codepath.
105 * 0 if the thread is not exiting
106 * 1 if the thread is exiting
108 __API_AVAILABLE(macos(10.16), ios(14.0), tvos(14.0), watchos(7.0))
109 int pthread_self_is_exiting_np(void);
112 #define _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET -8
113 #define _PTHREAD_STRUCT_DIRECT_TSD_OFFSET 224
115 #define _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET -16
116 #define _PTHREAD_STRUCT_DIRECT_TSD_OFFSET 176
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)
131 #error "unknown configuration"
133 #endif // !TARGET_OS_SIMULATOR
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)
139 #ifdef _pthread_direct_tsd_relative_access
140 return *_pthread_direct_tsd_relative_access(uint64_t, THREADID
);
142 uint64_t threadid
= 0;
143 pthread_threadid_np(NULL
, &threadid
);
148 __header_always_inline __pure2 pthread_t
149 _pthread_self_direct(void)
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
);
157 #error unsupported architecture
161 __header_always_inline __pure2 mach_port_t
162 _pthread_mach_thread_self_direct(void)
164 return (mach_port_t
)(uintptr_t)
165 _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
);
168 __header_always_inline
int *
169 _pthread_errno_address_direct(void)
171 return (int *)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_ERRNO
);
174 /* get the thread specific errno value */
175 __header_always_inline
int
176 _pthread_get_errno_direct(void)
178 return *_pthread_errno_address_direct();
181 /* set the thread specific errno value */
182 __header_always_inline
void
183 _pthread_set_errno_direct(int value
)
185 *_pthread_errno_address_direct() = value
;
188 #endif // __PTHREAD_PRIVATE_H__