]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_internals.h
1a7458d1078b7869a7f5831dec093f581f348a5b
[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 /*
119 * This will cause a compile-time failure if someone moved the tsd field
120 * and we need to change _PTHREAD_TSD_OFFSET in pthread_machdep.h
121 */
122 typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
123
124 /*
125 * Thread attributes
126 */
127 typedef struct
128 {
129 long sig; /* Unique signature for this structure */
130 pthread_lock_t lock; /* Used for internal mutex on structure */
131 u_int32_t detached:8,
132 inherit:8,
133 policy:8,
134 reserved1:8;
135 size_t guardsize; /* size in bytes to guard stack overflow */
136 int reserved2;
137 struct sched_param param;
138 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
139 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
140 boolean_t freeStackOnExit;/* Should we free the stack when we exit? */
141 } pthread_attr_t;
142
143 /*
144 * Mutex attributes
145 */
146 typedef struct
147 {
148 long sig; /* Unique signature for this structure */
149 int prioceiling;
150 u_int32_t protocol:2, /* protocol attribute */
151 type:2, /* mutex type */
152 rfu:28;
153 } pthread_mutexattr_t;
154
155 /*
156 * Mutex variables
157 */
158 typedef struct _pthread_mutex
159 {
160 long sig; /* Unique signature for this structure */
161 pthread_lock_t lock; /* Used for internal mutex on structure */
162 u_int32_t waiters; /* Count of threads waiting for this mutex */
163 pthread_t owner; /* Which thread has this mutex locked */
164 semaphore_t sem; /* Semaphore used for waiting */
165 u_int32_t protocol:2, /* protocol */
166 type:2, /* mutex type */
167 rfu:12,
168 lock_count:16;
169 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
170 struct _pthread_cond *busy; /* List of condition variables using this mutex */
171 int16_t prioceiling;
172 int16_t priority; /* Priority to restore when mutex unlocked */
173 semaphore_t order;
174 } pthread_mutex_t;
175
176 /*
177 * Condition variable attributes
178 */
179 typedef struct
180 {
181 long sig; /* Unique signature for this structure */
182 int unsupported;
183 } pthread_condattr_t;
184
185 /*
186 * Condition variables
187 */
188 typedef struct _pthread_cond
189 {
190 long sig; /* Unique signature for this structure */
191 pthread_lock_t lock; /* Used for internal mutex on structure */
192 semaphore_t sem; /* Kernel semaphore */
193 struct _pthread_cond *next, *prev; /* List of condition variables using mutex */
194 struct _pthread_mutex *busy; /* mutex associated with variable */
195 u_int32_t waiters:16, /* Number of threads waiting */
196 sigspending:16; /* Number of outstanding signals */
197 } pthread_cond_t;
198
199 /*
200 * Initialization control (once) variables
201 */
202 typedef struct
203 {
204 long sig; /* Unique signature for this structure */
205 pthread_lock_t lock; /* Used for internal mutex on structure */
206 } pthread_once_t;
207
208 typedef struct {
209 long sig; /* Unique signature for this structure */
210 int pshared;
211 int rfu[2]; /* reserved for future use */
212 } pthread_rwlockattr_t;
213
214 typedef struct {
215 long sig;
216 pthread_mutex_t lock; /* monitor lock */
217 int state;
218 pthread_cond_t read_signal;
219 pthread_cond_t write_signal;
220 int blocked_writers;
221 int pshared;
222 int rfu[3];
223 } pthread_rwlock_t;
224
225 #include "pthread.h"
226
227 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
228 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
229 #define _PTHREAD_DEFAULT_PRIOCEILING 0
230 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
231 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
232
233 #define _PTHREAD_NO_SIG 0x00000000
234 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
235 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
236 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
237 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
238 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
239 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
240 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
241 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
242 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
243 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
244 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
245 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
246 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
247
248 #define _PTHREAD_CREATE_PARENT 4
249 #define _PTHREAD_EXITED 8
250
251 #if defined(DEBUG)
252 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
253 #else
254 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
255 #endif
256 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
257
258 #define _PTHREAD_CANCEL_STATE_MASK 0xFE
259 #define _PTHREAD_CANCEL_TYPE_MASK 0xFD
260 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
261
262 extern boolean_t swtch_pri(int);
263
264 #ifndef ESUCCESS
265 #define ESUCCESS 0
266 #endif
267
268 #ifndef PTHREAD_MACH_CALL
269 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
270 #endif
271
272 /* Prototypes. */
273
274 /* Functions defined in machine-dependent files. */
275 extern vm_address_t _sp(void);
276 extern vm_address_t _adjust_sp(vm_address_t sp);
277 extern void _pthread_setup(pthread_t th, void (*f)(pthread_t), void *sp, int suspended, int needresume);
278
279 extern void _pthread_tsd_cleanup(pthread_t self);
280
281 __private_extern__ semaphore_t new_sem_from_pool(void);
282 __private_extern__ void restore_sem_to_pool(semaphore_t);
283 #endif /* _POSIX_PTHREAD_INTERNALS_H */