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 Threads - IEEE 1003.1c
52 #ifndef _POSIX_PTHREAD_INTERNALS_H
53 #define _POSIX_PTHREAD_INTERNALS_H
55 #define _PTHREAD_BUILDING_PTHREAD_
57 // suppress pthread_attr_t typedef in sys/signal.h
58 #define _PTHREAD_ATTR_T
59 struct _pthread_attr_t
; /* forward reference */
60 typedef struct _pthread_attr_t pthread_attr_t
;
69 #include <TargetConditionals.h>
70 #include <libkern/OSAtomic.h>
71 #include <mach/mach.h>
72 #include <mach/mach_error.h>
73 #include <sys/queue.h>
75 #define __OS_EXPOSE_INTERNALS__ 1
76 #include <os/internal/internal_shared.h>
77 #include <os/once_private.h>
79 #define PTHREAD_INTERNAL_CRASH(c, x) do { \
80 _os_set_crash_log_cause_and_message((c), \
81 "BUG IN LIBPTHREAD: " x); \
85 #define PTHREAD_CLIENT_CRASH(c, x) do { \
86 _os_set_crash_log_cause_and_message((c), \
87 "BUG IN CLIENT OF LIBPTHREAD: " x); \
95 #ifndef PTHREAD_LAYOUT_SPI
96 #define PTHREAD_LAYOUT_SPI 1
99 #include "posix_sched.h"
100 #include "tsd_private.h"
101 #include "spinlock_private.h"
103 #define OS_UNFAIR_LOCK_INLINE 1
104 #include <os/lock_private.h>
105 typedef os_unfair_lock _pthread_lock
;
106 #define _PTHREAD_LOCK_INITIALIZER OS_UNFAIR_LOCK_INIT
107 #define _PTHREAD_LOCK_INIT(lock) ((lock) = (_pthread_lock)_PTHREAD_LOCK_INITIALIZER)
108 #define _PTHREAD_LOCK(lock) os_unfair_lock_lock_with_options_inline(&(lock), OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION)
109 #define _PTHREAD_LOCK_FROM_MACH_THREAD(lock) os_unfair_lock_lock_inline_no_tsd_4libpthread(&(lock))
110 #define _PTHREAD_UNLOCK(lock) os_unfair_lock_unlock_inline(&(lock))
111 #define _PTHREAD_UNLOCK_FROM_MACH_THREAD(lock) os_unfair_lock_unlock_inline_no_tsd_4libpthread(&(lock))
113 #if TARGET_IPHONE_SIMULATOR
114 #error Unsupported target
117 // List of all pthreads in the process.
118 TAILQ_HEAD(__pthread_list
, _pthread
);
119 extern struct __pthread_list __pthread_head
;
121 // Lock protects access to above list.
122 extern _pthread_lock _pthread_list_lock
;
124 extern int __is_threaded
;
129 #if TARGET_OS_EMBEDDED
130 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 256
131 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
132 #define _INTERNAL_POSIX_THREAD_KEYS_END 512
134 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
135 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
136 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
139 #define MAXTHREADNAMESIZE 64
141 typedef struct _pthread
{
143 // ABI - These fields are externally known as struct _opaque_pthread_t.
145 long sig
; // _PTHREAD_SIG
146 struct __darwin_pthread_handler_rec
*__cleanup_stack
;
149 // SPI - These fields are private.
151 // these fields are globally protected by _pthread_list_lock:
157 _pthread_lock lock
; // protect access to everything below
167 #if defined(__LP64__)
171 void *(*fun
)(void*); // thread start routine
172 void *arg
; // thread start routine argument
173 void *exit_value
; // thread exit value storage
175 semaphore_t joiner_notify
; // pthread_join notification
178 int cancel_state
; // whether the thread can be cancelled
181 int err_no
; // thread-local errno
183 struct _pthread
*joiner
;
185 struct sched_param param
; // [aligned]
187 TAILQ_ENTRY(_pthread
) plist
; // global thread list [aligned]
189 char pthread_name
[MAXTHREADNAMESIZE
]; // includes NUL [aligned]
191 void *stackaddr
; // base of the stack (page aligned)
192 size_t stacksize
; // size of stack (page multiple and >= PTHREAD_STACK_MIN)
194 void* freeaddr
; // stack/thread allocation base address
195 size_t freesize
; // stack/thread allocation size
196 size_t guardsize
; // guard page size in bytes
198 // tsd-base relative accessed elements
199 __attribute__((aligned(8)))
200 uint64_t thread_id
; // 64-bit unique thread id
202 /* Thread Specific Data slots
204 * The offset of this field from the start of the structure is difficult to
205 * change on OS X because of a thorny bitcompat issue: mono has hard coded
206 * the value into their source. Newer versions of mono will fall back to
207 * scanning to determine it at runtime, but there's lots of software built
208 * with older mono that won't. We will have to break them someday...
210 __attribute__ ((aligned (16)))
211 void *tsd
[_EXTERNAL_POSIX_THREAD_KEYS_MAX
+ _INTERNAL_POSIX_THREAD_KEYS_MAX
];
215 struct _pthread_attr_t
{
225 struct sched_param param
; // [aligned]
226 void *stackaddr
; // stack base; vm_page_size aligned
227 size_t stacksize
; // stack size; multiple of vm_page_size and >= PTHREAD_STACK_MIN
228 size_t guardsize
; // size in bytes of stack overflow guard area
229 unsigned long qosclass
;
230 #if defined(__LP64__)
231 uint32_t _reserved
[2];
233 uint32_t _reserved
[1];
240 #define _PTHREAD_MUTEX_POLICY_NONE 0
241 #define _PTHREAD_MUTEX_POLICY_FAIRSHARE 1
242 #define _PTHREAD_MUTEX_POLICY_FIRSTFIT 2
243 #define _PTHREAD_MUTEX_POLICY_REALTIME 3
244 #define _PTHREAD_MUTEX_POLICY_ADAPTIVE 4
245 #define _PTHREAD_MUTEX_POLICY_PRIPROTECT 5
246 #define _PTHREAD_MUTEX_POLICY_PRIINHERIT 6
248 #define _PTHREAD_MUTEXATTR_T
257 } pthread_mutexattr_t
;
259 struct _pthread_mutex_options
{
277 struct _pthread_mutex_options options
;
281 #if defined(__LP64__)
284 uint32_t m_tid
[2]; // thread id of thread that has mutex locked
285 uint32_t m_seq
[2]; // mutex sequence id
286 uint32_t m_mis
[2]; // for misaligned locks m_tid/m_seq will span into here
287 #if defined(__LP64__)
288 uint32_t _reserved
[4];
290 uint32_t _reserved
[1];
295 #define _PTHREAD_CONDATTR_T
300 } pthread_condattr_t
;
309 _pthread_mutex
*busy
;
311 #if defined(__LP64__)
312 uint32_t _reserved
[3];
317 #define _PTHREAD_ONCE_T
324 #define _PTHREAD_RWLOCKATTR_T
328 #if defined(__LP64__)
329 uint32_t _reserved
[3];
331 uint32_t _reserved
[2];
333 } pthread_rwlockattr_t
;
343 #if defined(__LP64__)
346 volatile uint32_t rw_seq
[4];
347 struct _pthread
*rw_owner
;
348 volatile uint32_t *rw_lcntaddr
;
349 volatile uint32_t *rw_seqaddr
;
350 volatile uint32_t *rw_ucntaddr
;
351 #if defined(__LP64__)
352 uint32_t _reserved
[31];
354 uint32_t _reserved
[19];
359 #include "pthread_spis.h"
361 _Static_assert(sizeof(_pthread_mutex
) == sizeof(pthread_mutex_t
),
362 "Incorrect _pthread_mutex structure size");
364 // Internal references to pthread_self() use TSD slot 0 directly.
365 inline static pthread_t
__attribute__((__pure__
))
366 _pthread_self_direct(void)
368 return _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF
);
370 #define pthread_self() _pthread_self_direct()
372 inline static pthread_t
__attribute__((__pure__
))
373 _pthread_selfid_direct(void)
375 return (_pthread_self_direct())->thread_id
;
378 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
379 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
380 #define _PTHREAD_DEFAULT_PRIOCEILING 0
381 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
382 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
383 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
385 #define _PTHREAD_NO_SIG 0x00000000
386 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
387 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
388 #define _PTHREAD_MUTEX_SIG_fast 0x4D55545A /* 'MUTZ' */
389 #define _PTHREAD_MUTEX_SIG_MASK 0xfffffffd
390 #define _PTHREAD_MUTEX_SIG_CMP 0x4D555458 /* _PTHREAD_MUTEX_SIG & _PTHREAD_MUTEX_SIG_MASK */
391 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
392 #define _PTHREAD_ERRORCHECK_MUTEX_SIG_init 0x32AAABA1
393 #define _PTHREAD_RECURSIVE_MUTEX_SIG_init 0x32AAABA2
394 #define _PTHREAD_FIRSTFIT_MUTEX_SIG_init 0x32AAABA3
395 #define _PTHREAD_MUTEX_SIG_init_MASK 0xfffffff0
396 #define _PTHREAD_MUTEX_SIG_init_CMP 0x32AAABA0
397 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
398 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
399 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
400 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
401 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
402 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
403 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
404 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
405 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
406 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
409 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
410 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
411 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
413 #define _PTHREAD_CREATE_PARENT 4
414 #define _PTHREAD_EXITED 8
416 #define _PTHREAD_WASCANCEL 0x10
420 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
422 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
424 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
426 #define _PTHREAD_CANCEL_STATE_MASK 0x01
427 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
428 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
430 extern boolean_t
swtch_pri(int);
432 #define PTHREAD_EXPORT extern __attribute__((visibility("default")))
433 #define PTHREAD_EXTERN extern
434 #define PTHREAD_NOEXPORT __attribute__((visibility("hidden")))
435 #define PTHREAD_NORETURN __attribute__((__noreturn__))
436 #define PTHREAD_ALWAYS_INLINE __attribute__((always_inline))
437 #define PTHREAD_NOINLINE __attribute__((noinline))
438 #define PTHREAD_WEAK __attribute__((weak))
439 #define PTHREAD_USED __attribute__((used))
441 #include "kern/kern_internal.h"
445 /* Internal globals. */
446 PTHREAD_NOEXPORT
extern int __pthread_supported_features
;
448 /* Functions defined in machine-dependent files. */
449 PTHREAD_NOEXPORT
void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
451 PTHREAD_NOEXPORT
void _pthread_tsd_cleanup(pthread_t self
);
453 PTHREAD_NOEXPORT
int __mtx_droplock(_pthread_mutex
*mutex
, uint32_t * flagp
, uint32_t ** pmtxp
, uint32_t * mgenp
, uint32_t * ugenp
);
455 /* internally redirected upcalls. */
456 PTHREAD_NOEXPORT
void* malloc(size_t);
457 PTHREAD_NOEXPORT
void free(void*);
459 /* syscall interfaces */
460 extern uint32_t __psynch_mutexwait(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
461 extern uint32_t __psynch_mutexdrop(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
463 extern uint32_t __psynch_cvbroad(pthread_cond_t
* cv
, uint64_t cvlsgen
, uint64_t cvudgen
, uint32_t flags
, pthread_mutex_t
* mutex
, uint64_t mugen
, uint64_t tid
);
464 extern uint32_t __psynch_cvsignal(pthread_cond_t
* cv
, uint64_t cvlsgen
, uint32_t cvugen
, int thread_port
, pthread_mutex_t
* mutex
, uint64_t mugen
, uint64_t tid
, uint32_t flags
);
465 extern uint32_t __psynch_cvwait(pthread_cond_t
* cv
, uint64_t cvlsgen
, uint32_t cvugen
, pthread_mutex_t
* mutex
, uint64_t mugen
, uint32_t flags
, int64_t sec
, uint32_t nsec
);
466 extern uint32_t __psynch_cvclrprepost(void * cv
, uint32_t cvgen
, uint32_t cvugen
, uint32_t cvsgen
, uint32_t prepocnt
, uint32_t preposeq
, uint32_t flags
);
467 extern uint32_t __psynch_rw_longrdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
468 extern uint32_t __psynch_rw_yieldwrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
469 extern int __psynch_rw_downgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
470 extern uint32_t __psynch_rw_upgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
471 extern uint32_t __psynch_rw_rdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
472 extern uint32_t __psynch_rw_wrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
473 extern uint32_t __psynch_rw_unlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
474 extern uint32_t __psynch_rw_unlock2(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
475 extern uint32_t __bsdthread_ctl(uintptr_t cmd
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
479 __proc_info(int callnum
, int pid
, int flavor
, uint64_t arg
, void * buffer
, int buffersize
);
481 PTHREAD_NOEXPORT
int _pthread_lookup_thread(pthread_t thread
, mach_port_t
* port
, int only_joinable
);
482 PTHREAD_NOEXPORT
int _pthread_join_cleanup(pthread_t thread
, void ** value_ptr
, int conforming
);
484 PTHREAD_NORETURN PTHREAD_NOEXPORT
486 __pthread_abort(void);
488 PTHREAD_NORETURN PTHREAD_NOEXPORT
490 __pthread_abort_reason(const char *fmt
, ...);
494 _pthread_set_main_qos(pthread_priority_t qos
);
498 _pthread_key_global_init(const char *envp
[]);
502 _pthread_start(pthread_t self
, mach_port_t kport
, void *(*fun
)(void *), void * funarg
, size_t stacksize
, unsigned int flags
);
506 _pthread_wqthread(pthread_t self
, mach_port_t kport
, void *stackaddr
, void *keventlist
, int flags
, int nkevents
);
510 __pthread_fork_child_internal(pthread_t p
);
514 _pthread_clear_qos_tsd(mach_port_t thread_port
);
518 _pthread_testcancel(pthread_t thread
, int isconforming
);
522 _pthread_exit_if_canceled(int error
);
524 PTHREAD_ALWAYS_INLINE
525 static inline mach_port_t
526 _pthread_kernel_thread(pthread_t t
)
528 return t
->tsd
[_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
];
531 PTHREAD_ALWAYS_INLINE
533 _pthread_set_kernel_thread(pthread_t t
, mach_port_t p
)
535 if (os_slowpath(!MACH_PORT_VALID(p
))) {
536 PTHREAD_INTERNAL_CRASH(t
, "Invalid thread port");
538 t
->tsd
[_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
] = p
;
541 #define PTHREAD_ABORT(f,...) __pthread_abort_reason("%s:%s:%u: " f, __FILE__, __func__, __LINE__, ## __VA_ARGS__)
543 #define PTHREAD_ASSERT(b) do { if (!(b)) PTHREAD_ABORT("failed assertion `%s'", #b); } while (0)
545 #include <os/semaphore_private.h>
546 #include <os/alloc_once_private.h>
548 struct pthread_atfork_entry
{
549 void (*prepare
)(void);
550 void (*parent
)(void);
554 #define PTHREAD_ATFORK_INLINE_MAX 10
555 #define PTHREAD_ATFORK_MAX (vm_page_size/sizeof(struct pthread_atfork_entry))
557 struct pthread_globals_s
{
559 pthread_t psaved_self
;
560 _pthread_lock psaved_self_global_lock
;
561 _pthread_lock pthread_atfork_lock
;
564 struct pthread_atfork_entry atfork_storage
[PTHREAD_ATFORK_INLINE_MAX
];
565 struct pthread_atfork_entry
*atfork
;
567 typedef struct pthread_globals_s
*pthread_globals_t
;
569 __attribute__((__pure__
))
570 static inline pthread_globals_t
571 _pthread_globals(void)
573 return os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_PTHREAD
,
574 sizeof(struct pthread_globals_s
),
578 #pragma mark _pthread_mutex_check_signature
580 PTHREAD_ALWAYS_INLINE
582 _pthread_mutex_check_signature_fast(_pthread_mutex
*mutex
)
584 return os_fastpath(mutex
->sig
== _PTHREAD_MUTEX_SIG_fast
);
587 PTHREAD_ALWAYS_INLINE
589 _pthread_mutex_check_signature(_pthread_mutex
*mutex
)
591 return os_fastpath((mutex
->sig
& _PTHREAD_MUTEX_SIG_MASK
) == _PTHREAD_MUTEX_SIG_CMP
);
594 PTHREAD_ALWAYS_INLINE
596 _pthread_mutex_check_signature_init(_pthread_mutex
*mutex
)
598 return os_fastpath((mutex
->sig
& _PTHREAD_MUTEX_SIG_init_MASK
) == _PTHREAD_MUTEX_SIG_init_CMP
);
601 #endif /* _POSIX_PTHREAD_INTERNALS_H */