]> git.saurik.com Git - apple/libc.git/blame - pthreads/pthread_internals.h
Libc-391.5.18.tar.gz
[apple/libc.git] / pthreads / pthread_internals.h
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9385eb3d
A
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 */
e9ce8d39
A
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
3d9156a7
A
55// suppress pthread_attr_t typedef in sys/signal.h
56#define _PTHREAD_ATTR_T
57struct _pthread_attr_t; /* forward reference */
58typedef struct _pthread_attr_t pthread_attr_t;
e9ce8d39
A
59
60#include <assert.h>
9385eb3d
A
61#include <stddef.h>
62#include <stdint.h>
63#include <stdlib.h>
3d9156a7
A
64#include <limits.h>
65#include <errno.h>
9385eb3d 66#include <mach/mach.h>
e9ce8d39 67#include <mach/mach_error.h>
e9ce8d39
A
68
69
70#ifndef __POSIX_LIB__
71#define __POSIX_LIB__
72#endif
73
74#include "posix_sched.h" /* For POSIX scheduling policy & parameter */
9385eb3d 75#include <sys/queue.h> /* For POSIX scheduling policy & parameter */
e9ce8d39 76#include "pthread_machdep.h" /* Machine-dependent definitions. */
9385eb3d 77#include "pthread_spinlock.h" /* spinlock definitions. */
e9ce8d39 78
9385eb3d
A
79LIST_HEAD(__pthread_list, _pthread);
80extern struct __pthread_list __pthread_head; /* head of list of open files */
81extern pthread_lock_t _pthread_list_lock;
e9ce8d39
A
82/*
83 * Compiled-in limits
84 */
85#undef _POSIX_THREAD_KEYS_MAX
86#define _POSIX_THREAD_KEYS_MAX 128
87
88/*
89 * Threads
90 */
3d9156a7 91#define _PTHREAD_T
e9ce8d39
A
92typedef struct _pthread
93{
94 long sig; /* Unique signature for this structure */
3d9156a7 95 struct __darwin_pthread_handler_rec *__cleanup_stack;
e9ce8d39 96 pthread_lock_t lock; /* Used for internal mutex on structure */
9385eb3d
A
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 */
3d9156a7
A
102#if !defined(__LP64__)
103 int pad0; /* for backwards compatibility */
104#endif
e9ce8d39
A
105 struct sched_param param;
106 struct _pthread_mutex *mutexes;
5b2abdfb 107 struct _pthread *joiner;
3d9156a7
A
108#if !defined(__LP64__)
109 int pad1; /* for backwards compatibility */
110#endif
5b2abdfb
A
111 void *exit_value;
112 semaphore_t death; /* pthread_join() uses this to wait for death's call */
e9ce8d39
A
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 */
5b2abdfb 117 int err_no; /* thread-local errno */
e9ce8d39
A
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 */
3d9156a7
A
122#if defined(__LP64__)
123 int pad2; /* for natural alignment */
124#endif
125 void *cthread_self; /* cthread_self() if somebody calls cthread_set_self() */
9385eb3d 126 boolean_t freeStackOnExit; /* Should we free the stack when we're done? */
3d9156a7
A
127#if defined(__LP64__)
128 int pad3; /* for natural alignment */
129#endif
9385eb3d 130 LIST_ENTRY(_pthread) plist;
e9ce8d39
A
131} *pthread_t;
132
9385eb3d
A
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 */
137typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
e9ce8d39
A
138
139/*
140 * Thread attributes
141 */
3d9156a7 142struct _pthread_attr_t
e9ce8d39
A
143{
144 long sig; /* Unique signature for this structure */
145 pthread_lock_t lock; /* Used for internal mutex on structure */
9385eb3d
A
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;
e9ce8d39
A
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? */
3d9156a7 156};
59e0d9fe 157
e9ce8d39
A
158/*
159 * Mutex attributes
160 */
3d9156a7 161#define _PTHREAD_MUTEXATTR_T
e9ce8d39
A
162typedef struct
163{
164 long sig; /* Unique signature for this structure */
165 int prioceiling;
5b2abdfb
A
166 u_int32_t protocol:2, /* protocol attribute */
167 type:2, /* mutex type */
168 rfu:28;
e9ce8d39
A
169} pthread_mutexattr_t;
170
171/*
172 * Mutex variables
173 */
3d9156a7 174#define _PTHREAD_MUTEX_T
e9ce8d39
A
175typedef struct _pthread_mutex
176{
177 long sig; /* Unique signature for this structure */
178 pthread_lock_t lock; /* Used for internal mutex on structure */
9385eb3d 179 u_int32_t waiters; /* Count of threads waiting for this mutex */
e9ce8d39 180 pthread_t owner; /* Which thread has this mutex locked */
9385eb3d 181 semaphore_t sem; /* Semaphore used for waiting */
5b2abdfb
A
182 u_int32_t protocol:2, /* protocol */
183 type:2, /* mutex type */
184 rfu:12,
185 lock_count:16;
e9ce8d39
A
186 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
187 struct _pthread_cond *busy; /* List of condition variables using this mutex */
9385eb3d
A
188 int16_t prioceiling;
189 int16_t priority; /* Priority to restore when mutex unlocked */
190 semaphore_t order;
e9ce8d39
A
191} pthread_mutex_t;
192
193/*
194 * Condition variable attributes
195 */
3d9156a7 196#define _PTHREAD_CONDATTR_T
e9ce8d39
A
197typedef struct
198{
199 long sig; /* Unique signature for this structure */
200 int unsupported;
201} pthread_condattr_t;
202
203/*
204 * Condition variables
205 */
3d9156a7 206#define _PTHREAD_COND_T
e9ce8d39
A
207typedef struct _pthread_cond
208{
209 long sig; /* Unique signature for this structure */
210 pthread_lock_t lock; /* Used for internal mutex on structure */
9385eb3d 211 semaphore_t sem; /* Kernel semaphore */
e9ce8d39
A
212 struct _pthread_cond *next, *prev; /* List of condition variables using mutex */
213 struct _pthread_mutex *busy; /* mutex associated with variable */
9385eb3d 214 u_int32_t waiters:16, /* Number of threads waiting */
e9ce8d39
A
215 sigspending:16; /* Number of outstanding signals */
216} pthread_cond_t;
217
218/*
219 * Initialization control (once) variables
220 */
3d9156a7 221#define _PTHREAD_ONCE_T
e9ce8d39
A
222typedef 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
3d9156a7 228#define _PTHREAD_RWLOCKATTR_T
5b2abdfb
A
229typedef 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
3d9156a7 235#define _PTHREAD_RWLOCK_T
5b2abdfb
A
236typedef 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
e9ce8d39
A
247#include "pthread.h"
248
8e029c65 249#if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__)
3d9156a7
A
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 */
254inline static pthread_t __attribute__((__pure__))
255_pthread_self_direct(void)
256{
257 pthread_t ret;
8e029c65
A
258#if defined(__i386__) || defined(__x86_64__)
259 asm("mov %%gs:%P1, %0" : "=r" (ret) : "i" (offsetof(struct _pthread, tsd[0])));
3d9156a7
A
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
e9ce8d39
A
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' */
5b2abdfb 279#define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
e9ce8d39
A
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' */
5b2abdfb
A
286#define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
287#define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
288#define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
e9ce8d39 289
e9ce8d39 290#define _PTHREAD_CREATE_PARENT 4
5b2abdfb 291#define _PTHREAD_EXITED 8
e9ce8d39 292
9385eb3d
A
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
3d9156a7
A
300#define _PTHREAD_CANCEL_STATE_MASK 0x01
301#define _PTHREAD_CANCEL_TYPE_MASK 0x02
e9ce8d39
A
302#define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
303
304extern boolean_t swtch_pri(int);
305
e9ce8d39
A
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. */
317extern vm_address_t _sp(void);
318extern vm_address_t _adjust_sp(vm_address_t sp);
5b2abdfb 319extern void _pthread_setup(pthread_t th, void (*f)(pthread_t), void *sp, int suspended, int needresume);
e9ce8d39
A
320
321extern 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);
3d9156a7 325__private_extern__ void _pthread_atfork_queue_init(void);
e9ce8d39 326#endif /* _POSIX_PTHREAD_INTERNALS_H */