]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_internals.h
Libc-498.1.1.tar.gz
[apple/libc.git] / pthreads / pthread_internals.h
1 /*
2 * Copyright (c) 2000-2003, 2007 Apple 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 TAILQ_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 extern size_t pthreadsize;
83 /*
84 * Compiled-in limits
85 */
86 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
87 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
88 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
89
90 /*
91 * Threads
92 */
93 #define _PTHREAD_T
94 typedef struct _pthread
95 {
96 long sig; /* Unique signature for this structure */
97 struct __darwin_pthread_handler_rec *__cleanup_stack;
98 pthread_lock_t lock; /* Used for internal mutex on structure */
99 u_int32_t detached:8,
100 inherit:8,
101 policy:8,
102 freeStackOnExit:1,
103 newstyle:1,
104 kernalloc:1,
105 schedset:1,
106 wqthread:1,
107 pad:3;
108 size_t guardsize; /* size in bytes to guard stack overflow */
109 #if !defined(__LP64__)
110 int pad0; /* for backwards compatibility */
111 #endif
112 struct sched_param param;
113 struct _pthread_mutex *mutexes;
114 struct _pthread *joiner;
115 #if !defined(__LP64__)
116 int pad1; /* for backwards compatibility */
117 #endif
118 void *exit_value;
119 semaphore_t death; /* pthread_join() uses this to wait for death's call */
120 mach_port_t kernel_thread; /* kernel thread this thread is bound to */
121 void *(*fun)(void*);/* Thread start routine */
122 void *arg; /* Argment for thread start routine */
123 int cancel_state; /* Whether thread can be cancelled */
124 int err_no; /* thread-local errno */
125 void *tsd[_EXTERNAL_POSIX_THREAD_KEYS_MAX + _INTERNAL_POSIX_THREAD_KEYS_MAX]; /* Thread specific data */
126 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
127 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
128 mach_port_t reply_port; /* Cached MiG reply port */
129 #if defined(__LP64__)
130 int pad2; /* for natural alignment */
131 #endif
132 void *cthread_self; /* cthread_self() if somebody calls cthread_set_self() */
133 /* protected by list lock */
134 u_int32_t childrun:1,
135 parentcheck:1,
136 childexit:1,
137 pad3:29;
138 #if defined(__LP64__)
139 int pad4; /* for natural alignment */
140 #endif
141 TAILQ_ENTRY(_pthread) plist;
142 void * freeaddr;
143 size_t freesize;
144 mach_port_t joiner_notify;
145 char pthread_name[64]; /* including nulll the name */
146 int max_tsd_key;
147 void * cur_workq;
148 void * cur_workitem;
149 } *pthread_t;
150
151 /*
152 * This will cause a compile-time failure if someone moved the tsd field
153 * and we need to change _PTHREAD_TSD_OFFSET in pthread_machdep.h
154 */
155 typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
156
157 /*
158 * Thread attributes
159 */
160 struct _pthread_attr_t
161 {
162 long sig; /* Unique signature for this structure */
163 pthread_lock_t lock;
164 u_int32_t detached:8,
165 inherit:8,
166 policy:8,
167 freeStackOnExit:1,
168 fastpath:1,
169 schedset:1,
170 reserved1:5;
171 size_t guardsize; /* size in bytes to guard stack overflow */
172 int reserved2; /* Should we free the stack when we exit? */
173 struct sched_param param;
174 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
175 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
176 boolean_t reserved3;
177 };
178
179 /*
180 * Mutex attributes
181 */
182 #define _PTHREAD_MUTEXATTR_T
183 typedef struct
184 {
185 long sig; /* Unique signature for this structure */
186 int prioceiling;
187 u_int32_t protocol:2, /* protocol attribute */
188 type:2, /* mutex type */
189 pshared:2,
190 rfu:26;
191 } pthread_mutexattr_t;
192
193 /*
194 * Mutex variables
195 */
196 #define _PTHREAD_MUTEX_T
197 typedef struct _pthread_mutex
198 {
199 long sig; /* Unique signature for this structure */
200 pthread_lock_t lock; /* Used for internal mutex on structure */
201 u_int32_t waiters; /* Count of threads waiting for this mutex */
202 #define _pthread_mutex_kernid waiters
203 pthread_t owner; /* Which thread has this mutex locked */
204 semaphore_t sem; /* Semaphore used for waiting */
205 u_int32_t protocol:2, /* protocol */
206 type:2, /* mutex type */
207 pshared:2, /* mutex type */
208 rfu:10,
209 lock_count:16;
210 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
211 struct _pthread_cond *busy; /* List of condition variables using this mutex */
212 int16_t prioceiling;
213 int16_t priority; /* Priority to restore when mutex unlocked */
214 semaphore_t order;
215 } pthread_mutex_t;
216
217
218
219 /*
220 * Condition variable attributes
221 */
222 #define _PTHREAD_CONDATTR_T
223 typedef struct
224 {
225 long sig; /* Unique signature for this structure */
226 u_int32_t pshared:2, /* pshared */
227 unsupported:30;
228 } pthread_condattr_t;
229
230 /*
231 * Condition variables
232 */
233 #define _PTHREAD_COND_T
234 typedef struct _pthread_cond
235 {
236 long sig; /* Unique signature for this structure */
237 pthread_lock_t lock; /* Used for internal mutex on structure */
238 semaphore_t sem; /* Kernel semaphore */
239 #define _pthread_cond_kernid sem
240 struct _pthread_cond *next, *prev; /* List of condition variables using mutex */
241 struct _pthread_mutex *busy; /* mutex associated with variable */
242 u_int32_t waiters:15, /* Number of threads waiting */
243 sigspending:15, /* Number of outstanding signals */
244 pshared:2;
245 } pthread_cond_t;
246
247 /*
248 * Initialization control (once) variables
249 */
250 #define _PTHREAD_ONCE_T
251 typedef struct
252 {
253 long sig; /* Unique signature for this structure */
254 pthread_lock_t lock; /* Used for internal mutex on structure */
255 } pthread_once_t;
256
257 #define _PTHREAD_RWLOCKATTR_T
258 typedef struct {
259 long sig; /* Unique signature for this structure */
260 int pshared;
261 int rfu[2]; /* reserved for future use */
262 } pthread_rwlockattr_t;
263
264 #define _PTHREAD_RWLOCK_T
265 typedef struct {
266 long sig;
267 pthread_mutex_t lock; /* monitor lock */
268 int state;
269 #define _pthread_rwlock_kernid state
270 pthread_cond_t read_signal;
271 pthread_cond_t write_signal;
272 int blocked_writers;
273 int pshared;
274 pthread_t owner;
275 int rfu[2];
276 } pthread_rwlock_t;
277
278 /* keep the size to 64bytes for both 64 and 32 */
279 #define _PTHREAD_WORKQUEUE_ATTR_T
280 typedef struct {
281 u_int32_t sig;
282 #if defined(__ppc64__) || defined(__x86_64__)
283 u_int32_t resv1;
284 #endif
285 size_t stacksize;
286 int istimeshare;
287 int importance;
288 int affinity;
289 int queueprio;
290 #if defined(__ppc64__) || defined(__x86_64__)
291 unsigned int resv2[8];
292 #else
293 unsigned int resv2[10];
294 #endif
295 } pthread_workqueue_attr_t;
296
297 #define _PTHREAD_WORKITEM_T
298 typedef struct _pthread_workitem {
299 TAILQ_ENTRY(_pthread_workitem) item_entry; /* pthread_workitem list in prio */
300 void (*func)(void *);
301 void * func_arg;
302 struct _pthread_workqueue * workq;
303 unsigned int flags;
304 } * pthread_workitem_t;
305
306 #define PTH_WQITEM_INKERNEL_QUEUE 1
307 #define PTH_WQITEM_RUNNING 2
308 #define PTH_WQITEM_COMPLETED 4
309 #define PTH_WQITEM_REMOVED 8
310 #define PTH_WQITEM_BARRIER 0x10
311 #define PTH_WQITEM_DESTROY 0x20
312 #define PTH_WQITEM_NOTINLIST 0x40
313 #define PTH_WQITEM_APPLIED 0x80
314 #define PTH_WQITEM_KERN_COUNT 0x100
315
316 #define WORKITEM_POOL_SIZE 1000
317 TAILQ_HEAD(__pthread_workitem_pool, _pthread_workitem);
318 extern struct __pthread_workitem_pool __pthread_workitem_pool_head; /* head list of workitem pool */
319
320 #define WQ_NUM_PRIO_QS 5 /* -2 to +2 */
321 #define WORK_QUEUE_NORMALIZER 2 /* so all internal usages are from 0 to 4 */
322
323 #define _PTHREAD_WORKQUEUE_HEAD_T
324 typedef struct _pthread_workqueue_head {
325 TAILQ_HEAD(, _pthread_workqueue) wqhead;
326 struct _pthread_workqueue * next_workq;
327 } * pthread_workqueue_head_t;
328
329
330 #define _PTHREAD_WORKQUEUE_T
331 typedef struct _pthread_workqueue {
332 unsigned int sig; /* Unique signature for this structure */
333 pthread_lock_t lock; /* Used for internal mutex on structure */
334 TAILQ_ENTRY(_pthread_workqueue) wq_list; /* workqueue list in prio */
335 TAILQ_HEAD(, _pthread_workitem) item_listhead; /* pthread_workitem list in prio */
336 TAILQ_HEAD(, _pthread_workitem) item_kernhead; /* pthread_workitem list in prio */
337 unsigned int flags;
338 size_t stacksize;
339 int istimeshare;
340 int importance;
341 int affinity;
342 int queueprio;
343 int barrier_count;
344 int kq_count;
345 void (*term_callback)(struct _pthread_workqueue *,void *);
346 void * term_callarg;
347 pthread_workqueue_head_t headp;
348 int suspend_count;
349 #if defined(__ppc64__) || defined(__x86_64__)
350 unsigned int rev2[2];
351 #else
352 unsigned int rev2[12];
353 #endif
354 } * pthread_workqueue_t;
355
356 #define PTHREAD_WORKQ_IN_CREATION 1
357 #define PTHREAD_WORKQ_IN_TERMINATE 2
358 #define PTHREAD_WORKQ_BARRIER_ON 4
359 #define PTHREAD_WORKQ_TERM_ON 8
360 #define PTHREAD_WORKQ_DESTROYED 0x10
361 #define PTHREAD_WORKQ_REQUEUED 0x20
362 #define PTHREAD_WORKQ_SUSPEND 0x40
363
364 #define WORKQUEUE_POOL_SIZE 100
365 TAILQ_HEAD(__pthread_workqueue_pool, _pthread_workqueue);
366 extern struct __pthread_workqueue_pool __pthread_workqueue_pool_head; /* head list of workqueue pool */
367
368 #include "pthread.h"
369
370 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__)
371 /*
372 * Inside libSystem, we can use r13 or %gs directly to get access to the
373 * thread-specific data area. The current thread is in the first slot.
374 */
375 inline static pthread_t __attribute__((__pure__))
376 _pthread_self_direct(void)
377 {
378 pthread_t ret;
379 #if defined(__i386__) || defined(__x86_64__)
380 asm("mov %%gs:%P1, %0" : "=r" (ret) : "i" (offsetof(struct _pthread, tsd[0])));
381 #elif defined(__ppc64__)
382 register const pthread_t __pthread_self asm ("r13");
383 ret = __pthread_self;
384 #endif
385 return ret;
386 }
387 #define pthread_self() _pthread_self_direct()
388 #endif
389
390 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
391 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
392 #define _PTHREAD_DEFAULT_PRIOCEILING 0
393 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
394 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
395 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
396
397 #define _PTHREAD_NO_SIG 0x00000000
398 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
399 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
400 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
401 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
402 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
403 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
404 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
405 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
406 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
407 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
408 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
409 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
410 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
411
412
413 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
414 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
415 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
416
417 #define _PTHREAD_CREATE_PARENT 4
418 #define _PTHREAD_EXITED 8
419 // 4597450: begin
420 #define _PTHREAD_WASCANCEL 0x10
421 // 4597450: end
422
423 #if defined(DEBUG)
424 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
425 #else
426 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
427 #endif
428 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
429
430 #define _PTHREAD_CANCEL_STATE_MASK 0x01
431 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
432 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
433
434 extern boolean_t swtch_pri(int);
435
436 #ifndef PTHREAD_MACH_CALL
437 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
438 #endif
439
440 /* Prototypes. */
441
442 /* Functions defined in machine-dependent files. */
443 extern vm_address_t _sp(void);
444 extern vm_address_t _adjust_sp(vm_address_t sp);
445 extern void _pthread_setup(pthread_t th, void (*f)(pthread_t), void *sp, int suspended, int needresume);
446
447 extern void _pthread_tsd_cleanup(pthread_t self);
448
449 __private_extern__ semaphore_t new_sem_from_pool(void);
450 __private_extern__ void restore_sem_to_pool(semaphore_t);
451 __private_extern__ void _pthread_atfork_queue_init(void);
452 int _pthread_lookup_thread(pthread_t thread, mach_port_t * port, int only_joinable);
453 int _pthread_join_cleanup(pthread_t thread, void ** value_ptr, int conforming);
454 #endif /* _POSIX_PTHREAD_INTERNALS_H */