]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_internals.h
Libc-391.5.22.tar.gz
[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 * 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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
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.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
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.
42 *
43 */
44 /*
45 * MkLinux
46 */
47
48 /*
49 * POSIX Threads - IEEE 1003.1c
50 */
51
52 #ifndef _POSIX_PTHREAD_INTERNALS_H
53 #define _POSIX_PTHREAD_INTERNALS_H
54
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;
59
60 #include <assert.h>
61 #include <stddef.h>
62 #include <stdint.h>
63 #include <stdlib.h>
64 #include <limits.h>
65 #include <errno.h>
66 #include <mach/mach.h>
67 #include <mach/mach_error.h>
68
69
70 #ifndef __POSIX_LIB__
71 #define __POSIX_LIB__
72 #endif
73
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. */
78
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;
82 /*
83 * Compiled-in limits
84 */
85 #undef _POSIX_THREAD_KEYS_MAX
86 #define _POSIX_THREAD_KEYS_MAX 128
87
88 /*
89 * Threads
90 */
91 #define _PTHREAD_T
92 typedef struct _pthread
93 {
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 */
97 u_int32_t detached:8,
98 inherit:8,
99 policy:8,
100 pad:8;
101 size_t guardsize; /* size in bytes to guard stack overflow */
102 #if !defined(__LP64__)
103 int pad0; /* for backwards compatibility */
104 #endif
105 struct sched_param param;
106 struct _pthread_mutex *mutexes;
107 struct _pthread *joiner;
108 #if !defined(__LP64__)
109 int pad1; /* for backwards compatibility */
110 #endif
111 void *exit_value;
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 */
124 #endif
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 */
129 #endif
130 LIST_ENTRY(_pthread) plist;
131 } *pthread_t;
132
133 /*
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
136 */
137 typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
138
139 /*
140 * Thread attributes
141 */
142 struct _pthread_attr_t
143 {
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,
147 inherit:8,
148 policy:8,
149 reserved1:8;
150 size_t guardsize; /* size in bytes to guard stack overflow */
151 int reserved2;
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? */
156 };
157
158 /*
159 * Mutex attributes
160 */
161 #define _PTHREAD_MUTEXATTR_T
162 typedef struct
163 {
164 long sig; /* Unique signature for this structure */
165 int prioceiling;
166 u_int32_t protocol:2, /* protocol attribute */
167 type:2, /* mutex type */
168 rfu:28;
169 } pthread_mutexattr_t;
170
171 /*
172 * Mutex variables
173 */
174 #define _PTHREAD_MUTEX_T
175 typedef struct _pthread_mutex
176 {
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 */
184 rfu:12,
185 lock_count:16;
186 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
187 struct _pthread_cond *busy; /* List of condition variables using this mutex */
188 int16_t prioceiling;
189 int16_t priority; /* Priority to restore when mutex unlocked */
190 semaphore_t order;
191 } pthread_mutex_t;
192
193 /*
194 * Condition variable attributes
195 */
196 #define _PTHREAD_CONDATTR_T
197 typedef struct
198 {
199 long sig; /* Unique signature for this structure */
200 int unsupported;
201 } pthread_condattr_t;
202
203 /*
204 * Condition variables
205 */
206 #define _PTHREAD_COND_T
207 typedef struct _pthread_cond
208 {
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 */
216 } pthread_cond_t;
217
218 /*
219 * Initialization control (once) variables
220 */
221 #define _PTHREAD_ONCE_T
222 typedef struct
223 {
224 long sig; /* Unique signature for this structure */
225 pthread_lock_t lock; /* Used for internal mutex on structure */
226 } pthread_once_t;
227
228 #define _PTHREAD_RWLOCKATTR_T
229 typedef struct {
230 long sig; /* Unique signature for this structure */
231 int pshared;
232 int rfu[2]; /* reserved for future use */
233 } pthread_rwlockattr_t;
234
235 #define _PTHREAD_RWLOCK_T
236 typedef struct {
237 long sig;
238 pthread_mutex_t lock; /* monitor lock */
239 int state;
240 pthread_cond_t read_signal;
241 pthread_cond_t write_signal;
242 int blocked_writers;
243 int pshared;
244 int rfu[3];
245 } pthread_rwlock_t;
246
247 #include "pthread.h"
248
249 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__)
250 /*
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.
253 */
254 inline static pthread_t __attribute__((__pure__))
255 _pthread_self_direct(void)
256 {
257 pthread_t ret;
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;
263 #endif
264 return ret;
265 }
266 #define pthread_self() _pthread_self_direct()
267 #endif
268
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 */
274
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' */
289
290 #define _PTHREAD_CREATE_PARENT 4
291 #define _PTHREAD_EXITED 8
292
293 #if defined(DEBUG)
294 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
295 #else
296 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
297 #endif
298 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
299
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 */
303
304 extern boolean_t swtch_pri(int);
305
306 #ifndef ESUCCESS
307 #define ESUCCESS 0
308 #endif
309
310 #ifndef PTHREAD_MACH_CALL
311 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
312 #endif
313
314 /* Prototypes. */
315
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);
320
321 extern void _pthread_tsd_cleanup(pthread_t self);
322
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 */