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 mach_port_t joiners
; /* pthread_join() uses this to wait for death's call */
80 mach_port_t death
; /* pthread_exit() uses this to wait for the hearse */
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 */
119 } pthread_mutexattr_t
;
124 typedef struct _pthread_mutex
126 long sig
; /* Unique signature for this structure */
127 pthread_lock_t lock
; /* Used for internal mutex on structure */
129 int priority
; /* Priority to restore when mutex unlocked */
131 pthread_t owner
; /* Which thread has this mutex locked */
132 struct _pthread_mutex
*next
, *prev
; /* List of other mutexes he owns */
133 struct _pthread_cond
*busy
; /* List of condition variables using this mutex */
134 int waiters
:31, /* Count of threads waiting for this mutex */
135 cond_lock
:1; /* Is there a condition locking me? */
136 mach_port_t sem
; /* Semaphore used for waiting */
140 * Condition variable attributes
144 long sig
; /* Unique signature for this structure */
146 } pthread_condattr_t
;
149 * Condition variables
151 typedef struct _pthread_cond
153 long sig
; /* Unique signature for this structure */
154 pthread_lock_t lock
; /* Used for internal mutex on structure */
155 mach_port_t sem
; /* Kernel semaphore */
156 struct _pthread_cond
*next
, *prev
; /* List of condition variables using mutex */
157 struct _pthread_mutex
*busy
; /* mutex associated with variable */
158 int waiters
:16, /* Number of threads waiting */
159 sigspending
:16; /* Number of outstanding signals */
163 * Initialization control (once) variables
167 long sig
; /* Unique signature for this structure */
168 pthread_lock_t lock
; /* Used for internal mutex on structure */
173 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
174 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
175 #define _PTHREAD_DEFAULT_PRIOCEILING 0
176 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
177 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
179 #define _PTHREAD_NO_SIG 0x00000000
180 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
181 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
182 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
183 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
184 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
185 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
186 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
187 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
188 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
190 #define _PTHREAD_EXITED 3
191 #define _PTHREAD_CREATE_PARENT 4
193 #define _PTHREAD_CANCEL_STATE_MASK 0xFE
194 #define _PTHREAD_CANCEL_TYPE_MASK 0xFD
195 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
197 extern boolean_t
swtch_pri(int);
199 /* Number of times to spin when the lock is unavailable and we are on a
200 multiprocessor. On a uniprocessor we yield the processor immediately. */
201 #define SPIN_TRIES 10
202 extern int _spin_tries
;
203 extern int __is_threaded
;
204 extern int _cpu_has_altivec
;
206 /* Internal mutex locks for data structures */
207 #define TRY_LOCK(v) (!__is_threaded || _spin_lock_try((pthread_lock_t *)&v))
209 #define LOCK(v) if (__is_threaded) _spin_lock((pthread_lock_t)&v)
212 if (__is_threaded) { \
213 while (!_spin_lock_try((pthread_lock_t *)&v)) { \
214 syscall_thread_switch(THREAD_NULL, SWITCH_OPTION_WAIT, 1); \
218 #define UNLOCK(v) if (__is_threaded) _spin_unlock((pthread_lock_t *)&v)
223 #ifndef PTHREAD_MACH_CALL
224 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
229 /* Functions defined in machine-dependent files. */
230 extern vm_address_t
_sp(void);
231 extern vm_address_t
_adjust_sp(vm_address_t sp
);
232 extern void _spin_lock(pthread_lock_t
*lockp
);
233 extern int _spin_lock_try(pthread_lock_t
*lockp
);
234 extern void _spin_unlock(pthread_lock_t
*lockp
);
235 extern void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), vm_address_t sp
);
237 extern void _pthread_tsd_cleanup(pthread_t self
);
239 __private_extern__ semaphore_t
new_sem_from_pool(void);
240 __private_extern__
void restore_sem_to_pool(semaphore_t
);
241 #endif /* _POSIX_PTHREAD_INTERNALS_H */