]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/locks.h
99017d2cc19e04383d9acbb5207aed923f18926b
[apple/xnu.git] / osfmk / kern / locks.h
1 /*
2 * Copyright (c) 2003-2012 Apple 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 _KERN_LOCKS_H_
30 #define _KERN_LOCKS_H_
31
32 #include <sys/cdefs.h>
33 #include <sys/appleapiopts.h>
34 #include <mach/boolean.h>
35 #include <mach/mach_types.h>
36 #include <kern/kern_types.h>
37 #include <machine/locks.h>
38
39 #ifdef MACH_KERNEL_PRIVATE
40 #include <kern/queue.h>
41
42 extern void lck_mod_init(
43 void);
44
45 typedef unsigned int lck_type_t;
46
47 #define LCK_TYPE_SPIN 1
48 #define LCK_TYPE_MTX 2
49 #define LCK_TYPE_RW 3
50
51 #endif
52
53 typedef unsigned int lck_sleep_action_t;
54
55 #define LCK_SLEEP_DEFAULT 0x00 /* Release the lock while waiting for the event, then reclaim */
56 /* RW locks are returned in the same mode */
57 #define LCK_SLEEP_UNLOCK 0x01 /* Release the lock and return unheld */
58 #define LCK_SLEEP_SHARED 0x02 /* Reclaim the lock in shared mode (RW only) */
59 #define LCK_SLEEP_EXCLUSIVE 0x04 /* Reclaim the lock in exclusive mode (RW only) */
60 #define LCK_SLEEP_SPIN 0x08 /* Reclaim the lock in spin mode (mutex only) */
61 #define LCK_SLEEP_PROMOTED_PRI 0x10 /* Sleep at a promoted priority */
62 #define LCK_SLEEP_SPIN_ALWAYS 0x20 /* Reclaim the lock in spin-always mode (mutex only) */
63
64 #define LCK_SLEEP_MASK 0x3f /* Valid actions */
65
66 #ifdef MACH_KERNEL_PRIVATE
67
68 typedef struct {
69 uint64_t lck_grp_spin_util_cnt;
70 uint64_t lck_grp_spin_held_cnt;
71 uint64_t lck_grp_spin_miss_cnt;
72 uint64_t lck_grp_spin_held_max;
73 uint64_t lck_grp_spin_held_cum;
74 } lck_grp_spin_stat_t;
75
76 typedef struct {
77 uint64_t lck_grp_mtx_util_cnt;
78 /* On x86, this is used as the "direct wait" count */
79 uint64_t lck_grp_mtx_held_cnt;
80 uint64_t lck_grp_mtx_miss_cnt;
81 uint64_t lck_grp_mtx_wait_cnt;
82 /* Rest currently unused */
83 uint64_t lck_grp_mtx_held_max;
84 uint64_t lck_grp_mtx_held_cum;
85 uint64_t lck_grp_mtx_wait_max;
86 uint64_t lck_grp_mtx_wait_cum;
87 } lck_grp_mtx_stat_t;
88
89 typedef struct {
90 uint64_t lck_grp_rw_util_cnt;
91 uint64_t lck_grp_rw_held_cnt;
92 uint64_t lck_grp_rw_miss_cnt;
93 uint64_t lck_grp_rw_wait_cnt;
94 uint64_t lck_grp_rw_held_max;
95 uint64_t lck_grp_rw_held_cum;
96 uint64_t lck_grp_rw_wait_max;
97 uint64_t lck_grp_rw_wait_cum;
98 } lck_grp_rw_stat_t;
99
100 typedef struct _lck_grp_stat_ {
101 lck_grp_spin_stat_t lck_grp_spin_stat;
102 lck_grp_mtx_stat_t lck_grp_mtx_stat;
103 lck_grp_rw_stat_t lck_grp_rw_stat;
104 } lck_grp_stat_t;
105
106 #define LCK_GRP_MAX_NAME 64
107
108 typedef struct _lck_grp_ {
109 queue_chain_t lck_grp_link;
110 uint32_t lck_grp_refcnt;
111 uint32_t lck_grp_spincnt;
112 uint32_t lck_grp_mtxcnt;
113 uint32_t lck_grp_rwcnt;
114 uint32_t lck_grp_attr;
115 char lck_grp_name[LCK_GRP_MAX_NAME];
116 lck_grp_stat_t lck_grp_stat;
117 } lck_grp_t;
118
119 #define LCK_GRP_NULL (lck_grp_t *)0
120
121 #else
122 typedef struct __lck_grp__ lck_grp_t;
123 #endif
124
125 #ifdef MACH_KERNEL_PRIVATE
126 typedef struct _lck_grp_attr_ {
127 uint32_t grp_attr_val;
128 } lck_grp_attr_t;
129
130 extern lck_grp_attr_t LockDefaultGroupAttr;
131
132 #define LCK_GRP_ATTR_STAT 0x1
133
134 #else
135 typedef struct __lck_grp_attr__ lck_grp_attr_t;
136 #endif
137
138 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
139
140 __BEGIN_DECLS
141
142 extern lck_grp_attr_t *lck_grp_attr_alloc_init(
143 void);
144
145 extern void lck_grp_attr_setdefault(
146 lck_grp_attr_t *attr);
147
148 extern void lck_grp_attr_setstat(
149 lck_grp_attr_t *attr);
150
151 extern void lck_grp_attr_free(
152 lck_grp_attr_t *attr);
153
154 extern lck_grp_t *lck_grp_alloc_init(
155 const char* grp_name,
156 lck_grp_attr_t *attr);
157
158 __END_DECLS
159
160 #ifdef MACH_KERNEL_PRIVATE
161 extern void lck_grp_init(
162 lck_grp_t *grp,
163 const char* grp_name,
164 lck_grp_attr_t *attr);
165
166 extern void lck_grp_reference(
167 lck_grp_t *grp);
168
169 extern void lck_grp_deallocate(
170 lck_grp_t *grp);
171
172 extern void lck_grp_lckcnt_incr(
173 lck_grp_t *grp,
174 lck_type_t lck_type);
175
176 extern void lck_grp_lckcnt_decr(
177 lck_grp_t *grp,
178 lck_type_t lck_type);
179 #endif
180
181 __BEGIN_DECLS
182
183 extern void lck_grp_free(
184 lck_grp_t *grp);
185
186 __END_DECLS
187
188 #ifdef MACH_KERNEL_PRIVATE
189 typedef struct _lck_attr_ {
190 unsigned int lck_attr_val;
191 } lck_attr_t;
192
193 extern lck_attr_t LockDefaultLckAttr;
194
195 #define LCK_ATTR_NONE 0
196
197 #define LCK_ATTR_DEBUG 0x00000001
198 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
199
200 #else
201 typedef struct __lck_attr__ lck_attr_t;
202 #endif
203
204 #define LCK_ATTR_NULL (lck_attr_t *)0
205
206 __BEGIN_DECLS
207
208 extern lck_attr_t *lck_attr_alloc_init(
209 void);
210
211 extern void lck_attr_setdefault(
212 lck_attr_t *attr);
213
214 extern void lck_attr_setdebug(
215 lck_attr_t *attr);
216
217 extern void lck_attr_cleardebug(
218 lck_attr_t *attr);
219
220 #ifdef XNU_KERNEL_PRIVATE
221 extern void lck_attr_rw_shared_priority(
222 lck_attr_t *attr);
223 #endif
224
225 extern void lck_attr_free(
226 lck_attr_t *attr);
227
228 #define decl_lck_spin_data(class,name) class lck_spin_t name;
229
230 extern lck_spin_t *lck_spin_alloc_init(
231 lck_grp_t *grp,
232 lck_attr_t *attr);
233
234 extern void lck_spin_init(
235 lck_spin_t *lck,
236 lck_grp_t *grp,
237 lck_attr_t *attr);
238
239 extern void lck_spin_lock(
240 lck_spin_t *lck);
241
242 extern void lck_spin_unlock(
243 lck_spin_t *lck);
244
245 extern void lck_spin_destroy(
246 lck_spin_t *lck,
247 lck_grp_t *grp);
248
249 extern void lck_spin_free(
250 lck_spin_t *lck,
251 lck_grp_t *grp);
252
253 extern wait_result_t lck_spin_sleep(
254 lck_spin_t *lck,
255 lck_sleep_action_t lck_sleep_action,
256 event_t event,
257 wait_interrupt_t interruptible);
258
259 extern wait_result_t lck_spin_sleep_deadline(
260 lck_spin_t *lck,
261 lck_sleep_action_t lck_sleep_action,
262 event_t event,
263 wait_interrupt_t interruptible,
264 uint64_t deadline);
265
266 #ifdef KERNEL_PRIVATE
267
268 extern boolean_t lck_spin_try_lock( lck_spin_t *lck);
269
270 /* NOT SAFE: To be used only by kernel debugger to avoid deadlock. */
271 extern boolean_t kdp_lck_spin_is_acquired( lck_spin_t *lck);
272
273 struct _lck_mtx_ext_;
274 extern void lck_mtx_init_ext(lck_mtx_t *lck, struct _lck_mtx_ext_ *lck_ext,
275 lck_grp_t *grp, lck_attr_t *attr);
276
277 #endif
278
279
280 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
281
282 extern lck_mtx_t *lck_mtx_alloc_init(
283 lck_grp_t *grp,
284 lck_attr_t *attr);
285
286 extern void lck_mtx_init(
287 lck_mtx_t *lck,
288 lck_grp_t *grp,
289 lck_attr_t *attr);
290 extern void lck_mtx_lock(
291 lck_mtx_t *lck);
292
293 extern void lck_mtx_unlock(
294 lck_mtx_t *lck);
295
296 extern void lck_mtx_destroy(
297 lck_mtx_t *lck,
298 lck_grp_t *grp);
299
300 extern void lck_mtx_free(
301 lck_mtx_t *lck,
302 lck_grp_t *grp);
303
304 extern wait_result_t lck_mtx_sleep(
305 lck_mtx_t *lck,
306 lck_sleep_action_t lck_sleep_action,
307 event_t event,
308 wait_interrupt_t interruptible);
309
310 extern wait_result_t lck_mtx_sleep_deadline(
311 lck_mtx_t *lck,
312 lck_sleep_action_t lck_sleep_action,
313 event_t event,
314 wait_interrupt_t interruptible,
315 uint64_t deadline);
316
317 #ifdef KERNEL_PRIVATE
318
319 extern boolean_t lck_mtx_try_lock(
320 lck_mtx_t *lck);
321
322 extern void mutex_pause(uint32_t);
323
324 extern void lck_mtx_yield (
325 lck_mtx_t *lck);
326
327 extern boolean_t lck_mtx_try_lock_spin(
328 lck_mtx_t *lck);
329
330 extern void lck_mtx_lock_spin(
331 lck_mtx_t *lck);
332
333 extern boolean_t kdp_lck_mtx_lock_spin_is_acquired(
334 lck_mtx_t *lck);
335
336 extern void lck_mtx_convert_spin(
337 lck_mtx_t *lck);
338
339 extern void lck_mtx_lock_spin_always(
340 lck_mtx_t *lck);
341
342 extern boolean_t lck_mtx_try_lock_spin_always(
343 lck_mtx_t *lck);
344
345 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
346
347 extern void lck_spin_assert(
348 lck_spin_t *lck,
349 unsigned int type);
350
351 extern boolean_t kdp_lck_rw_lock_is_acquired_exclusive(
352 lck_rw_t *lck);
353
354 #endif /* KERNEL_PRIVATE */
355
356 extern void lck_mtx_assert(
357 lck_mtx_t *lck,
358 unsigned int type);
359
360 #if MACH_ASSERT
361 #define LCK_MTX_ASSERT(lck,type) lck_mtx_assert((lck),(type))
362 #define LCK_SPIN_ASSERT(lck,type) lck_spin_assert((lck),(type))
363 #define LCK_RW_ASSERT(lck,type) lck_rw_assert((lck),(type))
364 #else /* MACH_ASSERT */
365 #define LCK_MTX_ASSERT(lck,type)
366 #define LCK_SPIN_ASSERT(lck,type)
367 #define LCK_RW_ASSERT(lck,type)
368 #endif /* MACH_ASSERT */
369
370 #if DEBUG
371 #define LCK_MTX_ASSERT_DEBUG(lck,type) lck_mtx_assert((lck),(type))
372 #define LCK_SPIN_ASSERT_DEBUG(lck,type) lck_spin_assert((lck),(type))
373 #define LCK_RW_ASSERT_DEBUG(lck,type) lck_rw_assert((lck),(type))
374 #else /* DEBUG */
375 #define LCK_MTX_ASSERT_DEBUG(lck,type)
376 #define LCK_SPIN_ASSERT_DEBUG(lck,type)
377 #define LCK_RW_ASSERT_DEBUG(lck,type)
378 #endif /* DEBUG */
379
380 __END_DECLS
381
382 #define LCK_ASSERT_OWNED 1
383 #define LCK_ASSERT_NOTOWNED 2
384
385 #define LCK_MTX_ASSERT_OWNED LCK_ASSERT_OWNED
386 #define LCK_MTX_ASSERT_NOTOWNED LCK_ASSERT_NOTOWNED
387
388 #ifdef MACH_KERNEL_PRIVATE
389 extern void lck_mtx_lock_wait(
390 lck_mtx_t *lck,
391 thread_t holder);
392
393 extern int lck_mtx_lock_acquire(
394 lck_mtx_t *lck);
395
396 extern void lck_mtx_unlock_wakeup(
397 lck_mtx_t *lck,
398 thread_t holder);
399 extern void lck_mtx_unlockspin_wakeup(
400 lck_mtx_t *lck);
401
402 extern boolean_t lck_mtx_ilk_unlock(
403 lck_mtx_t *lck);
404
405 extern boolean_t lck_mtx_ilk_try_lock(
406 lck_mtx_t *lck);
407
408 #endif
409
410 #define decl_lck_rw_data(class,name) class lck_rw_t name;
411
412 typedef unsigned int lck_rw_type_t;
413
414 #define LCK_RW_TYPE_SHARED 0x01
415 #define LCK_RW_TYPE_EXCLUSIVE 0x02
416
417 #ifdef XNU_KERNEL_PRIVATE
418 #define LCK_RW_ASSERT_SHARED 0x01
419 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
420 #define LCK_RW_ASSERT_HELD 0x03
421 #define LCK_RW_ASSERT_NOTHELD 0x04
422 #endif
423
424 __BEGIN_DECLS
425
426 extern lck_rw_t *lck_rw_alloc_init(
427 lck_grp_t *grp,
428 lck_attr_t *attr);
429
430 extern void lck_rw_init(
431 lck_rw_t *lck,
432 lck_grp_t *grp,
433 lck_attr_t *attr);
434
435 extern void lck_rw_lock(
436 lck_rw_t *lck,
437 lck_rw_type_t lck_rw_type);
438
439 extern void lck_rw_unlock(
440 lck_rw_t *lck,
441 lck_rw_type_t lck_rw_type);
442
443 extern void lck_rw_lock_shared(
444 lck_rw_t *lck);
445
446 extern void lck_rw_unlock_shared(
447 lck_rw_t *lck);
448
449 extern boolean_t lck_rw_lock_yield_shared(
450 lck_rw_t *lck,
451 boolean_t force_yield);
452
453 extern void lck_rw_lock_exclusive(
454 lck_rw_t *lck);
455
456 extern void lck_rw_unlock_exclusive(
457 lck_rw_t *lck);
458
459 #ifdef XNU_KERNEL_PRIVATE
460 /*
461 * CAUTION
462 * read-write locks do not have a concept of ownership, so lck_rw_assert()
463 * merely asserts that someone is holding the lock, not necessarily the caller.
464 */
465 extern void lck_rw_assert(
466 lck_rw_t *lck,
467 unsigned int type);
468
469 extern void lck_rw_clear_promotion(
470 thread_t thread);
471 extern void lck_rw_set_promotion_locked(thread_t thread);
472 #endif
473
474 #ifdef KERNEL_PRIVATE
475
476 extern lck_rw_type_t lck_rw_done(
477 lck_rw_t *lck);
478 #endif
479
480 extern void lck_rw_destroy(
481 lck_rw_t *lck,
482 lck_grp_t *grp);
483
484 extern void lck_rw_free(
485 lck_rw_t *lck,
486 lck_grp_t *grp);
487
488 extern wait_result_t lck_rw_sleep(
489 lck_rw_t *lck,
490 lck_sleep_action_t lck_sleep_action,
491 event_t event,
492 wait_interrupt_t interruptible);
493
494 extern wait_result_t lck_rw_sleep_deadline(
495 lck_rw_t *lck,
496 lck_sleep_action_t lck_sleep_action,
497 event_t event,
498 wait_interrupt_t interruptible,
499 uint64_t deadline);
500
501 extern boolean_t lck_rw_lock_shared_to_exclusive(
502 lck_rw_t *lck);
503
504 extern void lck_rw_lock_exclusive_to_shared(
505 lck_rw_t *lck);
506
507 extern boolean_t lck_rw_try_lock(
508 lck_rw_t *lck,
509 lck_rw_type_t lck_rw_type);
510
511 #ifdef KERNEL_PRIVATE
512
513 extern boolean_t lck_rw_try_lock_shared(
514 lck_rw_t *lck);
515
516 extern boolean_t lck_rw_try_lock_exclusive(
517 lck_rw_t *lck);
518 #endif
519
520 __END_DECLS
521
522 #endif /* _KERN_LOCKS_H_ */