2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #ifndef _KERN_LOCKS_H_
24 #define _KERN_LOCKS_H_
26 #include <sys/cdefs.h>
27 #include <sys/appleapiopts.h>
28 #include <mach/boolean.h>
29 #include <mach/mach_types.h>
30 #include <kern/kern_types.h>
31 #include <machine/locks.h>
33 #ifdef MACH_KERNEL_PRIVATE
34 #include <kern/queue.h>
36 extern void lck_mod_init(
39 typedef unsigned int lck_type_t
;
41 #define LCK_TYPE_SPIN 1
42 #define LCK_TYPE_MTX 2
47 typedef unsigned int lck_sleep_action_t
;
49 #define LCK_SLEEP_DEFAULT 0x00 /* Release the lock while waiting for the event, then reclaim */
50 /* RW locks are returned in the same mode */
51 #define LCK_SLEEP_UNLOCK 0x01 /* Release the lock and return unheld */
52 #define LCK_SLEEP_SHARED 0x02 /* Reclaim the lock in shared mode (RW only) */
53 #define LCK_SLEEP_EXCLUSIVE 0x04 /* Reclaim the lock in exclusive mode (RW only) */
55 #define LCK_SLEEP_MASK 0x07 /* Valid actions */
57 #ifdef MACH_KERNEL_PRIVATE
60 uint64_t lck_grp_spin_util_cnt
;
61 uint64_t lck_grp_spin_held_cnt
;
62 uint64_t lck_grp_spin_miss_cnt
;
63 uint64_t lck_grp_spin_held_max
;
64 uint64_t lck_grp_spin_held_cum
;
65 } lck_grp_spin_stat_t
;
68 uint64_t lck_grp_mtx_util_cnt
;
69 uint64_t lck_grp_mtx_held_cnt
;
70 uint64_t lck_grp_mtx_miss_cnt
;
71 uint64_t lck_grp_mtx_wait_cnt
;
72 uint64_t lck_grp_mtx_held_max
;
73 uint64_t lck_grp_mtx_held_cum
;
74 uint64_t lck_grp_mtx_wait_max
;
75 uint64_t lck_grp_mtx_wait_cum
;
79 uint64_t lck_grp_rw_util_cnt
;
80 uint64_t lck_grp_rw_held_cnt
;
81 uint64_t lck_grp_rw_miss_cnt
;
82 uint64_t lck_grp_rw_wait_cnt
;
83 uint64_t lck_grp_rw_held_max
;
84 uint64_t lck_grp_rw_held_cum
;
85 uint64_t lck_grp_rw_wait_max
;
86 uint64_t lck_grp_rw_wait_cum
;
89 typedef struct _lck_grp_stat_
{
90 lck_grp_spin_stat_t lck_grp_spin_stat
;
91 lck_grp_mtx_stat_t lck_grp_mtx_stat
;
92 lck_grp_rw_stat_t lck_grp_rw_stat
;
95 #define LCK_GRP_MAX_NAME 64
97 typedef struct _lck_grp_
{
98 queue_chain_t lck_grp_link
;
99 unsigned int lck_grp_refcnt
;
100 unsigned int lck_grp_spincnt
;
101 unsigned int lck_grp_mtxcnt
;
102 unsigned int lck_grp_rwcnt
;
103 unsigned int lck_grp_attr
;
104 char lck_grp_name
[LCK_GRP_MAX_NAME
];
105 lck_grp_stat_t lck_grp_stat
;
108 #define LCK_GRP_NULL (lck_grp_t *)0
111 typedef struct __lck_grp__ lck_grp_t
;
114 #ifdef MACH_KERNEL_PRIVATE
115 typedef struct _lck_grp_attr_
{
116 unsigned int grp_attr_val
;
119 extern lck_grp_attr_t LockDefaultGroupAttr
;
121 #define LCK_GRP_ATTR_STAT 0x1
124 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
127 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
131 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
134 extern void lck_grp_attr_setdefault(
135 lck_grp_attr_t
*attr
);
137 extern void lck_grp_attr_setstat(
138 lck_grp_attr_t
*attr
);
140 extern void lck_grp_attr_free(
141 lck_grp_attr_t
*attr
);
143 extern lck_grp_t
*lck_grp_alloc_init(
144 const char* grp_name
,
145 lck_grp_attr_t
*attr
);
149 #ifdef MACH_KERNEL_PRIVATE
150 extern void lck_grp_init(
152 const char* grp_name
,
153 lck_grp_attr_t
*attr
);
155 extern void lck_grp_reference(
158 extern void lck_grp_deallocate(
161 extern void lck_grp_lckcnt_incr(
163 lck_type_t lck_type
);
165 extern void lck_grp_lckcnt_decr(
167 lck_type_t lck_type
);
172 extern void lck_grp_free(
177 #ifdef MACH_KERNEL_PRIVATE
178 typedef struct _lck_attr_
{
179 unsigned int lck_attr_val
;
182 extern lck_attr_t LockDefaultLckAttr
;
184 #define LCK_ATTR_NONE 0
186 #define LCK_ATTR_DEBUG 0x00000001
187 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
190 typedef struct __lck_attr__ lck_attr_t
;
193 #define LCK_ATTR_NULL (lck_attr_t *)0
197 extern lck_attr_t
*lck_attr_alloc_init(
200 extern void lck_attr_setdefault(
203 extern void lck_attr_setdebug(
206 #ifdef XNU_KERNEL_PRIVATE
207 extern void lck_attr_rw_shared_priority(
211 extern void lck_attr_free(
214 #define decl_lck_spin_data(class,name) class lck_spin_t name;
216 extern lck_spin_t
*lck_spin_alloc_init(
220 extern void lck_spin_init(
225 extern void lck_spin_lock(
228 extern void lck_spin_unlock(
231 extern void lck_spin_destroy(
235 extern void lck_spin_free(
239 extern wait_result_t
lck_spin_sleep(
241 lck_sleep_action_t lck_sleep_action
,
243 wait_interrupt_t interruptible
);
245 extern wait_result_t
lck_spin_sleep_deadline(
247 lck_sleep_action_t lck_sleep_action
,
249 wait_interrupt_t interruptible
,
252 #ifdef KERNEL_PRIVATE
254 extern boolean_t
lck_spin_try_lock(
260 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
262 extern lck_mtx_t
*lck_mtx_alloc_init(
266 extern void lck_mtx_init(
271 extern void lck_mtx_lock(
274 extern void lck_mtx_unlock(
277 extern void lck_mtx_destroy(
281 extern void lck_mtx_free(
285 extern wait_result_t
lck_mtx_sleep(
287 lck_sleep_action_t lck_sleep_action
,
289 wait_interrupt_t interruptible
);
291 extern wait_result_t
lck_mtx_sleep_deadline(
293 lck_sleep_action_t lck_sleep_action
,
295 wait_interrupt_t interruptible
,
298 #ifdef KERNEL_PRIVATE
300 extern boolean_t
lck_mtx_try_lock(
303 #endif /* KERNEL_PRIVATE */
305 extern void lck_mtx_assert(
311 #define LCK_MTX_ASSERT_OWNED 0x01
312 #define LCK_MTX_ASSERT_NOTOWNED 0x02
314 #ifdef MACH_KERNEL_PRIVATE
315 extern void lck_mtx_lock_wait(
319 extern int lck_mtx_lock_acquire(
322 extern void lck_mtx_unlock_wakeup(
326 extern boolean_t
lck_mtx_ilk_unlock(
330 #define decl_lck_rw_data(class,name) class lck_rw_t name;
332 typedef unsigned int lck_rw_type_t
;
334 #define LCK_RW_TYPE_SHARED 0x01
335 #define LCK_RW_TYPE_EXCLUSIVE 0x02
339 extern lck_rw_t
*lck_rw_alloc_init(
343 extern void lck_rw_init(
348 extern void lck_rw_lock(
350 lck_rw_type_t lck_rw_type
);
352 extern void lck_rw_unlock(
354 lck_rw_type_t lck_rw_type
);
356 extern void lck_rw_lock_shared(
359 extern void lck_rw_unlock_shared(
362 extern void lck_rw_lock_exclusive(
365 extern void lck_rw_unlock_exclusive(
368 #ifdef KERNEL_PRIVATE
370 extern lck_rw_type_t
lck_rw_done(
374 extern void lck_rw_destroy(
378 extern void lck_rw_free(
382 extern wait_result_t
lck_rw_sleep(
384 lck_sleep_action_t lck_sleep_action
,
386 wait_interrupt_t interruptible
);
388 extern wait_result_t
lck_rw_sleep_deadline(
390 lck_sleep_action_t lck_sleep_action
,
392 wait_interrupt_t interruptible
,
395 #ifdef KERNEL_PRIVATE
397 extern boolean_t
lck_rw_lock_shared_to_exclusive(
400 extern void lck_rw_lock_exclusive_to_shared(
403 extern boolean_t
lck_rw_try_lock(
405 lck_rw_type_t lck_rw_type
);
407 extern boolean_t
lck_rw_try_lock_shared(
410 extern boolean_t
lck_rw_try_lock_exclusive(
416 #endif /* _KERN_LOCKS_H_ */