2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #ifndef _KERN_LOCKS_H_
32 #define _KERN_LOCKS_H_
34 #include <sys/cdefs.h>
35 #include <sys/appleapiopts.h>
36 #include <mach/boolean.h>
37 #include <mach/mach_types.h>
38 #include <kern/kern_types.h>
39 #include <machine/locks.h>
41 #ifdef MACH_KERNEL_PRIVATE
42 #include <kern/queue.h>
44 extern void lck_mod_init(
47 typedef unsigned int lck_type_t
;
49 #define LCK_TYPE_SPIN 1
50 #define LCK_TYPE_MTX 2
55 typedef unsigned int lck_sleep_action_t
;
57 #define LCK_SLEEP_DEFAULT 0x00 /* Release the lock while waiting for the event, then reclaim */
58 /* RW locks are returned in the same mode */
59 #define LCK_SLEEP_UNLOCK 0x01 /* Release the lock and return unheld */
60 #define LCK_SLEEP_SHARED 0x02 /* Reclaim the lock in shared mode (RW only) */
61 #define LCK_SLEEP_EXCLUSIVE 0x04 /* Reclaim the lock in exclusive mode (RW only) */
63 #define LCK_SLEEP_MASK 0x07 /* 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 uint64_t lck_grp_mtx_held_cnt
;
78 uint64_t lck_grp_mtx_miss_cnt
;
79 uint64_t lck_grp_mtx_wait_cnt
;
80 uint64_t lck_grp_mtx_held_max
;
81 uint64_t lck_grp_mtx_held_cum
;
82 uint64_t lck_grp_mtx_wait_max
;
83 uint64_t lck_grp_mtx_wait_cum
;
87 uint64_t lck_grp_rw_util_cnt
;
88 uint64_t lck_grp_rw_held_cnt
;
89 uint64_t lck_grp_rw_miss_cnt
;
90 uint64_t lck_grp_rw_wait_cnt
;
91 uint64_t lck_grp_rw_held_max
;
92 uint64_t lck_grp_rw_held_cum
;
93 uint64_t lck_grp_rw_wait_max
;
94 uint64_t lck_grp_rw_wait_cum
;
97 typedef struct _lck_grp_stat_
{
98 lck_grp_spin_stat_t lck_grp_spin_stat
;
99 lck_grp_mtx_stat_t lck_grp_mtx_stat
;
100 lck_grp_rw_stat_t lck_grp_rw_stat
;
103 #define LCK_GRP_MAX_NAME 64
105 typedef struct _lck_grp_
{
106 queue_chain_t lck_grp_link
;
107 unsigned int lck_grp_refcnt
;
108 unsigned int lck_grp_spincnt
;
109 unsigned int lck_grp_mtxcnt
;
110 unsigned int lck_grp_rwcnt
;
111 unsigned int lck_grp_attr
;
112 char lck_grp_name
[LCK_GRP_MAX_NAME
];
113 lck_grp_stat_t lck_grp_stat
;
116 #define LCK_GRP_NULL (lck_grp_t *)0
119 typedef struct __lck_grp__ lck_grp_t
;
122 #ifdef MACH_KERNEL_PRIVATE
123 typedef struct _lck_grp_attr_
{
124 unsigned int grp_attr_val
;
127 extern lck_grp_attr_t LockDefaultGroupAttr
;
129 #define LCK_GRP_ATTR_STAT 0x1
132 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
135 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
139 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
142 extern void lck_grp_attr_setdefault(
143 lck_grp_attr_t
*attr
);
145 extern void lck_grp_attr_setstat(
146 lck_grp_attr_t
*attr
);
148 extern void lck_grp_attr_free(
149 lck_grp_attr_t
*attr
);
151 extern lck_grp_t
*lck_grp_alloc_init(
152 const char* grp_name
,
153 lck_grp_attr_t
*attr
);
157 #ifdef MACH_KERNEL_PRIVATE
158 extern void lck_grp_init(
160 const char* grp_name
,
161 lck_grp_attr_t
*attr
);
163 extern void lck_grp_reference(
166 extern void lck_grp_deallocate(
169 extern void lck_grp_lckcnt_incr(
171 lck_type_t lck_type
);
173 extern void lck_grp_lckcnt_decr(
175 lck_type_t lck_type
);
180 extern void lck_grp_free(
185 #ifdef MACH_KERNEL_PRIVATE
186 typedef struct _lck_attr_
{
187 unsigned int lck_attr_val
;
190 extern lck_attr_t LockDefaultLckAttr
;
192 #define LCK_ATTR_NONE 0
194 #define LCK_ATTR_DEBUG 0x00000001
195 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
198 typedef struct __lck_attr__ lck_attr_t
;
201 #define LCK_ATTR_NULL (lck_attr_t *)0
205 extern lck_attr_t
*lck_attr_alloc_init(
208 extern void lck_attr_setdefault(
211 extern void lck_attr_setdebug(
214 #ifdef XNU_KERNEL_PRIVATE
215 extern void lck_attr_rw_shared_priority(
219 extern void lck_attr_free(
222 #define decl_lck_spin_data(class,name) class lck_spin_t name;
224 extern lck_spin_t
*lck_spin_alloc_init(
228 extern void lck_spin_init(
233 extern void lck_spin_lock(
236 extern void lck_spin_unlock(
239 extern void lck_spin_destroy(
243 extern void lck_spin_free(
247 extern wait_result_t
lck_spin_sleep(
249 lck_sleep_action_t lck_sleep_action
,
251 wait_interrupt_t interruptible
);
253 extern wait_result_t
lck_spin_sleep_deadline(
255 lck_sleep_action_t lck_sleep_action
,
257 wait_interrupt_t interruptible
,
260 #ifdef KERNEL_PRIVATE
262 extern boolean_t
lck_spin_try_lock(
268 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
270 extern lck_mtx_t
*lck_mtx_alloc_init(
274 extern void lck_mtx_init(
279 extern void lck_mtx_lock(
282 extern void lck_mtx_unlock(
285 extern void lck_mtx_destroy(
289 extern void lck_mtx_free(
293 extern wait_result_t
lck_mtx_sleep(
295 lck_sleep_action_t lck_sleep_action
,
297 wait_interrupt_t interruptible
);
299 extern wait_result_t
lck_mtx_sleep_deadline(
301 lck_sleep_action_t lck_sleep_action
,
303 wait_interrupt_t interruptible
,
306 #ifdef KERNEL_PRIVATE
308 extern boolean_t
lck_mtx_try_lock(
311 #endif /* KERNEL_PRIVATE */
313 extern void lck_mtx_assert(
319 #define LCK_MTX_ASSERT_OWNED 0x01
320 #define LCK_MTX_ASSERT_NOTOWNED 0x02
322 #ifdef MACH_KERNEL_PRIVATE
323 extern void lck_mtx_lock_wait(
327 extern int lck_mtx_lock_acquire(
330 extern void lck_mtx_unlock_wakeup(
334 extern boolean_t
lck_mtx_ilk_unlock(
338 #define decl_lck_rw_data(class,name) class lck_rw_t name;
340 typedef unsigned int lck_rw_type_t
;
342 #define LCK_RW_TYPE_SHARED 0x01
343 #define LCK_RW_TYPE_EXCLUSIVE 0x02
347 extern lck_rw_t
*lck_rw_alloc_init(
351 extern void lck_rw_init(
356 extern void lck_rw_lock(
358 lck_rw_type_t lck_rw_type
);
360 extern void lck_rw_unlock(
362 lck_rw_type_t lck_rw_type
);
364 extern void lck_rw_lock_shared(
367 extern void lck_rw_unlock_shared(
370 extern void lck_rw_lock_exclusive(
373 extern void lck_rw_unlock_exclusive(
376 #ifdef KERNEL_PRIVATE
378 extern lck_rw_type_t
lck_rw_done(
382 extern void lck_rw_destroy(
386 extern void lck_rw_free(
390 extern wait_result_t
lck_rw_sleep(
392 lck_sleep_action_t lck_sleep_action
,
394 wait_interrupt_t interruptible
);
396 extern wait_result_t
lck_rw_sleep_deadline(
398 lck_sleep_action_t lck_sleep_action
,
400 wait_interrupt_t interruptible
,
403 #ifdef KERNEL_PRIVATE
405 extern boolean_t
lck_rw_lock_shared_to_exclusive(
408 extern void lck_rw_lock_exclusive_to_shared(
411 extern boolean_t
lck_rw_try_lock(
413 lck_rw_type_t lck_rw_type
);
415 extern boolean_t
lck_rw_try_lock_shared(
418 extern boolean_t
lck_rw_try_lock_exclusive(
424 #endif /* _KERN_LOCKS_H_ */