]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/locks.h
f7b65df47e2064e242fd7de568692e501a30f106
[apple/xnu.git] / osfmk / kern / locks.h
1 /*
2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #ifndef _KERN_LOCKS_H_
32 #define _KERN_LOCKS_H_
33
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>
40
41 #ifdef MACH_KERNEL_PRIVATE
42 #include <kern/queue.h>
43
44 extern void lck_mod_init(
45 void);
46
47 typedef unsigned int lck_type_t;
48
49 #define LCK_TYPE_SPIN 1
50 #define LCK_TYPE_MTX 2
51 #define LCK_TYPE_RW 3
52
53 #endif
54
55 typedef unsigned int lck_sleep_action_t;
56
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) */
62
63 #define LCK_SLEEP_MASK 0x07 /* Valid actions */
64
65 #ifdef MACH_KERNEL_PRIVATE
66
67 typedef struct {
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;
74
75 typedef struct {
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;
84 } lck_grp_mtx_stat_t;
85
86 typedef struct {
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;
95 } lck_grp_rw_stat_t;
96
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;
101 } lck_grp_stat_t;
102
103 #define LCK_GRP_MAX_NAME 64
104
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;
114 } lck_grp_t;
115
116 #define LCK_GRP_NULL (lck_grp_t *)0
117
118 #else
119 typedef struct __lck_grp__ lck_grp_t;
120 #endif
121
122 #ifdef MACH_KERNEL_PRIVATE
123 typedef struct _lck_grp_attr_ {
124 unsigned int grp_attr_val;
125 } lck_grp_attr_t;
126
127 extern lck_grp_attr_t LockDefaultGroupAttr;
128
129 #define LCK_GRP_ATTR_STAT 0x1
130
131 #else
132 typedef struct __lck_grp_attr__ lck_grp_attr_t;
133 #endif
134
135 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
136
137 __BEGIN_DECLS
138
139 extern lck_grp_attr_t *lck_grp_attr_alloc_init(
140 void);
141
142 extern void lck_grp_attr_setdefault(
143 lck_grp_attr_t *attr);
144
145 extern void lck_grp_attr_setstat(
146 lck_grp_attr_t *attr);
147
148 extern void lck_grp_attr_free(
149 lck_grp_attr_t *attr);
150
151 extern lck_grp_t *lck_grp_alloc_init(
152 const char* grp_name,
153 lck_grp_attr_t *attr);
154
155 __END_DECLS
156
157 #ifdef MACH_KERNEL_PRIVATE
158 extern void lck_grp_init(
159 lck_grp_t *grp,
160 const char* grp_name,
161 lck_grp_attr_t *attr);
162
163 extern void lck_grp_reference(
164 lck_grp_t *grp);
165
166 extern void lck_grp_deallocate(
167 lck_grp_t *grp);
168
169 extern void lck_grp_lckcnt_incr(
170 lck_grp_t *grp,
171 lck_type_t lck_type);
172
173 extern void lck_grp_lckcnt_decr(
174 lck_grp_t *grp,
175 lck_type_t lck_type);
176 #endif
177
178 __BEGIN_DECLS
179
180 extern void lck_grp_free(
181 lck_grp_t *grp);
182
183 __END_DECLS
184
185 #ifdef MACH_KERNEL_PRIVATE
186 typedef struct _lck_attr_ {
187 unsigned int lck_attr_val;
188 } lck_attr_t;
189
190 extern lck_attr_t LockDefaultLckAttr;
191
192 #define LCK_ATTR_NONE 0
193
194 #define LCK_ATTR_DEBUG 0x00000001
195 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
196
197 #else
198 typedef struct __lck_attr__ lck_attr_t;
199 #endif
200
201 #define LCK_ATTR_NULL (lck_attr_t *)0
202
203 __BEGIN_DECLS
204
205 extern lck_attr_t *lck_attr_alloc_init(
206 void);
207
208 extern void lck_attr_setdefault(
209 lck_attr_t *attr);
210
211 extern void lck_attr_setdebug(
212 lck_attr_t *attr);
213
214 #ifdef XNU_KERNEL_PRIVATE
215 extern void lck_attr_rw_shared_priority(
216 lck_attr_t *attr);
217 #endif
218
219 extern void lck_attr_free(
220 lck_attr_t *attr);
221
222 #define decl_lck_spin_data(class,name) class lck_spin_t name;
223
224 extern lck_spin_t *lck_spin_alloc_init(
225 lck_grp_t *grp,
226 lck_attr_t *attr);
227
228 extern void lck_spin_init(
229 lck_spin_t *lck,
230 lck_grp_t *grp,
231 lck_attr_t *attr);
232
233 extern void lck_spin_lock(
234 lck_spin_t *lck);
235
236 extern void lck_spin_unlock(
237 lck_spin_t *lck);
238
239 extern void lck_spin_destroy(
240 lck_spin_t *lck,
241 lck_grp_t *grp);
242
243 extern void lck_spin_free(
244 lck_spin_t *lck,
245 lck_grp_t *grp);
246
247 extern wait_result_t lck_spin_sleep(
248 lck_spin_t *lck,
249 lck_sleep_action_t lck_sleep_action,
250 event_t event,
251 wait_interrupt_t interruptible);
252
253 extern wait_result_t lck_spin_sleep_deadline(
254 lck_spin_t *lck,
255 lck_sleep_action_t lck_sleep_action,
256 event_t event,
257 wait_interrupt_t interruptible,
258 uint64_t deadline);
259
260 #ifdef KERNEL_PRIVATE
261
262 extern boolean_t lck_spin_try_lock(
263 lck_spin_t *lck);
264
265 #endif
266
267
268 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
269
270 extern lck_mtx_t *lck_mtx_alloc_init(
271 lck_grp_t *grp,
272 lck_attr_t *attr);
273
274 extern void lck_mtx_init(
275 lck_mtx_t *lck,
276 lck_grp_t *grp,
277 lck_attr_t *attr);
278
279 extern void lck_mtx_lock(
280 lck_mtx_t *lck);
281
282 extern void lck_mtx_unlock(
283 lck_mtx_t *lck);
284
285 extern void lck_mtx_destroy(
286 lck_mtx_t *lck,
287 lck_grp_t *grp);
288
289 extern void lck_mtx_free(
290 lck_mtx_t *lck,
291 lck_grp_t *grp);
292
293 extern wait_result_t lck_mtx_sleep(
294 lck_mtx_t *lck,
295 lck_sleep_action_t lck_sleep_action,
296 event_t event,
297 wait_interrupt_t interruptible);
298
299 extern wait_result_t lck_mtx_sleep_deadline(
300 lck_mtx_t *lck,
301 lck_sleep_action_t lck_sleep_action,
302 event_t event,
303 wait_interrupt_t interruptible,
304 uint64_t deadline);
305
306 #ifdef KERNEL_PRIVATE
307
308 extern boolean_t lck_mtx_try_lock(
309 lck_mtx_t *lck);
310
311 #endif /* KERNEL_PRIVATE */
312
313 extern void lck_mtx_assert(
314 lck_mtx_t *lck,
315 unsigned int type);
316
317 __END_DECLS
318
319 #define LCK_MTX_ASSERT_OWNED 0x01
320 #define LCK_MTX_ASSERT_NOTOWNED 0x02
321
322 #ifdef MACH_KERNEL_PRIVATE
323 extern void lck_mtx_lock_wait(
324 lck_mtx_t *lck,
325 thread_t holder);
326
327 extern int lck_mtx_lock_acquire(
328 lck_mtx_t *lck);
329
330 extern void lck_mtx_unlock_wakeup(
331 lck_mtx_t *lck,
332 thread_t holder);
333
334 extern boolean_t lck_mtx_ilk_unlock(
335 lck_mtx_t *lck);
336 #endif
337
338 #define decl_lck_rw_data(class,name) class lck_rw_t name;
339
340 typedef unsigned int lck_rw_type_t;
341
342 #define LCK_RW_TYPE_SHARED 0x01
343 #define LCK_RW_TYPE_EXCLUSIVE 0x02
344
345 __BEGIN_DECLS
346
347 extern lck_rw_t *lck_rw_alloc_init(
348 lck_grp_t *grp,
349 lck_attr_t *attr);
350
351 extern void lck_rw_init(
352 lck_rw_t *lck,
353 lck_grp_t *grp,
354 lck_attr_t *attr);
355
356 extern void lck_rw_lock(
357 lck_rw_t *lck,
358 lck_rw_type_t lck_rw_type);
359
360 extern void lck_rw_unlock(
361 lck_rw_t *lck,
362 lck_rw_type_t lck_rw_type);
363
364 extern void lck_rw_lock_shared(
365 lck_rw_t *lck);
366
367 extern void lck_rw_unlock_shared(
368 lck_rw_t *lck);
369
370 extern void lck_rw_lock_exclusive(
371 lck_rw_t *lck);
372
373 extern void lck_rw_unlock_exclusive(
374 lck_rw_t *lck);
375
376 #ifdef KERNEL_PRIVATE
377
378 extern lck_rw_type_t lck_rw_done(
379 lck_rw_t *lck);
380 #endif
381
382 extern void lck_rw_destroy(
383 lck_rw_t *lck,
384 lck_grp_t *grp);
385
386 extern void lck_rw_free(
387 lck_rw_t *lck,
388 lck_grp_t *grp);
389
390 extern wait_result_t lck_rw_sleep(
391 lck_rw_t *lck,
392 lck_sleep_action_t lck_sleep_action,
393 event_t event,
394 wait_interrupt_t interruptible);
395
396 extern wait_result_t lck_rw_sleep_deadline(
397 lck_rw_t *lck,
398 lck_sleep_action_t lck_sleep_action,
399 event_t event,
400 wait_interrupt_t interruptible,
401 uint64_t deadline);
402
403 #ifdef KERNEL_PRIVATE
404
405 extern boolean_t lck_rw_lock_shared_to_exclusive(
406 lck_rw_t *lck);
407
408 extern void lck_rw_lock_exclusive_to_shared(
409 lck_rw_t *lck);
410
411 extern boolean_t lck_rw_try_lock(
412 lck_rw_t *lck,
413 lck_rw_type_t lck_rw_type);
414
415 extern boolean_t lck_rw_try_lock_shared(
416 lck_rw_t *lck);
417
418 extern boolean_t lck_rw_try_lock_exclusive(
419 lck_rw_t *lck);
420 #endif
421
422 __END_DECLS
423
424 #endif /* _KERN_LOCKS_H_ */