2 * Copyright (c) 2000-2003, 2007 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 // suppress pthread_attr_t typedef in sys/signal.h
56 #define _PTHREAD_ATTR_T
57 struct _pthread_attr_t
; /* forward reference */
58 typedef struct _pthread_attr_t pthread_attr_t
;
66 #include <TargetConditionals.h>
67 #include <mach/mach.h>
68 #include <mach/mach_error.h>
69 #include <libkern/OSAtomic.h>
76 #include "posix_sched.h" /* For POSIX scheduling policy & parameter */
77 #include <sys/queue.h> /* For POSIX scheduling policy & parameter */
78 #include "pthread_machdep.h" /* Machine-dependent definitions. */
79 #include "pthread_spinlock.h" /* spinlock definitions. */
81 TAILQ_HEAD(__pthread_list
, _pthread
);
83 extern int __pthread_lock_debug
;
84 extern int __pthread_lock_old
;
86 extern struct __pthread_list __pthread_head
; /* head of list of open files */
87 extern pthread_lock_t _pthread_list_lock
;
88 extern size_t pthreadsize
;
92 #if TARGET_OS_EMBEDDED
93 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 256
94 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
95 #define _INTERNAL_POSIX_THREAD_KEYS_END 512
97 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
98 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
99 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
105 #define MAXTHREADNAMESIZE 64
107 typedef struct _pthread
109 long sig
; /* Unique signature for this structure */
110 struct __darwin_pthread_handler_rec
*__cleanup_stack
;
111 pthread_lock_t lock
; /* Used for internal mutex on structure */
122 size_t guardsize
; /* size in bytes to guard stack overflow */
123 #if !defined(__LP64__)
124 int pad0
; /* for backwards compatibility */
126 struct sched_param param
;
127 uint32_t cancel_error
;
128 #if defined(__LP64__)
129 uint32_t cancel_pad
; /* pad value for alignment */
131 struct _pthread
*joiner
;
132 #if !defined(__LP64__)
133 int pad1
; /* for backwards compatibility */
136 semaphore_t death
; /* pthread_join() uses this to wait for death's call */
137 mach_port_t kernel_thread
; /* kernel thread this thread is bound to */
138 void *(*fun
)(void*);/* Thread start routine */
139 void *arg
; /* Argment for thread start routine */
140 int cancel_state
; /* Whether thread can be cancelled */
141 int err_no
; /* thread-local errno */
142 void *tsd
[_EXTERNAL_POSIX_THREAD_KEYS_MAX
+ _INTERNAL_POSIX_THREAD_KEYS_MAX
]; /* Thread specific data */
143 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
144 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
145 mach_port_t reply_port
; /* Cached MiG reply port */
146 #if defined(__LP64__)
147 int pad2
; /* for natural alignment */
149 void *cthread_self
; /* cthread_self() if somebody calls cthread_set_self() */
150 /* protected by list lock */
155 #if defined(__LP64__)
156 int pad4
; /* for natural alignment */
158 TAILQ_ENTRY(_pthread
) plist
;
161 mach_port_t joiner_notify
;
162 char pthread_name
[MAXTHREADNAMESIZE
]; /* including nulll the name */
172 struct _pthread_attr_t
174 long sig
; /* Unique signature for this structure */
183 size_t guardsize
; /* size in bytes to guard stack overflow */
184 int reserved2
; /* Should we free the stack when we exit? */
185 struct sched_param param
;
186 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
187 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
194 #define _PTHREAD_MUTEX_POLICY_NONE 0
195 #define _PTHREAD_MUTEX_POLICY_FAIRSHARE 1
196 #define _PTHREAD_MUTEX_POLICY_FIRSTFIT 2
197 #define _PTHREAD_MUTEX_POLICY_REALTIME 3
198 #define _PTHREAD_MUTEX_POLICY_ADAPTIVE 4
199 #define _PTHREAD_MUTEX_POLICY_PRIPROTECT 5
200 #define _PTHREAD_MUTEX_POLICY_PRIINHERIT 6
202 #define _PTHREAD_MUTEXATTR_T
205 long sig
; /* Unique signature for this structure */
207 uint32_t protocol
:2, /* protocol attribute */
208 type
:2, /* mutex type */
212 } pthread_mutexattr_t
;
217 struct _pthread_mutex_options
{
218 uint32_t protocol
:2, /* protocol */
219 type
:2, /* mutex type */
220 pshared
:2, /* mutex type */
223 misalign
:1, /* 8 byte aligned? */
224 notify
:1, /* CV notify field for kernel */
225 mutex
:1, /* used in clrprepo that it is a mutex */
231 #define _PTHREAD_MTX_OPT_PSHARED 0x010
232 #define _PTHREAD_MTX_OPT_HOLD 0x200 /* current owner of the mutex */
233 #define _PTHREAD_MTX_OPT_NOMTX 0x400 /* no mutex refs held */
235 #define _PTHREAD_MTX_OPT_NOTIFY 0x1000 /* notify to drop mutex handling in cvwait */
236 #define _PTHREAD_MTX_OPT_MUTEX 0x2000 /* this is a mutex type */
239 #define _PTHREAD_MUTEX_T
240 typedef struct _pthread_mutex
242 long sig
; /* Unique signature for this structure */
243 pthread_lock_t lock
; /* Used for internal mutex on structure */
246 struct _pthread_mutex_options options
;
249 int16_t priority
; /* Priority to restore when mutex unlocked */
250 uint32_t waiters
; /* Count of threads waiting for this mutex */
251 pthread_t owner
; /* Which thread has this mutex locked */
252 struct _pthread_mutex
*next
, *prev
; /* List of other mutexes he owns */
253 struct _pthread_cond
*busy
; /* List of condition variables using this mutex */
254 semaphore_t sem
; /* Semaphore used for waiting */
259 typedef struct _npthread_mutex
261 /* keep same as pthread_mutex_t from here to .. */
262 long sig
; /* Unique signature for this structure */
263 pthread_lock_t lock
; /* Used for static init sequencing */
266 struct _pthread_mutex_options options
;
269 int16_t priority
; /* Priority to restore when mutex unlocked */
271 #if defined(__LP64__)
272 uint64_t m_tid
; /* Which thread has this mutex locked */
273 uint32_t * m_lseqaddr
;
274 uint32_t * m_useqaddr
;
275 uint32_t reserved
[2];
277 uint32_t * m_lseqaddr
;
278 uint64_t m_tid
; /* Which thread has this mutex locked */
279 uint32_t * m_useqaddr
;
287 * Condition variable attributes
289 #define _PTHREAD_CONDATTR_T
292 long sig
; /* Unique signature for this structure */
293 uint32_t pshared
:2, /* pshared */
295 } pthread_condattr_t
;
298 * Condition variables
300 #define _PTHREAD_COND_T
301 typedef struct _pthread_cond
303 long sig
; /* Unique signature for this structure */
304 pthread_lock_t lock
; /* Used for internal mutex on structure */
305 uint32_t waiters
:15, /* Number of threads waiting */
306 sigspending
:15, /* Number of outstanding signals */
308 struct _pthread_cond
*next
, *prev
; /* List of condition variables using mutex */
309 struct _pthread_mutex
*busy
; /* mutex associated with variable */
310 semaphore_t sem
; /* Kernel semaphore */
314 typedef struct _npthread_cond
316 long sig
; /* Unique signature for this structure */
317 pthread_lock_t lock
; /* Used for internal mutex on structure */
318 uint32_t rfu
:29, /* not in use*/
319 misalign
: 1, /* structure is not aligned to 8 byte boundary */
321 struct _npthread_mutex
*busy
; /* mutex associated with variable */
323 #if defined(__LP64__)
324 uint32_t reserved
[3];
325 #endif /* __LP64__ */
329 * Initialization control (once) variables
331 #define _PTHREAD_ONCE_T
334 long sig
; /* Unique signature for this structure */
335 pthread_lock_t lock
; /* Used for internal mutex on structure */
338 #define _PTHREAD_RWLOCKATTR_T
340 long sig
; /* Unique signature for this structure */
342 int rfu
[2]; /* reserved for future use */
343 } pthread_rwlockattr_t
;
345 #define _PTHREAD_RWLOCK_T
348 pthread_mutex_t lock
; /* monitor lock */
350 pthread_cond_t read_signal
;
351 pthread_cond_t write_signal
;
359 #define PTHRW_RFU_64BIT 124 /* 31 * sizeof(uint32_t) */
360 #define PTHRW_RFU_32BIT 72 /* 18 * sizeof(uint32_t) */
362 #define _PTHREAD_RWLOCK_T
366 #if defined(__LP64__)
368 volatile uint32_t rw_seq
[4];
371 volatile uint32_t rw_seq
[4];
374 #endif /* __LP64__ */
375 volatile uint32_t * rw_lcntaddr
;
376 volatile uint32_t * rw_seqaddr
;
377 volatile uint32_t * rw_ucntaddr
;
380 #if defined(__LP64__)
381 char rfu
[PTHRW_RFU_64BIT
];
383 char rfu
[PTHRW_RFU_32BIT
];
384 #endif /* __LP64__ */
388 /* flags for rw_flags */
389 #define PTHRW_KERN_PROCESS_SHARED 0x10
390 #define PTHRW_KERN_PROCESS_PRIVATE 0x20
391 #define PTHRW_KERN_PROCESS_FLAGS_MASK 0x30
392 #define _PTHREAD_RWLOCK_UPGRADE_TRY 0x10000
394 /* New model bits on Lword */
395 #define PTH_RWL_KBIT 0x01 /* users cannot acquire in user mode */
396 #define PTH_RWL_EBIT 0x02 /* exclusive lock in progress */
397 #define PTH_RWL_WBIT 0x04 /* write waiters pending in kernel */
398 #define PTH_RWL_PBIT 0x04 /* prepost (cv) pending in kernel */
399 #define PTH_RWL_YBIT 0x08 /* yielding write waiters pending in kernel */
400 #define PTH_RWL_RETRYBIT 0x08 /* mutex retry wait */
401 #define PTH_RWL_LBIT 0x10 /* long read in progress */
402 #define PTH_RWL_MTXNONE 0x10 /* indicates the cvwait does not have mutex held */
403 #define PTH_RWL_UBIT 0x20 /* upgrade request pending */
404 #define PTH_RWL_MTX_WAIT 0x20 /* in cvar in mutex wait */
405 #define PTH_RWL_RBIT 0x40 /* reader pending in kernel(not used) */
406 #define PTH_RWL_MBIT 0x40 /* overlapping grants from kernel */
407 #define PTH_RWL_TRYLKBIT 0x40 /* sets try lock attempt */
408 #define PTH_RWL_IBIT 0x80 /* lock reset, held untill first succeesful unlock */
410 /* UBIT values for mutex, cvar */
411 #define PTH_RWU_SBIT 0x01
412 #define PTH_RWU_BBIT 0x02
414 #define PTHRW_RWL_INIT PTH_RWL_IBIT /* reset on the lock bits (U)*/
415 #define PTHRW_RWLOCK_INIT (PTH_RWL_IBIT | PTH_RWL_RBIT) /* reset on the lock bits (U)*/
416 #define PTH_RWLOCK_RESET_RBIT 0xffffffbf
418 #define PTHRW_INC 0x100
419 #define PTHRW_BIT_MASK 0x000000ff
421 #define PTHRW_UN_BIT_MASK 0x000000bf /* remove overlap bit */
424 /* New model bits on Sword */
425 #define PTH_RWS_SBIT 0x01 /* kernel transition seq not set yet*/
426 #define PTH_RWS_IBIT 0x02 /* Sequence is not set on return from kernel */
428 #define PTH_RWS_CV_CBIT PTH_RWS_SBIT /* kernel has cleared all info w.r.s.t CV */
429 #define PTH_RWS_CV_PBIT PTH_RWS_IBIT /* kernel has prepost/fake structs only,no waiters */
430 #define PTH_RWS_CV_BITSALL (PTH_RWS_CV_CBIT | PTH_RWS_CV_PBIT)
431 #define PTH_RWS_CV_MBIT PTH_RWL_MBIT /* to indicate prepost return from kernel */
432 #define PTH_RWS_CV_RESET_PBIT 0xfffffffd
434 #define PTH_RWS_WSVBIT 0x04 /* save W bit */
435 #define PTH_RWS_USVBIT 0x08 /* save U bit */
436 #define PTH_RWS_YSVBIT 0x10 /* save Y bit */
437 #define PTHRW_RWS_INIT PTH_RWS_SBIT /* reset on the lock bits (U)*/
438 #define PTHRW_RWS_SAVEMASK (PTH_RWS_WSVBIT|PTH_RWS_USVBIT|PTH_RWS_YSVBIT) /*save bits mask*/
439 #define PTHRW_SW_Reset_BIT_MASK 0x000000fe /* remove S bit and get rest of the bits */
441 #define PTHRW_COUNT_SHIFT 8
442 #define PTHRW_COUNT_MASK 0xffffff00
443 #define PTHRW_MAX_READERS 0xffffff00
446 #define PTHREAD_MTX_TID_SWITCHING (uint64_t)-1
448 /* new L word defns */
449 #define can_rwl_readinuser(x) ((((x) & (PTH_RWL_UBIT | PTH_RWL_WBIT | PTH_RWL_KBIT)) == 0)||(((x) & PTH_RWL_LBIT) != 0))
450 #define can_rwl_longreadinuser(x) (((x) & (PTH_RWL_UBIT | PTH_RWL_WBIT | PTH_RWL_KBIT | PTH_RWL_YBIT)) == 0)
451 #define is_rwl_ebit_set(x) (((x) & PTH_RWL_EBIT) != 0)
452 #define is_rwl_eubit_set(x) (((x) & (PTH_RWL_EBIT | PTH_RWL_UBIT)) != 0)
453 #define is_rwl_wbit_set(x) (((x) & PTH_RWL_WBIT) != 0)
454 #define is_rwl_lbit_set(x) (((x) & PTH_RWL_LBIT) != 0)
455 #define is_rwl_ebit_clear(x) (((x) & PTH_RWL_EBIT) == 0)
456 #define is_rwl_lbit_clear(x) (((x) & PTH_RWL_LBIT) == 0)
457 #define is_rwl_readoverlap(x) (((x) & PTH_RWL_MBIT) != 0)
460 #define is_rws_setseq(x) (((x) & PTH_RWS_SBIT))
461 #define is_rws_setunlockinit(x) (((x) & PTH_RWS_IBIT))
463 /* is x lower than Y */
464 static inline int is_seqlower(uint32_t x
, uint32_t y
) {
466 if ((y
-x
) < (PTHRW_MAX_READERS
/2))
469 if ((x
-y
) > (PTHRW_MAX_READERS
/2))
475 /* is x lower than or eq Y */
476 static inline int is_seqlower_eq(uint32_t x
, uint32_t y
) {
480 return(is_seqlower(x
,y
));
483 /* is x greater than Y */
484 static inline int is_seqhigher(uint32_t x
, uint32_t y
) {
486 if ((x
-y
) < (PTHRW_MAX_READERS
/2))
489 if ((y
-x
) > (PTHRW_MAX_READERS
/2))
495 static inline int diff_genseq(uint32_t x
, uint32_t y
) {
501 return((PTHRW_MAX_READERS
- y
) + x
+ PTHRW_INC
);
505 /* keep the size to 64bytes for both 64 and 32 */
506 #define _PTHREAD_WORKQUEUE_ATTR_T
511 unsigned int resv2
[13];
512 } pthread_workqueue_attr_t
;
514 #define _PTHREAD_WORKITEM_T
515 typedef struct _pthread_workitem
{
516 TAILQ_ENTRY(_pthread_workitem
) item_entry
; /* pthread_workitem list in prio */
517 void (*func
)(void *);
519 struct _pthread_workqueue
* workq
;
521 unsigned int fromcache
; /* padding for 64bit */
522 } * pthread_workitem_t
;
524 #define PTH_WQITEM_INKERNEL_QUEUE 1
525 #define PTH_WQITEM_RUNNING 2
526 #define PTH_WQITEM_COMPLETED 4
527 #define PTH_WQITEM_REMOVED 8
528 #define PTH_WQITEM_BARRIER 0x10
529 #define PTH_WQITEM_DESTROY 0x20
530 #define PTH_WQITEM_NOTINLIST 0x40
531 #define PTH_WQITEM_APPLIED 0x80
532 #define PTH_WQITEM_KERN_COUNT 0x100
534 /* try to fit these within multiple of pages (8 pages for now) */
535 #define WORKITEM_POOL_SIZE 680
536 /* ensure some multiple of the chunk is the pool size */
537 #define WORKITEM_CHUNK_SIZE 40
539 #define WORKITEM_STARTPOOL_SIZE WORKITEM_CHUNK_SIZE
541 TAILQ_HEAD(__pthread_workitem_pool
, _pthread_workitem
);
542 extern struct __pthread_workitem_pool __pthread_workitem_pool_head
; /* head list of workitem pool */
544 #define _PTHREAD_WORKQUEUE_HEAD_T
545 typedef struct _pthread_workqueue_head
{
546 TAILQ_HEAD(, _pthread_workqueue
) wqhead
;
547 struct _pthread_workqueue
* next_workq
;
548 } * pthread_workqueue_head_t
;
551 /* sized to be 128 bytes both in 32 and 64bit archs */
552 #define _PTHREAD_WORKQUEUE_T
553 typedef struct _pthread_workqueue
{
554 unsigned int sig
; /* Unique signature for this structure */
555 pthread_lock_t lock
; /* Used for internal mutex on structure */
556 TAILQ_ENTRY(_pthread_workqueue
) wq_list
; /* workqueue list in prio */
557 TAILQ_HEAD(, _pthread_workitem
) item_listhead
; /* pthread_workitem list in prio */
558 TAILQ_HEAD(, _pthread_workitem
) item_kernhead
; /* pthread_workitem list in prio */
567 void (*term_callback
)(struct _pthread_workqueue
*,void *);
569 pthread_workqueue_head_t headp
;
571 #if !defined(__LP64__)
572 unsigned int rev2
[12];
574 } * pthread_workqueue_t
;
576 #define PTHREAD_WORKQ_IN_CREATION 1
577 #define PTHREAD_WORKQ_IN_TERMINATE 2
578 #define PTHREAD_WORKQ_BARRIER_ON 4
579 #define PTHREAD_WORKQ_TERM_ON 8
580 #define PTHREAD_WORKQ_DESTROYED 0x10
581 #define PTHREAD_WORKQ_REQUEUED 0x20
582 #define PTHREAD_WORKQ_SUSPEND 0x40
584 #define WORKQUEUE_POOL_SIZE 16
585 TAILQ_HEAD(__pthread_workqueue_pool
, _pthread_workqueue
);
586 extern struct __pthread_workqueue_pool __pthread_workqueue_pool_head
; /* head list of workqueue pool */
588 #include "pthread_spis.h"
590 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__) || (defined(__arm__) && (defined(_ARM_ARCH_7) || !defined(_ARM_ARCH_6) || !defined(__thumb__)))
592 * Inside libSystem, we can use r13 or %gs directly to get access to the
593 * thread-specific data area. The current thread is in the first slot.
595 inline static pthread_t
__attribute__((__pure__
))
596 _pthread_self_direct(void)
600 #if defined(__i386__) || defined(__x86_64__)
601 ret
= _pthread_getspecific_direct(0);
602 #elif defined(__ppc64__)
603 register const pthread_t __pthread_self
asm ("r13");
604 ret
= __pthread_self
;
605 #elif defined(__arm__) && defined(_ARM_ARCH_6)
606 ret
= _pthread_getspecific_direct(0);
607 #elif defined(__arm__) && !defined(_ARM_ARCH_6)
608 ret
= _pthread_getspecific_direct(0);
612 #define pthread_self() _pthread_self_direct()
615 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
616 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
617 #define _PTHREAD_DEFAULT_PRIOCEILING 0
618 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
619 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
620 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
622 #define _PTHREAD_NO_SIG 0x00000000
623 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
624 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
625 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
626 #define _PTHREAD_ERRORCHECK_MUTEX_SIG_init 0x32AAABA1
627 #define _PTHREAD_RECURSIVE_MUTEX_SIG_init 0x32AAABA2
628 #define _PTHREAD_FIRSTFIT_MUTEX_SIG_init 0x32AAABA3
629 #define _PTHREAD_MUTEX_SIG_init_MASK 0xfffffff0
630 #define _PTHREAD_MUTEX_SIG_CMP 0x32AAABA0
631 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
632 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
633 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
634 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
635 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
636 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
637 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
638 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
639 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
640 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
643 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
644 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
645 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
647 #define _PTHREAD_CREATE_PARENT 4
648 #define _PTHREAD_EXITED 8
650 #define _PTHREAD_WASCANCEL 0x10
654 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
656 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
658 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
660 #define _PTHREAD_CANCEL_STATE_MASK 0x01
661 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
662 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
664 extern boolean_t
swtch_pri(int);
666 #ifndef PTHREAD_MACH_CALL
667 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
672 /* Functions defined in machine-dependent files. */
673 extern vm_address_t
_sp(void);
674 extern vm_address_t
_adjust_sp(vm_address_t sp
);
675 extern void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
677 extern void _pthread_tsd_cleanup(pthread_t self
);
679 __private_extern__
void __mtx_holdlock(npthread_mutex_t
*mutex
, uint32_t diff
, uint32_t * flagp
, uint32_t ** pmtxp
, uint32_t * mgenp
, uint32_t * ugenp
, uint64_t *tidp
);
680 __private_extern__
int __mtx_droplock(npthread_mutex_t
*mutex
, uint32_t diff
, uint32_t * flagp
, uint32_t ** pmtxp
, uint32_t * mgenp
, uint32_t * ugenp
);
681 __private_extern__
int __mtx_updatebits(npthread_mutex_t
*mutex
, uint32_t updateval
, int firstfiti
, int fromcond
, uint64_t selfid
);
683 /* syscall interfaces */
684 extern uint32_t __psynch_mutexwait(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
685 extern uint32_t __psynch_mutexdrop(pthread_mutex_t
* mutex
, uint32_t mgen
, uint32_t ugen
, uint64_t tid
, uint32_t flags
);
687 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
);
688 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
);
689 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
);
690 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
);
691 extern uint32_t __psynch_rw_longrdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
692 extern uint32_t __psynch_rw_yieldwrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
693 extern int __psynch_rw_downgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
694 extern uint32_t __psynch_rw_upgrade(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
695 extern uint32_t __psynch_rw_rdlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
696 extern uint32_t __psynch_rw_wrlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
697 extern uint32_t __psynch_rw_unlock(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
698 extern uint32_t __psynch_rw_unlock2(pthread_rwlock_t
* rwlock
, uint32_t lgenval
, uint32_t ugenval
, uint32_t rw_wc
, int flags
);
700 __private_extern__ semaphore_t
new_sem_from_pool(void);
701 __private_extern__
void restore_sem_to_pool(semaphore_t
);
702 __private_extern__
void _pthread_atfork_queue_init(void);
703 int _pthread_lookup_thread(pthread_t thread
, mach_port_t
* port
, int only_joinable
);
704 int _pthread_join_cleanup(pthread_t thread
, void ** value_ptr
, int conforming
);
705 __private_extern__
int proc_setthreadname(void * buffer
, int buffersize
);
707 #endif /* _POSIX_PTHREAD_INTERNALS_H */