2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _KERN_LOCKS_H_
25 #define _KERN_LOCKS_H_
27 #include <sys/cdefs.h>
28 #include <sys/appleapiopts.h>
29 #include <mach/boolean.h>
30 #include <mach/mach_types.h>
31 #include <kern/kern_types.h>
32 #include <machine/locks.h>
34 #ifdef MACH_KERNEL_PRIVATE
35 #include <kern/queue.h>
37 extern void lck_mod_init(
40 typedef unsigned int lck_type_t
;
42 #define LCK_TYPE_SPIN 1
43 #define LCK_TYPE_MTX 2
48 typedef unsigned int lck_sleep_action_t
;
50 #define LCK_SLEEP_DEFAULT 0x00 /* Release the lock while waiting for the event, then reclaim */
51 /* RW locks are returned in the same mode */
52 #define LCK_SLEEP_UNLOCK 0x01 /* Release the lock and return unheld */
53 #define LCK_SLEEP_SHARED 0x02 /* Reclaim the lock in shared mode (RW only) */
54 #define LCK_SLEEP_EXCLUSIVE 0x04 /* Reclaim the lock in exclusive mode (RW only) */
56 #define LCK_SLEEP_MASK 0x07 /* Valid actions */
58 #ifdef MACH_KERNEL_PRIVATE
61 uint64_t lck_grp_spin_util_cnt
;
62 uint64_t lck_grp_spin_held_cnt
;
63 uint64_t lck_grp_spin_miss_cnt
;
64 uint64_t lck_grp_spin_held_max
;
65 uint64_t lck_grp_spin_held_cum
;
66 } lck_grp_spin_stat_t
;
69 uint64_t lck_grp_mtx_util_cnt
;
70 uint64_t lck_grp_mtx_held_cnt
;
71 uint64_t lck_grp_mtx_miss_cnt
;
72 uint64_t lck_grp_mtx_wait_cnt
;
73 uint64_t lck_grp_mtx_held_max
;
74 uint64_t lck_grp_mtx_held_cum
;
75 uint64_t lck_grp_mtx_wait_max
;
76 uint64_t lck_grp_mtx_wait_cum
;
80 uint64_t lck_grp_rw_util_cnt
;
81 uint64_t lck_grp_rw_held_cnt
;
82 uint64_t lck_grp_rw_miss_cnt
;
83 uint64_t lck_grp_rw_wait_cnt
;
84 uint64_t lck_grp_rw_held_max
;
85 uint64_t lck_grp_rw_held_cum
;
86 uint64_t lck_grp_rw_wait_max
;
87 uint64_t lck_grp_rw_wait_cum
;
90 typedef struct _lck_grp_stat_
{
91 lck_grp_spin_stat_t lck_grp_spin_stat
;
92 lck_grp_mtx_stat_t lck_grp_mtx_stat
;
93 lck_grp_rw_stat_t lck_grp_rw_stat
;
96 #define LCK_GRP_MAX_NAME 64
98 typedef struct _lck_grp_
{
99 queue_chain_t lck_grp_link
;
100 unsigned int lck_grp_refcnt
;
101 unsigned int lck_grp_spincnt
;
102 unsigned int lck_grp_mtxcnt
;
103 unsigned int lck_grp_rwcnt
;
104 unsigned int lck_grp_attr
;
105 char lck_grp_name
[LCK_GRP_MAX_NAME
];
106 lck_grp_stat_t lck_grp_stat
;
109 #define LCK_GRP_NULL (lck_grp_t *)0
112 typedef struct __lck_grp__ lck_grp_t
;
115 #ifdef MACH_KERNEL_PRIVATE
116 typedef struct _lck_grp_attr_
{
117 unsigned int grp_attr_val
;
120 extern lck_grp_attr_t LockDefaultGroupAttr
;
122 #define LCK_GRP_ATTR_STAT 0x1
125 typedef struct __lck_grp_attr__ lck_grp_attr_t
;
128 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
132 extern lck_grp_attr_t
*lck_grp_attr_alloc_init(
135 extern void lck_grp_attr_setdefault(
136 lck_grp_attr_t
*attr
);
138 extern void lck_grp_attr_setstat(
139 lck_grp_attr_t
*attr
);
141 extern void lck_grp_attr_free(
142 lck_grp_attr_t
*attr
);
144 extern lck_grp_t
*lck_grp_alloc_init(
145 const char* grp_name
,
146 lck_grp_attr_t
*attr
);
150 #ifdef MACH_KERNEL_PRIVATE
151 extern void lck_grp_init(
153 const char* grp_name
,
154 lck_grp_attr_t
*attr
);
156 extern void lck_grp_reference(
159 extern void lck_grp_deallocate(
162 extern void lck_grp_lckcnt_incr(
164 lck_type_t lck_type
);
166 extern void lck_grp_lckcnt_decr(
168 lck_type_t lck_type
);
173 extern void lck_grp_free(
178 #ifdef MACH_KERNEL_PRIVATE
179 typedef struct _lck_attr_
{
180 unsigned int lck_attr_val
;
183 extern lck_attr_t LockDefaultLckAttr
;
185 #define LCK_ATTR_NONE 0
186 #define LCK_ATTR_DEBUG 0x1
189 typedef struct __lck_attr__ lck_attr_t
;
192 #define LCK_ATTR_NULL (lck_attr_t *)0
196 extern lck_attr_t
*lck_attr_alloc_init(
199 extern void lck_attr_setdefault(
202 extern void lck_attr_setdebug(
205 extern void lck_attr_free(
208 #define decl_lck_spin_data(class,name) class lck_spin_t name;
210 extern lck_spin_t
*lck_spin_alloc_init(
214 extern void lck_spin_init(
219 extern void lck_spin_lock(
222 extern void lck_spin_unlock(
225 extern void lck_spin_destroy(
229 extern void lck_spin_free(
233 extern wait_result_t
lck_spin_sleep(
235 lck_sleep_action_t lck_sleep_action
,
237 wait_interrupt_t interruptible
);
239 extern wait_result_t
lck_spin_sleep_deadline(
241 lck_sleep_action_t lck_sleep_action
,
243 wait_interrupt_t interruptible
,
246 #ifdef KERNEL_PRIVATE
248 extern boolean_t
lck_spin_try_lock(
254 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
256 extern lck_mtx_t
*lck_mtx_alloc_init(
260 extern void lck_mtx_init(
265 extern void lck_mtx_lock(
268 extern void lck_mtx_unlock(
271 extern void lck_mtx_destroy(
275 extern void lck_mtx_free(
279 extern wait_result_t
lck_mtx_sleep(
281 lck_sleep_action_t lck_sleep_action
,
283 wait_interrupt_t interruptible
);
285 extern wait_result_t
lck_mtx_sleep_deadline(
287 lck_sleep_action_t lck_sleep_action
,
289 wait_interrupt_t interruptible
,
292 #ifdef KERNEL_PRIVATE
294 extern boolean_t
lck_mtx_try_lock(
297 #endif /* KERNEL_PRIVATE */
299 extern void lck_mtx_assert(
305 #define LCK_MTX_ASSERT_OWNED 0x01
306 #define LCK_MTX_ASSERT_NOTOWNED 0x02
308 #ifdef MACH_KERNEL_PRIVATE
309 extern void lck_mtx_lock_wait(
313 extern int lck_mtx_lock_acquire(
316 extern void lck_mtx_unlock_wakeup(
320 extern boolean_t
lck_mtx_ilk_unlock(
324 #define decl_lck_rw_data(class,name) class lck_rw_t name;
326 typedef unsigned int lck_rw_type_t
;
328 #define LCK_RW_TYPE_SHARED 0x01
329 #define LCK_RW_TYPE_EXCLUSIVE 0x02
333 extern lck_rw_t
*lck_rw_alloc_init(
337 extern void lck_rw_init(
342 extern void lck_rw_lock(
344 lck_rw_type_t lck_rw_type
);
346 extern void lck_rw_unlock(
348 lck_rw_type_t lck_rw_type
);
350 extern void lck_rw_lock_shared(
353 extern void lck_rw_unlock_shared(
356 extern void lck_rw_lock_exclusive(
359 extern void lck_rw_unlock_exclusive(
362 #ifdef KERNEL_PRIVATE
364 extern lck_rw_type_t
lck_rw_done(
368 extern void lck_rw_destroy(
372 extern void lck_rw_free(
376 extern wait_result_t
lck_rw_sleep(
378 lck_sleep_action_t lck_sleep_action
,
380 wait_interrupt_t interruptible
);
382 extern wait_result_t
lck_rw_sleep_deadline(
384 lck_sleep_action_t lck_sleep_action
,
386 wait_interrupt_t interruptible
,
389 #ifdef KERNEL_PRIVATE
391 extern boolean_t
lck_rw_lock_shared_to_exclusive(
394 extern void lck_rw_lock_exclusive_to_shared(
397 extern boolean_t
lck_rw_try_lock(
399 lck_rw_type_t lck_rw_type
);
401 extern boolean_t
lck_rw_try_lock_shared(
404 extern boolean_t
lck_rw_try_lock_exclusive(
410 #endif /* _KERN_LOCKS_H_ */