2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _SYS_PTHREAD_INTERNAL_H_
30 #define _SYS_PTHREAD_INTERNAL_H_
32 #undef pthread_mutexattr_t;
34 #include <kern/thread_call.h>
41 long sig
; /* Unique signature for this structure */
43 u_int32_t protocol
:2, /* protocol attribute */
44 type
:2, /* mutex type */
47 } pthread_mutexattr_t
;
49 #undef pthread_mutex_t
53 typedef struct _pthread_mutex
55 long sig
; /* Unique signature for this structure */
56 lck_mtx_t
* mutex
; /* the kernel internal mutex */
58 thread_t owner
; /* Which thread has this mutex locked */
59 proc_t owner_proc
; /* Which thread has this mutex locked */
60 u_int32_t protocol
:2, /* protocol */
61 type
:2, /* mutex type */
62 pshared
:2, /* mutex type */
66 int16_t priority
; /* Priority to restore when mutex unlocked */
69 #define MTX_LOCK lck_mtx_lock
70 #define MTX_UNLOCK lck_mtx_unlock
73 * Condition variable attributes
75 #undef pthread_condattr_t
78 long sig
; /* Unique signature for this structure */
79 u_int32_t pshared
:2, /* pshared */
87 typedef struct _pthread_cond
89 long sig
; /* Unique signature for this structure */
90 lck_mtx_t
* lock
; /* Used for internal mutex on structure */
91 u_int32_t waiters
:15, /* Number of threads waiting */
92 sigpending
:15, /* Number of outstanding signals */
95 pthread_mutex_t
* mutex
;
96 proc_t owner_proc
; /* Which thread has this mutex locked */
99 #define COND_LOCK lck_mtx_lock
100 #define COND_UNLOCK lck_mtx_unlock
102 #undef pthread_rwlockattr_t
104 long sig
; /* Unique signature for this structure */
106 int rfu
[2]; /* reserved for future use */
107 } pthread_rwlockattr_t
;
109 #undef pthread_rwlock_t
118 #define _PTHREAD_NO_SIG 0x00000000
119 #define _PTHREAD_MUTEX_ATTR_SIG 0x4D545841 /* 'MTXA' */
120 #define _PTHREAD_MUTEX_SIG 0x4D555458 /* 'MUTX' */
121 #define _PTHREAD_MUTEX_SIG_init 0x32AAABA7 /* [almost] ~'MUTX' */
122 #define _PTHREAD_COND_ATTR_SIG 0x434E4441 /* 'CNDA' */
123 #define _PTHREAD_COND_SIG 0x434F4E44 /* 'COND' */
124 #define _PTHREAD_COND_SIG_init 0x3CB0B1BB /* [almost] ~'COND' */
125 #define _PTHREAD_ATTR_SIG 0x54484441 /* 'THDA' */
126 #define _PTHREAD_ONCE_SIG 0x4F4E4345 /* 'ONCE' */
127 #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA /* [almost] ~'ONCE' */
128 #define _PTHREAD_SIG 0x54485244 /* 'THRD' */
129 #define _PTHREAD_RWLOCK_ATTR_SIG 0x52574C41 /* 'RWLA' */
130 #define _PTHREAD_RWLOCK_SIG 0x52574C4B /* 'RWLK' */
131 #define _PTHREAD_RWLOCK_SIG_init 0x2DA8B3B4 /* [almost] ~'RWLK' */
133 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
134 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
135 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
138 #define PTHREAD_PROCESS_SHARED 1
139 #define PTHREAD_PROCESS_PRIVATE 2
141 #define WORKQUEUE_MAXTHREADS 64
142 #define WORKITEM_SIZE 64
143 #define WORKQUEUE_NUMPRIOS 5
146 TAILQ_ENTRY(threadlist
) th_entry
;
149 uint32_t th_unparked
;
150 uint32_t th_affinity_tag
;
151 struct workqueue
*th_workq
;
152 mach_vm_size_t th_stacksize
;
153 mach_vm_size_t th_allocsize
;
154 mach_vm_offset_t th_stackaddr
;
155 mach_port_t th_thport
;
157 #define TH_LIST_INITED 0x01
158 #define TH_LIST_RUNNING 0x02
159 #define TH_LIST_BLOCKED 0x04
160 #define TH_LIST_SUSPENDED 0x08
163 TAILQ_ENTRY(workitem
) wi_entry
;
167 struct workitemlist
{
168 TAILQ_HEAD(, workitem
) wl_itemlist
;
169 TAILQ_HEAD(, workitem
) wl_freelist
;
174 struct workitem wq_array
[WORKITEM_SIZE
* WORKQUEUE_NUMPRIOS
];
178 thread_call_t wq_timer_call
;
181 struct timeval wq_lastran_ts
;
182 struct timeval wq_reduce_ts
;
183 uint32_t wq_stalled_count
;
184 uint32_t wq_max_threads_scheduled
;
185 uint32_t wq_affinity_max
;
186 uint32_t wq_threads_scheduled
;
187 uint32_t wq_nthreads
;
188 uint32_t wq_nextaffinitytag
;
189 struct workitemlist wq_list
[WORKQUEUE_NUMPRIOS
]; /* prio based item list */
190 TAILQ_HEAD(, threadlist
) wq_thrunlist
;
191 TAILQ_HEAD(wq_thidlelist
, threadlist
) * wq_thidlelist
;
192 uint32_t * wq_thactivecount
;
193 uint32_t * wq_thcount
;
195 #define WQ_LIST_INITED 0x01
197 #define WQ_TIMER_RUNNING 0x04
198 #define WQ_TIMER_WATCH 0x08
199 #define WQ_ADD_TO_POOL 0x10
201 #define WQ_STALLED_WINDOW_USECS 20000
202 #define WQ_REDUCE_POOL_WINDOW_USECS 3000000
203 #define WQ_MAX_RUN_LATENCY_USECS 500
204 #define WQ_TIMER_INTERVAL_MSECS 40
206 /* workq_ops commands */
207 #define WQOPS_QUEUE_ADD 1
208 #define WQOPS_QUEUE_REMOVE 2
209 #define WQOPS_THREAD_RETURN 4
211 #define PTH_DEFAULT_STACKSIZE 512*1024
212 #define PTH_DEFAULT_GUARDSIZE 4*1024
213 #define MAX_PTHREAD_SIZE 64*1024
215 void workqueue_exit(struct proc
*);
217 pthread_mutex_t
* pthread_id_to_mutex(int mutexid
);
218 int pthread_id_mutex_add(pthread_mutex_t
*);
219 void pthread_id_mutex_remove(int);
220 void pthread_mutex_release(pthread_mutex_t
*);
221 pthread_cond_t
* pthread_id_to_cond(int condid
);
222 int pthread_id_cond_add(pthread_cond_t
*);
223 void pthread_id_cond_remove(int);
224 void pthread_cond_release(pthread_cond_t
*);
226 void pthread_list_lock(void);
227 void pthread_list_unlock(void);
229 #endif /* _SYS_PTHREAD_INTERNAL_H_ */