2 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both the copyright notice and this permission notice appear in
9 * supporting documentation.
11 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
12 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
15 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
16 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
18 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
19 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * POSIX Threads - IEEE 1003.1c
30 #ifndef _POSIX_PTHREAD_INTERNALS_H
31 #define _POSIX_PTHREAD_INTERNALS_H
35 #include <mach/port.h>
36 #include <mach/mach_error.h>
37 #include <mach/message.h>
38 #include <mach/machine/vm_types.h>
39 #include <mach/std_types.h>
40 #include <mach/policy.h>
41 #include <mach/sync.h>
42 #include <mach/sync_policy.h>
43 #include <mach/mach_traps.h>
44 #include <mach/thread_switch.h>
45 #include <mach/mach_host.h>
46 #include <mach/mach.h> /* For generic MACH support */
53 #include "posix_sched.h" /* For POSIX scheduling policy & parameter */
54 #include "pthread_machdep.h" /* Machine-dependent definitions. */
56 extern kern_return_t
syscall_thread_switch(mach_port_name_t
, int, mach_msg_timeout_t
);
61 #undef _POSIX_THREAD_KEYS_MAX
62 #define _POSIX_THREAD_KEYS_MAX 128
67 typedef struct _pthread
69 long sig
; /* Unique signature for this structure */
70 struct _pthread_handler_rec
*cleanup_stack
;
71 pthread_lock_t lock
; /* Used for internal mutex on structure */
75 struct sched_param param
;
76 struct _pthread_mutex
*mutexes
;
77 struct _pthread
*joiner
;
80 semaphore_t death
; /* pthread_join() uses this to wait for death's call */
81 mach_port_t kernel_thread
; /* kernel thread this thread is bound to */
82 void *(*fun
)(void*);/* Thread start routine */
83 void *arg
; /* Argment for thread start routine */
84 int cancel_state
; /* Whether thread can be cancelled */
85 int err_no
; /* thread-local errno */
86 void *tsd
[_POSIX_THREAD_KEYS_MAX
]; /* Thread specific data */
87 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
88 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
89 mach_port_t reply_port
; /* Cached MiG reply port */
90 void *cthread_self
; /* cthread_self() if somebody calls cthread_set_self() */
91 boolean_t freeStackOnExit
;/* Should we free the stack when we're done? */
100 long sig
; /* Unique signature for this structure */
101 pthread_lock_t lock
; /* Used for internal mutex on structure */
105 struct sched_param param
;
106 void *stackaddr
; /* Base of the stack (is aligned on vm_page_size boundary */
107 size_t stacksize
; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
108 boolean_t freeStackOnExit
;/* Should we free the stack when we exit? */
116 long sig
; /* Unique signature for this structure */
118 u_int32_t protocol
:2, /* protocol attribute */
119 type
:2, /* mutex type */
121 } pthread_mutexattr_t
;
126 typedef struct _pthread_mutex
128 long sig
; /* Unique signature for this structure */
129 pthread_lock_t lock
; /* Used for internal mutex on structure */
130 int waiters
:30, /* Count of threads waiting for this mutex */
132 cond_lock
:1; /* Is there a condition locking me? */
133 pthread_t owner
; /* Which thread has this mutex locked */
134 mach_port_t sem
; /* Semaphore used for waiting */
135 u_int32_t protocol
:2, /* protocol */
136 type
:2, /* mutex type */
139 struct _pthread_mutex
*next
, *prev
; /* List of other mutexes he owns */
140 struct _pthread_cond
*busy
; /* List of condition variables using this mutex */
142 int priority
; /* Priority to restore when mutex unlocked */
146 * Condition variable attributes
150 long sig
; /* Unique signature for this structure */
152 } pthread_condattr_t
;
155 * Condition variables
157 typedef struct _pthread_cond
159 long sig
; /* Unique signature for this structure */
160 pthread_lock_t lock
; /* Used for internal mutex on structure */
161 mach_port_t sem
; /* Kernel semaphore */
162 struct _pthread_cond
*next
, *prev
; /* List of condition variables using mutex */
163 struct _pthread_mutex
*busy
; /* mutex associated with variable */
164 int waiters
:16, /* Number of threads waiting */
165 sigspending
:16; /* Number of outstanding signals */
169 * Initialization control (once) variables
173 long sig
; /* Unique signature for this structure */
174 pthread_lock_t lock
; /* Used for internal mutex on structure */
178 long sig
; /* Unique signature for this structure */
180 int rfu
[2]; /* reserved for future use */
181 } pthread_rwlockattr_t
;
185 pthread_mutex_t lock
; /* monitor lock */
187 pthread_cond_t read_signal
;
188 pthread_cond_t write_signal
;
196 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
197 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
198 #define _PTHREAD_DEFAULT_PRIOCEILING 0
199 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
200 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
202 #define _PTHREAD_NO_SIG 0x00000000
203 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
204 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
205 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
206 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
207 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
208 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
209 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
210 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
211 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
212 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
213 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
214 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
215 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
217 #define _PTHREAD_CREATE_PARENT 4
218 #define _PTHREAD_EXITED 8
220 #define _PTHREAD_CANCEL_STATE_MASK 0xFE
221 #define _PTHREAD_CANCEL_TYPE_MASK 0xFD
222 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
224 extern boolean_t
swtch_pri(int);
226 /* Number of times to spin when the lock is unavailable and we are on a
227 multiprocessor. On a uniprocessor we yield the processor immediately. */
228 #define MP_SPIN_TRIES 1000
229 extern int _spin_tries
;
230 extern int __is_threaded
;
231 extern int _cpu_has_altivec
;
233 /* Internal mutex locks for data structures */
234 #define TRY_LOCK(v) (!__is_threaded || _spin_lock_try((pthread_lock_t *)&(v)))
237 if (__is_threaded) { \
238 int tries = _spin_tries; \
240 while (!_spin_lock_try((pthread_lock_t *)&(v))) { \
244 syscall_thread_switch(THREAD_NULL, SWITCH_OPTION_DEPRESS, 1); \
245 tries = _spin_tries; \
252 _spin_unlock((pthread_lock_t *)&(v)); \
259 #ifndef PTHREAD_MACH_CALL
260 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
265 /* Functions defined in machine-dependent files. */
266 extern vm_address_t
_sp(void);
267 extern vm_address_t
_adjust_sp(vm_address_t sp
);
268 extern void _spin_lock(pthread_lock_t
*lockp
);
269 extern int _spin_lock_try(pthread_lock_t
*lockp
);
270 extern void _spin_unlock(pthread_lock_t
*lockp
);
271 extern void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
273 extern void _pthread_tsd_cleanup(pthread_t self
);
275 __private_extern__ semaphore_t
new_sem_from_pool(void);
276 __private_extern__
void restore_sem_to_pool(semaphore_t
);
277 #endif /* _POSIX_PTHREAD_INTERNALS_H */