2 * Copyright (c) 2003-2012 Apple 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 _KERN_LOCKS_H_
30 #define _KERN_LOCKS_H_
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>
39 #ifdef MACH_KERNEL_PRIVATE
40 #include <kern/queue.h>
42 extern void lck_mod_init(
45 typedef unsigned int lck_type_t
;
47 #define LCK_TYPE_SPIN 1
48 #define LCK_TYPE_MTX 2
53 typedef unsigned int lck_sleep_action_t
;
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 */
63 #define LCK_SLEEP_MASK 0x1f /* Valid actions */
65 #ifdef MACH_KERNEL_PRIVATE
68 uint64_t lck_grp_spin_util_cnt
;
69 uint64_t lck_grp_spin_held_cnt
;
70 uint64_t lck_grp_spin_miss_cnt
;
71 uint64_t lck_grp_spin_held_max
;
72 uint64_t lck_grp_spin_held_cum
;
73 } lck_grp_spin_stat_t
;
76 uint64_t lck_grp_mtx_util_cnt
;
77 /* On x86, this is used as the "direct wait" count */
78 uint64_t lck_grp_mtx_held_cnt
;
79 uint64_t lck_grp_mtx_miss_cnt
;
80 uint64_t lck_grp_mtx_wait_cnt
;
81 /* Rest currently unused */
82 uint64_t lck_grp_mtx_held_max
;
83 uint64_t lck_grp_mtx_held_cum
;
84 uint64_t lck_grp_mtx_wait_max
;
85 uint64_t lck_grp_mtx_wait_cum
;
89 uint64_t lck_grp_rw_util_cnt
;
90 uint64_t lck_grp_rw_held_cnt
;
91 uint64_t lck_grp_rw_miss_cnt
;
92 uint64_t lck_grp_rw_wait_cnt
;
93 uint64_t lck_grp_rw_held_max
;
94 uint64_t lck_grp_rw_held_cum
;
95 uint64_t lck_grp_rw_wait_max
;
96 uint64_t lck_grp_rw_wait_cum
;
99 typedef struct _lck_grp_stat_
{
100 lck_grp_spin_stat_t lck_grp_spin_stat
;
101 lck_grp_mtx_stat_t lck_grp_mtx_stat
;
102 lck_grp_rw_stat_t lck_grp_rw_stat
;
105 #define LCK_GRP_MAX_NAME 64
107 typedef struct _lck_grp_
{
108 queue_chain_t lck_grp_link
;
109 uint32_t lck_grp_refcnt
;
110 uint32_t lck_grp_spincnt
;
111 uint32_t lck_grp_mtxcnt
;
112 uint32_t lck_grp_rwcnt
;
113 uint32_t lck_grp_attr
;
114 char lck_grp_name
[LCK_GRP_MAX_NAME
];
115 lck_grp_stat_t lck_grp_stat
;
118 #define LCK_GRP_NULL (lck_grp_t *)0
121 typedef struct __lck_grp__ lck_grp_t
;
124 #ifdef MACH_KERNEL_PRIVATE
125 typedef struct _lck_grp_attr_
{
126 uint32_t grp_attr_val
;
129 extern lck_grp_attr_t LockDefaultGroupAttr
;
131 #define LCK_GRP_ATTR_STAT 0x1
134 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
137 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
141 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
144 extern void lck_grp_attr_setdefault(
145 lck_grp_attr_t
*attr
);
147 extern void lck_grp_attr_setstat(
148 lck_grp_attr_t
*attr
);
150 extern void lck_grp_attr_free(
151 lck_grp_attr_t
*attr
);
153 extern lck_grp_t
*lck_grp_alloc_init(
154 const char* grp_name
,
155 lck_grp_attr_t
*attr
);
159 #ifdef MACH_KERNEL_PRIVATE
160 extern void lck_grp_init(
162 const char* grp_name
,
163 lck_grp_attr_t
*attr
);
165 extern void lck_grp_reference(
168 extern void lck_grp_deallocate(
171 extern void lck_grp_lckcnt_incr(
173 lck_type_t lck_type
);
175 extern void lck_grp_lckcnt_decr(
177 lck_type_t lck_type
);
182 extern void lck_grp_free(
187 #ifdef MACH_KERNEL_PRIVATE
188 typedef struct _lck_attr_
{
189 unsigned int lck_attr_val
;
192 extern lck_attr_t LockDefaultLckAttr
;
194 #define LCK_ATTR_NONE 0
196 #define LCK_ATTR_DEBUG 0x00000001
197 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
200 typedef struct __lck_attr__ lck_attr_t
;
203 #define LCK_ATTR_NULL (lck_attr_t *)0
207 extern lck_attr_t
*lck_attr_alloc_init(
210 extern void lck_attr_setdefault(
213 extern void lck_attr_setdebug(
216 extern void lck_attr_cleardebug(
219 #ifdef XNU_KERNEL_PRIVATE
220 extern void lck_attr_rw_shared_priority(
224 extern void lck_attr_free(
227 #define decl_lck_spin_data(class,name) class lck_spin_t name;
229 extern lck_spin_t
*lck_spin_alloc_init(
233 extern void lck_spin_init(
238 extern void lck_spin_lock(
241 extern void lck_spin_unlock(
244 extern void lck_spin_destroy(
248 extern void lck_spin_free(
252 extern wait_result_t
lck_spin_sleep(
254 lck_sleep_action_t lck_sleep_action
,
256 wait_interrupt_t interruptible
);
258 extern wait_result_t
lck_spin_sleep_deadline(
260 lck_sleep_action_t lck_sleep_action
,
262 wait_interrupt_t interruptible
,
265 #ifdef KERNEL_PRIVATE
267 extern boolean_t
lck_spin_try_lock( lck_spin_t
*lck
);
269 /* NOT SAFE: To be used only by kernel debugger to avoid deadlock. */
270 extern boolean_t
kdp_lck_spin_is_acquired( lck_spin_t
*lck
);
272 struct _lck_mtx_ext_
;
273 extern void lck_mtx_init_ext(lck_mtx_t
*lck
, struct _lck_mtx_ext_
*lck_ext
,
274 lck_grp_t
*grp
, lck_attr_t
*attr
);
279 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
281 extern lck_mtx_t
*lck_mtx_alloc_init(
285 extern void lck_mtx_init(
289 extern void lck_mtx_lock(
292 extern void lck_mtx_unlock(
295 extern void lck_mtx_destroy(
299 extern void lck_mtx_free(
303 extern wait_result_t
lck_mtx_sleep(
305 lck_sleep_action_t lck_sleep_action
,
307 wait_interrupt_t interruptible
);
309 extern wait_result_t
lck_mtx_sleep_deadline(
311 lck_sleep_action_t lck_sleep_action
,
313 wait_interrupt_t interruptible
,
316 #ifdef KERNEL_PRIVATE
318 extern boolean_t
lck_mtx_try_lock(
321 extern void mutex_pause(uint32_t);
323 extern void lck_mtx_yield (
326 #if defined(__i386__) || defined(__x86_64__)
327 extern boolean_t
lck_mtx_try_lock_spin(
330 extern boolean_t
lck_mtx_try_lock_spin_always(
333 extern void lck_mtx_lock_spin_always(
336 extern void lck_mtx_lock_spin(
339 extern void lck_mtx_convert_spin(
342 extern boolean_t
kdp_lck_mtx_lock_spin_is_acquired(
344 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
347 #define lck_mtx_try_lock_spin(l) lck_mtx_try_lock(l)
348 #define lck_mtx_lock_spin(l) lck_mtx_lock(l)
349 #define lck_mtx_try_lock_spin_always(l) lck_spin_try_lock(l)
350 #define lck_mtx_lock_spin_always(l) lck_spin_lock(l)
351 #define kdp_lck_mtx_lock_spin_is_acquired(l) kdp_lck_spin_is_acquired(l)
352 #define lck_mtx_unlock_always(l) lck_spin_unlock(l)
353 #define lck_mtx_convert_spin(l) do {} while (0)
356 extern boolean_t
kdp_lck_rw_lock_is_acquired_exclusive(
359 #endif /* KERNEL_PRIVATE */
361 extern void lck_mtx_assert(
367 #define LCK_MTX_ASSERT_OWNED 0x01
368 #define LCK_MTX_ASSERT_NOTOWNED 0x02
370 #ifdef MACH_KERNEL_PRIVATE
371 extern void lck_mtx_lock_wait(
375 extern int lck_mtx_lock_acquire(
378 extern void lck_mtx_unlock_wakeup(
381 extern void lck_mtx_unlockspin_wakeup(
384 extern boolean_t
lck_mtx_ilk_unlock(
387 extern boolean_t
lck_mtx_ilk_try_lock(
392 #define decl_lck_rw_data(class,name) class lck_rw_t name;
394 typedef unsigned int lck_rw_type_t
;
396 #define LCK_RW_TYPE_SHARED 0x01
397 #define LCK_RW_TYPE_EXCLUSIVE 0x02
399 #ifdef XNU_KERNEL_PRIVATE
400 #define LCK_RW_ASSERT_SHARED 0x01
401 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
402 #define LCK_RW_ASSERT_HELD 0x03
403 #define LCK_RW_ASSERT_NOTHELD 0x04
408 extern lck_rw_t
*lck_rw_alloc_init(
412 extern void lck_rw_init(
417 extern void lck_rw_lock(
419 lck_rw_type_t lck_rw_type
);
421 extern void lck_rw_unlock(
423 lck_rw_type_t lck_rw_type
);
425 extern void lck_rw_lock_shared(
428 extern void lck_rw_unlock_shared(
431 extern void lck_rw_lock_exclusive(
434 extern void lck_rw_unlock_exclusive(
437 #ifdef XNU_KERNEL_PRIVATE
440 * read-write locks do not have a concept of ownership, so lck_rw_assert()
441 * merely asserts that someone is holding the lock, not necessarily the caller.
443 extern void lck_rw_assert(
447 extern void lck_rw_clear_promotion(
451 #ifdef KERNEL_PRIVATE
453 extern lck_rw_type_t
lck_rw_done(
457 extern void lck_rw_destroy(
461 extern void lck_rw_free(
465 extern wait_result_t
lck_rw_sleep(
467 lck_sleep_action_t lck_sleep_action
,
469 wait_interrupt_t interruptible
);
471 extern wait_result_t
lck_rw_sleep_deadline(
473 lck_sleep_action_t lck_sleep_action
,
475 wait_interrupt_t interruptible
,
478 extern boolean_t
lck_rw_lock_shared_to_exclusive(
481 extern void lck_rw_lock_exclusive_to_shared(
484 extern boolean_t
lck_rw_try_lock(
486 lck_rw_type_t lck_rw_type
);
488 #ifdef KERNEL_PRIVATE
490 extern boolean_t
lck_rw_try_lock_shared(
493 extern boolean_t
lck_rw_try_lock_exclusive(
499 #endif /* _KERN_LOCKS_H_ */