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 */
62 #define LCK_SLEEP_SPIN_ALWAYS 0x20 /* Reclaim the lock in spin-always mode (mutex only) */
64 #define LCK_SLEEP_MASK 0x3f /* Valid actions */
66 #ifdef MACH_KERNEL_PRIVATE
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
;
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
;
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
;
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
;
106 #define LCK_GRP_MAX_NAME 64
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
;
119 #define lck_grp_miss lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_miss_cnt
120 #define lck_grp_held lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_held_cnt
121 #define lck_grp_util lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_util_cnt
122 #define lck_grp_wait lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_wait_cnt
123 #define lck_grp_direct_wait lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_held_cnt
125 #define LCK_GRP_NULL (lck_grp_t *)0
128 typedef struct __lck_grp__ lck_grp_t
;
131 #ifdef MACH_KERNEL_PRIVATE
132 typedef struct _lck_grp_attr_
{
133 uint32_t grp_attr_val
;
136 extern lck_grp_attr_t LockDefaultGroupAttr
;
138 #define LCK_GRP_ATTR_STAT 0x1
141 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
144 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
148 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
151 extern void lck_grp_attr_setdefault(
152 lck_grp_attr_t
*attr
);
154 extern void lck_grp_attr_setstat(
155 lck_grp_attr_t
*attr
);
157 extern void lck_grp_attr_free(
158 lck_grp_attr_t
*attr
);
160 extern lck_grp_t
*lck_grp_alloc_init(
161 const char* grp_name
,
162 lck_grp_attr_t
*attr
);
166 #ifdef MACH_KERNEL_PRIVATE
167 extern void lck_grp_init(
169 const char* grp_name
,
170 lck_grp_attr_t
*attr
);
172 extern void lck_grp_reference(
175 extern void lck_grp_deallocate(
178 extern void lck_grp_lckcnt_incr(
180 lck_type_t lck_type
);
182 extern void lck_grp_lckcnt_decr(
184 lck_type_t lck_type
);
189 extern void lck_grp_free(
194 #ifdef MACH_KERNEL_PRIVATE
195 typedef struct _lck_attr_
{
196 unsigned int lck_attr_val
;
199 extern lck_attr_t LockDefaultLckAttr
;
201 #define LCK_ATTR_NONE 0
203 #define LCK_ATTR_DEBUG 0x00000001
204 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
207 typedef struct __lck_attr__ lck_attr_t
;
210 #define LCK_ATTR_NULL (lck_attr_t *)0
214 extern lck_attr_t
*lck_attr_alloc_init(
217 extern void lck_attr_setdefault(
220 extern void lck_attr_setdebug(
223 extern void lck_attr_cleardebug(
226 #ifdef XNU_KERNEL_PRIVATE
227 extern void lck_attr_rw_shared_priority(
231 extern void lck_attr_free(
234 #define decl_lck_spin_data(class,name) class lck_spin_t name;
236 extern lck_spin_t
*lck_spin_alloc_init(
240 extern void lck_spin_init(
245 extern void lck_spin_lock(
248 extern void lck_spin_unlock(
251 extern void lck_spin_destroy(
255 extern void lck_spin_free(
259 extern wait_result_t
lck_spin_sleep(
261 lck_sleep_action_t lck_sleep_action
,
263 wait_interrupt_t interruptible
);
265 extern wait_result_t
lck_spin_sleep_deadline(
267 lck_sleep_action_t lck_sleep_action
,
269 wait_interrupt_t interruptible
,
272 #ifdef KERNEL_PRIVATE
274 extern void lck_spin_lock_nopreempt( lck_spin_t
*lck
);
276 extern void lck_spin_unlock_nopreempt( lck_spin_t
*lck
);
278 extern boolean_t
lck_spin_try_lock( lck_spin_t
*lck
);
280 extern boolean_t
lck_spin_try_lock_nopreempt( lck_spin_t
*lck
);
282 /* NOT SAFE: To be used only by kernel debugger to avoid deadlock. */
283 extern boolean_t
kdp_lck_spin_is_acquired( lck_spin_t
*lck
);
285 struct _lck_mtx_ext_
;
286 extern void lck_mtx_init_ext(lck_mtx_t
*lck
, struct _lck_mtx_ext_
*lck_ext
,
287 lck_grp_t
*grp
, lck_attr_t
*attr
);
292 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
294 extern lck_mtx_t
*lck_mtx_alloc_init(
298 extern void lck_mtx_init(
302 extern void lck_mtx_lock(
305 extern void lck_mtx_unlock(
308 extern void lck_mtx_destroy(
312 extern void lck_mtx_free(
316 extern wait_result_t
lck_mtx_sleep(
318 lck_sleep_action_t lck_sleep_action
,
320 wait_interrupt_t interruptible
);
322 extern wait_result_t
lck_mtx_sleep_deadline(
324 lck_sleep_action_t lck_sleep_action
,
326 wait_interrupt_t interruptible
,
328 #if DEVELOPMENT || DEBUG
329 extern void erase_all_test_mtx_stats(void);
330 extern int get_test_mtx_stats_string(char* buffer
, int buffer_size
);
331 extern void lck_mtx_test_init(void);
332 extern void lck_mtx_test_lock(void);
333 extern void lck_mtx_test_unlock(void);
334 extern int lck_mtx_test_mtx_uncontended(int iter
, char* buffer
, int buffer_size
);
335 extern int lck_mtx_test_mtx_contended(int iter
, char* buffer
, int buffer_size
);
336 extern int lck_mtx_test_mtx_uncontended_loop_time(int iter
, char* buffer
, int buffer_size
);
337 extern int lck_mtx_test_mtx_contended_loop_time(int iter
, char* buffer
, int buffer_size
);
340 #ifdef KERNEL_PRIVATE
342 extern boolean_t
lck_mtx_try_lock(
345 extern void mutex_pause(uint32_t);
347 extern void lck_mtx_yield (
350 extern boolean_t
lck_mtx_try_lock_spin(
353 extern void lck_mtx_lock_spin(
356 extern boolean_t
kdp_lck_mtx_lock_spin_is_acquired(
359 extern void lck_mtx_convert_spin(
362 extern void lck_mtx_lock_spin_always(
365 extern boolean_t
lck_mtx_try_lock_spin_always(
368 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
370 extern void lck_spin_assert(
374 extern boolean_t
kdp_lck_rw_lock_is_acquired_exclusive(
377 #endif /* KERNEL_PRIVATE */
379 extern void lck_mtx_assert(
384 #define LCK_MTX_ASSERT(lck,type) lck_mtx_assert((lck),(type))
385 #define LCK_SPIN_ASSERT(lck,type) lck_spin_assert((lck),(type))
386 #define LCK_RW_ASSERT(lck,type) lck_rw_assert((lck),(type))
387 #else /* MACH_ASSERT */
388 #define LCK_MTX_ASSERT(lck,type)
389 #define LCK_SPIN_ASSERT(lck,type)
390 #define LCK_RW_ASSERT(lck,type)
391 #endif /* MACH_ASSERT */
394 #define LCK_MTX_ASSERT_DEBUG(lck,type) lck_mtx_assert((lck),(type))
395 #define LCK_SPIN_ASSERT_DEBUG(lck,type) lck_spin_assert((lck),(type))
396 #define LCK_RW_ASSERT_DEBUG(lck,type) lck_rw_assert((lck),(type))
398 #define LCK_MTX_ASSERT_DEBUG(lck,type)
399 #define LCK_SPIN_ASSERT_DEBUG(lck,type)
400 #define LCK_RW_ASSERT_DEBUG(lck,type)
405 #define LCK_ASSERT_OWNED 1
406 #define LCK_ASSERT_NOTOWNED 2
408 #define LCK_MTX_ASSERT_OWNED LCK_ASSERT_OWNED
409 #define LCK_MTX_ASSERT_NOTOWNED LCK_ASSERT_NOTOWNED
411 #ifdef MACH_KERNEL_PRIVATE
412 extern void lck_mtx_lock_wait(
416 extern int lck_mtx_lock_acquire(
419 extern void lck_mtx_unlock_wakeup(
423 extern boolean_t
lck_mtx_ilk_unlock(
426 extern boolean_t
lck_mtx_ilk_try_lock(
429 extern void lck_mtx_wakeup_adjust_pri(thread_t thread
, integer_t priority
);
433 #define decl_lck_rw_data(class,name) class lck_rw_t name;
435 typedef unsigned int lck_rw_type_t
;
437 #define LCK_RW_TYPE_SHARED 0x01
438 #define LCK_RW_TYPE_EXCLUSIVE 0x02
440 #ifdef XNU_KERNEL_PRIVATE
441 #define LCK_RW_ASSERT_SHARED 0x01
442 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
443 #define LCK_RW_ASSERT_HELD 0x03
444 #define LCK_RW_ASSERT_NOTHELD 0x04
449 extern lck_rw_t
*lck_rw_alloc_init(
453 extern void lck_rw_init(
458 extern void lck_rw_lock(
460 lck_rw_type_t lck_rw_type
);
462 extern void lck_rw_unlock(
464 lck_rw_type_t lck_rw_type
);
466 extern void lck_rw_lock_shared(
469 extern void lck_rw_unlock_shared(
472 extern boolean_t
lck_rw_lock_yield_shared(
474 boolean_t force_yield
);
476 extern void lck_rw_lock_exclusive(
479 extern void lck_rw_unlock_exclusive(
482 #ifdef XNU_KERNEL_PRIVATE
485 * read-write locks do not have a concept of ownership, so lck_rw_assert()
486 * merely asserts that someone is holding the lock, not necessarily the caller.
488 extern void lck_rw_assert(
492 extern void lck_rw_clear_promotion(thread_t thread
, uintptr_t trace_obj
);
493 extern void lck_rw_set_promotion_locked(thread_t thread
);
495 uintptr_t unslide_for_kdebug(void* object
);
498 #ifdef KERNEL_PRIVATE
500 extern lck_rw_type_t
lck_rw_done(
504 extern void lck_rw_destroy(
508 extern void lck_rw_free(
512 extern wait_result_t
lck_rw_sleep(
514 lck_sleep_action_t lck_sleep_action
,
516 wait_interrupt_t interruptible
);
518 extern wait_result_t
lck_rw_sleep_deadline(
520 lck_sleep_action_t lck_sleep_action
,
522 wait_interrupt_t interruptible
,
525 extern boolean_t
lck_rw_lock_shared_to_exclusive(
528 extern void lck_rw_lock_exclusive_to_shared(
531 extern boolean_t
lck_rw_try_lock(
533 lck_rw_type_t lck_rw_type
);
535 #ifdef KERNEL_PRIVATE
537 extern boolean_t
lck_rw_try_lock_shared(
540 extern boolean_t
lck_rw_try_lock_exclusive(
546 #endif /* _KERN_LOCKS_H_ */