]> git.saurik.com Git - apple/libpthread.git/blob - src/pthread.c
3c3ea6a02645a2a1d6ad3e3ab27d0fbc640f69fa
[apple/libpthread.git] / src / pthread.c
1 /*
2 * Copyright (c) 2000-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 /*
45 * MkLinux
46 */
47
48 /*
49 * POSIX Pthread Library
50 */
51
52 #include "resolver.h"
53 #include "internal.h"
54 #include "private.h"
55 #include "workqueue_private.h"
56 #include "introspection_private.h"
57 #include "qos_private.h"
58 #include "tsd_private.h"
59 #include "pthread/stack_np.h"
60 #include "offsets.h" // included to validate the offsets at build time
61
62 #include <stdlib.h>
63 #include <errno.h>
64 #include <signal.h>
65 #include <unistd.h>
66 #include <mach/mach_init.h>
67 #include <mach/mach_vm.h>
68 #include <mach/mach_sync_ipc.h>
69 #include <sys/time.h>
70 #include <sys/resource.h>
71 #include <sys/sysctl.h>
72 #include <sys/queue.h>
73 #include <sys/ulock.h>
74 #include <sys/mman.h>
75 #include <machine/vmparam.h>
76 #define __APPLE_API_PRIVATE
77 #include <machine/cpu_capabilities.h>
78
79 #include <_simple.h>
80 #include <platform/string.h>
81 #include <platform/compat.h>
82
83 extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
84 void *newp, size_t newlen);
85 extern void __exit(int) __attribute__((noreturn));
86 extern int __pthread_kill(mach_port_t, int);
87
88 extern void _pthread_joiner_wake(pthread_t thread);
89
90 #if !VARIANT_DYLD
91 PTHREAD_NOEXPORT extern struct _pthread *_main_thread_ptr;
92 #define main_thread() (_main_thread_ptr)
93 #endif // VARIANT_DYLD
94
95 // Default stack size is 512KB; independent of the main thread's stack size.
96 #define DEFAULT_STACK_SIZE (size_t)(512 * 1024)
97
98
99 //
100 // Global constants
101 //
102
103 /*
104 * The pthread may be offset into a page. In that event, by contract
105 * with the kernel, the allocation will extend PTHREAD_SIZE from the
106 * start of the next page. There's also one page worth of allocation
107 * below stacksize for the guard page. <rdar://problem/19941744>
108 */
109 #define PTHREAD_SIZE ((size_t)mach_vm_round_page(sizeof(struct _pthread)))
110 #define PTHREAD_ALLOCADDR(stackaddr, stacksize) ((stackaddr - stacksize) - vm_page_size)
111 #define PTHREAD_ALLOCSIZE(stackaddr, stacksize) ((round_page((uintptr_t)stackaddr) + PTHREAD_SIZE) - (uintptr_t)PTHREAD_ALLOCADDR(stackaddr, stacksize))
112
113 static const pthread_attr_t _pthread_attr_default = {
114 .sig = _PTHREAD_ATTR_SIG,
115 .stacksize = 0,
116 .detached = PTHREAD_CREATE_JOINABLE,
117 .inherit = _PTHREAD_DEFAULT_INHERITSCHED,
118 .policy = _PTHREAD_DEFAULT_POLICY,
119 .defaultguardpage = true,
120 // compile time constant for _pthread_default_priority(0)
121 .qosclass = (1U << (THREAD_QOS_LEGACY - 1 + _PTHREAD_PRIORITY_QOS_CLASS_SHIFT)) |
122 ((uint8_t)-1 & _PTHREAD_PRIORITY_PRIORITY_MASK),
123 };
124
125 #if PTHREAD_LAYOUT_SPI
126
127 const struct pthread_layout_offsets_s pthread_layout_offsets = {
128 .plo_version = 1,
129 .plo_pthread_tsd_base_offset = offsetof(struct _pthread, tsd),
130 .plo_pthread_tsd_base_address_offset = 0,
131 .plo_pthread_tsd_entry_size = sizeof(((struct _pthread *)NULL)->tsd[0]),
132 };
133
134 #endif // PTHREAD_LAYOUT_SPI
135
136 //
137 // Global exported variables
138 //
139
140 // This global should be used (carefully) by anyone needing to know if a
141 // pthread (other than the main thread) has been created.
142 int __is_threaded = 0;
143 int __unix_conforming = 0;
144
145 //
146 // Global internal variables
147 //
148
149 // _pthread_list_lock protects _pthread_count, access to the __pthread_head
150 // list. Externally imported by pthread_cancelable.c.
151 struct __pthread_list __pthread_head = TAILQ_HEAD_INITIALIZER(__pthread_head);
152 _pthread_lock _pthread_list_lock = _PTHREAD_LOCK_INITIALIZER;
153
154 uint32_t _main_qos;
155
156 #if VARIANT_DYLD
157 // The main thread's pthread_t
158 struct _pthread _main_thread __attribute__((aligned(64))) = { };
159 #define main_thread() (&_main_thread)
160 #else // VARIANT_DYLD
161 struct _pthread *_main_thread_ptr;
162 #endif // VARIANT_DYLD
163
164 #if PTHREAD_DEBUG_LOG
165 #include <fcntl.h>
166 int _pthread_debuglog;
167 uint64_t _pthread_debugstart;
168 #endif
169
170 //
171 // Global static variables
172 //
173 static bool __workq_newapi;
174 static uint8_t default_priority;
175 #if !VARIANT_DYLD
176 static uint8_t max_priority;
177 static uint8_t min_priority;
178 #endif // !VARIANT_DYLD
179 static int _pthread_count = 1;
180 static int pthread_concurrency;
181 static uintptr_t _pthread_ptr_munge_token;
182
183 static void (*exitf)(int) = __exit;
184 #if !VARIANT_DYLD
185 static void *(*_pthread_malloc)(size_t) = NULL;
186 static void (*_pthread_free)(void *) = NULL;
187 #endif // !VARIANT_DYLD
188
189 // work queue support data
190 PTHREAD_NORETURN
191 static void
192 __pthread_invalid_keventfunction(void **events, int *nevents)
193 {
194 PTHREAD_CLIENT_CRASH(0, "Invalid kqworkq setup");
195 }
196
197 PTHREAD_NORETURN
198 static void
199 __pthread_invalid_workloopfunction(uint64_t *workloop_id, void **events, int *nevents)
200 {
201 PTHREAD_CLIENT_CRASH(0, "Invalid kqwl setup");
202 }
203 static pthread_workqueue_function2_t __libdispatch_workerfunction;
204 static pthread_workqueue_function_kevent_t __libdispatch_keventfunction = &__pthread_invalid_keventfunction;
205 static pthread_workqueue_function_workloop_t __libdispatch_workloopfunction = &__pthread_invalid_workloopfunction;
206 static int __libdispatch_offset;
207 static int __pthread_supported_features; // supported feature set
208
209 #if defined(__i386__) || defined(__x86_64__)
210 static mach_vm_address_t __pthread_stack_hint = 0xB0000000;
211 #else
212 #error no __pthread_stack_hint for this architecture
213 #endif
214
215 //
216 // Function prototypes
217 //
218
219 // pthread primitives
220 static inline void _pthread_struct_init(pthread_t t, const pthread_attr_t *attrs,
221 void *stack, size_t stacksize, void *freeaddr, size_t freesize);
222
223 #if VARIANT_DYLD
224 static void _pthread_set_self_dyld(void);
225 #endif // VARIANT_DYLD
226 static inline void _pthread_set_self_internal(pthread_t, bool needs_tsd_base_set);
227
228 static void _pthread_dealloc_reply_port(pthread_t t);
229 static void _pthread_dealloc_special_reply_port(pthread_t t);
230
231 static inline void __pthread_started_thread(pthread_t t);
232
233 static void _pthread_exit(pthread_t self, void *value_ptr) __dead2;
234
235 static inline void _pthread_introspection_thread_create(pthread_t t);
236 static inline void _pthread_introspection_thread_start(pthread_t t);
237 static inline void _pthread_introspection_thread_terminate(pthread_t t);
238 static inline void _pthread_introspection_thread_destroy(pthread_t t);
239
240 extern void _pthread_set_self(pthread_t);
241 extern void start_wqthread(pthread_t self, mach_port_t kport, void *stackaddr, void *unused, int reuse); // trampoline into _pthread_wqthread
242 extern void thread_start(pthread_t self, mach_port_t kport, void *(*fun)(void *), void * funarg, size_t stacksize, unsigned int flags); // trampoline into _pthread_start
243
244 /*
245 * Flags filed passed to bsdthread_create and back in pthread_start
246 * 31 <---------------------------------> 0
247 * _________________________________________
248 * | flags(8) | policy(8) | importance(16) |
249 * -----------------------------------------
250 */
251 #define PTHREAD_START_CUSTOM 0x01000000 // <rdar://problem/34501401>
252 #define PTHREAD_START_SETSCHED 0x02000000
253 // was PTHREAD_START_DETACHED 0x04000000
254 #define PTHREAD_START_QOSCLASS 0x08000000
255 #define PTHREAD_START_TSD_BASE_SET 0x10000000
256 #define PTHREAD_START_SUSPENDED 0x20000000
257 #define PTHREAD_START_QOSCLASS_MASK 0x00ffffff
258 #define PTHREAD_START_POLICY_BITSHIFT 16
259 #define PTHREAD_START_POLICY_MASK 0xff
260 #define PTHREAD_START_IMPORTANCE_MASK 0xffff
261
262 #if (!defined(__OPEN_SOURCE__) && TARGET_OS_OSX) || OS_VARIANT_RESOLVED // 40703288
263 static int pthread_setschedparam_internal(pthread_t, mach_port_t, int,
264 const struct sched_param *);
265 #endif
266
267 extern pthread_t __bsdthread_create(void *(*func)(void *), void * func_arg, void * stack, pthread_t thread, unsigned int flags);
268 extern int __bsdthread_register(void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), void (*)(pthread_t, mach_port_t, void *, void *, int), int,void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), int32_t *,__uint64_t);
269 extern int __bsdthread_terminate(void * freeaddr, size_t freesize, mach_port_t kport, mach_port_t joinsem);
270 extern __uint64_t __thread_selfid( void );
271
272 #if __LP64__
273 _Static_assert(offsetof(struct _pthread, tsd) == 224, "TSD LP64 offset");
274 #else
275 _Static_assert(offsetof(struct _pthread, tsd) == 176, "TSD ILP32 offset");
276 #endif
277 _Static_assert(offsetof(struct _pthread, tsd) + _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET
278 == offsetof(struct _pthread, thread_id),
279 "_PTHREAD_STRUCT_DIRECT_THREADID_OFFSET is correct");
280
281 #pragma mark pthread attrs
282
283 _Static_assert(sizeof(struct _pthread_attr_t) == sizeof(__darwin_pthread_attr_t),
284 "internal pthread_attr_t == external pthread_attr_t");
285
286 int
287 pthread_attr_destroy(pthread_attr_t *attr)
288 {
289 int ret = EINVAL;
290 if (attr->sig == _PTHREAD_ATTR_SIG) {
291 attr->sig = 0;
292 ret = 0;
293 }
294 return ret;
295 }
296
297 int
298 pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
299 {
300 int ret = EINVAL;
301 if (attr->sig == _PTHREAD_ATTR_SIG) {
302 *detachstate = attr->detached;
303 ret = 0;
304 }
305 return ret;
306 }
307
308 int
309 pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
310 {
311 int ret = EINVAL;
312 if (attr->sig == _PTHREAD_ATTR_SIG) {
313 *inheritsched = attr->inherit;
314 ret = 0;
315 }
316 return ret;
317 }
318
319 static PTHREAD_ALWAYS_INLINE void
320 _pthread_attr_get_schedparam(const pthread_attr_t *attr,
321 struct sched_param *param)
322 {
323 if (attr->schedset) {
324 *param = attr->param;
325 } else {
326 param->sched_priority = default_priority;
327 param->quantum = 10; /* quantum isn't public yet */
328 }
329 }
330
331 int
332 pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
333 {
334 int ret = EINVAL;
335 if (attr->sig == _PTHREAD_ATTR_SIG) {
336 _pthread_attr_get_schedparam(attr, param);
337 ret = 0;
338 }
339 return ret;
340 }
341
342 int
343 pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
344 {
345 int ret = EINVAL;
346 if (attr->sig == _PTHREAD_ATTR_SIG) {
347 *policy = attr->policy;
348 ret = 0;
349 }
350 return ret;
351 }
352
353 int
354 pthread_attr_init(pthread_attr_t *attr)
355 {
356 *attr = _pthread_attr_default;
357 return 0;
358 }
359
360 int
361 pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
362 {
363 int ret = EINVAL;
364 if (attr->sig == _PTHREAD_ATTR_SIG &&
365 (detachstate == PTHREAD_CREATE_JOINABLE ||
366 detachstate == PTHREAD_CREATE_DETACHED)) {
367 attr->detached = detachstate;
368 ret = 0;
369 }
370 return ret;
371 }
372
373 int
374 pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
375 {
376 int ret = EINVAL;
377 if (attr->sig == _PTHREAD_ATTR_SIG &&
378 (inheritsched == PTHREAD_INHERIT_SCHED ||
379 inheritsched == PTHREAD_EXPLICIT_SCHED)) {
380 attr->inherit = inheritsched;
381 ret = 0;
382 }
383 return ret;
384 }
385
386 int
387 pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
388 {
389 int ret = EINVAL;
390 if (attr->sig == _PTHREAD_ATTR_SIG) {
391 /* TODO: Validate sched_param fields */
392 attr->param = *param;
393 attr->schedset = 1;
394 ret = 0;
395 }
396 return ret;
397 }
398
399 int
400 pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
401 {
402 int ret = EINVAL;
403 if (attr->sig == _PTHREAD_ATTR_SIG && (policy == SCHED_OTHER ||
404 policy == SCHED_RR || policy == SCHED_FIFO)) {
405 if (!_PTHREAD_POLICY_IS_FIXEDPRI(policy)) {
406 /* non-fixedpri policy should remove cpupercent */
407 attr->cpupercentset = 0;
408 }
409 attr->policy = policy;
410 attr->policyset = 1;
411 ret = 0;
412 }
413 return ret;
414 }
415
416 int
417 pthread_attr_setscope(pthread_attr_t *attr, int scope)
418 {
419 int ret = EINVAL;
420 if (attr->sig == _PTHREAD_ATTR_SIG) {
421 if (scope == PTHREAD_SCOPE_SYSTEM) {
422 // No attribute yet for the scope.
423 ret = 0;
424 } else if (scope == PTHREAD_SCOPE_PROCESS) {
425 ret = ENOTSUP;
426 }
427 }
428 return ret;
429 }
430
431 int
432 pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
433 {
434 int ret = EINVAL;
435 if (attr->sig == _PTHREAD_ATTR_SIG) {
436 *scope = PTHREAD_SCOPE_SYSTEM;
437 ret = 0;
438 }
439 return ret;
440 }
441
442 int
443 pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
444 {
445 int ret = EINVAL;
446 if (attr->sig == _PTHREAD_ATTR_SIG) {
447 *stackaddr = attr->stackaddr;
448 ret = 0;
449 }
450 return ret;
451 }
452
453 int
454 pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
455 {
456 int ret = EINVAL;
457 if (attr->sig == _PTHREAD_ATTR_SIG &&
458 ((uintptr_t)stackaddr % vm_page_size) == 0) {
459 attr->stackaddr = stackaddr;
460 attr->defaultguardpage = false;
461 attr->guardsize = 0;
462 ret = 0;
463 }
464 return ret;
465 }
466
467 static inline size_t
468 _pthread_attr_stacksize(const pthread_attr_t *attr)
469 {
470 return attr->stacksize ? attr->stacksize : DEFAULT_STACK_SIZE;
471 }
472
473 int
474 pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
475 {
476 int ret = EINVAL;
477 if (attr->sig == _PTHREAD_ATTR_SIG) {
478 *stacksize = _pthread_attr_stacksize(attr);
479 ret = 0;
480 }
481 return ret;
482 }
483
484 int
485 pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
486 {
487 int ret = EINVAL;
488 if (attr->sig == _PTHREAD_ATTR_SIG &&
489 (stacksize % vm_page_size) == 0 &&
490 stacksize >= PTHREAD_STACK_MIN) {
491 attr->stacksize = stacksize;
492 ret = 0;
493 }
494 return ret;
495 }
496
497 int
498 pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t * stacksize)
499 {
500 int ret = EINVAL;
501 if (attr->sig == _PTHREAD_ATTR_SIG) {
502 *stackaddr = (void *)((uintptr_t)attr->stackaddr - attr->stacksize);
503 *stacksize = _pthread_attr_stacksize(attr);
504 ret = 0;
505 }
506 return ret;
507 }
508
509 // Per SUSv3, the stackaddr is the base address, the lowest addressable byte
510 // address. This is not the same as in pthread_attr_setstackaddr.
511 int
512 pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
513 {
514 int ret = EINVAL;
515 if (attr->sig == _PTHREAD_ATTR_SIG &&
516 ((uintptr_t)stackaddr % vm_page_size) == 0 &&
517 (stacksize % vm_page_size) == 0 &&
518 stacksize >= PTHREAD_STACK_MIN) {
519 attr->stackaddr = (void *)((uintptr_t)stackaddr + stacksize);
520 attr->stacksize = stacksize;
521 ret = 0;
522 }
523 return ret;
524 }
525
526 int
527 pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
528 {
529 int ret = EINVAL;
530 if (attr->sig == _PTHREAD_ATTR_SIG && (guardsize % vm_page_size) == 0) {
531 /* Guardsize of 0 is valid, means no guard */
532 attr->defaultguardpage = false;
533 attr->guardsize = guardsize;
534 ret = 0;
535 }
536 return ret;
537 }
538
539 static inline size_t
540 _pthread_attr_guardsize(const pthread_attr_t *attr)
541 {
542 return attr->defaultguardpage ? vm_page_size : attr->guardsize;
543 }
544
545 int
546 pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
547 {
548 int ret = EINVAL;
549 if (attr->sig == _PTHREAD_ATTR_SIG) {
550 *guardsize = _pthread_attr_guardsize(attr);
551 ret = 0;
552 }
553 return ret;
554 }
555
556 int
557 pthread_attr_setcpupercent_np(pthread_attr_t *attr, int percent,
558 unsigned long refillms)
559 {
560 int ret = EINVAL;
561 if (attr->sig == _PTHREAD_ATTR_SIG && percent < UINT8_MAX &&
562 refillms < _PTHREAD_ATTR_REFILLMS_MAX && attr->policyset &&
563 _PTHREAD_POLICY_IS_FIXEDPRI(attr->policy)) {
564 attr->cpupercent = percent;
565 attr->refillms = (uint32_t)(refillms & 0x00ffffff);
566 attr->cpupercentset = 1;
567 ret = 0;
568 }
569 return ret;
570 }
571
572 #pragma mark pthread lifetime
573
574 // Allocate a thread structure, stack and guard page.
575 //
576 // The thread structure may optionally be placed in the same allocation as the
577 // stack, residing above the top of the stack. This cannot be done if a
578 // custom stack address is provided.
579 //
580 // Similarly the guard page cannot be allocated if a custom stack address is
581 // provided.
582 //
583 // The allocated thread structure is initialized with values that indicate how
584 // it should be freed.
585
586 static pthread_t
587 _pthread_allocate(const pthread_attr_t *attrs, void **stack)
588 {
589 mach_vm_address_t allocaddr = __pthread_stack_hint;
590 size_t allocsize, guardsize, stacksize, pthreadoff;
591 kern_return_t kr;
592 pthread_t t;
593
594 PTHREAD_ASSERT(attrs->stacksize == 0 ||
595 attrs->stacksize >= PTHREAD_STACK_MIN);
596
597 // Allocate a pthread structure if necessary
598
599 if (attrs->stackaddr != NULL) {
600 PTHREAD_ASSERT(((uintptr_t)attrs->stackaddr % vm_page_size) == 0);
601 allocsize = PTHREAD_SIZE;
602 guardsize = 0;
603 pthreadoff = 0;
604 // <rdar://problem/42588315> if the attrs struct specifies a custom
605 // stack address but not a custom size, using ->stacksize here instead
606 // of _pthread_attr_stacksize stores stacksize as zero, indicating
607 // that the stack size is unknown.
608 stacksize = attrs->stacksize;
609 } else {
610 guardsize = _pthread_attr_guardsize(attrs);
611 stacksize = _pthread_attr_stacksize(attrs) + PTHREAD_T_OFFSET;
612 pthreadoff = stacksize + guardsize;
613 allocsize = pthreadoff + PTHREAD_SIZE;
614 allocsize = mach_vm_round_page(allocsize);
615 }
616
617 kr = mach_vm_map(mach_task_self(), &allocaddr, allocsize, vm_page_size - 1,
618 VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE, MEMORY_OBJECT_NULL,
619 0, FALSE, VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
620
621 if (kr != KERN_SUCCESS) {
622 kr = mach_vm_allocate(mach_task_self(), &allocaddr, allocsize,
623 VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE);
624 }
625 if (kr != KERN_SUCCESS) {
626 *stack = NULL;
627 return NULL;
628 }
629
630 // The stack grows down.
631 // Set the guard page at the lowest address of the
632 // newly allocated stack. Return the highest address
633 // of the stack.
634 if (guardsize) {
635 (void)mach_vm_protect(mach_task_self(), allocaddr, guardsize,
636 FALSE, VM_PROT_NONE);
637 }
638
639 // Thread structure resides at the top of the stack (when using a
640 // custom stack, allocsize == PTHREAD_SIZE, so places the pthread_t
641 // at allocaddr).
642 t = (pthread_t)(allocaddr + pthreadoff);
643 if (attrs->stackaddr) {
644 *stack = attrs->stackaddr;
645 } else {
646 *stack = t;
647 }
648
649 _pthread_struct_init(t, attrs, *stack, stacksize, allocaddr, allocsize);
650 return t;
651 }
652
653 PTHREAD_NOINLINE
654 void
655 _pthread_deallocate(pthread_t t, bool from_mach_thread)
656 {
657 kern_return_t ret;
658
659 // Don't free the main thread.
660 if (t != main_thread()) {
661 if (!from_mach_thread) { // see __pthread_add_thread
662 _pthread_introspection_thread_destroy(t);
663 }
664 ret = mach_vm_deallocate(mach_task_self(), t->freeaddr, t->freesize);
665 PTHREAD_ASSERT(ret == KERN_SUCCESS);
666 }
667 }
668
669 #pragma clang diagnostic push
670 #pragma clang diagnostic ignored "-Wreturn-stack-address"
671
672 PTHREAD_NOINLINE
673 static void*
674 _pthread_current_stack_address(void)
675 {
676 int a;
677 return &a;
678 }
679
680 #pragma clang diagnostic pop
681
682 void
683 _pthread_joiner_wake(pthread_t thread)
684 {
685 uint32_t *exit_gate = &thread->tl_exit_gate;
686
687 for (;;) {
688 int ret = __ulock_wake(UL_UNFAIR_LOCK | ULF_NO_ERRNO, exit_gate, 0);
689 if (ret == 0 || ret == -ENOENT) {
690 return;
691 }
692 if (ret != -EINTR) {
693 PTHREAD_INTERNAL_CRASH(-ret, "pthread_join() wake failure");
694 }
695 }
696 }
697
698 // Terminates the thread if called from the currently running thread.
699 PTHREAD_NORETURN PTHREAD_NOINLINE PTHREAD_NOT_TAIL_CALLED
700 static void
701 _pthread_terminate(pthread_t t, void *exit_value)
702 {
703 PTHREAD_ASSERT(t == pthread_self());
704
705 _pthread_introspection_thread_terminate(t);
706
707 uintptr_t freeaddr = (uintptr_t)t->freeaddr;
708 size_t freesize = t->freesize;
709 bool should_exit;
710
711 // the size of just the stack
712 size_t freesize_stack = t->freesize;
713
714 // We usually pass our structure+stack to bsdthread_terminate to free, but
715 // if we get told to keep the pthread_t structure around then we need to
716 // adjust the free size and addr in the pthread_t to just refer to the
717 // structure and not the stack. If we do end up deallocating the
718 // structure, this is useless work since no one can read the result, but we
719 // can't do it after the call to pthread_remove_thread because it isn't
720 // safe to dereference t after that.
721 if ((void*)t > t->freeaddr && (void*)t < t->freeaddr + t->freesize){
722 // Check to ensure the pthread structure itself is part of the
723 // allocation described by freeaddr/freesize, in which case we split and
724 // only deallocate the area below the pthread structure. In the event of a
725 // custom stack, the freeaddr/size will be the pthread structure itself, in
726 // which case we shouldn't free anything (the final else case).
727 freesize_stack = trunc_page((uintptr_t)t - (uintptr_t)freeaddr);
728
729 // describe just the remainder for deallocation when the pthread_t goes away
730 t->freeaddr += freesize_stack;
731 t->freesize -= freesize_stack;
732 } else if (t == main_thread()) {
733 freeaddr = t->stackaddr - pthread_get_stacksize_np(t);
734 uintptr_t stackborder = trunc_page((uintptr_t)_pthread_current_stack_address());
735 freesize_stack = stackborder - freeaddr;
736 } else {
737 freesize_stack = 0;
738 }
739
740 mach_port_t kport = _pthread_kernel_thread(t);
741 bool keep_thread_struct = false, needs_wake = false;
742 semaphore_t custom_stack_sema = MACH_PORT_NULL;
743
744 _pthread_dealloc_special_reply_port(t);
745 _pthread_dealloc_reply_port(t);
746
747 _PTHREAD_LOCK(_pthread_list_lock);
748
749 // This piece of code interacts with pthread_join. It will always:
750 // - set tl_exit_gate to MACH_PORT_DEAD (thread exited)
751 // - set tl_exit_value to the value passed to pthread_exit()
752 // - decrement _pthread_count, so that we can exit the process when all
753 // threads exited even if not all of them were joined.
754 t->tl_exit_gate = MACH_PORT_DEAD;
755 t->tl_exit_value = exit_value;
756 should_exit = (--_pthread_count <= 0);
757
758 // If we see a joiner, we prepost that the join has to succeed,
759 // and the joiner is committed to finish (even if it was canceled)
760 if (t->tl_join_ctx) {
761 custom_stack_sema = _pthread_joiner_prepost_wake(t); // unsets tl_joinable
762 needs_wake = true;
763 }
764
765 // Joinable threads that have no joiner yet are kept on the thread list
766 // so that pthread_join() can later discover the thread when it is joined,
767 // and will have to do the pthread_t cleanup.
768 if (t->tl_joinable) {
769 t->tl_joiner_cleans_up = keep_thread_struct = true;
770 } else {
771 TAILQ_REMOVE(&__pthread_head, t, tl_plist);
772 }
773
774 _PTHREAD_UNLOCK(_pthread_list_lock);
775
776 if (needs_wake) {
777 // When we found a waiter, we want to drop the very contended list lock
778 // before we do the syscall in _pthread_joiner_wake(). Then, we decide
779 // who gets to cleanup the pthread_t between the joiner and the exiting
780 // thread:
781 // - the joiner tries to set tl_join_ctx to NULL
782 // - the exiting thread tries to set tl_joiner_cleans_up to true
783 // Whoever does it first commits the other guy to cleanup the pthread_t
784 _pthread_joiner_wake(t);
785 _PTHREAD_LOCK(_pthread_list_lock);
786 if (t->tl_join_ctx) {
787 t->tl_joiner_cleans_up = true;
788 keep_thread_struct = true;
789 }
790 _PTHREAD_UNLOCK(_pthread_list_lock);
791 }
792
793 //
794 // /!\ dereferencing `t` past this point is not safe /!\
795 //
796
797 if (keep_thread_struct || t == main_thread()) {
798 // Use the adjusted freesize of just the stack that we computed above.
799 freesize = freesize_stack;
800 } else {
801 _pthread_introspection_thread_destroy(t);
802 }
803
804 // Check if there is nothing to free because the thread has a custom
805 // stack allocation and is joinable.
806 if (freesize == 0) {
807 freeaddr = 0;
808 }
809 if (should_exit) {
810 exitf(0);
811 }
812 __bsdthread_terminate((void *)freeaddr, freesize, kport, custom_stack_sema);
813 PTHREAD_INTERNAL_CRASH(t, "thread didn't terminate");
814 }
815
816 PTHREAD_NORETURN
817 static void
818 _pthread_terminate_invoke(pthread_t t, void *exit_value)
819 {
820 #if PTHREAD_T_OFFSET
821 void *p = NULL;
822 // <rdar://problem/25688492> During pthread termination there is a race
823 // between pthread_join and pthread_terminate; if the joiner is responsible
824 // for cleaning up the pthread_t struct, then it may destroy some part of the
825 // stack with it on 16k OSes. So that this doesn't cause _pthread_terminate()
826 // to crash because its stack has been removed from under its feet, just make
827 // sure termination happens in a part of the stack that is not on the same
828 // page as the pthread_t.
829 if (trunc_page((uintptr_t)__builtin_frame_address(0)) ==
830 trunc_page((uintptr_t)t)) {
831 p = alloca(PTHREAD_T_OFFSET);
832 }
833 // And this __asm__ volatile is needed to stop the compiler from optimising
834 // away the alloca() completely.
835 __asm__ volatile ("" : : "r"(p) );
836 #endif
837 _pthread_terminate(t, exit_value);
838 }
839
840 #pragma mark pthread start / body
841
842 /*
843 * Create and start execution of a new thread.
844 */
845 PTHREAD_NOINLINE PTHREAD_NORETURN
846 static void
847 _pthread_body(pthread_t self, bool needs_tsd_base_set)
848 {
849 _pthread_set_self_internal(self, needs_tsd_base_set);
850 __pthread_started_thread(self);
851 _pthread_exit(self, (self->fun)(self->arg));
852 }
853
854 PTHREAD_NORETURN
855 void
856 _pthread_start(pthread_t self, mach_port_t kport,
857 __unused void *(*fun)(void *), __unused void *arg,
858 __unused size_t stacksize, unsigned int pflags)
859 {
860 bool thread_tsd_bsd_set = (bool)(pflags & PTHREAD_START_TSD_BASE_SET);
861
862 if (os_unlikely(pflags & PTHREAD_START_SUSPENDED)) {
863 PTHREAD_INTERNAL_CRASH(0,
864 "kernel without PTHREAD_START_SUSPENDED support");
865 }
866 #if DEBUG
867 PTHREAD_ASSERT(MACH_PORT_VALID(kport));
868 PTHREAD_ASSERT(_pthread_kernel_thread(self) == kport);
869 #endif
870 // will mark the thread initialized
871 _pthread_markcancel_if_canceled(self, kport);
872
873 _pthread_body(self, !thread_tsd_bsd_set);
874 }
875
876 PTHREAD_ALWAYS_INLINE
877 static inline void
878 _pthread_struct_init(pthread_t t, const pthread_attr_t *attrs,
879 void *stackaddr, size_t stacksize, void *freeaddr, size_t freesize)
880 {
881 #if DEBUG
882 PTHREAD_ASSERT(t->sig != _PTHREAD_SIG);
883 #endif
884
885 t->sig = _PTHREAD_SIG;
886 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_SELF] = t;
887 t->tsd[_PTHREAD_TSD_SLOT_ERRNO] = &t->err_no;
888 if (attrs->schedset == 0) {
889 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = attrs->qosclass;
890 } else {
891 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] =
892 _pthread_unspecified_priority();
893 }
894 t->tsd[_PTHREAD_TSD_SLOT_PTR_MUNGE] = _pthread_ptr_munge_token;
895 t->tl_has_custom_stack = (attrs->stackaddr != NULL);
896
897 _PTHREAD_LOCK_INIT(t->lock);
898
899 t->stackaddr = stackaddr;
900 t->stackbottom = stackaddr - stacksize;
901 t->freeaddr = freeaddr;
902 t->freesize = freesize;
903
904 t->guardsize = _pthread_attr_guardsize(attrs);
905 t->tl_joinable = (attrs->detached == PTHREAD_CREATE_JOINABLE);
906 t->inherit = attrs->inherit;
907 t->tl_policy = attrs->policy;
908 t->schedset = attrs->schedset;
909 _pthread_attr_get_schedparam(attrs, &t->tl_param);
910 t->cancel_state = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
911 }
912
913 #pragma mark pthread public interface
914
915 /* Need to deprecate this in future */
916 int
917 _pthread_is_threaded(void)
918 {
919 return __is_threaded;
920 }
921
922 /* Non portable public api to know whether this process has(had) atleast one thread
923 * apart from main thread. There could be race if there is a thread in the process of
924 * creation at the time of call . It does not tell whether there are more than one thread
925 * at this point of time.
926 */
927 int
928 pthread_is_threaded_np(void)
929 {
930 return __is_threaded;
931 }
932
933
934 PTHREAD_NOEXPORT_VARIANT
935 mach_port_t
936 pthread_mach_thread_np(pthread_t t)
937 {
938 mach_port_t kport = MACH_PORT_NULL;
939 (void)_pthread_is_valid(t, &kport);
940 return kport;
941 }
942
943 PTHREAD_NOEXPORT_VARIANT
944 pthread_t
945 pthread_from_mach_thread_np(mach_port_t kernel_thread)
946 {
947 struct _pthread *p = NULL;
948
949 /* No need to wait as mach port is already known */
950 _PTHREAD_LOCK(_pthread_list_lock);
951
952 TAILQ_FOREACH(p, &__pthread_head, tl_plist) {
953 if (_pthread_kernel_thread(p) == kernel_thread) {
954 break;
955 }
956 }
957
958 _PTHREAD_UNLOCK(_pthread_list_lock);
959
960 return p;
961 }
962
963 PTHREAD_NOEXPORT_VARIANT
964 size_t
965 pthread_get_stacksize_np(pthread_t t)
966 {
967 size_t size = 0;
968 size_t stacksize = t->stackaddr - t->stackbottom;
969
970 if (t == NULL) {
971 return ESRCH; // XXX bug?
972 }
973
974 #if !defined(__arm__) && !defined(__arm64__)
975 // The default rlimit based allocations will be provided with a stacksize
976 // of the current limit and a freesize of the max. However, custom
977 // allocations will just have the guard page to free. If we aren't in the
978 // latter case, call into rlimit to determine the current stack size. In
979 // the event that the current limit == max limit then we'll fall down the
980 // fast path, but since it's unlikely that the limit is going to be lowered
981 // after it's been change to the max, we should be fine.
982 //
983 // Of course, on arm rlim_cur == rlim_max and there's only the one guard
984 // page. So, we can skip all this there.
985 if (t == main_thread() && stacksize + vm_page_size != t->freesize) {
986 // We want to call getrlimit() just once, as it's relatively expensive
987 static size_t rlimit_stack;
988
989 if (rlimit_stack == 0) {
990 struct rlimit limit;
991 int ret = getrlimit(RLIMIT_STACK, &limit);
992
993 if (ret == 0) {
994 rlimit_stack = (size_t) limit.rlim_cur;
995 }
996 }
997
998 if (rlimit_stack == 0 || rlimit_stack > t->freesize) {
999 return stacksize;
1000 } else {
1001 return rlimit_stack;
1002 }
1003 }
1004 #endif /* !defined(__arm__) && !defined(__arm64__) */
1005
1006 if (t == pthread_self() || t == main_thread()) {
1007 size = stacksize;
1008 goto out;
1009 }
1010
1011 if (_pthread_validate_thread_and_list_lock(t)) {
1012 size = stacksize;
1013 _PTHREAD_UNLOCK(_pthread_list_lock);
1014 } else {
1015 size = ESRCH; // XXX bug?
1016 }
1017
1018 out:
1019 // <rdar://problem/42588315> binary compatibility issues force us to return
1020 // DEFAULT_STACK_SIZE here when we do not know the size of the stack
1021 return size ? size : DEFAULT_STACK_SIZE;
1022 }
1023
1024 PTHREAD_NOEXPORT_VARIANT
1025 void *
1026 pthread_get_stackaddr_np(pthread_t t)
1027 {
1028 // since the main thread will not get de-allocated from underneath us
1029 if (t == pthread_self() || t == main_thread()) {
1030 return t->stackaddr;
1031 }
1032
1033 if (!_pthread_validate_thread_and_list_lock(t)) {
1034 return (void *)(uintptr_t)ESRCH; // XXX bug?
1035 }
1036
1037 void *addr = t->stackaddr;
1038 _PTHREAD_UNLOCK(_pthread_list_lock);
1039 return addr;
1040 }
1041
1042
1043 static mach_port_t
1044 _pthread_reply_port(pthread_t t)
1045 {
1046 void *p;
1047 if (t == NULL) {
1048 p = _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY);
1049 } else {
1050 p = t->tsd[_PTHREAD_TSD_SLOT_MIG_REPLY];
1051 }
1052 return (mach_port_t)(uintptr_t)p;
1053 }
1054
1055 static void
1056 _pthread_set_reply_port(pthread_t t, mach_port_t reply_port)
1057 {
1058 void *p = (void *)(uintptr_t)reply_port;
1059 if (t == NULL) {
1060 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY, p);
1061 } else {
1062 t->tsd[_PTHREAD_TSD_SLOT_MIG_REPLY] = p;
1063 }
1064 }
1065
1066 static void
1067 _pthread_dealloc_reply_port(pthread_t t)
1068 {
1069 mach_port_t reply_port = _pthread_reply_port(t);
1070 if (reply_port != MACH_PORT_NULL) {
1071 mig_dealloc_reply_port(reply_port);
1072 }
1073 }
1074
1075 static mach_port_t
1076 _pthread_special_reply_port(pthread_t t)
1077 {
1078 void *p;
1079 if (t == NULL) {
1080 p = _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY);
1081 } else {
1082 p = t->tsd[_PTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY];
1083 }
1084 return (mach_port_t)(uintptr_t)p;
1085 }
1086
1087 static void
1088 _pthread_dealloc_special_reply_port(pthread_t t)
1089 {
1090 mach_port_t special_reply_port = _pthread_special_reply_port(t);
1091 if (special_reply_port != MACH_PORT_NULL) {
1092 thread_destruct_special_reply_port(special_reply_port,
1093 THREAD_SPECIAL_REPLY_PORT_ALL);
1094 }
1095 }
1096
1097 pthread_t
1098 pthread_main_thread_np(void)
1099 {
1100 return main_thread();
1101 }
1102
1103 /* returns non-zero if the current thread is the main thread */
1104 int
1105 pthread_main_np(void)
1106 {
1107 return pthread_self() == main_thread();
1108 }
1109
1110
1111 /*
1112 * if we are passed in a pthread_t that is NULL, then we return the current
1113 * thread's thread_id. So folks don't have to call pthread_self, in addition to
1114 * us doing it, if they just want their thread_id.
1115 */
1116 PTHREAD_NOEXPORT_VARIANT
1117 int
1118 pthread_threadid_np(pthread_t thread, uint64_t *thread_id)
1119 {
1120 int res = 0;
1121 pthread_t self = pthread_self();
1122
1123 if (thread_id == NULL) {
1124 return EINVAL;
1125 }
1126
1127 if (thread == NULL || thread == self) {
1128 *thread_id = self->thread_id;
1129 } else if (!_pthread_validate_thread_and_list_lock(thread)) {
1130 res = ESRCH;
1131 } else {
1132 if (thread->thread_id == 0) {
1133 res = EINVAL;
1134 } else {
1135 *thread_id = thread->thread_id;
1136 }
1137 _PTHREAD_UNLOCK(_pthread_list_lock);
1138 }
1139 return res;
1140 }
1141
1142 PTHREAD_NOEXPORT_VARIANT
1143 int
1144 pthread_getname_np(pthread_t thread, char *threadname, size_t len)
1145 {
1146 if (thread == pthread_self()) {
1147 strlcpy(threadname, thread->pthread_name, len);
1148 return 0;
1149 }
1150
1151 if (!_pthread_validate_thread_and_list_lock(thread)) {
1152 return ESRCH;
1153 }
1154
1155 strlcpy(threadname, thread->pthread_name, len);
1156 _PTHREAD_UNLOCK(_pthread_list_lock);
1157 return 0;
1158 }
1159
1160
1161 int
1162 pthread_setname_np(const char *name)
1163 {
1164 int res;
1165 pthread_t self = pthread_self();
1166
1167 size_t len = 0;
1168 if (name != NULL) {
1169 len = strlen(name);
1170 }
1171
1172 /* protytype is in pthread_internals.h */
1173 res = __proc_info(5, getpid(), 2, (uint64_t)0, (void*)name, (int)len);
1174 if (res == 0) {
1175 if (len > 0) {
1176 strlcpy(self->pthread_name, name, MAXTHREADNAMESIZE);
1177 } else {
1178 bzero(self->pthread_name, MAXTHREADNAMESIZE);
1179 }
1180 }
1181 return res;
1182
1183 }
1184
1185 PTHREAD_ALWAYS_INLINE
1186 static inline void
1187 __pthread_add_thread(pthread_t t, bool from_mach_thread)
1188 {
1189 if (from_mach_thread) {
1190 _PTHREAD_LOCK_FROM_MACH_THREAD(_pthread_list_lock);
1191 } else {
1192 _PTHREAD_LOCK(_pthread_list_lock);
1193 }
1194
1195 TAILQ_INSERT_TAIL(&__pthread_head, t, tl_plist);
1196 _pthread_count++;
1197
1198 if (from_mach_thread) {
1199 _PTHREAD_UNLOCK_FROM_MACH_THREAD(_pthread_list_lock);
1200 } else {
1201 _PTHREAD_UNLOCK(_pthread_list_lock);
1202 }
1203
1204 if (!from_mach_thread) {
1205 // PR-26275485: Mach threads will likely crash trying to run
1206 // introspection code. Since the fall out from the introspection
1207 // code not seeing the injected thread is likely less than crashing
1208 // in the introspection code, just don't make the call.
1209 _pthread_introspection_thread_create(t);
1210 }
1211 }
1212
1213 PTHREAD_ALWAYS_INLINE
1214 static inline void
1215 __pthread_undo_add_thread(pthread_t t, bool from_mach_thread)
1216 {
1217 if (from_mach_thread) {
1218 _PTHREAD_LOCK_FROM_MACH_THREAD(_pthread_list_lock);
1219 } else {
1220 _PTHREAD_LOCK(_pthread_list_lock);
1221 }
1222
1223 TAILQ_REMOVE(&__pthread_head, t, tl_plist);
1224 _pthread_count--;
1225
1226 if (from_mach_thread) {
1227 _PTHREAD_UNLOCK_FROM_MACH_THREAD(_pthread_list_lock);
1228 } else {
1229 _PTHREAD_UNLOCK(_pthread_list_lock);
1230 }
1231 }
1232
1233 PTHREAD_ALWAYS_INLINE
1234 static inline void
1235 __pthread_started_thread(pthread_t t)
1236 {
1237 mach_port_t kport = _pthread_kernel_thread(t);
1238 if (os_slowpath(!MACH_PORT_VALID(kport))) {
1239 PTHREAD_CLIENT_CRASH(kport,
1240 "Unable to allocate thread port, possible port leak");
1241 }
1242 _pthread_introspection_thread_start(t);
1243 }
1244
1245 #define _PTHREAD_CREATE_NONE 0x0
1246 #define _PTHREAD_CREATE_FROM_MACH_THREAD 0x1
1247 #define _PTHREAD_CREATE_SUSPENDED 0x2
1248
1249 static int
1250 _pthread_create(pthread_t *thread, const pthread_attr_t *attrs,
1251 void *(*start_routine)(void *), void *arg, unsigned int create_flags)
1252 {
1253 pthread_t t = NULL;
1254 void *stack = NULL;
1255 bool from_mach_thread = (create_flags & _PTHREAD_CREATE_FROM_MACH_THREAD);
1256
1257 if (attrs == NULL) {
1258 attrs = &_pthread_attr_default;
1259 } else if (attrs->sig != _PTHREAD_ATTR_SIG) {
1260 return EINVAL;
1261 }
1262
1263 unsigned int flags = PTHREAD_START_CUSTOM;
1264 if (attrs->schedset != 0) {
1265 struct sched_param p;
1266 _pthread_attr_get_schedparam(attrs, &p);
1267 flags |= PTHREAD_START_SETSCHED;
1268 flags |= ((attrs->policy & PTHREAD_START_POLICY_MASK) << PTHREAD_START_POLICY_BITSHIFT);
1269 flags |= (p.sched_priority & PTHREAD_START_IMPORTANCE_MASK);
1270 } else if (attrs->qosclass != 0) {
1271 flags |= PTHREAD_START_QOSCLASS;
1272 flags |= (attrs->qosclass & PTHREAD_START_QOSCLASS_MASK);
1273 }
1274 if (create_flags & _PTHREAD_CREATE_SUSPENDED) {
1275 flags |= PTHREAD_START_SUSPENDED;
1276 }
1277
1278 __is_threaded = 1;
1279
1280 t =_pthread_allocate(attrs, &stack);
1281 if (t == NULL) {
1282 return EAGAIN;
1283 }
1284
1285 t->arg = arg;
1286 t->fun = start_routine;
1287 __pthread_add_thread(t, from_mach_thread);
1288
1289 if (__bsdthread_create(start_routine, arg, stack, t, flags) ==
1290 (pthread_t)-1) {
1291 if (errno == EMFILE) {
1292 PTHREAD_CLIENT_CRASH(0,
1293 "Unable to allocate thread port, possible port leak");
1294 }
1295 __pthread_undo_add_thread(t, from_mach_thread);
1296 _pthread_deallocate(t, from_mach_thread);
1297 return EAGAIN;
1298 }
1299
1300 if (create_flags & _PTHREAD_CREATE_SUSPENDED) {
1301 _pthread_markcancel_if_canceled(t, _pthread_kernel_thread(t));
1302 }
1303
1304 // n.b. if a thread is created detached and exits, t will be invalid
1305 *thread = t;
1306 return 0;
1307 }
1308
1309 int
1310 pthread_create(pthread_t *thread, const pthread_attr_t *attr,
1311 void *(*start_routine)(void *), void *arg)
1312 {
1313 unsigned int flags = _PTHREAD_CREATE_NONE;
1314 return _pthread_create(thread, attr, start_routine, arg, flags);
1315 }
1316
1317 int
1318 pthread_create_from_mach_thread(pthread_t *thread, const pthread_attr_t *attr,
1319 void *(*start_routine)(void *), void *arg)
1320 {
1321 unsigned int flags = _PTHREAD_CREATE_FROM_MACH_THREAD;
1322 return _pthread_create(thread, attr, start_routine, arg, flags);
1323 }
1324
1325 #if !defined(__OPEN_SOURCE__) && TARGET_OS_OSX // 40703288
1326 /* Functions defined in machine-dependent files. */
1327 PTHREAD_NOEXPORT void _pthread_setup_suspended(pthread_t th, void (*f)(pthread_t), void *sp);
1328
1329 PTHREAD_NORETURN
1330 static void
1331 _pthread_suspended_body(pthread_t self)
1332 {
1333 _pthread_set_self(self);
1334 __pthread_started_thread(self);
1335 _pthread_exit(self, (self->fun)(self->arg));
1336 }
1337
1338 static int
1339 _pthread_create_suspended_np(pthread_t *thread, const pthread_attr_t *attrs,
1340 void *(*start_routine)(void *), void *arg)
1341 {
1342 pthread_t t;
1343 void *stack;
1344 mach_port_t kernel_thread = MACH_PORT_NULL;
1345
1346 if (attrs == NULL) {
1347 attrs = &_pthread_attr_default;
1348 } else if (attrs->sig != _PTHREAD_ATTR_SIG) {
1349 return EINVAL;
1350 }
1351
1352 t = _pthread_allocate(attrs, &stack);
1353 if (t == NULL) {
1354 return EAGAIN;
1355 }
1356
1357 if (thread_create(mach_task_self(), &kernel_thread) != KERN_SUCCESS) {
1358 _pthread_deallocate(t, false);
1359 return EAGAIN;
1360 }
1361
1362 _pthread_set_kernel_thread(t, kernel_thread);
1363 (void)pthread_setschedparam_internal(t, kernel_thread,
1364 t->tl_policy, &t->tl_param);
1365
1366 __is_threaded = 1;
1367
1368 t->arg = arg;
1369 t->fun = start_routine;
1370 t->cancel_state |= _PTHREAD_CANCEL_INITIALIZED;
1371 __pthread_add_thread(t, false);
1372
1373 // Set up a suspended thread.
1374 _pthread_setup_suspended(t, _pthread_suspended_body, stack);
1375 *thread = t;
1376 return 0;
1377 }
1378 #endif // !defined(__OPEN_SOURCE__) && TARGET_OS_OSX
1379
1380 int
1381 pthread_create_suspended_np(pthread_t *thread, const pthread_attr_t *attr,
1382 void *(*start_routine)(void *), void *arg)
1383 {
1384 #if !defined(__OPEN_SOURCE__) && TARGET_OS_OSX // 40703288
1385 if (_os_xbs_chrooted) {
1386 return _pthread_create_suspended_np(thread, attr, start_routine, arg);
1387 }
1388 #endif
1389 unsigned int flags = _PTHREAD_CREATE_SUSPENDED;
1390 return _pthread_create(thread, attr, start_routine, arg, flags);
1391 }
1392
1393
1394 PTHREAD_NOEXPORT_VARIANT
1395 int
1396 pthread_detach(pthread_t thread)
1397 {
1398 int res = 0;
1399 bool join = false, wake = false;
1400
1401 if (!_pthread_validate_thread_and_list_lock(thread)) {
1402 return ESRCH;
1403 }
1404
1405 if (!thread->tl_joinable) {
1406 res = EINVAL;
1407 } else if (thread->tl_exit_gate == MACH_PORT_DEAD) {
1408 // Join the thread if it's already exited.
1409 join = true;
1410 } else {
1411 thread->tl_joinable = false; // _pthread_joiner_prepost_wake uses this
1412 if (thread->tl_join_ctx) {
1413 (void)_pthread_joiner_prepost_wake(thread);
1414 wake = true;
1415 }
1416 }
1417 _PTHREAD_UNLOCK(_pthread_list_lock);
1418
1419 if (join) {
1420 pthread_join(thread, NULL);
1421 } else if (wake) {
1422 _pthread_joiner_wake(thread);
1423 }
1424 return res;
1425 }
1426
1427 PTHREAD_NOEXPORT_VARIANT
1428 int
1429 pthread_kill(pthread_t th, int sig)
1430 {
1431 if (sig < 0 || sig > NSIG) {
1432 return EINVAL;
1433 }
1434
1435 mach_port_t kport = MACH_PORT_NULL;
1436 if (!_pthread_is_valid(th, &kport)) {
1437 return ESRCH; // Not a valid thread.
1438 }
1439
1440 // Don't signal workqueue threads.
1441 if (th->wqthread != 0 && th->wqkillset == 0) {
1442 return ENOTSUP;
1443 }
1444
1445 int ret = __pthread_kill(kport, sig);
1446
1447 if (ret == -1) {
1448 ret = errno;
1449 }
1450 return ret;
1451 }
1452
1453 PTHREAD_NOEXPORT_VARIANT
1454 int
1455 __pthread_workqueue_setkill(int enable)
1456 {
1457 pthread_t self = pthread_self();
1458
1459 _PTHREAD_LOCK(self->lock);
1460 self->wqkillset = enable ? 1 : 0;
1461 _PTHREAD_UNLOCK(self->lock);
1462
1463 return 0;
1464 }
1465
1466
1467 /* For compatibility... */
1468
1469 pthread_t
1470 _pthread_self(void)
1471 {
1472 return pthread_self();
1473 }
1474
1475 /*
1476 * Terminate a thread.
1477 */
1478 extern int __disable_threadsignal(int);
1479
1480 PTHREAD_NORETURN
1481 static void
1482 _pthread_exit(pthread_t self, void *exit_value)
1483 {
1484 struct __darwin_pthread_handler_rec *handler;
1485
1486 // Disable signal delivery while we clean up
1487 __disable_threadsignal(1);
1488
1489 // Set cancel state to disable and type to deferred
1490 _pthread_setcancelstate_exit(self, exit_value);
1491
1492 while ((handler = self->__cleanup_stack) != 0) {
1493 (handler->__routine)(handler->__arg);
1494 self->__cleanup_stack = handler->__next;
1495 }
1496 _pthread_tsd_cleanup(self);
1497
1498 // Clear per-thread semaphore cache
1499 os_put_cached_semaphore(SEMAPHORE_NULL);
1500
1501 _pthread_terminate_invoke(self, exit_value);
1502 }
1503
1504 void
1505 pthread_exit(void *exit_value)
1506 {
1507 pthread_t self = pthread_self();
1508 if (os_unlikely(self->wqthread)) {
1509 PTHREAD_CLIENT_CRASH(0, "pthread_exit() called from a thread "
1510 "not created by pthread_create()");
1511 }
1512 _pthread_exit(self, exit_value);
1513 }
1514
1515
1516 PTHREAD_NOEXPORT_VARIANT
1517 int
1518 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
1519 {
1520 if (!_pthread_validate_thread_and_list_lock(thread)) {
1521 return ESRCH;
1522 }
1523
1524 if (policy) *policy = thread->tl_policy;
1525 if (param) *param = thread->tl_param;
1526 _PTHREAD_UNLOCK(_pthread_list_lock);
1527 return 0;
1528 }
1529
1530
1531
1532 PTHREAD_ALWAYS_INLINE
1533 static inline int
1534 pthread_setschedparam_internal(pthread_t thread, mach_port_t kport, int policy,
1535 const struct sched_param *param)
1536 {
1537 policy_base_data_t bases;
1538 policy_base_t base;
1539 mach_msg_type_number_t count;
1540 kern_return_t ret;
1541
1542 switch (policy) {
1543 case SCHED_OTHER:
1544 bases.ts.base_priority = param->sched_priority;
1545 base = (policy_base_t)&bases.ts;
1546 count = POLICY_TIMESHARE_BASE_COUNT;
1547 break;
1548 case SCHED_FIFO:
1549 bases.fifo.base_priority = param->sched_priority;
1550 base = (policy_base_t)&bases.fifo;
1551 count = POLICY_FIFO_BASE_COUNT;
1552 break;
1553 case SCHED_RR:
1554 bases.rr.base_priority = param->sched_priority;
1555 /* quantum isn't public yet */
1556 bases.rr.quantum = param->quantum;
1557 base = (policy_base_t)&bases.rr;
1558 count = POLICY_RR_BASE_COUNT;
1559 break;
1560 default:
1561 return EINVAL;
1562 }
1563 ret = thread_policy(kport, policy, base, count, TRUE);
1564 return (ret != KERN_SUCCESS) ? EINVAL : 0;
1565 }
1566
1567 PTHREAD_NOEXPORT_VARIANT
1568 int
1569 pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param)
1570 {
1571 mach_port_t kport = MACH_PORT_NULL;
1572 int bypass = 1;
1573
1574 // since the main thread will not get de-allocated from underneath us
1575 if (t == pthread_self() || t == main_thread()) {
1576 kport = _pthread_kernel_thread(t);
1577 } else {
1578 bypass = 0;
1579 if (!_pthread_is_valid(t, &kport)) {
1580 return ESRCH;
1581 }
1582 }
1583
1584 int res = pthread_setschedparam_internal(t, kport, policy, param);
1585 if (res) return res;
1586
1587 if (bypass) {
1588 _PTHREAD_LOCK(_pthread_list_lock);
1589 } else if (!_pthread_validate_thread_and_list_lock(t)) {
1590 // Ensure the thread is still valid.
1591 return ESRCH;
1592 }
1593
1594 t->tl_policy = policy;
1595 t->tl_param = *param;
1596 _PTHREAD_UNLOCK(_pthread_list_lock);
1597 return 0;
1598 }
1599
1600
1601 int
1602 sched_get_priority_min(int policy)
1603 {
1604 return default_priority - 16;
1605 }
1606
1607 int
1608 sched_get_priority_max(int policy)
1609 {
1610 return default_priority + 16;
1611 }
1612
1613 int
1614 pthread_equal(pthread_t t1, pthread_t t2)
1615 {
1616 return (t1 == t2);
1617 }
1618
1619 /*
1620 * Force LLVM not to optimise this to a call to __pthread_set_self, if it does
1621 * then _pthread_set_self won't be bound when secondary threads try and start up.
1622 */
1623 PTHREAD_NOINLINE
1624 void
1625 _pthread_set_self(pthread_t p)
1626 {
1627 #if VARIANT_DYLD
1628 if (os_likely(!p)) {
1629 return _pthread_set_self_dyld();
1630 }
1631 #endif // VARIANT_DYLD
1632 _pthread_set_self_internal(p, true);
1633 }
1634
1635 #if VARIANT_DYLD
1636 // _pthread_set_self_dyld is noinline+noexport to allow the option for
1637 // static libsyscall to adopt this as the entry point from mach_init if
1638 // desired
1639 PTHREAD_NOINLINE PTHREAD_NOEXPORT
1640 void
1641 _pthread_set_self_dyld(void)
1642 {
1643 pthread_t p = main_thread();
1644 p->thread_id = __thread_selfid();
1645
1646 if (os_unlikely(p->thread_id == -1ull)) {
1647 PTHREAD_INTERNAL_CRASH(0, "failed to set thread_id");
1648 }
1649
1650 // <rdar://problem/40930651> pthread self and the errno address are the
1651 // bare minimium TSD setup that dyld needs to actually function. Without
1652 // this, TSD access will fail and crash if it uses bits of Libc prior to
1653 // library initialization. __pthread_init will finish the initialization
1654 // during library init.
1655 p->tsd[_PTHREAD_TSD_SLOT_PTHREAD_SELF] = p;
1656 p->tsd[_PTHREAD_TSD_SLOT_ERRNO] = &p->err_no;
1657 _thread_set_tsd_base(&p->tsd[0]);
1658 }
1659 #endif // VARIANT_DYLD
1660
1661 PTHREAD_ALWAYS_INLINE
1662 static inline void
1663 _pthread_set_self_internal(pthread_t p, bool needs_tsd_base_set)
1664 {
1665 p->thread_id = __thread_selfid();
1666
1667 if (os_unlikely(p->thread_id == -1ull)) {
1668 PTHREAD_INTERNAL_CRASH(0, "failed to set thread_id");
1669 }
1670
1671 if (needs_tsd_base_set) {
1672 _thread_set_tsd_base(&p->tsd[0]);
1673 }
1674 }
1675
1676
1677 // <rdar://problem/28984807> pthread_once should have an acquire barrier
1678 PTHREAD_ALWAYS_INLINE
1679 static inline void
1680 _os_once_acquire(os_once_t *predicate, void *context, os_function_t function)
1681 {
1682 if (OS_EXPECT(os_atomic_load(predicate, acquire), ~0l) != ~0l) {
1683 _os_once(predicate, context, function);
1684 OS_COMPILER_CAN_ASSUME(*predicate == ~0l);
1685 }
1686 }
1687
1688 struct _pthread_once_context {
1689 pthread_once_t *pthread_once;
1690 void (*routine)(void);
1691 };
1692
1693 static void
1694 __pthread_once_handler(void *context)
1695 {
1696 struct _pthread_once_context *ctx = context;
1697 pthread_cleanup_push((void*)__os_once_reset, &ctx->pthread_once->once);
1698 ctx->routine();
1699 pthread_cleanup_pop(0);
1700 ctx->pthread_once->sig = _PTHREAD_ONCE_SIG;
1701 }
1702
1703 PTHREAD_NOEXPORT_VARIANT
1704 int
1705 pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1706 {
1707 struct _pthread_once_context ctx = { once_control, init_routine };
1708 do {
1709 _os_once_acquire(&once_control->once, &ctx, __pthread_once_handler);
1710 } while (once_control->sig == _PTHREAD_ONCE_SIG_init);
1711 return 0;
1712 }
1713
1714
1715 int
1716 pthread_getconcurrency(void)
1717 {
1718 return pthread_concurrency;
1719 }
1720
1721 int
1722 pthread_setconcurrency(int new_level)
1723 {
1724 if (new_level < 0) {
1725 return EINVAL;
1726 }
1727 pthread_concurrency = new_level;
1728 return 0;
1729 }
1730
1731 #if !defined(VARIANT_STATIC)
1732 void *
1733 malloc(size_t sz)
1734 {
1735 if (_pthread_malloc) {
1736 return _pthread_malloc(sz);
1737 } else {
1738 return NULL;
1739 }
1740 }
1741
1742 void
1743 free(void *p)
1744 {
1745 if (_pthread_free) {
1746 _pthread_free(p);
1747 }
1748 }
1749 #endif // VARIANT_STATIC
1750
1751 /*
1752 * Perform package initialization - called automatically when application starts
1753 */
1754 struct ProgramVars; /* forward reference */
1755
1756 #if !VARIANT_DYLD
1757 static unsigned long
1758 _pthread_strtoul(const char *p, const char **endptr, int base)
1759 {
1760 uintptr_t val = 0;
1761
1762 // Expect hex string starting with "0x"
1763 if ((base == 16 || base == 0) && p && p[0] == '0' && p[1] == 'x') {
1764 p += 2;
1765 while (1) {
1766 char c = *p;
1767 if ('0' <= c && c <= '9') {
1768 val = (val << 4) + (c - '0');
1769 } else if ('a' <= c && c <= 'f') {
1770 val = (val << 4) + (c - 'a' + 10);
1771 } else if ('A' <= c && c <= 'F') {
1772 val = (val << 4) + (c - 'A' + 10);
1773 } else {
1774 break;
1775 }
1776 ++p;
1777 }
1778 }
1779
1780 *endptr = (char *)p;
1781 return val;
1782 }
1783
1784 static int
1785 parse_main_stack_params(const char *apple[],
1786 void **stackaddr,
1787 size_t *stacksize,
1788 void **allocaddr,
1789 size_t *allocsize)
1790 {
1791 const char *p = _simple_getenv(apple, "main_stack");
1792 if (!p) return 0;
1793
1794 int ret = 0;
1795 const char *s = p;
1796
1797 *stackaddr = _pthread_strtoul(s, &s, 16);
1798 if (*s != ',') goto out;
1799
1800 *stacksize = _pthread_strtoul(s + 1, &s, 16);
1801 if (*s != ',') goto out;
1802
1803 *allocaddr = _pthread_strtoul(s + 1, &s, 16);
1804 if (*s != ',') goto out;
1805
1806 *allocsize = _pthread_strtoul(s + 1, &s, 16);
1807 if (*s != ',' && *s != 0) goto out;
1808
1809 ret = 1;
1810 out:
1811 bzero((char *)p, strlen(p));
1812 return ret;
1813 }
1814
1815 static void
1816 parse_ptr_munge_params(const char *envp[], const char *apple[])
1817 {
1818 const char *p, *s;
1819 p = _simple_getenv(apple, "ptr_munge");
1820 if (p) {
1821 _pthread_ptr_munge_token = _pthread_strtoul(p, &s, 16);
1822 bzero((char *)p, strlen(p));
1823 }
1824 #if !DEBUG
1825 if (_pthread_ptr_munge_token) return;
1826 #endif
1827 p = _simple_getenv(envp, "PTHREAD_PTR_MUNGE_TOKEN");
1828 if (p) {
1829 uintptr_t t = _pthread_strtoul(p, &s, 16);
1830 if (t) _pthread_ptr_munge_token = t;
1831 }
1832 }
1833
1834 int
1835 __pthread_init(const struct _libpthread_functions *pthread_funcs,
1836 const char *envp[], const char *apple[],
1837 const struct ProgramVars *vars __unused)
1838 {
1839 // Save our provided pushed-down functions
1840 if (pthread_funcs) {
1841 exitf = pthread_funcs->exit;
1842
1843 if (pthread_funcs->version >= 2) {
1844 _pthread_malloc = pthread_funcs->malloc;
1845 _pthread_free = pthread_funcs->free;
1846 }
1847 }
1848
1849 //
1850 // Get host information
1851 //
1852
1853 kern_return_t kr;
1854 host_flavor_t flavor = HOST_PRIORITY_INFO;
1855 mach_msg_type_number_t count = HOST_PRIORITY_INFO_COUNT;
1856 host_priority_info_data_t priority_info;
1857 host_t host = mach_host_self();
1858 kr = host_info(host, flavor, (host_info_t)&priority_info, &count);
1859 if (kr != KERN_SUCCESS) {
1860 PTHREAD_INTERNAL_CRASH(kr, "host_info() failed");
1861 } else {
1862 default_priority = (uint8_t)priority_info.user_priority;
1863 min_priority = (uint8_t)priority_info.minimum_priority;
1864 max_priority = (uint8_t)priority_info.maximum_priority;
1865 }
1866 mach_port_deallocate(mach_task_self(), host);
1867
1868 //
1869 // Set up the main thread structure
1870 //
1871
1872 // Get the address and size of the main thread's stack from the kernel.
1873 void *stackaddr = 0;
1874 size_t stacksize = 0;
1875 void *allocaddr = 0;
1876 size_t allocsize = 0;
1877 if (!parse_main_stack_params(apple, &stackaddr, &stacksize, &allocaddr, &allocsize) ||
1878 stackaddr == NULL || stacksize == 0) {
1879 // Fall back to previous bevhaior.
1880 size_t len = sizeof(stackaddr);
1881 int mib[] = { CTL_KERN, KERN_USRSTACK };
1882 if (__sysctl(mib, 2, &stackaddr, &len, NULL, 0) != 0) {
1883 #if defined(__LP64__)
1884 stackaddr = (void *)USRSTACK64;
1885 #else
1886 stackaddr = (void *)USRSTACK;
1887 #endif
1888 }
1889 stacksize = DFLSSIZ;
1890 allocaddr = 0;
1891 allocsize = 0;
1892 }
1893
1894 // Initialize random ptr_munge token from the kernel.
1895 parse_ptr_munge_params(envp, apple);
1896
1897 // libpthread.a in dyld "owns" the main thread structure itself and sets
1898 // up the tsd to point to it. So take the pthread_self() from there
1899 // and make it our main thread point.
1900 pthread_t thread = (pthread_t)_pthread_getspecific_direct(
1901 _PTHREAD_TSD_SLOT_PTHREAD_SELF);
1902 PTHREAD_ASSERT(thread);
1903 _main_thread_ptr = thread;
1904
1905 PTHREAD_ASSERT(_pthread_attr_default.qosclass ==
1906 _pthread_default_priority(0));
1907 _pthread_struct_init(thread, &_pthread_attr_default,
1908 stackaddr, stacksize, allocaddr, allocsize);
1909 thread->tl_joinable = true;
1910
1911 // Finish initialization with common code that is reinvoked on the
1912 // child side of a fork.
1913
1914 // Finishes initialization of main thread attributes.
1915 // Initializes the thread list and add the main thread.
1916 // Calls _pthread_set_self() to prepare the main thread for execution.
1917 _pthread_main_thread_init(thread);
1918
1919 struct _pthread_registration_data registration_data;
1920 // Set up kernel entry points with __bsdthread_register.
1921 _pthread_bsdthread_init(&registration_data);
1922
1923 // Have pthread_key and pthread_mutex do their init envvar checks.
1924 _pthread_key_global_init(envp);
1925 _pthread_mutex_global_init(envp, &registration_data);
1926
1927 #if PTHREAD_DEBUG_LOG
1928 _SIMPLE_STRING path = _simple_salloc();
1929 _simple_sprintf(path, "/var/tmp/libpthread.%d.log", getpid());
1930 _pthread_debuglog = open(_simple_string(path),
1931 O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0666);
1932 _simple_sfree(path);
1933 _pthread_debugstart = mach_absolute_time();
1934 #endif
1935
1936 return 0;
1937 }
1938 #endif // !VARIANT_DYLD
1939
1940 PTHREAD_NOEXPORT void
1941 _pthread_main_thread_init(pthread_t p)
1942 {
1943 TAILQ_INIT(&__pthread_head);
1944 _PTHREAD_LOCK_INIT(_pthread_list_lock);
1945 _PTHREAD_LOCK_INIT(p->lock);
1946 _pthread_set_kernel_thread(p, mach_thread_self());
1947 _pthread_set_reply_port(p, mach_reply_port());
1948 p->__cleanup_stack = NULL;
1949 p->tl_join_ctx = NULL;
1950 p->tl_exit_gate = MACH_PORT_NULL;
1951 p->tsd[__TSD_SEMAPHORE_CACHE] = (void*)(uintptr_t)SEMAPHORE_NULL;
1952 p->tsd[__TSD_MACH_SPECIAL_REPLY] = 0;
1953 p->cancel_state |= _PTHREAD_CANCEL_INITIALIZED;
1954
1955 // Initialize the list of threads with the new main thread.
1956 TAILQ_INSERT_HEAD(&__pthread_head, p, tl_plist);
1957 _pthread_count = 1;
1958
1959 _pthread_introspection_thread_start(p);
1960 }
1961
1962 PTHREAD_NOEXPORT
1963 void
1964 _pthread_main_thread_postfork_init(pthread_t p)
1965 {
1966 _pthread_main_thread_init(p);
1967 _pthread_set_self_internal(p, false);
1968 }
1969
1970 int
1971 sched_yield(void)
1972 {
1973 swtch_pri(0);
1974 return 0;
1975 }
1976
1977 // XXX remove
1978 void
1979 cthread_yield(void)
1980 {
1981 sched_yield();
1982 }
1983
1984 void
1985 pthread_yield_np(void)
1986 {
1987 sched_yield();
1988 }
1989
1990
1991
1992 // Libsystem knows about this symbol and exports it to libsyscall
1993 PTHREAD_NOEXPORT_VARIANT
1994 void
1995 _pthread_clear_qos_tsd(mach_port_t thread_port)
1996 {
1997 if (thread_port == MACH_PORT_NULL || (uintptr_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF) == thread_port) {
1998 /* Clear the current thread's TSD, that can be done inline. */
1999 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS,
2000 _pthread_unspecified_priority());
2001 } else {
2002 pthread_t p;
2003
2004 _PTHREAD_LOCK(_pthread_list_lock);
2005
2006 TAILQ_FOREACH(p, &__pthread_head, tl_plist) {
2007 mach_port_t kp = _pthread_kernel_thread(p);
2008 if (thread_port == kp) {
2009 p->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] =
2010 _pthread_unspecified_priority();
2011 break;
2012 }
2013 }
2014
2015 _PTHREAD_UNLOCK(_pthread_list_lock);
2016 }
2017 }
2018
2019
2020 #pragma mark pthread/stack_np.h public interface
2021
2022
2023 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
2024 typedef uintptr_t frame_data_addr_t;
2025
2026 struct frame_data {
2027 frame_data_addr_t frame_addr_next;
2028 frame_data_addr_t ret_addr;
2029 };
2030 #else
2031 #error ********** Unimplemented architecture
2032 #endif
2033
2034 uintptr_t
2035 pthread_stack_frame_decode_np(uintptr_t frame_addr, uintptr_t *return_addr)
2036 {
2037 struct frame_data *frame = (struct frame_data *)frame_addr;
2038
2039 if (return_addr) {
2040 *return_addr = (uintptr_t)frame->ret_addr;
2041 }
2042
2043 return (uintptr_t)frame->frame_addr_next;
2044 }
2045
2046
2047 #pragma mark pthread workqueue support routines
2048
2049
2050 PTHREAD_NOEXPORT void
2051 _pthread_bsdthread_init(struct _pthread_registration_data *data)
2052 {
2053 bzero(data, sizeof(*data));
2054 data->version = sizeof(struct _pthread_registration_data);
2055 data->dispatch_queue_offset = __PTK_LIBDISPATCH_KEY0 * sizeof(void *);
2056 data->return_to_kernel_offset = __TSD_RETURN_TO_KERNEL * sizeof(void *);
2057 data->tsd_offset = offsetof(struct _pthread, tsd);
2058 data->mach_thread_self_offset = __TSD_MACH_THREAD_SELF * sizeof(void *);
2059
2060 int rv = __bsdthread_register(thread_start, start_wqthread, (int)PTHREAD_SIZE,
2061 (void*)data, (uintptr_t)sizeof(*data), data->dispatch_queue_offset);
2062
2063 if (rv > 0) {
2064 int required_features =
2065 PTHREAD_FEATURE_FINEPRIO |
2066 PTHREAD_FEATURE_BSDTHREADCTL |
2067 PTHREAD_FEATURE_SETSELF |
2068 PTHREAD_FEATURE_QOS_MAINTENANCE |
2069 PTHREAD_FEATURE_QOS_DEFAULT;
2070 if ((rv & required_features) != required_features) {
2071 PTHREAD_INTERNAL_CRASH(rv, "Missing required kernel support");
2072 }
2073 __pthread_supported_features = rv;
2074 }
2075
2076 /*
2077 * TODO: differentiate between (-1, EINVAL) after fork (which has the side
2078 * effect of resetting the child's stack_addr_hint before bailing out) and
2079 * (-1, EINVAL) because of invalid arguments. We'd probably like to treat
2080 * the latter as fatal.
2081 *
2082 * <rdar://problem/36451838>
2083 */
2084
2085 pthread_priority_t main_qos = (pthread_priority_t)data->main_qos;
2086
2087 if (_pthread_priority_thread_qos(main_qos) != THREAD_QOS_UNSPECIFIED) {
2088 _pthread_set_main_qos(main_qos);
2089 main_thread()->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = main_qos;
2090 }
2091
2092 if (data->stack_addr_hint) {
2093 __pthread_stack_hint = data->stack_addr_hint;
2094 }
2095
2096 if (__libdispatch_workerfunction != NULL) {
2097 // prepare the kernel for workq action
2098 (void)__workq_open();
2099 }
2100 }
2101
2102 PTHREAD_NOINLINE
2103 static void
2104 _pthread_wqthread_legacy_worker_wrap(pthread_priority_t pp)
2105 {
2106 /* Old thread priorities are inverted from where we have them in
2107 * the new flexible priority scheme. The highest priority is zero,
2108 * up to 2, with background at 3.
2109 */
2110 pthread_workqueue_function_t func = (pthread_workqueue_function_t)__libdispatch_workerfunction;
2111 bool overcommit = (pp & _PTHREAD_PRIORITY_OVERCOMMIT_FLAG);
2112 int opts = overcommit ? WORKQ_ADDTHREADS_OPTION_OVERCOMMIT : 0;
2113
2114 switch (_pthread_priority_thread_qos(pp)) {
2115 case THREAD_QOS_USER_INITIATED:
2116 return (*func)(WORKQ_HIGH_PRIOQUEUE, opts, NULL);
2117 case THREAD_QOS_LEGACY:
2118 /* B&I builders can't pass a QOS_CLASS_DEFAULT thread to dispatch, for fear of the QoS being
2119 * picked up by NSThread (et al) and transported around the system. So change the TSD to
2120 * make this thread look like QOS_CLASS_USER_INITIATED even though it will still run as legacy.
2121 */
2122 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS,
2123 _pthread_priority_make_from_thread_qos(THREAD_QOS_USER_INITIATED, 0, 0));
2124 return (*func)(WORKQ_DEFAULT_PRIOQUEUE, opts, NULL);
2125 case THREAD_QOS_UTILITY:
2126 return (*func)(WORKQ_LOW_PRIOQUEUE, opts, NULL);
2127 case THREAD_QOS_BACKGROUND:
2128 return (*func)(WORKQ_BG_PRIOQUEUE, opts, NULL);
2129 }
2130 PTHREAD_INTERNAL_CRASH(pp, "Invalid pthread priority for the legacy interface");
2131 }
2132
2133 PTHREAD_ALWAYS_INLINE
2134 static inline pthread_priority_t
2135 _pthread_wqthread_priority(int flags)
2136 {
2137 pthread_priority_t pp = 0;
2138 thread_qos_t qos;
2139
2140 if (flags & WQ_FLAG_THREAD_KEVENT) {
2141 pp |= _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG;
2142 }
2143 if (flags & WQ_FLAG_THREAD_EVENT_MANAGER) {
2144 return pp | _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG;
2145 }
2146
2147 if (flags & WQ_FLAG_THREAD_OVERCOMMIT) {
2148 pp |= _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
2149 }
2150 if (flags & WQ_FLAG_THREAD_PRIO_QOS) {
2151 qos = (thread_qos_t)(flags & WQ_FLAG_THREAD_PRIO_MASK);
2152 pp = _pthread_priority_make_from_thread_qos(qos, 0, pp);
2153 } else if (flags & WQ_FLAG_THREAD_PRIO_SCHED) {
2154 pp |= _PTHREAD_PRIORITY_SCHED_PRI_MASK;
2155 pp |= (flags & WQ_FLAG_THREAD_PRIO_MASK);
2156 } else {
2157 PTHREAD_INTERNAL_CRASH(flags, "Missing priority");
2158 }
2159 return pp;
2160 }
2161
2162 PTHREAD_NOINLINE
2163 static void
2164 _pthread_wqthread_setup(pthread_t self, mach_port_t kport, void *stacklowaddr,
2165 int flags)
2166 {
2167 void *stackaddr = self;
2168 size_t stacksize = (uintptr_t)self - (uintptr_t)stacklowaddr;
2169
2170 _pthread_struct_init(self, &_pthread_attr_default, stackaddr, stacksize,
2171 PTHREAD_ALLOCADDR(stackaddr, stacksize),
2172 PTHREAD_ALLOCSIZE(stackaddr, stacksize));
2173
2174 _pthread_set_kernel_thread(self, kport);
2175 self->wqthread = 1;
2176 self->wqkillset = 0;
2177 self->tl_joinable = false;
2178 self->cancel_state |= _PTHREAD_CANCEL_INITIALIZED;
2179
2180 // Update the running thread count and set childrun bit.
2181 bool thread_tsd_base_set = (bool)(flags & WQ_FLAG_THREAD_TSD_BASE_SET);
2182 _pthread_set_self_internal(self, !thread_tsd_base_set);
2183 __pthread_add_thread(self, false);
2184 __pthread_started_thread(self);
2185 }
2186
2187 PTHREAD_NORETURN PTHREAD_NOINLINE
2188 static void
2189 _pthread_wqthread_exit(pthread_t self)
2190 {
2191 pthread_priority_t pp;
2192 thread_qos_t qos;
2193
2194 pp = (pthread_priority_t)self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS];
2195 qos = _pthread_priority_thread_qos(pp);
2196 if (qos == THREAD_QOS_UNSPECIFIED || qos > WORKQ_THREAD_QOS_CLEANUP) {
2197 // Reset QoS to something low for the cleanup process
2198 pp = _pthread_priority_make_from_thread_qos(WORKQ_THREAD_QOS_CLEANUP, 0, 0);
2199 self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = (void *)pp;
2200 }
2201
2202 _pthread_exit(self, NULL);
2203 }
2204
2205 // workqueue entry point from kernel
2206 void
2207 _pthread_wqthread(pthread_t self, mach_port_t kport, void *stacklowaddr,
2208 void *keventlist, int flags, int nkevents)
2209 {
2210 if ((flags & WQ_FLAG_THREAD_REUSE) == 0) {
2211 _pthread_wqthread_setup(self, kport, stacklowaddr, flags);
2212 }
2213
2214 pthread_priority_t pp;
2215 if (flags & WQ_FLAG_THREAD_OUTSIDEQOS) {
2216 self->wqoutsideqos = 1;
2217 pp = _pthread_priority_make_from_thread_qos(THREAD_QOS_LEGACY, 0,
2218 _PTHREAD_PRIORITY_FALLBACK_FLAG);
2219 } else {
2220 self->wqoutsideqos = 0;
2221 pp = _pthread_wqthread_priority(flags);
2222 }
2223
2224 self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = (void *)pp;
2225
2226 // avoid spills on the stack hard to keep used stack space minimal
2227 if (nkevents == WORKQ_EXIT_THREAD_NKEVENT) {
2228 goto exit;
2229 } else if (flags & WQ_FLAG_THREAD_WORKLOOP) {
2230 self->fun = (void *(*)(void*))__libdispatch_workloopfunction;
2231 self->wq_retop = WQOPS_THREAD_WORKLOOP_RETURN;
2232 self->wq_kqid_ptr = ((kqueue_id_t *)keventlist - 1);
2233 self->arg = keventlist;
2234 self->wq_nevents = nkevents;
2235 } else if (flags & WQ_FLAG_THREAD_KEVENT) {
2236 self->fun = (void *(*)(void*))__libdispatch_keventfunction;
2237 self->wq_retop = WQOPS_THREAD_KEVENT_RETURN;
2238 self->wq_kqid_ptr = NULL;
2239 self->arg = keventlist;
2240 self->wq_nevents = nkevents;
2241 } else {
2242 self->fun = (void *(*)(void*))__libdispatch_workerfunction;
2243 self->wq_retop = WQOPS_THREAD_RETURN;
2244 self->wq_kqid_ptr = NULL;
2245 self->arg = (void *)(uintptr_t)pp;
2246 self->wq_nevents = 0;
2247 if (os_likely(__workq_newapi)) {
2248 (*__libdispatch_workerfunction)(pp);
2249 } else {
2250 _pthread_wqthread_legacy_worker_wrap(pp);
2251 }
2252 goto just_return;
2253 }
2254
2255 if (nkevents > 0) {
2256 kevent_errors_retry:
2257 if (self->wq_retop == WQOPS_THREAD_WORKLOOP_RETURN) {
2258 ((pthread_workqueue_function_workloop_t)self->fun)
2259 (self->wq_kqid_ptr, &self->arg, &self->wq_nevents);
2260 } else {
2261 ((pthread_workqueue_function_kevent_t)self->fun)
2262 (&self->arg, &self->wq_nevents);
2263 }
2264 int rc = __workq_kernreturn(self->wq_retop, self->arg, self->wq_nevents, 0);
2265 if (os_unlikely(rc > 0)) {
2266 self->wq_nevents = rc;
2267 goto kevent_errors_retry;
2268 }
2269 if (os_unlikely(rc < 0)) {
2270 PTHREAD_INTERNAL_CRASH(self->err_no, "kevent (workloop) failed");
2271 }
2272 } else {
2273 just_return:
2274 __workq_kernreturn(self->wq_retop, NULL, 0, 0);
2275 }
2276
2277 exit:
2278 _pthread_wqthread_exit(self);
2279 }
2280
2281
2282 #pragma mark pthread workqueue API for libdispatch
2283
2284
2285 _Static_assert(WORKQ_KEVENT_EVENT_BUFFER_LEN == WQ_KEVENT_LIST_LEN,
2286 "Kernel and userland should agree on the event list size");
2287
2288 void
2289 pthread_workqueue_setdispatchoffset_np(int offset)
2290 {
2291 __libdispatch_offset = offset;
2292 }
2293
2294 static int
2295 pthread_workqueue_setdispatch_with_workloop_np(pthread_workqueue_function2_t queue_func,
2296 pthread_workqueue_function_kevent_t kevent_func,
2297 pthread_workqueue_function_workloop_t workloop_func)
2298 {
2299 int res = EBUSY;
2300 if (__libdispatch_workerfunction == NULL) {
2301 // Check whether the kernel supports new SPIs
2302 res = __workq_kernreturn(WQOPS_QUEUE_NEWSPISUPP, NULL, __libdispatch_offset, kevent_func != NULL ? 0x01 : 0x00);
2303 if (res == -1){
2304 res = ENOTSUP;
2305 } else {
2306 __libdispatch_workerfunction = queue_func;
2307 __libdispatch_keventfunction = kevent_func;
2308 __libdispatch_workloopfunction = workloop_func;
2309
2310 // Prepare the kernel for workq action
2311 (void)__workq_open();
2312 if (__is_threaded == 0) {
2313 __is_threaded = 1;
2314 }
2315 }
2316 }
2317 return res;
2318 }
2319
2320 int
2321 _pthread_workqueue_init_with_workloop(pthread_workqueue_function2_t queue_func,
2322 pthread_workqueue_function_kevent_t kevent_func,
2323 pthread_workqueue_function_workloop_t workloop_func,
2324 int offset, int flags)
2325 {
2326 if (flags != 0) {
2327 return ENOTSUP;
2328 }
2329
2330 __workq_newapi = true;
2331 __libdispatch_offset = offset;
2332
2333 int rv = pthread_workqueue_setdispatch_with_workloop_np(queue_func, kevent_func, workloop_func);
2334 return rv;
2335 }
2336
2337 int
2338 _pthread_workqueue_init_with_kevent(pthread_workqueue_function2_t queue_func,
2339 pthread_workqueue_function_kevent_t kevent_func,
2340 int offset, int flags)
2341 {
2342 return _pthread_workqueue_init_with_workloop(queue_func, kevent_func, NULL, offset, flags);
2343 }
2344
2345 int
2346 _pthread_workqueue_init(pthread_workqueue_function2_t func, int offset, int flags)
2347 {
2348 return _pthread_workqueue_init_with_kevent(func, NULL, offset, flags);
2349 }
2350
2351 int
2352 pthread_workqueue_setdispatch_np(pthread_workqueue_function_t worker_func)
2353 {
2354 return pthread_workqueue_setdispatch_with_workloop_np((pthread_workqueue_function2_t)worker_func, NULL, NULL);
2355 }
2356
2357 int
2358 _pthread_workqueue_supported(void)
2359 {
2360 if (os_unlikely(!__pthread_supported_features)) {
2361 PTHREAD_INTERNAL_CRASH(0, "libpthread has not been initialized");
2362 }
2363
2364 return __pthread_supported_features;
2365 }
2366
2367 int
2368 pthread_workqueue_addthreads_np(int queue_priority, int options, int numthreads)
2369 {
2370 int res = 0;
2371
2372 // Cannot add threads without a worker function registered.
2373 if (__libdispatch_workerfunction == NULL) {
2374 return EPERM;
2375 }
2376
2377 pthread_priority_t kp = 0;
2378 int compat_priority = queue_priority & WQ_FLAG_THREAD_PRIO_MASK;
2379 int flags = 0;
2380
2381 if (options & WORKQ_ADDTHREADS_OPTION_OVERCOMMIT) {
2382 flags = _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
2383 }
2384
2385 #pragma clang diagnostic push
2386 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2387 kp = _pthread_qos_class_encode_workqueue(compat_priority, flags);
2388 #pragma clang diagnostic pop
2389
2390 res = __workq_kernreturn(WQOPS_QUEUE_REQTHREADS, NULL, numthreads, (int)kp);
2391 if (res == -1) {
2392 res = errno;
2393 }
2394 return res;
2395 }
2396
2397 bool
2398 _pthread_workqueue_should_narrow(pthread_priority_t pri)
2399 {
2400 int res = __workq_kernreturn(WQOPS_SHOULD_NARROW, NULL, (int)pri, 0);
2401 if (res == -1) {
2402 return false;
2403 }
2404 return res;
2405 }
2406
2407 int
2408 _pthread_workqueue_addthreads(int numthreads, pthread_priority_t priority)
2409 {
2410 int res = 0;
2411
2412 if (__libdispatch_workerfunction == NULL) {
2413 return EPERM;
2414 }
2415
2416 #if TARGET_OS_OSX
2417 // <rdar://problem/37687655> Legacy simulators fail to boot
2418 //
2419 // Older sims set the deprecated _PTHREAD_PRIORITY_ROOTQUEUE_FLAG wrongly,
2420 // which is aliased to _PTHREAD_PRIORITY_SCHED_PRI_FLAG and that XNU
2421 // validates and rejects.
2422 //
2423 // As a workaround, forcefully unset this bit that cannot be set here
2424 // anyway.
2425 priority &= ~_PTHREAD_PRIORITY_SCHED_PRI_FLAG;
2426 #endif
2427
2428 res = __workq_kernreturn(WQOPS_QUEUE_REQTHREADS, NULL, numthreads, (int)priority);
2429 if (res == -1) {
2430 res = errno;
2431 }
2432 return res;
2433 }
2434
2435 int
2436 _pthread_workqueue_set_event_manager_priority(pthread_priority_t priority)
2437 {
2438 int res = __workq_kernreturn(WQOPS_SET_EVENT_MANAGER_PRIORITY, NULL, (int)priority, 0);
2439 if (res == -1) {
2440 res = errno;
2441 }
2442 return res;
2443 }
2444
2445 int
2446 _pthread_workloop_create(uint64_t workloop_id, uint64_t options, pthread_attr_t *attr)
2447 {
2448 struct kqueue_workloop_params params = {
2449 .kqwlp_version = sizeof(struct kqueue_workloop_params),
2450 .kqwlp_id = workloop_id,
2451 .kqwlp_flags = 0,
2452 };
2453
2454 if (!attr) {
2455 return EINVAL;
2456 }
2457
2458 if (attr->schedset) {
2459 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_SCHED_PRI;
2460 params.kqwlp_sched_pri = attr->param.sched_priority;
2461 }
2462
2463 if (attr->policyset) {
2464 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_SCHED_POL;
2465 params.kqwlp_sched_pol = attr->policy;
2466 }
2467
2468 if (attr->cpupercentset) {
2469 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_CPU_PERCENT;
2470 params.kqwlp_cpu_percent = attr->cpupercent;
2471 params.kqwlp_cpu_refillms = attr->refillms;
2472 }
2473
2474 int res = __kqueue_workloop_ctl(KQ_WORKLOOP_CREATE, 0, &params,
2475 sizeof(params));
2476 if (res == -1) {
2477 res = errno;
2478 }
2479 return res;
2480 }
2481
2482 int
2483 _pthread_workloop_destroy(uint64_t workloop_id)
2484 {
2485 struct kqueue_workloop_params params = {
2486 .kqwlp_version = sizeof(struct kqueue_workloop_params),
2487 .kqwlp_id = workloop_id,
2488 };
2489
2490 int res = __kqueue_workloop_ctl(KQ_WORKLOOP_DESTROY, 0, &params,
2491 sizeof(params));
2492 if (res == -1) {
2493 res = errno;
2494 }
2495 return res;
2496 }
2497
2498
2499 #pragma mark Introspection SPI for libpthread.
2500
2501
2502 static pthread_introspection_hook_t _pthread_introspection_hook;
2503
2504 pthread_introspection_hook_t
2505 pthread_introspection_hook_install(pthread_introspection_hook_t hook)
2506 {
2507 pthread_introspection_hook_t prev;
2508 prev = _pthread_atomic_xchg_ptr((void**)&_pthread_introspection_hook, hook);
2509 return prev;
2510 }
2511
2512 PTHREAD_NOINLINE
2513 static void
2514 _pthread_introspection_hook_callout_thread_create(pthread_t t)
2515 {
2516 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_CREATE, t, t,
2517 PTHREAD_SIZE);
2518 }
2519
2520 static inline void
2521 _pthread_introspection_thread_create(pthread_t t)
2522 {
2523 if (os_fastpath(!_pthread_introspection_hook)) return;
2524 _pthread_introspection_hook_callout_thread_create(t);
2525 }
2526
2527 PTHREAD_NOINLINE
2528 static void
2529 _pthread_introspection_hook_callout_thread_start(pthread_t t)
2530 {
2531 size_t freesize;
2532 void *freeaddr;
2533 if (t == main_thread()) {
2534 size_t stacksize = t->stackaddr - t->stackbottom;
2535 freesize = stacksize + t->guardsize;
2536 freeaddr = t->stackaddr - freesize;
2537 } else {
2538 freesize = t->freesize - PTHREAD_SIZE;
2539 freeaddr = t->freeaddr;
2540 }
2541 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_START, t,
2542 freeaddr, freesize);
2543 }
2544
2545 static inline void
2546 _pthread_introspection_thread_start(pthread_t t)
2547 {
2548 if (os_fastpath(!_pthread_introspection_hook)) return;
2549 _pthread_introspection_hook_callout_thread_start(t);
2550 }
2551
2552 PTHREAD_NOINLINE
2553 static void
2554 _pthread_introspection_hook_callout_thread_terminate(pthread_t t)
2555 {
2556 size_t freesize;
2557 void *freeaddr;
2558 if (t == main_thread()) {
2559 size_t stacksize = t->stackaddr - t->stackbottom;
2560 freesize = stacksize + t->guardsize;
2561 freeaddr = t->stackaddr - freesize;
2562 } else {
2563 freesize = t->freesize - PTHREAD_SIZE;
2564 freeaddr = t->freeaddr;
2565 }
2566 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_TERMINATE, t,
2567 freeaddr, freesize);
2568 }
2569
2570 static inline void
2571 _pthread_introspection_thread_terminate(pthread_t t)
2572 {
2573 if (os_fastpath(!_pthread_introspection_hook)) return;
2574 _pthread_introspection_hook_callout_thread_terminate(t);
2575 }
2576
2577 PTHREAD_NOINLINE
2578 static void
2579 _pthread_introspection_hook_callout_thread_destroy(pthread_t t)
2580 {
2581 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_DESTROY, t, t,
2582 PTHREAD_SIZE);
2583 }
2584
2585 static inline void
2586 _pthread_introspection_thread_destroy(pthread_t t)
2587 {
2588 if (os_fastpath(!_pthread_introspection_hook)) return;
2589 _pthread_introspection_hook_callout_thread_destroy(t);
2590 }
2591
2592 #pragma mark libplatform shims
2593
2594 #include <platform/string.h>
2595
2596 // pthread_setup initializes large structures to 0,
2597 // which the compiler turns into a library call to memset.
2598 //
2599 // To avoid linking against Libc, provide a simple wrapper
2600 // that calls through to the libplatform primitives
2601
2602 #undef memset
2603 PTHREAD_NOEXPORT
2604 void *
2605 memset(void *b, int c, size_t len)
2606 {
2607 return _platform_memset(b, c, len);
2608 }
2609
2610 #undef bzero
2611 PTHREAD_NOEXPORT
2612 void
2613 bzero(void *s, size_t n)
2614 {
2615 _platform_bzero(s, n);
2616 }
2617
2618 #undef memcpy
2619 PTHREAD_NOEXPORT
2620 void *
2621 memcpy(void* a, const void* b, unsigned long s)
2622 {
2623 return _platform_memmove(a, b, s);
2624 }
2625