2 * Copyright (c) 2003-2005 Apple Computer, 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) */
61 #define LCK_SLEEP_MASK 0x07 /* Valid actions */
63 #ifdef MACH_KERNEL_PRIVATE
66 uint64_t lck_grp_spin_util_cnt
;
67 uint64_t lck_grp_spin_held_cnt
;
68 uint64_t lck_grp_spin_miss_cnt
;
69 uint64_t lck_grp_spin_held_max
;
70 uint64_t lck_grp_spin_held_cum
;
71 } lck_grp_spin_stat_t
;
74 uint64_t lck_grp_mtx_util_cnt
;
75 uint64_t lck_grp_mtx_held_cnt
;
76 uint64_t lck_grp_mtx_miss_cnt
;
77 uint64_t lck_grp_mtx_wait_cnt
;
78 uint64_t lck_grp_mtx_held_max
;
79 uint64_t lck_grp_mtx_held_cum
;
80 uint64_t lck_grp_mtx_wait_max
;
81 uint64_t lck_grp_mtx_wait_cum
;
85 uint64_t lck_grp_rw_util_cnt
;
86 uint64_t lck_grp_rw_held_cnt
;
87 uint64_t lck_grp_rw_miss_cnt
;
88 uint64_t lck_grp_rw_wait_cnt
;
89 uint64_t lck_grp_rw_held_max
;
90 uint64_t lck_grp_rw_held_cum
;
91 uint64_t lck_grp_rw_wait_max
;
92 uint64_t lck_grp_rw_wait_cum
;
95 typedef struct _lck_grp_stat_
{
96 lck_grp_spin_stat_t lck_grp_spin_stat
;
97 lck_grp_mtx_stat_t lck_grp_mtx_stat
;
98 lck_grp_rw_stat_t lck_grp_rw_stat
;
101 #define LCK_GRP_MAX_NAME 64
103 typedef struct _lck_grp_
{
104 queue_chain_t lck_grp_link
;
105 unsigned int lck_grp_refcnt
;
106 unsigned int lck_grp_spincnt
;
107 unsigned int lck_grp_mtxcnt
;
108 unsigned int lck_grp_rwcnt
;
109 unsigned int lck_grp_attr
;
110 char lck_grp_name
[LCK_GRP_MAX_NAME
];
111 lck_grp_stat_t lck_grp_stat
;
114 #define LCK_GRP_NULL (lck_grp_t *)0
117 typedef struct __lck_grp__ lck_grp_t
;
120 #ifdef MACH_KERNEL_PRIVATE
121 typedef struct _lck_grp_attr_
{
122 unsigned int grp_attr_val
;
125 extern lck_grp_attr_t LockDefaultGroupAttr
;
127 #define LCK_GRP_ATTR_STAT 0x1
130 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
133 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
137 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
140 extern void lck_grp_attr_setdefault(
141 lck_grp_attr_t
*attr
);
143 extern void lck_grp_attr_setstat(
144 lck_grp_attr_t
*attr
);
146 extern void lck_grp_attr_free(
147 lck_grp_attr_t
*attr
);
149 extern lck_grp_t
*lck_grp_alloc_init(
150 const char* grp_name
,
151 lck_grp_attr_t
*attr
);
155 #ifdef MACH_KERNEL_PRIVATE
156 extern void lck_grp_init(
158 const char* grp_name
,
159 lck_grp_attr_t
*attr
);
161 extern void lck_grp_reference(
164 extern void lck_grp_deallocate(
167 extern void lck_grp_lckcnt_incr(
169 lck_type_t lck_type
);
171 extern void lck_grp_lckcnt_decr(
173 lck_type_t lck_type
);
178 extern void lck_grp_free(
183 #ifdef MACH_KERNEL_PRIVATE
184 typedef struct _lck_attr_
{
185 unsigned int lck_attr_val
;
188 extern lck_attr_t LockDefaultLckAttr
;
190 #define LCK_ATTR_NONE 0
191 #define LCK_ATTR_DEBUG 0x1
194 typedef struct __lck_attr__ lck_attr_t
;
197 #define LCK_ATTR_NULL (lck_attr_t *)0
201 extern lck_attr_t
*lck_attr_alloc_init(
204 extern void lck_attr_setdefault(
207 extern void lck_attr_setdebug(
210 extern void lck_attr_free(
213 #define decl_lck_spin_data(class,name) class lck_spin_t name;
215 extern lck_spin_t
*lck_spin_alloc_init(
219 extern void lck_spin_init(
224 extern void lck_spin_lock(
227 extern void lck_spin_unlock(
230 extern void lck_spin_destroy(
234 extern void lck_spin_free(
238 extern wait_result_t
lck_spin_sleep(
240 lck_sleep_action_t lck_sleep_action
,
242 wait_interrupt_t interruptible
);
244 extern wait_result_t
lck_spin_sleep_deadline(
246 lck_sleep_action_t lck_sleep_action
,
248 wait_interrupt_t interruptible
,
251 #ifdef KERNEL_PRIVATE
253 extern boolean_t
lck_spin_try_lock(
259 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
261 extern lck_mtx_t
*lck_mtx_alloc_init(
265 extern void lck_mtx_init(
270 extern void lck_mtx_lock(
273 extern void lck_mtx_unlock(
276 extern void lck_mtx_destroy(
280 extern void lck_mtx_free(
284 extern wait_result_t
lck_mtx_sleep(
286 lck_sleep_action_t lck_sleep_action
,
288 wait_interrupt_t interruptible
);
290 extern wait_result_t
lck_mtx_sleep_deadline(
292 lck_sleep_action_t lck_sleep_action
,
294 wait_interrupt_t interruptible
,
297 #ifdef KERNEL_PRIVATE
299 extern boolean_t
lck_mtx_try_lock(
302 #endif /* KERNEL_PRIVATE */
304 extern void lck_mtx_assert(
310 #define LCK_MTX_ASSERT_OWNED 0x01
311 #define LCK_MTX_ASSERT_NOTOWNED 0x02
313 #ifdef MACH_KERNEL_PRIVATE
314 extern void lck_mtx_lock_wait(
318 extern int lck_mtx_lock_acquire(
321 extern void lck_mtx_unlock_wakeup(
325 extern boolean_t
lck_mtx_ilk_unlock(
329 #define decl_lck_rw_data(class,name) class lck_rw_t name;
331 typedef unsigned int lck_rw_type_t
;
333 #define LCK_RW_TYPE_SHARED 0x01
334 #define LCK_RW_TYPE_EXCLUSIVE 0x02
338 extern lck_rw_t
*lck_rw_alloc_init(
342 extern void lck_rw_init(
347 extern void lck_rw_lock(
349 lck_rw_type_t lck_rw_type
);
351 extern void lck_rw_unlock(
353 lck_rw_type_t lck_rw_type
);
355 extern void lck_rw_lock_shared(
358 extern void lck_rw_unlock_shared(
361 extern void lck_rw_lock_exclusive(
364 extern void lck_rw_unlock_exclusive(
367 #ifdef KERNEL_PRIVATE
369 extern lck_rw_type_t
lck_rw_done(
373 extern void lck_rw_destroy(
377 extern void lck_rw_free(
381 extern wait_result_t
lck_rw_sleep(
383 lck_sleep_action_t lck_sleep_action
,
385 wait_interrupt_t interruptible
);
387 extern wait_result_t
lck_rw_sleep_deadline(
389 lck_sleep_action_t lck_sleep_action
,
391 wait_interrupt_t interruptible
,
394 #ifdef KERNEL_PRIVATE
396 extern boolean_t
lck_rw_lock_shared_to_exclusive(
399 extern void lck_rw_lock_exclusive_to_shared(
402 extern boolean_t
lck_rw_try_lock(
404 lck_rw_type_t lck_rw_type
);
406 extern boolean_t
lck_rw_try_lock_shared(
409 extern boolean_t
lck_rw_try_lock_exclusive(
415 #endif /* _KERN_LOCKS_H_ */