]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/locks.h
c1228b46d21a359a49ab5cf64780de113831b4a0
[apple/xnu.git] / osfmk / kern / locks.h
1 /*
2 * Copyright (c) 2003-2005 Apple Computer, 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 #ifndef _KERN_LOCKS_H_
25 #define _KERN_LOCKS_H_
26
27 #include <sys/cdefs.h>
28 #include <sys/appleapiopts.h>
29 #include <mach/boolean.h>
30 #include <mach/mach_types.h>
31 #include <kern/kern_types.h>
32 #include <machine/locks.h>
33
34 #ifdef MACH_KERNEL_PRIVATE
35 #include <kern/queue.h>
36
37 extern void lck_mod_init(
38 void);
39
40 typedef unsigned int lck_type_t;
41
42 #define LCK_TYPE_SPIN 1
43 #define LCK_TYPE_MTX 2
44 #define LCK_TYPE_RW 3
45
46 #endif
47
48 typedef unsigned int lck_sleep_action_t;
49
50 #define LCK_SLEEP_DEFAULT 0x00 /* Release the lock while waiting for the event, then reclaim */
51 /* RW locks are returned in the same mode */
52 #define LCK_SLEEP_UNLOCK 0x01 /* Release the lock and return unheld */
53 #define LCK_SLEEP_SHARED 0x02 /* Reclaim the lock in shared mode (RW only) */
54 #define LCK_SLEEP_EXCLUSIVE 0x04 /* Reclaim the lock in exclusive mode (RW only) */
55
56 #define LCK_SLEEP_MASK 0x07 /* Valid actions */
57
58 #ifdef MACH_KERNEL_PRIVATE
59
60 typedef struct {
61 uint64_t lck_grp_spin_util_cnt;
62 uint64_t lck_grp_spin_held_cnt;
63 uint64_t lck_grp_spin_miss_cnt;
64 uint64_t lck_grp_spin_held_max;
65 uint64_t lck_grp_spin_held_cum;
66 } lck_grp_spin_stat_t;
67
68 typedef struct {
69 uint64_t lck_grp_mtx_util_cnt;
70 uint64_t lck_grp_mtx_held_cnt;
71 uint64_t lck_grp_mtx_miss_cnt;
72 uint64_t lck_grp_mtx_wait_cnt;
73 uint64_t lck_grp_mtx_held_max;
74 uint64_t lck_grp_mtx_held_cum;
75 uint64_t lck_grp_mtx_wait_max;
76 uint64_t lck_grp_mtx_wait_cum;
77 } lck_grp_mtx_stat_t;
78
79 typedef struct {
80 uint64_t lck_grp_rw_util_cnt;
81 uint64_t lck_grp_rw_held_cnt;
82 uint64_t lck_grp_rw_miss_cnt;
83 uint64_t lck_grp_rw_wait_cnt;
84 uint64_t lck_grp_rw_held_max;
85 uint64_t lck_grp_rw_held_cum;
86 uint64_t lck_grp_rw_wait_max;
87 uint64_t lck_grp_rw_wait_cum;
88 } lck_grp_rw_stat_t;
89
90 typedef struct _lck_grp_stat_ {
91 lck_grp_spin_stat_t lck_grp_spin_stat;
92 lck_grp_mtx_stat_t lck_grp_mtx_stat;
93 lck_grp_rw_stat_t lck_grp_rw_stat;
94 } lck_grp_stat_t;
95
96 #define LCK_GRP_MAX_NAME 64
97
98 typedef struct _lck_grp_ {
99 queue_chain_t lck_grp_link;
100 unsigned int lck_grp_refcnt;
101 unsigned int lck_grp_spincnt;
102 unsigned int lck_grp_mtxcnt;
103 unsigned int lck_grp_rwcnt;
104 unsigned int lck_grp_attr;
105 char lck_grp_name[LCK_GRP_MAX_NAME];
106 lck_grp_stat_t lck_grp_stat;
107 } lck_grp_t;
108
109 #define LCK_GRP_NULL (lck_grp_t *)0
110
111 #else
112 typedef struct __lck_grp__ lck_grp_t;
113 #endif
114
115 #ifdef MACH_KERNEL_PRIVATE
116 typedef struct _lck_grp_attr_ {
117 unsigned int grp_attr_val;
118 } lck_grp_attr_t;
119
120 extern lck_grp_attr_t LockDefaultGroupAttr;
121
122 #define LCK_GRP_ATTR_STAT 0x1
123
124 #else
125 typedef struct __lck_grp_attr__ lck_grp_attr_t;
126 #endif
127
128 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
129
130 __BEGIN_DECLS
131
132 extern lck_grp_attr_t *lck_grp_attr_alloc_init(
133 void);
134
135 extern void lck_grp_attr_setdefault(
136 lck_grp_attr_t *attr);
137
138 extern void lck_grp_attr_setstat(
139 lck_grp_attr_t *attr);
140
141 extern void lck_grp_attr_free(
142 lck_grp_attr_t *attr);
143
144 extern lck_grp_t *lck_grp_alloc_init(
145 const char* grp_name,
146 lck_grp_attr_t *attr);
147
148 __END_DECLS
149
150 #ifdef MACH_KERNEL_PRIVATE
151 extern void lck_grp_init(
152 lck_grp_t *grp,
153 const char* grp_name,
154 lck_grp_attr_t *attr);
155
156 extern void lck_grp_reference(
157 lck_grp_t *grp);
158
159 extern void lck_grp_deallocate(
160 lck_grp_t *grp);
161
162 extern void lck_grp_lckcnt_incr(
163 lck_grp_t *grp,
164 lck_type_t lck_type);
165
166 extern void lck_grp_lckcnt_decr(
167 lck_grp_t *grp,
168 lck_type_t lck_type);
169 #endif
170
171 __BEGIN_DECLS
172
173 extern void lck_grp_free(
174 lck_grp_t *grp);
175
176 __END_DECLS
177
178 #ifdef MACH_KERNEL_PRIVATE
179 typedef struct _lck_attr_ {
180 unsigned int lck_attr_val;
181 } lck_attr_t;
182
183 extern lck_attr_t LockDefaultLckAttr;
184
185 #define LCK_ATTR_NONE 0
186 #define LCK_ATTR_DEBUG 0x1
187
188 #else
189 typedef struct __lck_attr__ lck_attr_t;
190 #endif
191
192 #define LCK_ATTR_NULL (lck_attr_t *)0
193
194 __BEGIN_DECLS
195
196 extern lck_attr_t *lck_attr_alloc_init(
197 void);
198
199 extern void lck_attr_setdefault(
200 lck_attr_t *attr);
201
202 extern void lck_attr_setdebug(
203 lck_attr_t *attr);
204
205 extern void lck_attr_free(
206 lck_attr_t *attr);
207
208 #define decl_lck_spin_data(class,name) class lck_spin_t name;
209
210 extern lck_spin_t *lck_spin_alloc_init(
211 lck_grp_t *grp,
212 lck_attr_t *attr);
213
214 extern void lck_spin_init(
215 lck_spin_t *lck,
216 lck_grp_t *grp,
217 lck_attr_t *attr);
218
219 extern void lck_spin_lock(
220 lck_spin_t *lck);
221
222 extern void lck_spin_unlock(
223 lck_spin_t *lck);
224
225 extern void lck_spin_destroy(
226 lck_spin_t *lck,
227 lck_grp_t *grp);
228
229 extern void lck_spin_free(
230 lck_spin_t *lck,
231 lck_grp_t *grp);
232
233 extern wait_result_t lck_spin_sleep(
234 lck_spin_t *lck,
235 lck_sleep_action_t lck_sleep_action,
236 event_t event,
237 wait_interrupt_t interruptible);
238
239 extern wait_result_t lck_spin_sleep_deadline(
240 lck_spin_t *lck,
241 lck_sleep_action_t lck_sleep_action,
242 event_t event,
243 wait_interrupt_t interruptible,
244 uint64_t deadline);
245
246 #ifdef KERNEL_PRIVATE
247
248 extern boolean_t lck_spin_try_lock(
249 lck_spin_t *lck);
250
251 #endif
252
253
254 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
255
256 extern lck_mtx_t *lck_mtx_alloc_init(
257 lck_grp_t *grp,
258 lck_attr_t *attr);
259
260 extern void lck_mtx_init(
261 lck_mtx_t *lck,
262 lck_grp_t *grp,
263 lck_attr_t *attr);
264
265 extern void lck_mtx_lock(
266 lck_mtx_t *lck);
267
268 extern void lck_mtx_unlock(
269 lck_mtx_t *lck);
270
271 extern void lck_mtx_destroy(
272 lck_mtx_t *lck,
273 lck_grp_t *grp);
274
275 extern void lck_mtx_free(
276 lck_mtx_t *lck,
277 lck_grp_t *grp);
278
279 extern wait_result_t lck_mtx_sleep(
280 lck_mtx_t *lck,
281 lck_sleep_action_t lck_sleep_action,
282 event_t event,
283 wait_interrupt_t interruptible);
284
285 extern wait_result_t lck_mtx_sleep_deadline(
286 lck_mtx_t *lck,
287 lck_sleep_action_t lck_sleep_action,
288 event_t event,
289 wait_interrupt_t interruptible,
290 uint64_t deadline);
291
292 #ifdef KERNEL_PRIVATE
293
294 extern boolean_t lck_mtx_try_lock(
295 lck_mtx_t *lck);
296
297 #endif /* KERNEL_PRIVATE */
298
299 extern void lck_mtx_assert(
300 lck_mtx_t *lck,
301 unsigned int type);
302
303 __END_DECLS
304
305 #define LCK_MTX_ASSERT_OWNED 0x01
306 #define LCK_MTX_ASSERT_NOTOWNED 0x02
307
308 #ifdef MACH_KERNEL_PRIVATE
309 extern void lck_mtx_lock_wait(
310 lck_mtx_t *lck,
311 thread_t holder);
312
313 extern int lck_mtx_lock_acquire(
314 lck_mtx_t *lck);
315
316 extern void lck_mtx_unlock_wakeup(
317 lck_mtx_t *lck,
318 thread_t holder);
319
320 extern boolean_t lck_mtx_ilk_unlock(
321 lck_mtx_t *lck);
322 #endif
323
324 #define decl_lck_rw_data(class,name) class lck_rw_t name;
325
326 typedef unsigned int lck_rw_type_t;
327
328 #define LCK_RW_TYPE_SHARED 0x01
329 #define LCK_RW_TYPE_EXCLUSIVE 0x02
330
331 __BEGIN_DECLS
332
333 extern lck_rw_t *lck_rw_alloc_init(
334 lck_grp_t *grp,
335 lck_attr_t *attr);
336
337 extern void lck_rw_init(
338 lck_rw_t *lck,
339 lck_grp_t *grp,
340 lck_attr_t *attr);
341
342 extern void lck_rw_lock(
343 lck_rw_t *lck,
344 lck_rw_type_t lck_rw_type);
345
346 extern void lck_rw_unlock(
347 lck_rw_t *lck,
348 lck_rw_type_t lck_rw_type);
349
350 extern void lck_rw_lock_shared(
351 lck_rw_t *lck);
352
353 extern void lck_rw_unlock_shared(
354 lck_rw_t *lck);
355
356 extern void lck_rw_lock_exclusive(
357 lck_rw_t *lck);
358
359 extern void lck_rw_unlock_exclusive(
360 lck_rw_t *lck);
361
362 #ifdef KERNEL_PRIVATE
363
364 extern lck_rw_type_t lck_rw_done(
365 lck_rw_t *lck);
366 #endif
367
368 extern void lck_rw_destroy(
369 lck_rw_t *lck,
370 lck_grp_t *grp);
371
372 extern void lck_rw_free(
373 lck_rw_t *lck,
374 lck_grp_t *grp);
375
376 extern wait_result_t lck_rw_sleep(
377 lck_rw_t *lck,
378 lck_sleep_action_t lck_sleep_action,
379 event_t event,
380 wait_interrupt_t interruptible);
381
382 extern wait_result_t lck_rw_sleep_deadline(
383 lck_rw_t *lck,
384 lck_sleep_action_t lck_sleep_action,
385 event_t event,
386 wait_interrupt_t interruptible,
387 uint64_t deadline);
388
389 #ifdef KERNEL_PRIVATE
390
391 extern boolean_t lck_rw_lock_shared_to_exclusive(
392 lck_rw_t *lck);
393
394 extern void lck_rw_lock_exclusive_to_shared(
395 lck_rw_t *lck);
396
397 extern boolean_t lck_rw_try_lock(
398 lck_rw_t *lck,
399 lck_rw_type_t lck_rw_type);
400
401 extern boolean_t lck_rw_try_lock_shared(
402 lck_rw_t *lck);
403
404 extern boolean_t lck_rw_try_lock_exclusive(
405 lck_rw_t *lck);
406 #endif
407
408 __END_DECLS
409
410 #endif /* _KERN_LOCKS_H_ */