2 * Copyright (c) 2000-2003, 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
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.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
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.
49 * POSIX Threads - IEEE 1003.1c
52 #ifndef _POSIX_PTHREAD_INTERNALS_H
53 #define _POSIX_PTHREAD_INTERNALS_H
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
;
66 #include <mach/mach.h>
67 #include <mach/mach_error.h>
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. */
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
;
86 #define _EXTERNAL_POSIX_THREAD_KEYS_MAX 512
87 #define _INTERNAL_POSIX_THREAD_KEYS_MAX 256
88 #define _INTERNAL_POSIX_THREAD_KEYS_END 768
94 typedef struct _pthread
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 */
108 size_t guardsize
; /* size in bytes to guard stack overflow */
109 #if !defined(__LP64__)
110 int pad0
; /* for backwards compatibility */
112 struct sched_param param
;
113 struct _pthread_mutex
*mutexes
;
114 struct _pthread
*joiner
;
115 #if !defined(__LP64__)
116 int pad1
; /* for backwards compatibility */
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 */
132 void *cthread_self
; /* cthread_self() if somebody calls cthread_set_self() */
133 /* protected by list lock */
134 u_int32_t childrun
:1,
138 #if defined(__LP64__)
139 int pad4
; /* for natural alignment */
141 TAILQ_ENTRY(_pthread
) plist
;
144 mach_port_t joiner_notify
;
145 char pthread_name
[64]; /* including nulll the name */
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
155 typedef char _need_to_change_PTHREAD_TSD_OFFSET
[(_PTHREAD_TSD_OFFSET
== offsetof(struct _pthread
, tsd
[0])) ? 0 : -1] ;
160 struct _pthread_attr_t
162 long sig
; /* Unique signature for this structure */
164 u_int32_t detached
:8,
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) */
182 #define _PTHREAD_MUTEXATTR_T
185 long sig
; /* Unique signature for this structure */
187 u_int32_t protocol
:2, /* protocol attribute */
188 type
:2, /* mutex type */
191 } pthread_mutexattr_t
;
196 #define _PTHREAD_MUTEX_T
197 typedef struct _pthread_mutex
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 */
210 struct _pthread_mutex
*next
, *prev
; /* List of other mutexes he owns */
211 struct _pthread_cond
*busy
; /* List of condition variables using this mutex */
213 int16_t priority
; /* Priority to restore when mutex unlocked */
220 * Condition variable attributes
222 #define _PTHREAD_CONDATTR_T
225 long sig
; /* Unique signature for this structure */
226 u_int32_t pshared
:2, /* pshared */
228 } pthread_condattr_t
;
231 * Condition variables
233 #define _PTHREAD_COND_T
234 typedef struct _pthread_cond
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 */
248 * Initialization control (once) variables
250 #define _PTHREAD_ONCE_T
253 long sig
; /* Unique signature for this structure */
254 pthread_lock_t lock
; /* Used for internal mutex on structure */
257 #define _PTHREAD_RWLOCKATTR_T
259 long sig
; /* Unique signature for this structure */
261 int rfu
[2]; /* reserved for future use */
262 } pthread_rwlockattr_t
;
264 #define _PTHREAD_RWLOCK_T
267 pthread_mutex_t lock
; /* monitor lock */
269 #define _pthread_rwlock_kernid state
270 pthread_cond_t read_signal
;
271 pthread_cond_t write_signal
;
278 /* keep the size to 64bytes for both 64 and 32 */
279 #define _PTHREAD_WORKQUEUE_ATTR_T
282 #if defined(__ppc64__) || defined(__x86_64__)
290 #if defined(__ppc64__) || defined(__x86_64__)
291 unsigned int resv2
[8];
293 unsigned int resv2
[10];
295 } pthread_workqueue_attr_t
;
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 *);
302 struct _pthread_workqueue
* workq
;
304 } * pthread_workitem_t
;
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
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 */
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 */
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
;
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 */
345 void (*term_callback
)(struct _pthread_workqueue
*,void *);
347 pthread_workqueue_head_t headp
;
349 #if defined(__ppc64__) || defined(__x86_64__)
350 unsigned int rev2
[2];
352 unsigned int rev2
[12];
354 } * pthread_workqueue_t
;
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
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 */
370 #if defined(__i386__) || defined(__ppc64__) || defined(__x86_64__) || defined(__arm__)
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.
375 inline static pthread_t
__attribute__((__pure__
))
376 _pthread_self_direct(void)
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 #elif defined(__arm__)
385 register const pthread_t __pthread_self
asm ("r9");
386 ret
= __pthread_self
;
390 #define pthread_self() _pthread_self_direct()
393 #define _PTHREAD_DEFAULT_INHERITSCHED PTHREAD_INHERIT_SCHED
394 #define _PTHREAD_DEFAULT_PROTOCOL PTHREAD_PRIO_NONE
395 #define _PTHREAD_DEFAULT_PRIOCEILING 0
396 #define _PTHREAD_DEFAULT_POLICY SCHED_OTHER
397 #define _PTHREAD_DEFAULT_STACKSIZE 0x80000 /* 512K */
398 #define _PTHREAD_DEFAULT_PSHARED PTHREAD_PROCESS_PRIVATE
400 #define _PTHREAD_NO_SIG 0x00000000
401 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
402 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
403 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
404 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
405 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
406 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
407 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
408 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
409 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
410 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
411 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
412 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
413 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
416 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
417 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
418 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
420 #define _PTHREAD_CREATE_PARENT 4
421 #define _PTHREAD_EXITED 8
423 #define _PTHREAD_WASCANCEL 0x10
427 #define _PTHREAD_MUTEX_OWNER_SELF pthread_self()
429 #define _PTHREAD_MUTEX_OWNER_SELF (pthread_t)0x12141968
431 #define _PTHREAD_MUTEX_OWNER_SWITCHING (pthread_t)(~0)
433 #define _PTHREAD_CANCEL_STATE_MASK 0x01
434 #define _PTHREAD_CANCEL_TYPE_MASK 0x02
435 #define _PTHREAD_CANCEL_PENDING 0x10 /* pthread_cancel() has been called for this thread */
437 extern boolean_t
swtch_pri(int);
439 #ifndef PTHREAD_MACH_CALL
440 #define PTHREAD_MACH_CALL(expr, ret) (ret) = (expr)
445 /* Functions defined in machine-dependent files. */
446 extern vm_address_t
_sp(void);
447 extern vm_address_t
_adjust_sp(vm_address_t sp
);
448 extern void _pthread_setup(pthread_t th
, void (*f
)(pthread_t
), void *sp
, int suspended
, int needresume
);
450 extern void _pthread_tsd_cleanup(pthread_t self
);
452 __private_extern__ semaphore_t
new_sem_from_pool(void);
453 __private_extern__
void restore_sem_to_pool(semaphore_t
);
454 __private_extern__
void _pthread_atfork_queue_init(void);
455 int _pthread_lookup_thread(pthread_t thread
, mach_port_t
* port
, int only_joinable
);
456 int _pthread_join_cleanup(pthread_t thread
, void ** value_ptr
, int conforming
);
457 #endif /* _POSIX_PTHREAD_INTERNALS_H */