2 * Copyright (c) 2000-2013 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 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
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.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
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.
49 * POSIX Pthread Library
54 #include "workqueue_private.h"
55 #include "introspection_private.h"
56 #include "qos_private.h"
62 #include <mach/mach_init.h>
63 #include <mach/mach_vm.h>
65 #include <sys/resource.h>
66 #include <sys/sysctl.h>
67 #include <sys/queue.h>
69 #include <machine/vmparam.h>
70 #define __APPLE_API_PRIVATE
71 #include <machine/cpu_capabilities.h>
72 #include <libkern/OSAtomic.h>
75 #include <platform/string.h>
76 #include <platform/compat.h>
78 extern int __sysctl(int *name
, u_int namelen
, void *oldp
, size_t *oldlenp
,
79 void *newp
, size_t newlen
);
80 extern void __exit(int) __attribute__((noreturn
));
82 static void (*exitf
)(int) = __exit
;
83 __private_extern__
void* (*_pthread_malloc
)(size_t) = NULL
;
84 __private_extern__
void (*_pthread_free
)(void *) = NULL
;
90 // This global should be used (carefully) by anyone needing to know if a
91 // pthread (other than the main thread) has been created.
92 int __is_threaded
= 0;
94 int __unix_conforming
= 0;
96 // _pthread_list_lock protects _pthread_count, access to the __pthread_head
97 // list, and the parentcheck, childrun and childexit flags of the pthread
98 // structure. Externally imported by pthread_cancelable.c.
99 __private_extern__ pthread_lock_t _pthread_list_lock
= LOCK_INITIALIZER
;
100 __private_extern__
struct __pthread_list __pthread_head
= TAILQ_HEAD_INITIALIZER(__pthread_head
);
101 static int _pthread_count
= 1;
103 #if PTHREAD_LAYOUT_SPI
105 const struct pthread_layout_offsets_s pthread_layout_offsets
= {
107 .plo_pthread_tsd_base_offset
= offsetof(struct _pthread
, tsd
),
108 .plo_pthread_tsd_base_address_offset
= 0,
109 .plo_pthread_tsd_entry_size
= sizeof(((struct _pthread
*)NULL
)->tsd
[0]),
112 #endif // PTHREAD_LAYOUT_SPI
118 // Mach message notification that a thread needs to be recycled.
119 typedef struct _pthread_reap_msg_t
{
120 mach_msg_header_t header
;
122 mach_msg_trailer_t trailer
;
123 } pthread_reap_msg_t
;
125 #define pthreadsize ((size_t)mach_vm_round_page(sizeof(struct _pthread)))
126 static pthread_attr_t _pthread_attr_default
= {0};
127 static struct _pthread _thread
= {0};
129 static int default_priority
;
130 static int max_priority
;
131 static int min_priority
;
132 static int pthread_concurrency
;
134 // work queue support data
135 static void (*__libdispatch_workerfunction
)(pthread_priority_t
) = NULL
;
136 static int __libdispatch_offset
;
138 // supported feature set
139 int __pthread_supported_features
;
142 // Function prototypes
145 // pthread primitives
146 static int _pthread_allocate(pthread_t
*thread
, const pthread_attr_t
*attrs
, void **stack
);
147 static int _pthread_deallocate(pthread_t t
);
149 static void _pthread_terminate(pthread_t t
);
151 static void _pthread_struct_init(pthread_t t
,
152 const pthread_attr_t
*attrs
,
157 extern void _pthread_set_self(pthread_t
);
159 static void _pthread_dealloc_reply_port(pthread_t t
);
161 static inline void __pthread_add_thread(pthread_t t
, bool parent
);
162 static inline int __pthread_remove_thread(pthread_t t
, bool child
, bool *should_exit
);
164 static int _pthread_find_thread(pthread_t thread
);
166 static void _pthread_exit(pthread_t self
, void *value_ptr
) __dead2
;
167 static void _pthread_setcancelstate_exit(pthread_t self
, void *value_ptr
, int conforming
);
169 static inline void _pthread_introspection_thread_create(pthread_t t
, bool destroy
);
170 static inline void _pthread_introspection_thread_start(pthread_t t
);
171 static inline void _pthread_introspection_thread_terminate(pthread_t t
, void *freeaddr
, size_t freesize
, bool destroy
);
172 static inline void _pthread_introspection_thread_destroy(pthread_t t
);
174 extern void start_wqthread(pthread_t self
, mach_port_t kport
, void *stackaddr
, void *unused
, int reuse
);
175 extern void thread_start(pthread_t self
, mach_port_t kport
, void *(*fun
)(void *), void * funarg
, size_t stacksize
, unsigned int flags
);
177 void pthread_workqueue_atfork_child(void);
179 static bool __workq_newapi
;
181 /* Compatibility: previous pthread API used WORKQUEUE_OVERCOMMIT to request overcommit threads from
182 * the kernel. This definition is kept here, in userspace only, to perform the compatibility shimm
183 * from old API requests to the new kext conventions.
185 #define WORKQUEUE_OVERCOMMIT 0x10000
188 * Flags filed passed to bsdthread_create and back in pthread_start
189 31 <---------------------------------> 0
190 _________________________________________
191 | flags(8) | policy(8) | importance(16) |
192 -----------------------------------------
195 #define PTHREAD_START_CUSTOM 0x01000000
196 #define PTHREAD_START_SETSCHED 0x02000000
197 #define PTHREAD_START_DETACHED 0x04000000
198 #define PTHREAD_START_QOSCLASS 0x08000000
199 #define PTHREAD_START_QOSCLASS_MASK 0xffffff
200 #define PTHREAD_START_POLICY_BITSHIFT 16
201 #define PTHREAD_START_POLICY_MASK 0xff
202 #define PTHREAD_START_IMPORTANCE_MASK 0xffff
204 static int pthread_setschedparam_internal(pthread_t
, mach_port_t
, int, const struct sched_param
*);
205 extern pthread_t
__bsdthread_create(void *(*func
)(void *), void * func_arg
, void * stack
, pthread_t thread
, unsigned int flags
);
206 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
);
207 extern int __bsdthread_terminate(void * freeaddr
, size_t freesize
, mach_port_t kport
, mach_port_t joinsem
);
208 extern __uint64_t
__thread_selfid( void );
209 extern int __pthread_canceled(int);
210 extern int __pthread_kill(mach_port_t
, int);
212 extern int __workq_open(void);
213 extern int __workq_kernreturn(int, void *, int, int);
215 #if defined(__i386__) || defined(__x86_64__)
216 static const mach_vm_address_t PTHREAD_STACK_HINT
= 0xB0000000;
218 #error no PTHREAD_STACK_HINT for this architecture
222 // Check for regression of <rdar://problem/13249323>
223 struct rdar_13249323_regression_static_assert
{ unsigned a
[offsetof(struct _pthread
, err_no
) == 68 ? 1 : -1]; };
226 // Allocate a thread structure, stack and guard page.
228 // The thread structure may optionally be placed in the same allocation as the
229 // stack, residing above the top of the stack. This cannot be done if a
230 // custom stack address is provided.
232 // Similarly the guard page cannot be allocated if a custom stack address is
235 // The allocated thread structure is initialized with values that indicate how
236 // it should be freed.
239 _pthread_allocate(pthread_t
*thread
, const pthread_attr_t
*attrs
, void **stack
)
244 mach_vm_address_t allocaddr
= PTHREAD_STACK_HINT
;
245 size_t allocsize
= 0;
246 size_t guardsize
= 0;
247 size_t stacksize
= 0;
249 PTHREAD_ASSERT(attrs
->stacksize
>= PTHREAD_STACK_MIN
);
254 // Allocate a pthread structure if necessary
256 if (attrs
->stackaddr
!= NULL
) {
257 PTHREAD_ASSERT(((uintptr_t)attrs
->stackaddr
% vm_page_size
) == 0);
258 *stack
= attrs
->stackaddr
;
259 allocsize
= pthreadsize
;
261 guardsize
= attrs
->guardsize
;
262 stacksize
= attrs
->stacksize
;
263 allocsize
= stacksize
+ guardsize
+ pthreadsize
;
266 kr
= mach_vm_map(mach_task_self(),
270 VM_MAKE_TAG(VM_MEMORY_STACK
)| VM_FLAGS_ANYWHERE
,
278 if (kr
!= KERN_SUCCESS
) {
279 kr
= mach_vm_allocate(mach_task_self(),
282 VM_MAKE_TAG(VM_MEMORY_STACK
)| VM_FLAGS_ANYWHERE
);
285 if (kr
== KERN_SUCCESS
) {
286 // The stack grows down.
287 // Set the guard page at the lowest address of the
288 // newly allocated stack. Return the highest address
291 (void)mach_vm_protect(mach_task_self(), allocaddr
, guardsize
, FALSE
, VM_PROT_NONE
);
294 // Thread structure resides at the top of the stack.
295 t
= (void *)(allocaddr
+ stacksize
+ guardsize
);
297 // Returns the top of the stack.
303 _pthread_struct_init(t
, attrs
, *stack
, 0, 0);
304 t
->freeaddr
= (void *)allocaddr
;
305 t
->freesize
= allocsize
;
315 _pthread_deallocate(pthread_t t
)
317 // Don't free the main thread.
319 (void)mach_vm_deallocate(mach_task_self(), t
->freeaddr
, t
->freesize
);
324 // Terminates the thread if called from the currently running thread.
327 _pthread_terminate(pthread_t t
)
329 PTHREAD_ASSERT(t
== pthread_self());
331 uintptr_t freeaddr
= (uintptr_t)t
->freeaddr
;
332 size_t freesize
= t
->freesize
- pthreadsize
;
334 mach_port_t kport
= _pthread_kernel_thread(t
);
335 semaphore_t joinsem
= t
->joiner_notify
;
337 _pthread_dealloc_reply_port(t
);
339 // Shrink the pthread_t so that it does not include the stack
340 // so that we're always responsible for deallocating the stack.
341 t
->freeaddr
+= freesize
;
342 t
->freesize
= pthreadsize
;
344 // After the call to __pthread_remove_thread, it is only safe to
345 // dereference the pthread_t structure if EBUSY has been returned.
347 bool destroy
, should_exit
;
348 destroy
= (__pthread_remove_thread(t
, true, &should_exit
) != EBUSY
);
351 // Don't free the main thread.
353 } else if (destroy
) {
354 // We were told not to keep the pthread_t structure around, so
355 // instead of just deallocating the stack, we should deallocate
356 // the entire structure.
357 freesize
+= pthreadsize
;
362 _pthread_introspection_thread_terminate(t
, freeaddr
, freesize
, destroy
);
367 __bsdthread_terminate((void *)freeaddr
, freesize
, kport
, joinsem
);
368 PTHREAD_ABORT("thread %p didn't terminate", t
);
372 pthread_attr_destroy(pthread_attr_t
*attr
)
375 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
383 pthread_attr_getdetachstate(const pthread_attr_t
*attr
, int *detachstate
)
386 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
387 *detachstate
= attr
->detached
;
394 pthread_attr_getinheritsched(const pthread_attr_t
*attr
, int *inheritsched
)
397 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
398 *inheritsched
= attr
->inherit
;
405 pthread_attr_getschedparam(const pthread_attr_t
*attr
, struct sched_param
*param
)
408 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
409 *param
= attr
->param
;
416 pthread_attr_getschedpolicy(const pthread_attr_t
*attr
, int *policy
)
419 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
420 *policy
= attr
->policy
;
426 // Default stack size is 512KB; independent of the main thread's stack size.
427 static const size_t DEFAULT_STACK_SIZE
= 512 * 1024;
430 pthread_attr_init(pthread_attr_t
*attr
)
432 attr
->stacksize
= DEFAULT_STACK_SIZE
;
433 attr
->stackaddr
= NULL
;
434 attr
->sig
= _PTHREAD_ATTR_SIG
;
435 attr
->param
.sched_priority
= default_priority
;
436 attr
->param
.quantum
= 10; /* quantum isn't public yet */
437 attr
->detached
= PTHREAD_CREATE_JOINABLE
;
438 attr
->inherit
= _PTHREAD_DEFAULT_INHERITSCHED
;
439 attr
->policy
= _PTHREAD_DEFAULT_POLICY
;
442 attr
->guardsize
= vm_page_size
;
443 attr
->qosclass
= _pthread_priority_make_newest(QOS_CLASS_DEFAULT
, 0, 0);
448 pthread_attr_setdetachstate(pthread_attr_t
*attr
, int detachstate
)
451 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
452 (detachstate
== PTHREAD_CREATE_JOINABLE
||
453 detachstate
== PTHREAD_CREATE_DETACHED
)) {
454 attr
->detached
= detachstate
;
461 pthread_attr_setinheritsched(pthread_attr_t
*attr
, int inheritsched
)
464 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
465 (inheritsched
== PTHREAD_INHERIT_SCHED
||
466 inheritsched
== PTHREAD_EXPLICIT_SCHED
)) {
467 attr
->inherit
= inheritsched
;
474 pthread_attr_setschedparam(pthread_attr_t
*attr
, const struct sched_param
*param
)
477 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
478 /* TODO: Validate sched_param fields */
479 attr
->param
= *param
;
487 pthread_attr_setschedpolicy(pthread_attr_t
*attr
, int policy
)
490 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
491 (policy
== SCHED_OTHER
||
492 policy
== SCHED_RR
||
493 policy
== SCHED_FIFO
)) {
494 attr
->policy
= policy
;
502 pthread_attr_setscope(pthread_attr_t
*attr
, int scope
)
505 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
506 if (scope
== PTHREAD_SCOPE_SYSTEM
) {
507 // No attribute yet for the scope.
509 } else if (scope
== PTHREAD_SCOPE_PROCESS
) {
517 pthread_attr_getscope(const pthread_attr_t
*attr
, int *scope
)
520 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
521 *scope
= PTHREAD_SCOPE_SYSTEM
;
528 pthread_attr_getstackaddr(const pthread_attr_t
*attr
, void **stackaddr
)
531 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
532 *stackaddr
= attr
->stackaddr
;
539 pthread_attr_setstackaddr(pthread_attr_t
*attr
, void *stackaddr
)
542 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
543 ((uintptr_t)stackaddr
% vm_page_size
) == 0) {
544 attr
->stackaddr
= stackaddr
;
553 pthread_attr_getstacksize(const pthread_attr_t
*attr
, size_t *stacksize
)
556 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
557 *stacksize
= attr
->stacksize
;
564 pthread_attr_setstacksize(pthread_attr_t
*attr
, size_t stacksize
)
567 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
568 (stacksize
% vm_page_size
) == 0 &&
569 stacksize
>= PTHREAD_STACK_MIN
) {
570 attr
->stacksize
= stacksize
;
577 pthread_attr_getstack(const pthread_attr_t
*attr
, void **stackaddr
, size_t * stacksize
)
580 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
581 *stackaddr
= (void *)((uintptr_t)attr
->stackaddr
- attr
->stacksize
);
582 *stacksize
= attr
->stacksize
;
588 // Per SUSv3, the stackaddr is the base address, the lowest addressable byte
589 // address. This is not the same as in pthread_attr_setstackaddr.
591 pthread_attr_setstack(pthread_attr_t
*attr
, void *stackaddr
, size_t stacksize
)
594 if (attr
->sig
== _PTHREAD_ATTR_SIG
&&
595 ((uintptr_t)stackaddr
% vm_page_size
) == 0 &&
596 (stacksize
% vm_page_size
) == 0 &&
597 stacksize
>= PTHREAD_STACK_MIN
) {
598 attr
->stackaddr
= (void *)((uintptr_t)stackaddr
+ stacksize
);
599 attr
->stacksize
= stacksize
;
607 pthread_attr_setguardsize(pthread_attr_t
*attr
, size_t guardsize
)
610 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
611 /* Guardsize of 0 is valid, ot means no guard */
612 if ((guardsize
% vm_page_size
) == 0) {
613 attr
->guardsize
= guardsize
;
622 pthread_attr_getguardsize(const pthread_attr_t
*attr
, size_t *guardsize
)
625 if (attr
->sig
== _PTHREAD_ATTR_SIG
) {
626 *guardsize
= attr
->guardsize
;
634 * Create and start execution of a new thread.
638 _pthread_body(pthread_t self
)
640 _pthread_set_self(self
);
641 __pthread_add_thread(self
, false);
642 _pthread_exit(self
, (self
->fun
)(self
->arg
));
646 _pthread_start(pthread_t self
, mach_port_t kport
, void *(*fun
)(void *), void *arg
, size_t stacksize
, unsigned int pflags
)
648 if ((pflags
& PTHREAD_START_CUSTOM
) == 0) {
649 void *stackaddr
= self
;
650 _pthread_struct_init(self
, &_pthread_attr_default
, stackaddr
, stacksize
, 1);
652 if (pflags
& PTHREAD_START_SETSCHED
) {
653 self
->policy
= ((pflags
>> PTHREAD_START_POLICY_BITSHIFT
) & PTHREAD_START_POLICY_MASK
);
654 self
->param
.sched_priority
= (pflags
& PTHREAD_START_IMPORTANCE_MASK
);
657 if ((pflags
& PTHREAD_START_DETACHED
) == PTHREAD_START_DETACHED
) {
658 self
->detached
&= ~PTHREAD_CREATE_JOINABLE
;
659 self
->detached
|= PTHREAD_CREATE_DETACHED
;
663 if ((pflags
& PTHREAD_START_QOSCLASS
) != 0) {
664 /* The QoS class is cached in the TSD of the pthread, so to reflect the
665 * class that the kernel brought us up at, the TSD must be primed from the
668 self
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
] = (pflags
& PTHREAD_START_QOSCLASS_MASK
);
670 /* Give the thread a default QoS tier, of zero. */
671 self
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
] = _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED
, 0, 0);
674 _pthread_set_kernel_thread(self
, kport
);
682 _pthread_struct_init(pthread_t t
,
683 const pthread_attr_t
*attrs
,
688 t
->sig
= _PTHREAD_SIG
;
689 t
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_SELF
] = t
;
690 t
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
] = _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED
, 0, 0);
692 t
->kernalloc
= kernalloc
;
693 if (kernalloc
!= 0) {
694 uintptr_t stackaddr
= (uintptr_t)t
;
695 t
->stacksize
= stacksize
;
696 t
->stackaddr
= (void *)stackaddr
;
697 t
->freeaddr
= (void *)(uintptr_t)(stackaddr
- stacksize
- vm_page_size
);
698 t
->freesize
= pthreadsize
+ stacksize
+ vm_page_size
;
700 t
->stacksize
= attrs
->stacksize
;
701 t
->stackaddr
= (void *)stack
;
703 t
->guardsize
= attrs
->guardsize
;
704 t
->detached
= attrs
->detached
;
705 t
->inherit
= attrs
->inherit
;
706 t
->policy
= attrs
->policy
;
707 t
->schedset
= attrs
->schedset
;
708 t
->param
= attrs
->param
;
709 t
->cancel_state
= PTHREAD_CANCEL_ENABLE
| PTHREAD_CANCEL_DEFERRED
;
712 /* Need to deprecate this in future */
714 _pthread_is_threaded(void)
716 return __is_threaded
;
719 /* Non portable public api to know whether this process has(had) atleast one thread
720 * apart from main thread. There could be race if there is a thread in the process of
721 * creation at the time of call . It does not tell whether there are more than one thread
722 * at this point of time.
725 pthread_is_threaded_np(void)
727 return __is_threaded
;
731 pthread_mach_thread_np(pthread_t t
)
733 mach_port_t kport
= MACH_PORT_NULL
;
735 if (t
== pthread_self()) {
737 * If the call is on self, return the kernel port. We cannot
738 * add this bypass for main thread as it might have exited,
739 * and we should not return stale port info.
741 kport
= _pthread_kernel_thread(t
);
743 (void)_pthread_lookup_thread(t
, &kport
, 0);
750 pthread_from_mach_thread_np(mach_port_t kernel_thread
)
752 struct _pthread
*p
= NULL
;
754 /* No need to wait as mach port is already known */
755 LOCK(_pthread_list_lock
);
757 TAILQ_FOREACH(p
, &__pthread_head
, plist
) {
758 if (_pthread_kernel_thread(p
) == kernel_thread
) {
763 UNLOCK(_pthread_list_lock
);
769 pthread_get_stacksize_np(pthread_t t
)
775 return ESRCH
; // XXX bug?
778 // since the main thread will not get de-allocated from underneath us
779 if (t
== pthread_self() || t
== &_thread
) {
783 LOCK(_pthread_list_lock
);
785 ret
= _pthread_find_thread(t
);
789 size
= ret
; // XXX bug?
792 UNLOCK(_pthread_list_lock
);
798 pthread_get_stackaddr_np(pthread_t t
)
804 return (void *)(uintptr_t)ESRCH
; // XXX bug?
807 // since the main thread will not get de-allocated from underneath us
808 if (t
== pthread_self() || t
== &_thread
) {
812 LOCK(_pthread_list_lock
);
814 ret
= _pthread_find_thread(t
);
818 addr
= (void *)(uintptr_t)ret
; // XXX bug?
821 UNLOCK(_pthread_list_lock
);
827 _pthread_reply_port(pthread_t t
)
831 p
= _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY
);
833 p
= t
->tsd
[_PTHREAD_TSD_SLOT_MIG_REPLY
];
835 return (mach_port_t
)(uintptr_t)p
;
839 _pthread_set_reply_port(pthread_t t
, mach_port_t reply_port
)
841 void *p
= (void *)(uintptr_t)reply_port
;
843 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY
, p
);
845 t
->tsd
[_PTHREAD_TSD_SLOT_MIG_REPLY
] = p
;
850 _pthread_dealloc_reply_port(pthread_t t
)
852 mach_port_t reply_port
= _pthread_reply_port(t
);
853 if (reply_port
!= MACH_PORT_NULL
) {
854 mig_dealloc_reply_port(reply_port
);
859 pthread_main_thread_np(void)
864 /* returns non-zero if the current thread is the main thread */
866 pthread_main_np(void)
868 pthread_t self
= pthread_self();
870 return ((self
->detached
& _PTHREAD_CREATE_PARENT
) == _PTHREAD_CREATE_PARENT
);
874 /* if we are passed in a pthread_t that is NULL, then we return
875 the current thread's thread_id. So folks don't have to call
876 pthread_self, in addition to us doing it, if they just want
880 pthread_threadid_np(pthread_t thread
, uint64_t *thread_id
)
883 pthread_t self
= pthread_self();
885 if (thread_id
== NULL
) {
889 if (thread
== NULL
|| thread
== self
) {
890 *thread_id
= self
->thread_id
;
892 LOCK(_pthread_list_lock
);
893 res
= _pthread_find_thread(thread
);
895 *thread_id
= thread
->thread_id
;
897 UNLOCK(_pthread_list_lock
);
903 pthread_getname_np(pthread_t thread
, char *threadname
, size_t len
)
907 if (thread
== NULL
) {
911 LOCK(_pthread_list_lock
);
912 res
= _pthread_find_thread(thread
);
914 strlcpy(threadname
, thread
->pthread_name
, len
);
916 UNLOCK(_pthread_list_lock
);
921 pthread_setname_np(const char *name
)
924 pthread_t self
= pthread_self();
931 /* protytype is in pthread_internals.h */
932 res
= __proc_info(5, getpid(), 2, (uint64_t)0, (void*)name
, (int)len
);
935 strlcpy(self
->pthread_name
, name
, MAXTHREADNAMESIZE
);
937 bzero(self
->pthread_name
, MAXTHREADNAMESIZE
);
944 PTHREAD_ALWAYS_INLINE
946 __pthread_add_thread(pthread_t t
, bool parent
)
948 bool should_deallocate
= false;
949 bool should_add
= true;
951 LOCK(_pthread_list_lock
);
953 // The parent and child threads race to add the thread to the list.
954 // When called by the parent:
955 // - set parentcheck to true
956 // - back off if childrun is true
957 // When called by the child:
958 // - set childrun to true
959 // - back off if parentcheck is true
963 // child got here first, don't add.
967 // If the child exits before we check in then it has to keep
968 // the thread structure memory alive so our dereferences above
969 // are valid. If it's a detached thread, then no joiner will
970 // deallocate the thread structure itself. So we do it here.
973 should_deallocate
= ((t
->detached
& PTHREAD_CREATE_DETACHED
) == PTHREAD_CREATE_DETACHED
);
977 if (t
->parentcheck
) {
978 // Parent got here first, don't add.
982 // Work queue threads have no parent. Simulate.
988 TAILQ_INSERT_TAIL(&__pthread_head
, t
, plist
);
992 UNLOCK(_pthread_list_lock
);
995 _pthread_introspection_thread_create(t
, should_deallocate
);
996 if (should_deallocate
) {
997 _pthread_deallocate(t
);
1000 _pthread_introspection_thread_start(t
);
1004 // <rdar://problem/12544957> must always inline this function to avoid epilogues
1005 // Returns EBUSY if the thread structure should be kept alive (is joinable).
1006 // Returns ESRCH if the thread structure is no longer valid (was detached).
1007 PTHREAD_ALWAYS_INLINE
1009 __pthread_remove_thread(pthread_t t
, bool child
, bool *should_exit
)
1013 bool should_remove
= true;
1015 LOCK(_pthread_list_lock
);
1017 // When a thread removes itself:
1018 // - Set the childexit flag indicating that the thread has exited.
1019 // - Return false if parentcheck is zero (must keep structure)
1020 // - If the thread is joinable, keep it on the list so that
1021 // the join operation succeeds. Still decrement the running
1022 // thread count so that we exit if no threads are running.
1023 // - Update the running thread count.
1024 // When another thread removes a joinable thread:
1025 // - CAREFUL not to dereference the thread before verifying that the
1026 // reference is still valid using _pthread_find_thread().
1027 // - Remove the thread from the list.
1031 if (t
->parentcheck
== 0) {
1034 if ((t
->detached
& PTHREAD_CREATE_JOINABLE
) != 0) {
1036 should_remove
= false;
1038 *should_exit
= (--_pthread_count
<= 0);
1040 ret
= _pthread_find_thread(t
);
1042 // If we found a thread but it's not joinable, bail.
1043 if ((t
->detached
& PTHREAD_CREATE_JOINABLE
) == 0) {
1044 should_remove
= false;
1049 if (should_remove
) {
1050 TAILQ_REMOVE(&__pthread_head
, t
, plist
);
1053 UNLOCK(_pthread_list_lock
);
1059 pthread_create(pthread_t
*thread
,
1060 const pthread_attr_t
*attr
,
1061 void *(*start_routine
)(void *),
1065 unsigned int flags
= 0;
1067 pthread_attr_t
*attrs
= (pthread_attr_t
*)attr
;
1068 if (attrs
== NULL
) {
1069 attrs
= &_pthread_attr_default
;
1070 } else if (attrs
->sig
!= _PTHREAD_ATTR_SIG
) {
1074 if (attrs
->detached
== PTHREAD_CREATE_DETACHED
) {
1075 flags
|= PTHREAD_START_DETACHED
;
1078 if (attrs
->schedset
!= 0) {
1079 flags
|= PTHREAD_START_SETSCHED
;
1080 flags
|= ((attrs
->policy
& PTHREAD_START_POLICY_MASK
) << PTHREAD_START_POLICY_BITSHIFT
);
1081 flags
|= (attrs
->param
.sched_priority
& PTHREAD_START_IMPORTANCE_MASK
);
1082 } else if (attrs
->qosclass
!= 0) {
1083 flags
|= PTHREAD_START_QOSCLASS
;
1084 flags
|= (attrs
->qosclass
& PTHREAD_START_QOSCLASS_MASK
);
1091 if (attrs
->fastpath
) {
1092 // kernel will allocate thread and stack, pass stacksize.
1093 stack
= (void *)attrs
->stacksize
;
1095 // allocate the thread and its stack
1096 flags
|= PTHREAD_START_CUSTOM
;
1099 res
= _pthread_allocate(&t
, attrs
, &stack
);
1105 t
->fun
= start_routine
;
1109 t2
= __bsdthread_create(start_routine
, arg
, stack
, t
, flags
);
1110 if (t2
== (pthread_t
)-1) {
1111 if (flags
& PTHREAD_START_CUSTOM
) {
1112 // free the thread and stack if we allocated it
1113 _pthread_deallocate(t
);
1121 __pthread_add_thread(t
, true);
1123 // XXX if a thread is created detached and exits, t will be invalid
1129 pthread_create_suspended_np(pthread_t
*thread
,
1130 const pthread_attr_t
*attr
,
1131 void *(*start_routine
)(void *),
1136 mach_port_t kernel_thread
= MACH_PORT_NULL
;
1138 const pthread_attr_t
*attrs
= attr
;
1139 if (attrs
== NULL
) {
1140 attrs
= &_pthread_attr_default
;
1141 } else if (attrs
->sig
!= _PTHREAD_ATTR_SIG
) {
1146 res
= _pthread_allocate(&t
, attrs
, &stack
);
1154 kr
= thread_create(mach_task_self(), &kernel_thread
);
1155 if (kr
!= KERN_SUCCESS
) {
1156 //PTHREAD_ABORT("thread_create() failed: %d", kern_res);
1157 return EINVAL
; /* Need better error here? */
1160 _pthread_set_kernel_thread(t
, kernel_thread
);
1161 (void)pthread_setschedparam_internal(t
, kernel_thread
, t
->policy
, &t
->param
);
1166 t
->fun
= start_routine
;
1168 __pthread_add_thread(t
, true);
1170 // Set up a suspended thread.
1171 _pthread_setup(t
, _pthread_body
, stack
, 1, 0);
1176 pthread_detach(pthread_t thread
)
1180 semaphore_t sema
= SEMAPHORE_NULL
;
1182 res
= _pthread_lookup_thread(thread
, NULL
, 1);
1184 return res
; // Not a valid thread to detach.
1188 if (thread
->detached
& PTHREAD_CREATE_JOINABLE
) {
1189 if (thread
->detached
& _PTHREAD_EXITED
) {
1190 // Join the thread if it's already exited.
1193 thread
->detached
&= ~PTHREAD_CREATE_JOINABLE
;
1194 thread
->detached
|= PTHREAD_CREATE_DETACHED
;
1195 sema
= thread
->joiner_notify
;
1200 UNLOCK(thread
->lock
);
1203 pthread_join(thread
, NULL
);
1205 semaphore_signal(sema
);
1212 pthread_kill(pthread_t th
, int sig
)
1214 if (sig
< 0 || sig
> NSIG
) {
1218 mach_port_t kport
= MACH_PORT_NULL
;
1219 if (_pthread_lookup_thread(th
, &kport
, 0) != 0) {
1220 return ESRCH
; // Not a valid thread.
1223 // Don't signal workqueue threads.
1224 if (th
->wqthread
!= 0 && th
->wqkillset
== 0) {
1228 int ret
= __pthread_kill(kport
, sig
);
1237 __pthread_workqueue_setkill(int enable
)
1239 pthread_t self
= pthread_self();
1242 self
->wqkillset
= enable
? 1 : 0;
1249 __pthread_get_exit_value(pthread_t t
, int conforming
)
1251 const int flags
= (PTHREAD_CANCEL_ENABLE
|_PTHREAD_CANCEL_PENDING
);
1252 void *value
= t
->exit_value
;
1254 if ((t
->cancel_state
& flags
) == flags
) {
1255 value
= PTHREAD_CANCELED
;
1261 /* For compatibility... */
1264 _pthread_self(void) {
1265 return pthread_self();
1269 * Terminate a thread.
1271 int __disable_threadsignal(int);
1275 _pthread_exit(pthread_t self
, void *value_ptr
)
1277 struct __darwin_pthread_handler_rec
*handler
;
1279 // Disable signal delivery while we clean up
1280 __disable_threadsignal(1);
1282 // Set cancel state to disable and type to deferred
1283 _pthread_setcancelstate_exit(self
, value_ptr
, __unix_conforming
);
1285 while ((handler
= self
->__cleanup_stack
) != 0) {
1286 (handler
->__routine
)(handler
->__arg
);
1287 self
->__cleanup_stack
= handler
->__next
;
1289 _pthread_tsd_cleanup(self
);
1292 self
->detached
|= _PTHREAD_EXITED
;
1293 self
->exit_value
= value_ptr
;
1295 if ((self
->detached
& PTHREAD_CREATE_JOINABLE
) &&
1296 self
->joiner_notify
== SEMAPHORE_NULL
) {
1297 self
->joiner_notify
= (semaphore_t
)os_get_cached_semaphore();
1301 // Clear per-thread semaphore cache
1302 os_put_cached_semaphore(SEMAPHORE_NULL
);
1304 _pthread_terminate(self
);
1308 pthread_exit(void *value_ptr
)
1310 pthread_t self
= pthread_self();
1311 if (self
->wqthread
== 0) {
1312 _pthread_exit(self
, value_ptr
);
1314 PTHREAD_ABORT("pthread_exit() may only be called against threads created via pthread_create()");
1319 pthread_getschedparam(pthread_t thread
,
1321 struct sched_param
*param
)
1325 if (thread
== NULL
) {
1329 LOCK(_pthread_list_lock
);
1331 ret
= _pthread_find_thread(thread
);
1334 *policy
= thread
->policy
;
1337 *param
= thread
->param
;
1341 UNLOCK(_pthread_list_lock
);
1347 pthread_setschedparam_internal(pthread_t thread
,
1350 const struct sched_param
*param
)
1352 policy_base_data_t bases
;
1354 mach_msg_type_number_t count
;
1359 bases
.ts
.base_priority
= param
->sched_priority
;
1360 base
= (policy_base_t
)&bases
.ts
;
1361 count
= POLICY_TIMESHARE_BASE_COUNT
;
1364 bases
.fifo
.base_priority
= param
->sched_priority
;
1365 base
= (policy_base_t
)&bases
.fifo
;
1366 count
= POLICY_FIFO_BASE_COUNT
;
1369 bases
.rr
.base_priority
= param
->sched_priority
;
1370 /* quantum isn't public yet */
1371 bases
.rr
.quantum
= param
->quantum
;
1372 base
= (policy_base_t
)&bases
.rr
;
1373 count
= POLICY_RR_BASE_COUNT
;
1378 ret
= thread_policy(kport
, policy
, base
, count
, TRUE
);
1379 return (ret
!= KERN_SUCCESS
) ? EINVAL
: 0;
1383 pthread_setschedparam(pthread_t t
, int policy
, const struct sched_param
*param
)
1385 mach_port_t kport
= MACH_PORT_NULL
;
1389 // since the main thread will not get de-allocated from underneath us
1390 if (t
== pthread_self() || t
== &_thread
) {
1391 kport
= _pthread_kernel_thread(t
);
1394 (void)_pthread_lookup_thread(t
, &kport
, 0);
1397 res
= pthread_setschedparam_internal(t
, kport
, policy
, param
);
1400 // Ensure the thread is still valid.
1401 LOCK(_pthread_list_lock
);
1402 res
= _pthread_find_thread(t
);
1407 UNLOCK(_pthread_list_lock
);
1417 sched_get_priority_min(int policy
)
1419 return default_priority
- 16;
1423 sched_get_priority_max(int policy
)
1425 return default_priority
+ 16;
1429 pthread_equal(pthread_t t1
, pthread_t t2
)
1434 // Force LLVM not to optimise this to a call to __pthread_set_self, if it does
1435 // then _pthread_set_self won't be bound when secondary threads try and start up.
1438 _pthread_set_self(pthread_t p
)
1440 extern void __pthread_set_self(void *);
1446 uint64_t tid
= __thread_selfid();
1448 PTHREAD_ABORT("failed to set thread_id");
1451 p
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_SELF
] = p
;
1452 p
->tsd
[_PTHREAD_TSD_SLOT_ERRNO
] = &p
->err_no
;
1454 __pthread_set_self(&p
->tsd
[0]);
1457 struct _pthread_once_context
{
1458 pthread_once_t
*pthread_once
;
1459 void (*routine
)(void);
1463 __pthread_once_handler(void *context
)
1465 struct _pthread_once_context
*ctx
= context
;
1466 pthread_cleanup_push((void*)__os_once_reset
, &ctx
->pthread_once
->once
);
1468 pthread_cleanup_pop(0);
1469 ctx
->pthread_once
->sig
= _PTHREAD_ONCE_SIG
;
1473 pthread_once(pthread_once_t
*once_control
, void (*init_routine
)(void))
1475 struct _pthread_once_context ctx
= { once_control
, init_routine
};
1477 os_once(&once_control
->once
, &ctx
, __pthread_once_handler
);
1478 } while (once_control
->sig
== _PTHREAD_ONCE_SIG_init
);
1483 _pthread_testcancel(pthread_t thread
, int isconforming
)
1485 const int flags
= (PTHREAD_CANCEL_ENABLE
|_PTHREAD_CANCEL_PENDING
);
1488 bool canceled
= ((thread
->cancel_state
& flags
) == flags
);
1489 UNLOCK(thread
->lock
);
1492 pthread_exit(isconforming
? PTHREAD_CANCELED
: 0);
1497 _pthread_exit_if_canceled(int error
)
1499 if (__unix_conforming
&& ((error
& 0xff) == EINTR
) && (__pthread_canceled(0) == 0)) {
1500 pthread_t self
= pthread_self();
1502 self
->cancel_error
= error
;
1504 pthread_exit(PTHREAD_CANCELED
);
1509 pthread_getconcurrency(void)
1511 return pthread_concurrency
;
1515 pthread_setconcurrency(int new_level
)
1517 if (new_level
< 0) {
1520 pthread_concurrency
= new_level
;
1525 _pthread_set_pfz(uintptr_t address
)
1529 #if !defined(PTHREAD_TARGET_EOS) && !defined(VARIANT_DYLD)
1533 if (_pthread_malloc
) {
1534 return _pthread_malloc(sz
);
1543 if (_pthread_free
) {
1550 * Perform package initialization - called automatically when application starts
1552 struct ProgramVars
; /* forward reference */
1555 __pthread_init(const struct _libpthread_functions
*pthread_funcs
, const char *envp
[] __unused
,
1556 const char *apple
[] __unused
, const struct ProgramVars
*vars __unused
)
1558 // Save our provided pushed-down functions
1559 if (pthread_funcs
) {
1560 exitf
= pthread_funcs
->exit
;
1562 if (pthread_funcs
->version
>= 2) {
1563 _pthread_malloc
= pthread_funcs
->malloc
;
1564 _pthread_free
= pthread_funcs
->free
;
1569 // Get host information
1573 host_flavor_t flavor
= HOST_PRIORITY_INFO
;
1574 mach_msg_type_number_t count
= HOST_PRIORITY_INFO_COUNT
;
1575 host_priority_info_data_t priority_info
;
1576 host_t host
= mach_host_self();
1577 kr
= host_info(host
, flavor
, (host_info_t
)&priority_info
, &count
);
1578 if (kr
!= KERN_SUCCESS
) {
1579 PTHREAD_ABORT("host_info(mach_host_self(), ...) failed: %s", mach_error_string(kr
));
1581 default_priority
= priority_info
.user_priority
;
1582 min_priority
= priority_info
.minimum_priority
;
1583 max_priority
= priority_info
.maximum_priority
;
1585 mach_port_deallocate(mach_task_self(), host
);
1588 // Set up the main thread structure
1592 size_t stacksize
= DFLSSIZ
;
1593 size_t len
= sizeof(stackaddr
);
1594 int mib
[] = { CTL_KERN
, KERN_USRSTACK
};
1595 if (__sysctl(mib
, 2, &stackaddr
, &len
, NULL
, 0) != 0) {
1596 stackaddr
= (void *)USRSTACK
;
1599 pthread_t thread
= &_thread
;
1600 pthread_attr_init(&_pthread_attr_default
);
1601 _pthread_struct_init(thread
, &_pthread_attr_default
, stackaddr
, stacksize
, 0);
1602 thread
->detached
= PTHREAD_CREATE_JOINABLE
;
1604 // Finish initialization with common code that is reinvoked on the
1605 // child side of a fork.
1607 // Finishes initialization of main thread attributes.
1608 // Initializes the thread list and add the main thread.
1609 // Calls _pthread_set_self() to prepare the main thread for execution.
1610 __pthread_fork_child_internal(thread
);
1612 // Set up kernel entry points with __bsdthread_register.
1613 pthread_workqueue_atfork_child();
1625 PTHREAD_NOEXPORT
void
1626 __pthread_fork_child_internal(pthread_t p
)
1628 TAILQ_INIT(&__pthread_head
);
1629 LOCK_INIT(_pthread_list_lock
);
1631 // Re-use the main thread's static storage if no thread was provided.
1633 if (_thread
.tsd
[0] != 0) {
1634 bzero(&_thread
, sizeof(struct _pthread
));
1640 _pthread_set_kernel_thread(p
, mach_thread_self());
1641 _pthread_set_reply_port(p
, mach_reply_port());
1642 p
->__cleanup_stack
= NULL
;
1643 p
->joiner_notify
= SEMAPHORE_NULL
;
1644 p
->joiner
= MACH_PORT_NULL
;
1645 p
->detached
|= _PTHREAD_CREATE_PARENT
;
1646 p
->tsd
[__TSD_SEMAPHORE_CACHE
] = SEMAPHORE_NULL
;
1648 // Initialize the list of threads with the new main thread.
1649 TAILQ_INSERT_HEAD(&__pthread_head
, p
, plist
);
1652 _pthread_set_self(p
);
1653 _pthread_introspection_thread_start(p
);
1657 * Query/update the cancelability 'state' of a thread
1659 PTHREAD_NOEXPORT
int
1660 _pthread_setcancelstate_internal(int state
, int *oldstate
, int conforming
)
1665 case PTHREAD_CANCEL_ENABLE
:
1667 __pthread_canceled(1);
1670 case PTHREAD_CANCEL_DISABLE
:
1672 __pthread_canceled(2);
1679 self
= pthread_self();
1682 *oldstate
= self
->cancel_state
& _PTHREAD_CANCEL_STATE_MASK
;
1684 self
->cancel_state
&= ~_PTHREAD_CANCEL_STATE_MASK
;
1685 self
->cancel_state
|= state
;
1688 _pthread_testcancel(self
, 0); /* See if we need to 'die' now... */
1693 /* When a thread exits set the cancellation state to DISABLE and DEFERRED */
1695 _pthread_setcancelstate_exit(pthread_t self
, void * value_ptr
, int conforming
)
1698 self
->cancel_state
&= ~(_PTHREAD_CANCEL_STATE_MASK
| _PTHREAD_CANCEL_TYPE_MASK
);
1699 self
->cancel_state
|= (PTHREAD_CANCEL_DISABLE
| PTHREAD_CANCEL_DEFERRED
);
1700 if (value_ptr
== PTHREAD_CANCELED
) {
1702 self
->detached
|= _PTHREAD_WASCANCEL
;
1709 _pthread_join_cleanup(pthread_t thread
, void ** value_ptr
, int conforming
)
1711 // Returns ESRCH if the thread was not created joinable.
1712 int ret
= __pthread_remove_thread(thread
, false, NULL
);
1718 *value_ptr
= __pthread_get_exit_value(thread
, conforming
);
1720 _pthread_introspection_thread_destroy(thread
);
1721 _pthread_deallocate(thread
);
1725 /* ALWAYS called with list lock and return with list lock */
1727 _pthread_find_thread(pthread_t thread
)
1729 if (thread
!= NULL
) {
1732 TAILQ_FOREACH(p
, &__pthread_head
, plist
) {
1734 if (_pthread_kernel_thread(thread
) == MACH_PORT_NULL
) {
1735 UNLOCK(_pthread_list_lock
);
1737 LOCK(_pthread_list_lock
);
1748 _pthread_lookup_thread(pthread_t thread
, mach_port_t
*portp
, int only_joinable
)
1750 mach_port_t kport
= MACH_PORT_NULL
;
1753 if (thread
== NULL
) {
1757 LOCK(_pthread_list_lock
);
1759 ret
= _pthread_find_thread(thread
);
1761 // Fail if we only want joinable threads and the thread found is
1762 // not in the detached state.
1763 if (only_joinable
!= 0 && (thread
->detached
& PTHREAD_CREATE_DETACHED
) != 0) {
1766 kport
= _pthread_kernel_thread(thread
);
1770 UNLOCK(_pthread_list_lock
);
1772 if (portp
!= NULL
) {
1780 _pthread_clear_qos_tsd(mach_port_t thread_port
)
1782 if (thread_port
== MACH_PORT_NULL
|| (uintptr_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
) == thread_port
) {
1783 /* Clear the current thread's TSD, that can be done inline. */
1784 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
, _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED
, 0, 0));
1788 LOCK(_pthread_list_lock
);
1790 TAILQ_FOREACH(p
, &__pthread_head
, plist
) {
1792 while ((kp
= _pthread_kernel_thread(p
)) == MACH_PORT_NULL
) {
1793 UNLOCK(_pthread_list_lock
);
1795 LOCK(_pthread_list_lock
);
1797 if (thread_port
== kp
) {
1798 p
->tsd
[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
] = _pthread_priority_make_newest(QOS_CLASS_UNSPECIFIED
, 0, 0);
1803 UNLOCK(_pthread_list_lock
);
1807 /***** pthread workqueue support routines *****/
1809 PTHREAD_NOEXPORT
void
1810 pthread_workqueue_atfork_child(void)
1812 struct _pthread_registration_data data
= {
1813 .dispatch_queue_offset
= __PTK_LIBDISPATCH_KEY0
* sizeof(void *),
1816 int rv
= __bsdthread_register(thread_start
,
1820 (uintptr_t)sizeof(data
),
1821 data
.dispatch_queue_offset
);
1824 __pthread_supported_features
= rv
;
1827 if (_pthread_priority_get_qos_newest(data
.main_qos
) != QOS_CLASS_UNSPECIFIED
) {
1828 _pthread_set_main_qos(data
.main_qos
);
1829 _thread
.tsd
[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
] = data
.main_qos
;
1832 if (__libdispatch_workerfunction
!= NULL
) {
1833 // prepare the kernel for workq action
1834 (void)__workq_open();
1839 _pthread_wqthread(pthread_t self
, mach_port_t kport
, void *stackaddr
, void *unused
, int flags
)
1841 PTHREAD_ASSERT(flags
& WQ_FLAG_THREAD_NEWSPI
);
1843 int thread_reuse
= flags
& WQ_FLAG_THREAD_REUSE
;
1844 int thread_class
= flags
& WQ_FLAG_THREAD_PRIOMASK
;
1845 int overcommit
= (flags
& WQ_FLAG_THREAD_OVERCOMMIT
) != 0;
1847 pthread_priority_t priority
;
1849 if ((__pthread_supported_features
& PTHREAD_FEATURE_QOS_MAINTENANCE
) == 0) {
1850 priority
= _pthread_priority_make_version2(thread_class
, 0, (overcommit
? _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
: 0));
1852 priority
= _pthread_priority_make_newest(thread_class
, 0, (overcommit
? _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
: 0));
1855 if (thread_reuse
== 0) {
1856 // New thread created by kernel, needs initialization.
1857 _pthread_struct_init(self
, &_pthread_attr_default
, stackaddr
, DEFAULT_STACK_SIZE
, 1);
1858 _pthread_set_kernel_thread(self
, kport
);
1860 self
->wqkillset
= 0;
1862 // Not a joinable thread.
1863 self
->detached
&= ~PTHREAD_CREATE_JOINABLE
;
1864 self
->detached
|= PTHREAD_CREATE_DETACHED
;
1866 // Update the running thread count and set childrun bit.
1867 // XXX this should be consolidated with pthread_body().
1868 _pthread_set_self(self
);
1869 _pthread_introspection_thread_create(self
, false);
1870 __pthread_add_thread(self
, false);
1872 // If we're running with fine-grained priority, we also need to
1873 // set this thread to have the QoS class provided to use by the kernel
1874 if (__pthread_supported_features
& PTHREAD_FEATURE_FINEPRIO
) {
1875 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
, _pthread_priority_make_newest(thread_class
, 0, 0));
1880 PTHREAD_ASSERT(self
);
1881 PTHREAD_ASSERT(self
== pthread_self());
1884 self
->fun
= (void *(*)(void *))__libdispatch_workerfunction
;
1885 self
->arg
= (void *)(uintptr_t)thread_class
;
1887 if (__pthread_supported_features
& PTHREAD_FEATURE_FINEPRIO
) {
1888 if (!__workq_newapi
) {
1889 /* Old thread priorities are inverted from where we have them in
1890 * the new flexible priority scheme. The highest priority is zero,
1891 * up to 2, with background at 3.
1893 pthread_workqueue_function_t func
= (pthread_workqueue_function_t
)__libdispatch_workerfunction
;
1895 int opts
= overcommit
? WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
: 0;
1897 if ((__pthread_supported_features
& PTHREAD_FEATURE_QOS_DEFAULT
) == 0) {
1898 /* Dirty hack to support kernels that don't have QOS_CLASS_DEFAULT. */
1899 switch (thread_class
) {
1900 case QOS_CLASS_USER_INTERACTIVE
:
1901 thread_class
= QOS_CLASS_USER_INITIATED
;
1903 case QOS_CLASS_USER_INITIATED
:
1904 thread_class
= QOS_CLASS_DEFAULT
;
1911 switch (thread_class
) {
1912 /* QOS_CLASS_USER_INTERACTIVE is not currently requested by for old dispatch priority compatibility */
1913 case QOS_CLASS_USER_INITIATED
:
1914 (*func
)(WORKQ_HIGH_PRIOQUEUE
, opts
, NULL
);
1917 case QOS_CLASS_DEFAULT
:
1918 /* B&I builders can't pass a QOS_CLASS_DEFAULT thread to dispatch, for fear of the QoS being
1919 * picked up by NSThread (et al) and transported around the system. So change the TSD to
1920 * make this thread look like QOS_CLASS_USER_INITIATED even though it will still run as legacy.
1922 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS
, _pthread_priority_make_newest(QOS_CLASS_USER_INITIATED
, 0, 0));
1923 (*func
)(WORKQ_DEFAULT_PRIOQUEUE
, opts
, NULL
);
1926 case QOS_CLASS_UTILITY
:
1927 (*func
)(WORKQ_LOW_PRIOQUEUE
, opts
, NULL
);
1930 case QOS_CLASS_BACKGROUND
:
1931 (*func
)(WORKQ_BG_PRIOQUEUE
, opts
, NULL
);
1934 /* Legacy dispatch does not use QOS_CLASS_MAINTENANCE, so no need to handle it here */
1938 /* "New" API, where dispatch is expecting to be given the thread priority */
1939 (*__libdispatch_workerfunction
)(priority
);
1942 /* We're the new library running on an old kext, so thread_class is really the workq priority. */
1943 pthread_workqueue_function_t func
= (pthread_workqueue_function_t
)__libdispatch_workerfunction
;
1944 int options
= overcommit
? WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
: 0;
1945 (*func
)(thread_class
, options
, NULL
);
1948 __workq_kernreturn(WQOPS_THREAD_RETURN
, NULL
, 0, 0);
1949 _pthread_exit(self
, NULL
);
1952 /***** pthread workqueue API for libdispatch *****/
1955 _pthread_workqueue_init(pthread_workqueue_function2_t func
, int offset
, int flags
)
1961 __workq_newapi
= true;
1962 __libdispatch_offset
= offset
;
1964 int rv
= pthread_workqueue_setdispatch_np((pthread_workqueue_function_t
)func
);
1969 pthread_workqueue_setdispatchoffset_np(int offset
)
1971 __libdispatch_offset
= offset
;
1975 pthread_workqueue_setdispatch_np(pthread_workqueue_function_t worker_func
)
1978 if (__libdispatch_workerfunction
== NULL
) {
1979 // Check whether the kernel supports new SPIs
1980 res
= __workq_kernreturn(WQOPS_QUEUE_NEWSPISUPP
, NULL
, __libdispatch_offset
, 0);
1984 __libdispatch_workerfunction
= (pthread_workqueue_function2_t
)worker_func
;
1986 // Prepare the kernel for workq action
1987 (void)__workq_open();
1988 if (__is_threaded
== 0) {
1997 _pthread_workqueue_supported(void)
1999 return __pthread_supported_features
;
2003 pthread_workqueue_addthreads_np(int queue_priority
, int options
, int numthreads
)
2007 // Cannot add threads without a worker function registered.
2008 if (__libdispatch_workerfunction
== NULL
) {
2012 pthread_priority_t kp
= 0;
2014 if (__pthread_supported_features
& PTHREAD_FEATURE_FINEPRIO
) {
2015 /* The new kernel API takes the new QoS class + relative priority style of
2016 * priority. This entry point is here for compatibility with old libdispatch
2017 * versions (ie. the simulator). We request the corresponding new bracket
2018 * from the kernel, then on the way out run all dispatch queues that were
2022 int compat_priority
= queue_priority
& WQ_FLAG_THREAD_PRIOMASK
;
2025 /* To make sure the library does not issue more threads to dispatch than
2026 * were requested, the total number of active requests is recorded in
2029 if (options
& WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
) {
2030 flags
= _PTHREAD_PRIORITY_OVERCOMMIT_FLAG
;
2033 kp
= _pthread_qos_class_encode_workqueue(compat_priority
, flags
);
2036 /* Running on the old kernel, queue_priority is what we pass directly to
2039 kp
= queue_priority
& WQ_FLAG_THREAD_PRIOMASK
;
2041 if (options
& WORKQ_ADDTHREADS_OPTION_OVERCOMMIT
) {
2042 kp
|= WORKQUEUE_OVERCOMMIT
;
2046 res
= __workq_kernreturn(WQOPS_QUEUE_REQTHREADS
, NULL
, numthreads
, (int)kp
);
2054 _pthread_workqueue_addthreads(int numthreads
, pthread_priority_t priority
)
2058 if (__libdispatch_workerfunction
== NULL
) {
2062 if ((__pthread_supported_features
& PTHREAD_FEATURE_FINEPRIO
) == 0) {
2066 res
= __workq_kernreturn(WQOPS_QUEUE_REQTHREADS
, NULL
, numthreads
, (int)priority
);
2074 * Introspection SPI for libpthread.
2077 static pthread_introspection_hook_t _pthread_introspection_hook
;
2079 pthread_introspection_hook_t
2080 pthread_introspection_hook_install(pthread_introspection_hook_t hook
)
2082 if (os_slowpath(!hook
)) {
2083 PTHREAD_ABORT("pthread_introspection_hook_install was passed NULL");
2085 pthread_introspection_hook_t prev
;
2086 prev
= __sync_swap(&_pthread_introspection_hook
, hook
);
2092 _pthread_introspection_hook_callout_thread_create(pthread_t t
, bool destroy
)
2094 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_CREATE
, t
, t
,
2096 if (!destroy
) return;
2097 _pthread_introspection_thread_destroy(t
);
2101 _pthread_introspection_thread_create(pthread_t t
, bool destroy
)
2103 if (os_fastpath(!_pthread_introspection_hook
)) return;
2104 _pthread_introspection_hook_callout_thread_create(t
, destroy
);
2109 _pthread_introspection_hook_callout_thread_start(pthread_t t
)
2113 if (t
== &_thread
) {
2114 freesize
= t
->stacksize
+ t
->guardsize
;
2115 freeaddr
= t
->stackaddr
- freesize
;
2117 freesize
= t
->freesize
- pthreadsize
;
2118 freeaddr
= t
->freeaddr
;
2120 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_START
, t
,
2121 freeaddr
, freesize
);
2125 _pthread_introspection_thread_start(pthread_t t
)
2127 if (os_fastpath(!_pthread_introspection_hook
)) return;
2128 _pthread_introspection_hook_callout_thread_start(t
);
2133 _pthread_introspection_hook_callout_thread_terminate(pthread_t t
,
2134 void *freeaddr
, size_t freesize
, bool destroy
)
2136 if (destroy
&& freesize
) {
2137 freesize
-= pthreadsize
;
2139 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_TERMINATE
, t
,
2140 freeaddr
, freesize
);
2141 if (!destroy
) return;
2142 _pthread_introspection_thread_destroy(t
);
2146 _pthread_introspection_thread_terminate(pthread_t t
, void *freeaddr
,
2147 size_t freesize
, bool destroy
)
2149 if (os_fastpath(!_pthread_introspection_hook
)) return;
2150 _pthread_introspection_hook_callout_thread_terminate(t
, freeaddr
, freesize
,
2156 _pthread_introspection_hook_callout_thread_destroy(pthread_t t
)
2158 if (t
== &_thread
) return;
2159 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_DESTROY
, t
, t
,
2164 _pthread_introspection_thread_destroy(pthread_t t
)
2166 if (os_fastpath(!_pthread_introspection_hook
)) return;
2167 _pthread_introspection_hook_callout_thread_destroy(t
);