2 * Copyright (c) 2000-2003 Apple Computer, 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 <mach/mach.h>
67 #include <mach/mach_error.h>
74 #include "posix_sched.h" /* For POSIX scheduling policy & parameter */
75 #include <sys/queue.h> /* For POSIX scheduling policy & parameter */
76 #include "pthread_machdep.h" /* Machine-dependent definitions. */
77 #include "pthread_spinlock.h" /* spinlock definitions. */
79 LIST_HEAD(__pthread_list
, _pthread
);
80 extern struct __pthread_list __pthread_head
; /* head of list of open files */
81 extern pthread_lock_t _pthread_list_lock
;
85 #undef _POSIX_THREAD_KEYS_MAX
86 #define _POSIX_THREAD_KEYS_MAX 128
92 typedef struct _pthread
94 long sig
; /* Unique signature for this structure */
95 struct __darwin_pthread_handler_rec
*__cleanup_stack
;
96 pthread_lock_t lock
; /* Used for internal mutex on structure */
101 size_t guardsize
; /* size in bytes to guard stack overflow */
102 #if !defined(__LP64__)
103 int pad0
; /* for backwards compatibility */
105 struct sched_param param
;
106 struct _pthread_mutex
*mutexes
;
107 struct _pthread
*joiner
;
108 #if !defined(__LP64__)
109 int pad1
; /* for backwards compatibility */
112 semaphore_t death
; /* pthread_join() uses this to wait for death's call */
113 mach_port_t kernel_thread
; /* kernel thread this thread is bound to */
114 void *(*fun
)(void*);/* Thread start routine */
115 void *arg
; /* Argment for thread start routine */
116 int cancel_state
; /* Whether thread can be cancelled */
117 int err_no
; /* thread-local errno */
118 void *tsd
[_POSIX_THREAD_KEYS_MAX
]; /* Thread specific data */
119 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
120 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
121 mach_port_t reply_port
; /* Cached MiG reply port */
122 #if defined(__LP64__)
123 int pad2
; /* for natural alignment */
125 void *cthread_self
; /* cthread_self() if somebody calls cthread_set_self() */
126 boolean_t freeStackOnExit
; /* Should we free the stack when we're done? */
127 #if defined(__LP64__)
128 int pad3
; /* for natural alignment */
130 LIST_ENTRY(_pthread
) plist
;
134 * This will cause a compile-time failure if someone moved the tsd field
135 * and we need to change _PTHREAD_TSD_OFFSET in pthread_machdep.h
137 typedef char _need_to_change_PTHREAD_TSD_OFFSET
[(_PTHREAD_TSD_OFFSET
== offsetof(struct _pthread
, tsd
[0])) ? 0 : -1] ;
142 struct _pthread_attr_t
144 long sig
; /* Unique signature for this structure */
145 pthread_lock_t lock
; /* Used for internal mutex on structure */
146 u_int32_t detached
:8,
150 size_t guardsize
; /* size in bytes to guard stack overflow */
152 struct sched_param param
;
153 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
154 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
155 boolean_t freeStackOnExit
;/* Should we free the stack when we exit? */
161 #define _PTHREAD_MUTEXATTR_T
164 long sig
; /* Unique signature for this structure */
166 u_int32_t protocol
:2, /* protocol attribute */
167 type
:2, /* mutex type */
169 } pthread_mutexattr_t
;
174 #define _PTHREAD_MUTEX_T
175 typedef struct _pthread_mutex
177 long sig
; /* Unique signature for this structure */
178 pthread_lock_t lock
; /* Used for internal mutex on structure */
179 u_int32_t waiters
; /* Count of threads waiting for this mutex */
180 pthread_t owner
; /* Which thread has this mutex locked */
181 semaphore_t sem
; /* Semaphore used for waiting */
182 u_int32_t protocol
:2, /* protocol */
183 type
:2, /* mutex type */
186 struct _pthread_mutex
*next
, *prev
; /* List of other mutexes he owns */
187 struct _pthread_cond
*busy
; /* List of condition variables using this mutex */
189 int16_t priority
; /* Priority to restore when mutex unlocked */
194 * Condition variable attributes
196 #define _PTHREAD_CONDATTR_T
199 long sig
; /* Unique signature for this structure */
201 } pthread_condattr_t
;
204 * Condition variables
206 #define _PTHREAD_COND_T
207 typedef struct _pthread_cond
209 long sig
; /* Unique signature for this structure */
210 pthread_lock_t lock
; /* Used for internal mutex on structure */
211 semaphore_t sem
; /* Kernel semaphore */
212 struct _pthread_cond
*next
, *prev
; /* List of condition variables using mutex */
213 struct _pthread_mutex
*busy
; /* mutex associated with variable */
214 u_int32_t waiters
:16, /* Number of threads waiting */
215 sigspending
:16; /* Number of outstanding signals */
219 * Initialization control (once) variables
221 #define _PTHREAD_ONCE_T
224 long sig
; /* Unique signature for this structure */
225 pthread_lock_t lock
; /* Used for internal mutex on structure */
228 #define _PTHREAD_RWLOCKATTR_T
230 long sig
; /* Unique signature for this structure */
232 int rfu
[2]; /* reserved for future use */
233 } pthread_rwlockattr_t
;
235 #define _PTHREAD_RWLOCK_T
238 pthread_mutex_t lock
; /* monitor lock */
240 pthread_cond_t read_signal
;
241 pthread_cond_t write_signal
;
249 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__)
251 * Inside libSystem, we can use r13 or %gs directly to get access to the
252 * thread-specific data area. The current thread is in the first slot.
254 inline static pthread_t
__attribute__((__pure__
))
255 _pthread_self_direct(void)
258 #if defined(__i386__) || defined(__x86_64__)
259 asm("mov %%gs:%P1, %0" : "=r" (ret
) : "i" (offsetof(struct _pthread
, tsd
[0])));
260 #elif defined(__ppc64__)
261 register const pthread_t __pthread_self
asm ("r13");
262 ret
= __pthread_self
;
266 #define pthread_self() _pthread_self_direct()
269 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
270 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
271 #define _PTHREAD_DEFAULT_PRIOCEILING 0
272 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
273 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
275 #define _PTHREAD_NO_SIG 0x00000000
276 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
277 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
278 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
279 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
280 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
281 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
282 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
283 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
284 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
285 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
286 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
287 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
288 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
290 #define _PTHREAD_CREATE_PARENT 4
291 #define _PTHREAD_EXITED 8
294 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
296 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
298 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
300 #define _PTHREAD_CANCEL_STATE_MASK 0x01
301 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
302 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
304 extern boolean_t
swtch_pri(int);
310 #ifndef PTHREAD_MACH_CALL
311 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
316 /* Functions defined in machine-dependent files. */
317 extern vm_address_t
_sp(void);
318 extern vm_address_t
_adjust_sp(vm_address_t sp
);
319 extern void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
321 extern void _pthread_tsd_cleanup(pthread_t self
);
323 __private_extern__ semaphore_t
new_sem_from_pool(void);
324 __private_extern__
void restore_sem_to_pool(semaphore_t
);
325 __private_extern__
void _pthread_atfork_queue_init(void);
326 #endif /* _POSIX_PTHREAD_INTERNALS_H */