]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/pthread_internal.h
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / sys / pthread_internal.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _SYS_PTHREAD_INTERNAL_H_
30 #define _SYS_PTHREAD_INTERNAL_H_
31
32 #undef pthread_mutexattr_t;
33
34 #include <kern/thread_call.h>
35
36 /*
37 * Mutex attributes
38 */
39 typedef struct
40 {
41 long sig; /* Unique signature for this structure */
42 int prioceiling;
43 u_int32_t protocol:2, /* protocol attribute */
44 type:2, /* mutex type */
45 pshared:2,
46 rfu:26;
47 } pthread_mutexattr_t;
48
49 #undef pthread_mutex_t
50 /*
51 * Mutex variables
52 */
53 typedef struct _pthread_mutex
54 {
55 long sig; /* Unique signature for this structure */
56 lck_mtx_t * mutex; /* the kernel internal mutex */
57 lck_mtx_t * lock;
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 */
63 refcount:10,
64 lock_count:16;
65 int16_t prioceiling;
66 int16_t priority; /* Priority to restore when mutex unlocked */
67 } pthread_mutex_t;
68
69 #define MTX_LOCK lck_mtx_lock
70 #define MTX_UNLOCK lck_mtx_unlock
71
72 /*
73 * Condition variable attributes
74 */
75 #undef pthread_condattr_t
76 typedef struct
77 {
78 long sig; /* Unique signature for this structure */
79 u_int32_t pshared:2, /* pshared */
80 unsupported:30;
81 } pthread_condattr_t;
82
83 /*
84 * Condition variables
85 */
86 #undef pthread_cond_t
87 typedef struct _pthread_cond
88 {
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 */
93 pshared:2;
94 int refcount;
95 pthread_mutex_t * mutex;
96 proc_t owner_proc; /* Which thread has this mutex locked */
97 semaphore_t sem;
98 } pthread_cond_t;
99 #define COND_LOCK lck_mtx_lock
100 #define COND_UNLOCK lck_mtx_unlock
101
102 #undef pthread_rwlockattr_t
103 typedef struct {
104 long sig; /* Unique signature for this structure */
105 int pshared;
106 int rfu[2]; /* reserved for future use */
107 } pthread_rwlockattr_t;
108
109 #undef pthread_rwlock_t
110 typedef struct {
111 long sig;
112 lck_rw_t * rwlock;
113 int pshared;
114 thread_t owner;
115 int rfu[2];
116 } pthread_rwlock_t;
117
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' */
132
133 #define _PTHREAD_KERN_COND_SIG 0x12345678 /* */
134 #define _PTHREAD_KERN_MUTEX_SIG 0x34567812 /* */
135 #define _PTHREAD_KERN_RWLOCK_SIG 0x56781234 /* */
136
137
138 #define PTHREAD_PROCESS_SHARED 1
139 #define PTHREAD_PROCESS_PRIVATE 2
140
141 #define WORKQUEUE_MAXTHREADS 64
142 #define WORKITEM_SIZE 64
143 #define WORKQUEUE_NUMPRIOS 5
144
145 struct threadlist {
146 TAILQ_ENTRY(threadlist) th_entry;
147 thread_t th_thread;
148 int th_flags;
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;
156 };
157 #define TH_LIST_INITED 0x01
158 #define TH_LIST_RUNNING 0x02
159 #define TH_LIST_BLOCKED 0x04
160 #define TH_LIST_SUSPENDED 0x08
161
162 struct workitem {
163 TAILQ_ENTRY(workitem) wi_entry;
164 user_addr_t wi_item;
165 };
166
167 struct workitemlist {
168 TAILQ_HEAD(, workitem) wl_itemlist;
169 TAILQ_HEAD(, workitem) wl_freelist;
170 };
171
172
173 struct workqueue {
174 struct workitem wq_array[WORKITEM_SIZE * WORKQUEUE_NUMPRIOS];
175 proc_t wq_proc;
176 vm_map_t wq_map;
177 task_t wq_task;
178 thread_call_t wq_timer_call;
179 int wq_flags;
180 int wq_itemcount;
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;
194 };
195 #define WQ_LIST_INITED 0x01
196 #define WQ_BUSY 0x02
197 #define WQ_TIMER_RUNNING 0x04
198 #define WQ_TIMER_WATCH 0x08
199 #define WQ_ADD_TO_POOL 0x10
200
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
205
206 /* workq_ops commands */
207 #define WQOPS_QUEUE_ADD 1
208 #define WQOPS_QUEUE_REMOVE 2
209 #define WQOPS_THREAD_RETURN 4
210
211 #define PTH_DEFAULT_STACKSIZE 512*1024
212 #define PTH_DEFAULT_GUARDSIZE 4*1024
213 #define MAX_PTHREAD_SIZE 64*1024
214
215 void workqueue_exit(struct proc *);
216
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 *);
225
226 void pthread_list_lock(void);
227 void pthread_list_unlock(void);
228
229 #endif /* _SYS_PTHREAD_INTERNAL_H_ */
230