]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_internals.h
705d0d2e59f87c8e2dbf7316fcb26cc9d0ec394f
[apple/libc.git] / pthreads / pthread_internals.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * All Rights Reserved
28 *
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both the copyright notice and this permission notice appear in
33 * supporting documentation.
34 *
35 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
36 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE.
38 *
39 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
40 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
41 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
42 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 */
46 /*
47 * MkLinux
48 */
49
50 /*
51 * POSIX Threads - IEEE 1003.1c
52 */
53
54 #ifndef _POSIX_PTHREAD_INTERNALS_H
55 #define _POSIX_PTHREAD_INTERNALS_H
56
57
58 #include <assert.h>
59 #include <stddef.h>
60 #include <stdint.h>
61 #include <stdlib.h>
62 #include <mach/mach.h>
63 #include <mach/mach_error.h>
64
65
66 #ifndef __POSIX_LIB__
67 #define __POSIX_LIB__
68 #endif
69
70 #include "posix_sched.h" /* For POSIX scheduling policy & parameter */
71 #include <sys/queue.h> /* For POSIX scheduling policy & parameter */
72 #include "pthread_machdep.h" /* Machine-dependent definitions. */
73 #include "pthread_spinlock.h" /* spinlock definitions. */
74
75 LIST_HEAD(__pthread_list, _pthread);
76 extern struct __pthread_list __pthread_head; /* head of list of open files */
77 extern pthread_lock_t _pthread_list_lock;
78 /*
79 * Compiled-in limits
80 */
81 #undef _POSIX_THREAD_KEYS_MAX
82 #define _POSIX_THREAD_KEYS_MAX 128
83
84 /*
85 * Threads
86 */
87 typedef struct _pthread
88 {
89 long sig; /* Unique signature for this structure */
90 struct _pthread_handler_rec *cleanup_stack;
91 pthread_lock_t lock; /* Used for internal mutex on structure */
92 u_int32_t detached:8,
93 inherit:8,
94 policy:8,
95 pad:8;
96 size_t guardsize; /* size in bytes to guard stack overflow */
97 int pad0;
98 struct sched_param param;
99 struct _pthread_mutex *mutexes;
100 struct _pthread *joiner;
101 int pad1;
102 void *exit_value;
103 semaphore_t death; /* pthread_join() uses this to wait for death's call */
104 mach_port_t kernel_thread; /* kernel thread this thread is bound to */
105 void *(*fun)(void*);/* Thread start routine */
106 void *arg; /* Argment for thread start routine */
107 int cancel_state; /* Whether thread can be cancelled */
108 int err_no; /* thread-local errno */
109 void *tsd[_POSIX_THREAD_KEYS_MAX]; /* Thread specific data */
110 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
111 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
112 mach_port_t reply_port; /* Cached MiG reply port */
113 void *cthread_self; /* cthread_self() if somebody calls cthread_set_self() */
114 boolean_t freeStackOnExit; /* Should we free the stack when we're done? */
115 LIST_ENTRY(_pthread) plist;
116 } *pthread_t;
117
118 // suppress pthread_t typedef in signal.h
119 #define _PTHREAD_T_DECLARED
120
121 /*
122 * This will cause a compile-time failure if someone moved the tsd field
123 * and we need to change _PTHREAD_TSD_OFFSET in pthread_machdep.h
124 */
125 typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
126
127 /*
128 * Thread attributes
129 */
130 typedef struct
131 {
132 long sig; /* Unique signature for this structure */
133 pthread_lock_t lock; /* Used for internal mutex on structure */
134 u_int32_t detached:8,
135 inherit:8,
136 policy:8,
137 reserved1:8;
138 size_t guardsize; /* size in bytes to guard stack overflow */
139 int reserved2;
140 struct sched_param param;
141 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
142 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
143 boolean_t freeStackOnExit;/* Should we free the stack when we exit? */
144 } pthread_attr_t;
145
146 // suppress pthread_attr_t typedef in sys/signal.h
147 #define _PTHREAD_ATTR_T_DECLARED
148
149 /*
150 * Mutex attributes
151 */
152 typedef struct
153 {
154 long sig; /* Unique signature for this structure */
155 int prioceiling;
156 u_int32_t protocol:2, /* protocol attribute */
157 type:2, /* mutex type */
158 rfu:28;
159 } pthread_mutexattr_t;
160
161 /*
162 * Mutex variables
163 */
164 typedef struct _pthread_mutex
165 {
166 long sig; /* Unique signature for this structure */
167 pthread_lock_t lock; /* Used for internal mutex on structure */
168 u_int32_t waiters; /* Count of threads waiting for this mutex */
169 pthread_t owner; /* Which thread has this mutex locked */
170 semaphore_t sem; /* Semaphore used for waiting */
171 u_int32_t protocol:2, /* protocol */
172 type:2, /* mutex type */
173 rfu:12,
174 lock_count:16;
175 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
176 struct _pthread_cond *busy; /* List of condition variables using this mutex */
177 int16_t prioceiling;
178 int16_t priority; /* Priority to restore when mutex unlocked */
179 semaphore_t order;
180 } pthread_mutex_t;
181
182 /*
183 * Condition variable attributes
184 */
185 typedef struct
186 {
187 long sig; /* Unique signature for this structure */
188 int unsupported;
189 } pthread_condattr_t;
190
191 /*
192 * Condition variables
193 */
194 typedef struct _pthread_cond
195 {
196 long sig; /* Unique signature for this structure */
197 pthread_lock_t lock; /* Used for internal mutex on structure */
198 semaphore_t sem; /* Kernel semaphore */
199 struct _pthread_cond *next, *prev; /* List of condition variables using mutex */
200 struct _pthread_mutex *busy; /* mutex associated with variable */
201 u_int32_t waiters:16, /* Number of threads waiting */
202 sigspending:16; /* Number of outstanding signals */
203 } pthread_cond_t;
204
205 /*
206 * Initialization control (once) variables
207 */
208 typedef struct
209 {
210 long sig; /* Unique signature for this structure */
211 pthread_lock_t lock; /* Used for internal mutex on structure */
212 } pthread_once_t;
213
214 typedef struct {
215 long sig; /* Unique signature for this structure */
216 int pshared;
217 int rfu[2]; /* reserved for future use */
218 } pthread_rwlockattr_t;
219
220 typedef struct {
221 long sig;
222 pthread_mutex_t lock; /* monitor lock */
223 int state;
224 pthread_cond_t read_signal;
225 pthread_cond_t write_signal;
226 int blocked_writers;
227 int pshared;
228 int rfu[3];
229 } pthread_rwlock_t;
230
231 #include "pthread.h"
232
233 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
234 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
235 #define _PTHREAD_DEFAULT_PRIOCEILING 0
236 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
237 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
238
239 #define _PTHREAD_NO_SIG 0x00000000
240 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
241 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
242 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
243 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
244 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
245 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
246 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
247 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
248 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
249 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
250 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
251 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
252 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
253
254 #define _PTHREAD_CREATE_PARENT 4
255 #define _PTHREAD_EXITED 8
256
257 #if defined(DEBUG)
258 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
259 #else
260 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
261 #endif
262 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
263
264 #define _PTHREAD_CANCEL_STATE_MASK 0xFE
265 #define _PTHREAD_CANCEL_TYPE_MASK 0xFD
266 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
267
268 extern boolean_t swtch_pri(int);
269
270 #ifndef ESUCCESS
271 #define ESUCCESS 0
272 #endif
273
274 #ifndef PTHREAD_MACH_CALL
275 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
276 #endif
277
278 /* Prototypes. */
279
280 /* Functions defined in machine-dependent files. */
281 extern vm_address_t _sp(void);
282 extern vm_address_t _adjust_sp(vm_address_t sp);
283 extern void _pthread_setup(pthread_t th, void (*f)(pthread_t), void *sp, int suspended, int needresume);
284
285 extern void _pthread_tsd_cleanup(pthread_t self);
286
287 __private_extern__ semaphore_t new_sem_from_pool(void);
288 __private_extern__ void restore_sem_to_pool(semaphore_t);
289 #endif /* _POSIX_PTHREAD_INTERNALS_H */