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 extern boolean_t
lck_mtx_try_lock_spin(
329 extern void lck_mtx_lock_spin(
332 extern boolean_t
kdp_lck_mtx_lock_spin_is_acquired(
335 extern void lck_mtx_convert_spin(
338 extern void lck_mtx_lock_spin_always(
341 extern boolean_t
lck_mtx_try_lock_spin_always(
344 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
346 extern void lck_spin_assert(
350 extern boolean_t
kdp_lck_rw_lock_is_acquired_exclusive(
353 #endif /* KERNEL_PRIVATE */
355 extern void lck_mtx_assert(
360 #define LCK_MTX_ASSERT(lck,type) lck_mtx_assert((lck),(type))
361 #else /* MACH_ASSERT */
362 #define LCK_MTX_ASSERT(lck,type)
363 #endif /* MACH_ASSERT */
366 #define LCK_MTX_ASSERT_DEBUG(lck,type) lck_mtx_assert((lck),(type))
368 #define LCK_MTX_ASSERT_DEBUG(lck,type)
373 #define LCK_ASSERT_OWNED 1
374 #define LCK_ASSERT_NOTOWNED 2
376 #define LCK_MTX_ASSERT_OWNED LCK_ASSERT_OWNED
377 #define LCK_MTX_ASSERT_NOTOWNED LCK_ASSERT_NOTOWNED
379 #ifdef MACH_KERNEL_PRIVATE
380 extern void lck_mtx_lock_wait(
384 extern int lck_mtx_lock_acquire(
387 extern void lck_mtx_unlock_wakeup(
390 extern void lck_mtx_unlockspin_wakeup(
393 extern boolean_t
lck_mtx_ilk_unlock(
396 extern boolean_t
lck_mtx_ilk_try_lock(
401 #define decl_lck_rw_data(class,name) class lck_rw_t name;
403 typedef unsigned int lck_rw_type_t
;
405 #define LCK_RW_TYPE_SHARED 0x01
406 #define LCK_RW_TYPE_EXCLUSIVE 0x02
408 #ifdef XNU_KERNEL_PRIVATE
409 #define LCK_RW_ASSERT_SHARED 0x01
410 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
411 #define LCK_RW_ASSERT_HELD 0x03
412 #define LCK_RW_ASSERT_NOTHELD 0x04
417 extern lck_rw_t
*lck_rw_alloc_init(
421 extern void lck_rw_init(
426 extern void lck_rw_lock(
428 lck_rw_type_t lck_rw_type
);
430 extern void lck_rw_unlock(
432 lck_rw_type_t lck_rw_type
);
434 extern void lck_rw_lock_shared(
437 extern void lck_rw_unlock_shared(
440 extern void lck_rw_lock_exclusive(
443 extern void lck_rw_unlock_exclusive(
446 #ifdef XNU_KERNEL_PRIVATE
449 * read-write locks do not have a concept of ownership, so lck_rw_assert()
450 * merely asserts that someone is holding the lock, not necessarily the caller.
452 extern void lck_rw_assert(
456 extern void lck_rw_clear_promotion(
458 extern void lck_rw_set_promotion_locked(thread_t thread
);
461 #ifdef KERNEL_PRIVATE
463 extern lck_rw_type_t
lck_rw_done(
467 extern void lck_rw_destroy(
471 extern void lck_rw_free(
475 extern wait_result_t
lck_rw_sleep(
477 lck_sleep_action_t lck_sleep_action
,
479 wait_interrupt_t interruptible
);
481 extern wait_result_t
lck_rw_sleep_deadline(
483 lck_sleep_action_t lck_sleep_action
,
485 wait_interrupt_t interruptible
,
488 extern boolean_t
lck_rw_lock_shared_to_exclusive(
491 extern void lck_rw_lock_exclusive_to_shared(
494 extern boolean_t
lck_rw_try_lock(
496 lck_rw_type_t lck_rw_type
);
498 #ifdef KERNEL_PRIVATE
500 extern boolean_t
lck_rw_try_lock_shared(
503 extern boolean_t
lck_rw_try_lock_exclusive(
509 #endif /* _KERN_LOCKS_H_ */