]> git.saurik.com Git - cycript.git/blob - include/pthread_internals.h
Attempting to better abstract FreeBSD.
[cycript.git] / include / 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 #include <libkern/OSAtomic.h>
69
70
71 #ifndef __POSIX_LIB__
72 #define __POSIX_LIB__
73 #endif
74
75 #include "posix_sched.h" /* For POSIX scheduling policy & parameter */
76 #include <sys/queue.h> /* For POSIX scheduling policy & parameter */
77 #include "pthread_machdep.h" /* Machine-dependent definitions. */
78 #include "pthread_spinlock.h" /* spinlock definitions. */
79
80 TAILQ_HEAD(__pthread_list, _pthread);
81 extern struct __pthread_list __pthread_head; /* head of list of open files */
82 extern pthread_lock_t _pthread_list_lock;
83 extern size_t pthreadsize;
84 /*
85 * Compiled-in limits
86 */
87 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
88 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
89 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
90
91 /*
92 * Threads
93 */
94 #define MAXTHREADNAMESIZE 64
95 #define _PTHREAD_T
96 typedef struct _pthread
97 {
98 long sig; /* Unique signature for this structure */
99 struct __darwin_pthread_handler_rec *__cleanup_stack;
100 pthread_lock_t lock; /* Used for internal mutex on structure */
101 uint32_t detached:8,
102 inherit:8,
103 policy:8,
104 freeStackOnExit:1,
105 newstyle:1,
106 kernalloc:1,
107 schedset:1,
108 wqthread:1,
109 wqkillset:1,
110 pad:2;
111 size_t guardsize; /* size in bytes to guard stack overflow */
112 #if !defined(__LP64__)
113 int pad0; /* for backwards compatibility */
114 #endif
115 struct sched_param param;
116 struct _pthread_mutex *mutexes;
117 struct _pthread *joiner;
118 #if !defined(__LP64__)
119 int pad1; /* for backwards compatibility */
120 #endif
121 void *exit_value;
122 semaphore_t death; /* pthread_join() uses this to wait for death's call */
123 mach_port_t kernel_thread; /* kernel thread this thread is bound to */
124 void *(*fun)(void*);/* Thread start routine */
125 void *arg; /* Argment for thread start routine */
126 int cancel_state; /* Whether thread can be cancelled */
127 int err_no; /* thread-local errno */
128 void *tsd[_EXTERNAL_POSIX_THREAD_KEYS_MAX + _INTERNAL_POSIX_THREAD_KEYS_MAX]; /* Thread specific data */
129 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
130 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
131 mach_port_t reply_port; /* Cached MiG reply port */
132 #if defined(__LP64__)
133 int pad2; /* for natural alignment */
134 #endif
135 void *cthread_self; /* cthread_self() if somebody calls cthread_set_self() */
136 /* protected by list lock */
137 uint32_t childrun:1,
138 parentcheck:1,
139 childexit:1,
140 pad3:29;
141 #if defined(__LP64__)
142 int pad4; /* for natural alignment */
143 #endif
144 TAILQ_ENTRY(_pthread) plist;
145 void * freeaddr;
146 size_t freesize;
147 mach_port_t joiner_notify;
148 char pthread_name[MAXTHREADNAMESIZE]; /* including nulll the name */
149 int max_tsd_key;
150 void * cur_workq;
151 void * cur_workitem;
152 uint64_t thread_id;
153 } *pthread_t;
154
155 /*
156 * This will cause a compile-time failure if someone moved the tsd field
157 * and we need to change _PTHREAD_TSD_OFFSET in pthread_machdep.h
158 */
159 typedef char _need_to_change_PTHREAD_TSD_OFFSET[(_PTHREAD_TSD_OFFSET == offsetof(struct _pthread, tsd[0])) ? 0 : -1] ;
160
161 /*
162 * Thread attributes
163 */
164 struct _pthread_attr_t
165 {
166 long sig; /* Unique signature for this structure */
167 pthread_lock_t lock;
168 uint32_t detached:8,
169 inherit:8,
170 policy:8,
171 freeStackOnExit:1,
172 fastpath:1,
173 schedset:1,
174 reserved1:5;
175 size_t guardsize; /* size in bytes to guard stack overflow */
176 int reserved2; /* Should we free the stack when we exit? */
177 struct sched_param param;
178 void *stackaddr; /* Base of the stack (is aligned on vm_page_size boundary */
179 size_t stacksize; /* Size of the stack (is a multiple of vm_page_size and >= PTHREAD_STACK_MIN) */
180 boolean_t reserved3;
181 };
182
183 /*
184 * Mutex attributes
185 */
186 #define _PTHREAD_MUTEX_POLICY_NONE 0
187 #define _PTHREAD_MUTEX_POLICY_FAIRSHARE 1
188 #define _PTHREAD_MUTEX_POLICY_FIRSTFIT 2
189 #define _PTHREAD_MUTEX_POLICY_REALTIME 3
190 #define _PTHREAD_MUTEX_POLICY_ADAPTIVE 4
191 #define _PTHREAD_MUTEX_POLICY_PRIPROTECT 5
192 #define _PTHREAD_MUTEX_POLICY_PRIINHERIT 6
193
194 #define _PTHREAD_MUTEXATTR_T
195 typedef struct
196 {
197 long sig; /* Unique signature for this structure */
198 int prioceiling;
199 uint32_t protocol:2, /* protocol attribute */
200 type:2, /* mutex type */
201 pshared:2,
202 policy:3,
203 rfu:23;
204 } pthread_mutexattr_t;
205
206 /*
207 * Mutex variables
208 */
209 struct _pthread_mutex_options {
210 uint32_t protocol:2, /* protocol */
211 type:2, /* mutex type */
212 pshared:2, /* mutex type */
213 policy:3,
214 hold:2,
215 misalign:1, /* 8 byte aligned? */
216 rfu:4,
217 lock_count:16;
218 };
219
220
221 #define _PTHREAD_MTX_OPT_PSHARED 0x010
222 #define _PTHREAD_MTX_OPT_HOLD 0x200
223 #define _PTHREAD_MTX_OPT_NOHOLD 0x400
224 #define _PTHREAD_MTX_OPT_LASTDROP (_PTHREAD_MTX_OPT_NOHOLD | _PTHREAD_MTX_OPT_HOLD)
225
226
227 #define _PTHREAD_MUTEX_T
228 typedef struct _pthread_mutex
229 {
230 long sig; /* Unique signature for this structure */
231 pthread_lock_t lock; /* Used for internal mutex on structure */
232 union {
233 uint32_t value;
234 struct _pthread_mutex_options options;
235 } mtxopts;
236 int16_t prioceiling;
237 int16_t priority; /* Priority to restore when mutex unlocked */
238 uint32_t waiters; /* Count of threads waiting for this mutex */
239 pthread_t owner; /* Which thread has this mutex locked */
240 struct _pthread_mutex *next, *prev; /* List of other mutexes he owns */
241 struct _pthread_cond *busy; /* List of condition variables using this mutex */
242 semaphore_t sem; /* Semaphore used for waiting */
243 semaphore_t order;
244 } pthread_mutex_t;
245
246
247 typedef struct _npthread_mutex
248 {
249 /* keep same as pthread_mutex_t from here to .. */
250 long sig; /* Unique signature for this structure */
251 pthread_lock_t lock; /* Used for static init sequencing */
252 union {
253 uint32_t value;
254 struct _pthread_mutex_options options;
255 } mtxopts;
256 int16_t prioceiling;
257 int16_t priority; /* Priority to restore when mutex unlocked */
258 uint32_t m_seq[3];
259 #if defined(__LP64__)
260 uint64_t m_tid; /* Which thread has this mutex locked */
261 uint32_t * m_lseqaddr;
262 uint32_t * m_useqaddr;
263 uint32_t reserved[2];
264 #else
265 uint32_t * m_lseqaddr;
266 uint64_t m_tid; /* Which thread has this mutex locked */
267 uint32_t * m_useqaddr;
268 #endif
269 } npthread_mutex_t;
270
271
272
273
274 /*
275 * Condition variable attributes
276 */
277 #define _PTHREAD_CONDATTR_T
278 typedef struct
279 {
280 long sig; /* Unique signature for this structure */
281 uint32_t pshared:2, /* pshared */
282 unsupported:30;
283 } pthread_condattr_t;
284
285 /*
286 * Condition variables
287 */
288 #define _PTHREAD_COND_T
289 typedef struct _pthread_cond
290 {
291 long sig; /* Unique signature for this structure */
292 pthread_lock_t lock; /* Used for internal mutex on structure */
293 uint32_t waiters:15, /* Number of threads waiting */
294 sigspending:15, /* Number of outstanding signals */
295 pshared:2;
296 struct _pthread_cond *next, *prev; /* List of condition variables using mutex */
297 struct _pthread_mutex *busy; /* mutex associated with variable */
298 semaphore_t sem; /* Kernel semaphore */
299 } pthread_cond_t;
300
301
302 typedef struct _npthread_cond
303 {
304 long sig; /* Unique signature for this structure */
305 pthread_lock_t lock; /* Used for internal mutex on structure */
306 uint32_t rfu:29, /* not in use*/
307 misalign: 1, /* structure is not aligned to 8 byte boundary */
308 pshared:2;
309 struct _npthread_mutex *busy; /* mutex associated with variable */
310 uint32_t c_seq[3];
311 #if defined(__LP64__)
312 uint32_t reserved[3];
313 #endif /* __LP64__ */
314 } npthread_cond_t;
315
316 /*
317 * Initialization control (once) variables
318 */
319 #define _PTHREAD_ONCE_T
320 typedef struct
321 {
322 long sig; /* Unique signature for this structure */
323 pthread_lock_t lock; /* Used for internal mutex on structure */
324 } pthread_once_t;
325
326 #define _PTHREAD_RWLOCKATTR_T
327 typedef struct {
328 long sig; /* Unique signature for this structure */
329 int pshared;
330 int rfu[2]; /* reserved for future use */
331 } pthread_rwlockattr_t;
332
333 #define _PTHREAD_RWLOCK_T
334 typedef struct {
335 long sig;
336 pthread_mutex_t lock; /* monitor lock */
337 int state;
338 pthread_cond_t read_signal;
339 pthread_cond_t write_signal;
340 int blocked_writers;
341 int reserved;
342 pthread_t owner;
343 int rfu[1];
344 int pshared;
345 } pthread_rwlock_t;
346
347 #define _PTHREAD_RWLOCK_T
348 typedef struct {
349 long sig;
350 pthread_lock_t lock;
351 #if defined(__LP64__)
352 int reserv;
353 volatile uint32_t rw_seq[4];
354 pthread_t rw_owner;
355 #else /* __LP64__ */
356 volatile uint32_t rw_seq[4];
357 pthread_t rw_owner;
358 int reserv;
359 #endif /* __LP64__ */
360 volatile uint32_t * rw_lseqaddr;
361 volatile uint32_t * rw_wcaddr;
362 volatile uint32_t * rw_useqaddr;
363 uint32_t rw_flags;
364 int misalign;
365 #if defined(__LP64__)
366 uint32_t rfu[31];
367 #else /* __LP64__ */
368 uint32_t rfu[18];
369 #endif /* __LP64__ */
370 int pshared;
371 } npthread_rwlock_t;
372
373 /* flags for rw_flags */
374 #define PTHRW_KERN_PROCESS_SHARED 0x10
375 #define PTHRW_KERN_PROCESS_PRIVATE 0x20
376 #define PTHRW_KERN_PROCESS_FLAGS_MASK 0x30
377
378 #define PTHRW_EBIT 0x01
379 #define PTHRW_LBIT 0x02
380 #define PTHRW_YBIT 0x04
381 #define PTHRW_WBIT 0x08
382 #define PTHRW_UBIT 0x10
383 #define PTHRW_RETRYBIT 0x20
384 #define PTHRW_SHADOW_W 0x20 /* same as 0x20, shadow W bit for rwlock */
385
386 #define PTHRW_TRYLKBIT 0x40
387 #define PTHRW_RW_HUNLOCK 0x40 /* readers responsible for handling unlock */
388
389 #define PTHRW_MTX_NONE 0x80
390 #define PTHRW_RW_INIT 0x80 /* reset on the lock bits */
391 #define PTHRW_RW_SPURIOUS 0x80 /* same as 0x80, spurious rwlock unlock ret from kernel */
392
393 #define PTHRW_INC 0x100
394 #define PTHRW_BIT_MASK 0x000000ff
395 #define PTHRW_UN_BIT_MASK 0x000000df /* remove shadow bit */
396
397 #define PTHRW_COUNT_SHIFT 8
398 #define PTHRW_COUNT_MASK 0xffffff00
399 #define PTHRW_MAX_READERS 0xffffff00
400
401
402 #define PTHREAD_MTX_TID_SWITCHING (uint64_t)-1
403
404 #define is_rw_ewubit_set(x) (((x) & (PTHRW_EBIT | PTHRW_WBIT | PTHRW_UBIT)) != 0)
405 #define is_rw_lbit_set(x) (((x) & PTHRW_LBIT) != 0)
406 #define is_rw_lybit_set(x) (((x) & (PTHRW_LBIT | PTHRW_YBIT)) != 0)
407 #define is_rw_ebit_set(x) (((x) & PTHRW_EBIT) != 0)
408 #define is_rw_ebit_clear(x) (((x) & PTHRW_EBIT) == 0)
409 #define is_rw_uebit_set(x) (((x) & (PTHRW_EBIT | PTHRW_UBIT)) != 0)
410 #define is_rw_ewuybit_set(x) (((x) & (PTHRW_EBIT | PTHRW_WBIT | PTHRW_UBIT | PTHRW_YBIT)) != 0)
411 #define is_rw_ewuybit_clear(x) (((x) & (PTHRW_EBIT | PTHRW_WBIT | PTHRW_UBIT | PTHRW_YBIT)) == 0)
412 #define is_rw_ewubit_set(x) (((x) & (PTHRW_EBIT | PTHRW_WBIT | PTHRW_UBIT)) != 0)
413 #define is_rw_ewubit_clear(x) (((x) & (PTHRW_EBIT | PTHRW_WBIT | PTHRW_UBIT)) == 0)
414
415 /* is x lower than Y */
416 #define is_seqlower(x, y) ((x < y) || ((x - y) > (PTHRW_MAX_READERS/2)))
417 /* is x lower than or eq Y */
418 #define is_seqlower_eq(x, y) ((x <= y) || ((x - y) > (PTHRW_MAX_READERS/2)))
419
420 /* is x greater than Y */
421 #define is_seqhigher(x, y) ((x > y) || ((y - x) > (PTHRW_MAX_READERS/2)))
422
423 static inline int diff_genseq(uint32_t x, uint32_t y) {
424 if (x > y) {
425 return(x-y);
426 } else {
427 return((PTHRW_MAX_READERS - y) + x +1);
428 }
429 }
430
431 /* keep the size to 64bytes for both 64 and 32 */
432 #define _PTHREAD_WORKQUEUE_ATTR_T
433 typedef struct {
434 uint32_t sig;
435 int queueprio;
436 int overcommit;
437 unsigned int resv2[13];
438 } pthread_workqueue_attr_t;
439
440 #define _PTHREAD_WORKITEM_T
441 typedef struct _pthread_workitem {
442 TAILQ_ENTRY(_pthread_workitem) item_entry; /* pthread_workitem list in prio */
443 void (*func)(void *);
444 void * func_arg;
445 struct _pthread_workqueue * workq;
446 unsigned int flags;
447 unsigned int gencount;
448 } * pthread_workitem_t;
449
450 #define PTH_WQITEM_INKERNEL_QUEUE 1
451 #define PTH_WQITEM_RUNNING 2
452 #define PTH_WQITEM_COMPLETED 4
453 #define PTH_WQITEM_REMOVED 8
454 #define PTH_WQITEM_BARRIER 0x10
455 #define PTH_WQITEM_DESTROY 0x20
456 #define PTH_WQITEM_NOTINLIST 0x40
457 #define PTH_WQITEM_APPLIED 0x80
458 #define PTH_WQITEM_KERN_COUNT 0x100
459
460 #define WORKITEM_POOL_SIZE 1000
461 TAILQ_HEAD(__pthread_workitem_pool, _pthread_workitem);
462 extern struct __pthread_workitem_pool __pthread_workitem_pool_head; /* head list of workitem pool */
463
464 #define WQ_NUM_PRIO_QS 3 /* WORKQ_HIGH/DEFAULT/LOW_PRIOQUEUE */
465
466 #define _PTHREAD_WORKQUEUE_HEAD_T
467 typedef struct _pthread_workqueue_head {
468 TAILQ_HEAD(, _pthread_workqueue) wqhead;
469 struct _pthread_workqueue * next_workq;
470 } * pthread_workqueue_head_t;
471
472
473 #define _PTHREAD_WORKQUEUE_T
474 typedef struct _pthread_workqueue {
475 unsigned int sig; /* Unique signature for this structure */
476 pthread_lock_t lock; /* Used for internal mutex on structure */
477 TAILQ_ENTRY(_pthread_workqueue) wq_list; /* workqueue list in prio */
478 TAILQ_HEAD(, _pthread_workitem) item_listhead; /* pthread_workitem list in prio */
479 TAILQ_HEAD(, _pthread_workitem) item_kernhead; /* pthread_workitem list in prio */
480 unsigned int flags;
481 size_t stacksize;
482 int istimeshare;
483 int importance;
484 int affinity;
485 int queueprio;
486 int barrier_count;
487 int kq_count;
488 void (*term_callback)(struct _pthread_workqueue *,void *);
489 void * term_callarg;
490 pthread_workqueue_head_t headp;
491 int overcommit;
492 #if defined(__ppc64__) || defined(__x86_64__)
493 unsigned int rev2[2];
494 #else
495 unsigned int rev2[12];
496 #endif
497 } * pthread_workqueue_t;
498
499 #define PTHREAD_WORKQ_IN_CREATION 1
500 #define PTHREAD_WORKQ_IN_TERMINATE 2
501 #define PTHREAD_WORKQ_BARRIER_ON 4
502 #define PTHREAD_WORKQ_TERM_ON 8
503 #define PTHREAD_WORKQ_DESTROYED 0x10
504 #define PTHREAD_WORKQ_REQUEUED 0x20
505 #define PTHREAD_WORKQ_SUSPEND 0x40
506
507 #define WORKQUEUE_POOL_SIZE 100
508 TAILQ_HEAD(__pthread_workqueue_pool, _pthread_workqueue);
509 extern struct __pthread_workqueue_pool __pthread_workqueue_pool_head; /* head list of workqueue pool */
510
511 #include "pthread.h"
512
513 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__) || (defined(__arm__) && (defined(_ARM_ARCH_7) || !defined(_ARM_ARCH_6) || !defined(__thumb__)))
514 /*
515 * Inside libSystem, we can use r13 or %gs directly to get access to the
516 * thread-specific data area. The current thread is in the first slot.
517 */
518 inline static pthread_t __attribute__((__pure__))
519 _pthread_self_direct(void)
520 {
521 pthread_t ret;
522 #if defined(__i386__) || defined(__x86_64__)
523 asm("mov %%gs:%P1, %0" : "=r" (ret) : "i" (offsetof(struct _pthread, tsd[0])));
524 #elif defined(__ppc64__)
525 register const pthread_t __pthread_self asm ("r13");
526 ret = __pthread_self;
527 #elif defined(__arm__) && defined(_ARM_ARCH_6)
528 __asm__ ("mrc p15, 0, %0, c13, c0, 3" : "=r"(ret));
529 #elif defined(__arm__) && !defined(_ARM_ARCH_6)
530 register const pthread_t __pthread_self asm ("r9");
531 ret = __pthread_self;
532 #endif
533 return ret;
534 }
535 #define pthread_self() _pthread_self_direct()
536 #endif
537
538 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
539 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
540 #define _PTHREAD_DEFAULT_PRIOCEILING 0
541 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
542 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
543 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
544
545 #define _PTHREAD_NO_SIG 0x00000000
546 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
547 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
548 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
549 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
550 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
551 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
552 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
553 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
554 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
555 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
556 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
557 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
558 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
559
560
561 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
562 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
563 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
564
565 #define _PTHREAD_CREATE_PARENT 4
566 #define _PTHREAD_EXITED 8
567 // 4597450: begin
568 #define _PTHREAD_WASCANCEL 0x10
569 // 4597450: end
570
571 #if defined(DEBUG)
572 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
573 #else
574 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
575 #endif
576 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
577
578 #define _PTHREAD_CANCEL_STATE_MASK 0x01
579 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
580 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
581
582 extern boolean_t swtch_pri(int);
583
584 #ifndef PTHREAD_MACH_CALL
585 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
586 #endif
587
588 /* Prototypes. */
589
590 /* Functions defined in machine-dependent files. */
591 extern vm_address_t _sp(void);
592 extern vm_address_t _adjust_sp(vm_address_t sp);
593 extern void _pthread_setup(pthread_t th, void (*f)(pthread_t), void *sp, int suspended, int needresume);
594
595 extern void _pthread_tsd_cleanup(pthread_t self);
596
597 #if defined(__i386__) || defined(__x86_64__)
598 __private_extern__ void __mtx_holdlock(npthread_mutex_t *mutex, uint32_t diff, uint32_t * flagp, uint32_t ** pmtxp, uint32_t * mgenp, uint32_t * ugenp);
599 __private_extern__ int __mtx_droplock(npthread_mutex_t *mutex, int count, uint32_t * flagp, uint32_t ** pmtxp, uint32_t * mgenp, uint32_t * ugenp, uint32_t * notifyp);
600 __private_extern__ int __mtx_updatebits(npthread_mutex_t *mutex, uint32_t updateval, int firstfiti, int fromcond);
601
602 /* syscall interfaces */
603 extern uint32_t __psynch_mutexwait(pthread_mutex_t * mutex, uint32_t mgen, uint32_t ugen, uint64_t tid, uint32_t flags);
604 extern uint32_t __psynch_mutexdrop(pthread_mutex_t * mutex, uint32_t mgen, uint32_t ugen, uint64_t tid, uint32_t flags);
605 extern int __psynch_cvbroad(pthread_cond_t * cv, uint32_t cvgen, uint32_t diffgen, pthread_mutex_t * mutex, uint32_t mgen, uint32_t ugen, uint64_t tid, uint32_t flags);
606 extern int __psynch_cvsignal(pthread_cond_t * cv, uint32_t cvgen, uint32_t cvugen, pthread_mutex_t * mutex, uint32_t mgen, uint32_t ugen, int thread_port, uint32_t flags);
607 extern uint32_t __psynch_cvwait(pthread_cond_t * cv, uint32_t cvgen, uint32_t cvugen, pthread_mutex_t * mutex, uint32_t mgen, uint32_t ugen, uint64_t sec, uint64_t usec);
608 extern uint32_t __psynch_rw_longrdlock(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
609 extern uint32_t __psynch_rw_yieldwrlock(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
610 extern int __psynch_rw_downgrade(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
611 extern uint32_t __psynch_rw_upgrade(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
612 extern uint32_t __psynch_rw_rdlock(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
613 extern uint32_t __psynch_rw_wrlock(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
614 extern uint32_t __psynch_rw_unlock(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
615 extern uint32_t __psynch_rw_unlock2(pthread_rwlock_t * rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags);
616 #endif /* __i386__ || __x86_64__ */
617
618 __private_extern__ semaphore_t new_sem_from_pool(void);
619 __private_extern__ void restore_sem_to_pool(semaphore_t);
620 __private_extern__ void _pthread_atfork_queue_init(void);
621 int _pthread_lookup_thread(pthread_t thread, mach_port_t * port, int only_joinable);
622 int _pthread_join_cleanup(pthread_t thread, void ** value_ptr, int conforming);
623
624 #endif /* _POSIX_PTHREAD_INTERNALS_H */