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 <os/once_private.h>
74 #include <sys/queue.h>
80 #ifndef PTHREAD_LAYOUT_SPI
81 #define PTHREAD_LAYOUT_SPI 1
84 #include "posix_sched.h"
85 #include "tsd_private.h"
86 #include "spinlock_private.h"
88 #if TARGET_IPHONE_SIMULATOR
89 #error Unsupported target
92 // List of all pthreads in the process.
93 TAILQ_HEAD(__pthread_list
, _pthread
);
94 extern struct __pthread_list __pthread_head
;
96 // Lock protects access to above list.
97 extern pthread_lock_t _pthread_list_lock
;
99 extern int __is_threaded
;
104 #if TARGET_OS_EMBEDDED
105 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 256
106 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
107 #define _INTERNAL_POSIX_THREAD_KEYS_END 512
109 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
110 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
111 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
114 #define MAXTHREADNAMESIZE 64
116 typedef struct _pthread
{
118 // ABI - These fields are externally known as struct _opaque_pthread_t.
120 long sig
; // _PTHREAD_SIG
121 struct __darwin_pthread_handler_rec
*__cleanup_stack
;
124 // SPI - These fields are private.
126 // these fields are globally protected by _pthread_list_lock:
132 pthread_lock_t lock
; // protect access to everything below
145 uint64_t thread_id
; // 64-bit unique thread id
147 void *(*fun
)(void*); // thread start routine
148 void *arg
; // thread start routine argument
149 void *exit_value
; // thread exit value storage
151 semaphore_t joiner_notify
; // pthread_join notification
154 int cancel_state
; // whether the thread can be cancelled
158 // i386 err_no must be at a 68 byte offset
159 // See <rdar://problem/13249323>
160 uint32_t __13249323_pad
[3];
162 int err_no
; // thread-local errno
164 struct _pthread
*joiner
;
166 struct sched_param param
; // [aligned]
168 TAILQ_ENTRY(_pthread
) plist
; // global thread list [aligned]
170 char pthread_name
[MAXTHREADNAMESIZE
]; // includes NUL [aligned]
172 void *stackaddr
; // base of the stack (page aligned)
173 size_t stacksize
; // size of stack (page multiple and >= PTHREAD_STACK_MIN)
175 void* freeaddr
; // stack/thread allocation base address
176 size_t freesize
; // stack/thread allocation size
177 size_t guardsize
; // guard page size in bytes
179 // thread specific data
180 void *tsd
[_EXTERNAL_POSIX_THREAD_KEYS_MAX
+ _INTERNAL_POSIX_THREAD_KEYS_MAX
] __attribute__ ((aligned (16)));
184 struct _pthread_attr_t
{
194 struct sched_param param
; // [aligned]
195 void *stackaddr
; // stack base; vm_page_size aligned
196 size_t stacksize
; // stack size; multiple of vm_page_size and >= PTHREAD_STACK_MIN
197 size_t guardsize
; // size in bytes of stack overflow guard area
198 unsigned long qosclass
;
199 #if defined(__LP64__)
200 uint32_t _reserved
[2];
202 uint32_t _reserved
[1];
209 #define _PTHREAD_MUTEX_POLICY_NONE 0
210 #define _PTHREAD_MUTEX_POLICY_FAIRSHARE 1
211 #define _PTHREAD_MUTEX_POLICY_FIRSTFIT 2
212 #define _PTHREAD_MUTEX_POLICY_REALTIME 3
213 #define _PTHREAD_MUTEX_POLICY_ADAPTIVE 4
214 #define _PTHREAD_MUTEX_POLICY_PRIPROTECT 5
215 #define _PTHREAD_MUTEX_POLICY_PRIINHERIT 6
217 #define _PTHREAD_MUTEXATTR_T
226 } pthread_mutexattr_t
;
228 struct _pthread_mutex_options
{
246 struct _pthread_mutex_options options
;
250 #if defined(__LP64__)
253 uint32_t m_tid
[2]; // thread id of thread that has mutex locked, misaligned locks may span to first field of m_seq
255 #if defined(__LP64__)
262 #define _PTHREAD_CONDATTR_T
267 } pthread_condattr_t
;
276 _pthread_mutex
*busy
;
278 #if defined(__LP64__)
279 uint32_t _reserved
[3];
284 #define _PTHREAD_ONCE_T
291 #define _PTHREAD_RWLOCKATTR_T
295 #if defined(__LP64__)
296 uint32_t _reserved
[3];
298 uint32_t _reserved
[2];
300 } pthread_rwlockattr_t
;
310 #if defined(__LP64__)
313 volatile uint32_t rw_seq
[4];
314 struct _pthread
*rw_owner
;
315 volatile uint32_t *rw_lcntaddr
;
316 volatile uint32_t *rw_seqaddr
;
317 volatile uint32_t *rw_ucntaddr
;
318 #if defined(__LP64__)
319 uint32_t _reserved
[31];
321 uint32_t _reserved
[19];
325 #include "pthread_spis.h"
327 // Internal references to pthread_self() use TSD slot 0 directly.
328 inline static pthread_t
__attribute__((__pure__
))
329 _pthread_self_direct(void)
331 return _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF
);
333 #define pthread_self() _pthread_self_direct()
335 inline static pthread_t
__attribute__((__pure__
))
336 _pthread_selfid_direct(void)
338 return (_pthread_self_direct())->thread_id
;
341 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
342 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
343 #define _PTHREAD_DEFAULT_PRIOCEILING 0
344 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
345 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
346 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
348 #define _PTHREAD_NO_SIG 0x00000000
349 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
350 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
351 #define _PTHREAD_MUTEX_SIG_fast 0x4D55545A /* 'MUTZ' */
352 #define _PTHREAD_MUTEX_SIG_MASK 0xfffffffd
353 #define _PTHREAD_MUTEX_SIG_CMP 0x4D555458 /* _PTHREAD_MUTEX_SIG & _PTHREAD_MUTEX_SIG_MASK */
354 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
355 #define _PTHREAD_ERRORCHECK_MUTEX_SIG_init 0x32AAABA1
356 #define _PTHREAD_RECURSIVE_MUTEX_SIG_init 0x32AAABA2
357 #define _PTHREAD_FIRSTFIT_MUTEX_SIG_init 0x32AAABA3
358 #define _PTHREAD_MUTEX_SIG_init_MASK 0xfffffff0
359 #define _PTHREAD_MUTEX_SIG_init_CMP 0x32AAABA0
360 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
361 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
362 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
363 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
364 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
365 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
366 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
367 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
368 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
369 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
372 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
373 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
374 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
376 #define _PTHREAD_CREATE_PARENT 4
377 #define _PTHREAD_EXITED 8
379 #define _PTHREAD_WASCANCEL 0x10
383 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
385 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
387 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
389 #define _PTHREAD_CANCEL_STATE_MASK 0x01
390 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
391 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
393 extern boolean_t
swtch_pri(int);
395 #define PTHREAD_EXPORT extern __attribute__((visibility("default")))
396 #define PTHREAD_EXTERN extern
397 #define PTHREAD_NOEXPORT __attribute__((visibility("hidden")))
398 #define PTHREAD_NORETURN __attribute__((__noreturn__))
399 #define PTHREAD_ALWAYS_INLINE __attribute__((always_inline))
400 #define PTHREAD_NOINLINE __attribute__((noinline))
401 #define PTHREAD_WEAK __attribute__((weak))
402 #define PTHREAD_USED __attribute__((used))
404 #include "kern/kern_internal.h"
408 /* Internal globals. */
409 PTHREAD_NOEXPORT
extern int __pthread_supported_features
;
411 /* Functions defined in machine-dependent files. */
412 PTHREAD_NOEXPORT
void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
414 PTHREAD_NOEXPORT
void _pthread_tsd_cleanup(pthread_t self
);
416 PTHREAD_NOEXPORT
int __mtx_droplock(_pthread_mutex
*mutex
, uint32_t * flagp
, uint32_t ** pmtxp
, uint32_t * mgenp
, uint32_t * ugenp
);
418 /* internally redirected upcalls. */
419 PTHREAD_NOEXPORT
void* malloc(size_t);
420 PTHREAD_NOEXPORT
void free(void*);
422 /* syscall interfaces */
423 extern uint32_t __psynch_mutexwait(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
424 extern uint32_t __psynch_mutexdrop(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
426 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
);
427 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
);
428 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
);
429 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
);
430 extern uint32_t __psynch_rw_longrdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
431 extern uint32_t __psynch_rw_yieldwrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
432 extern int __psynch_rw_downgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
433 extern uint32_t __psynch_rw_upgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
434 extern uint32_t __psynch_rw_rdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
435 extern uint32_t __psynch_rw_wrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
436 extern uint32_t __psynch_rw_unlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
437 extern uint32_t __psynch_rw_unlock2(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
438 extern uint32_t __bsdthread_ctl(uintptr_t cmd
, uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
);
442 __proc_info(int callnum
, int pid
, int flavor
, uint64_t arg
, void * buffer
, int buffersize
);
444 PTHREAD_NOEXPORT
int _pthread_lookup_thread(pthread_t thread
, mach_port_t
* port
, int only_joinable
);
445 PTHREAD_NOEXPORT
int _pthread_join_cleanup(pthread_t thread
, void ** value_ptr
, int conforming
);
447 PTHREAD_NORETURN PTHREAD_NOEXPORT
449 __pthread_abort(void);
451 PTHREAD_NORETURN PTHREAD_NOEXPORT
453 __pthread_abort_reason(const char *fmt
, ...);
457 _pthread_set_main_qos(pthread_priority_t qos
);
461 _pthread_key_global_init(const char *envp
[]);
465 _pthread_start(pthread_t self
, mach_port_t kport
, void *(*fun
)(void *), void * funarg
, size_t stacksize
, unsigned int flags
);
469 _pthread_wqthread(pthread_t self
, mach_port_t kport
, void *stackaddr
, void *keventlist
, int flags
, int nkevents
);
473 __pthread_fork_child_internal(pthread_t p
);
477 _pthread_clear_qos_tsd(mach_port_t thread_port
);
481 _pthread_testcancel(pthread_t thread
, int isconforming
);
485 _pthread_exit_if_canceled(int error
);
487 PTHREAD_ALWAYS_INLINE
488 static inline mach_port_t
489 _pthread_kernel_thread(pthread_t t
)
491 return t
->tsd
[_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
];
494 PTHREAD_ALWAYS_INLINE
496 _pthread_set_kernel_thread(pthread_t t
, mach_port_t p
)
498 t
->tsd
[_PTHREAD_TSD_SLOT_MACH_THREAD_SELF
] = p
;
501 #define PTHREAD_ABORT(f,...) __pthread_abort_reason("%s:%s:%u: " f, __FILE__, __func__, __LINE__, ## __VA_ARGS__)
503 #define PTHREAD_ASSERT(b) do { if (!(b)) PTHREAD_ABORT("failed assertion `%s'", #b); } while (0)
505 #include <os/semaphore_private.h>
506 #include <os/alloc_once_private.h>
508 struct pthread_atfork_entry
{
509 void (*prepare
)(void);
510 void (*parent
)(void);
514 #define PTHREAD_ATFORK_INLINE_MAX 10
515 #define PTHREAD_ATFORK_MAX (vm_page_size/sizeof(struct pthread_atfork_entry))
517 struct pthread_globals_s
{
519 pthread_t psaved_self
;
520 OSSpinLock psaved_self_global_lock
;
521 OSSpinLock pthread_atfork_lock
;
524 struct pthread_atfork_entry atfork_storage
[PTHREAD_ATFORK_INLINE_MAX
];
525 struct pthread_atfork_entry
*atfork
;
527 typedef struct pthread_globals_s
*pthread_globals_t
;
529 __attribute__((__pure__
))
530 static inline pthread_globals_t
531 _pthread_globals(void)
533 return os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_PTHREAD
,
534 sizeof(struct pthread_globals_s
),
538 #pragma mark _pthread_mutex_check_signature
540 PTHREAD_ALWAYS_INLINE
542 _pthread_mutex_check_signature_fast(_pthread_mutex
*mutex
)
544 return os_fastpath(mutex
->sig
== _PTHREAD_MUTEX_SIG_fast
);
547 PTHREAD_ALWAYS_INLINE
549 _pthread_mutex_check_signature(_pthread_mutex
*mutex
)
551 return os_fastpath((mutex
->sig
& _PTHREAD_MUTEX_SIG_MASK
) == _PTHREAD_MUTEX_SIG_CMP
);
554 PTHREAD_ALWAYS_INLINE
556 _pthread_mutex_check_signature_init(_pthread_mutex
*mutex
)
558 return os_fastpath((mutex
->sig
& _PTHREAD_MUTEX_SIG_init_MASK
) == _PTHREAD_MUTEX_SIG_init_CMP
);
561 #endif /* _POSIX_PTHREAD_INTERNALS_H */