]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/locks.h
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / kern / locks.h
1 /*
2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _KERN_LOCKS_H_
30 #define _KERN_LOCKS_H_
31
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>
38
39 #ifdef MACH_KERNEL_PRIVATE
40 #include <kern/queue.h>
41
42 extern void lck_mod_init(
43 void);
44
45 typedef unsigned int lck_type_t;
46
47 #define LCK_TYPE_SPIN 1
48 #define LCK_TYPE_MTX 2
49 #define LCK_TYPE_RW 3
50
51 #endif
52
53 typedef unsigned int lck_sleep_action_t;
54
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) */
60 #define LCK_SLEEP_SPIN 0x08 /* Reclaim the lock in spin mode (mutex only) */
61 #define LCK_SLEEP_PROMOTED_PRI 0x10 /* Sleep at a promoted priority */
62
63 #define LCK_SLEEP_MASK 0x1f /* 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 /* On x86, this is used as the "direct wait" count */
78 uint64_t lck_grp_mtx_held_cnt;
79 uint64_t lck_grp_mtx_miss_cnt;
80 uint64_t lck_grp_mtx_wait_cnt;
81 /* Rest currently unused */
82 uint64_t lck_grp_mtx_held_max;
83 uint64_t lck_grp_mtx_held_cum;
84 uint64_t lck_grp_mtx_wait_max;
85 uint64_t lck_grp_mtx_wait_cum;
86 } lck_grp_mtx_stat_t;
87
88 typedef struct {
89 uint64_t lck_grp_rw_util_cnt;
90 uint64_t lck_grp_rw_held_cnt;
91 uint64_t lck_grp_rw_miss_cnt;
92 uint64_t lck_grp_rw_wait_cnt;
93 uint64_t lck_grp_rw_held_max;
94 uint64_t lck_grp_rw_held_cum;
95 uint64_t lck_grp_rw_wait_max;
96 uint64_t lck_grp_rw_wait_cum;
97 } lck_grp_rw_stat_t;
98
99 typedef struct _lck_grp_stat_ {
100 lck_grp_spin_stat_t lck_grp_spin_stat;
101 lck_grp_mtx_stat_t lck_grp_mtx_stat;
102 lck_grp_rw_stat_t lck_grp_rw_stat;
103 } lck_grp_stat_t;
104
105 #define LCK_GRP_MAX_NAME 64
106
107 typedef struct _lck_grp_ {
108 queue_chain_t lck_grp_link;
109 uint32_t lck_grp_refcnt;
110 uint32_t lck_grp_spincnt;
111 uint32_t lck_grp_mtxcnt;
112 uint32_t lck_grp_rwcnt;
113 uint32_t lck_grp_attr;
114 char lck_grp_name[LCK_GRP_MAX_NAME];
115 lck_grp_stat_t lck_grp_stat;
116 } lck_grp_t;
117
118 #define LCK_GRP_NULL (lck_grp_t *)0
119
120 #else
121 typedef struct __lck_grp__ lck_grp_t;
122 #endif
123
124 #ifdef MACH_KERNEL_PRIVATE
125 typedef struct _lck_grp_attr_ {
126 uint32_t grp_attr_val;
127 } lck_grp_attr_t;
128
129 extern lck_grp_attr_t LockDefaultGroupAttr;
130
131 #define LCK_GRP_ATTR_STAT 0x1
132
133 #else
134 typedef struct __lck_grp_attr__ lck_grp_attr_t;
135 #endif
136
137 #define LCK_GRP_ATTR_NULL (lck_grp_attr_t *)0
138
139 __BEGIN_DECLS
140
141 extern lck_grp_attr_t *lck_grp_attr_alloc_init(
142 void);
143
144 extern void lck_grp_attr_setdefault(
145 lck_grp_attr_t *attr);
146
147 extern void lck_grp_attr_setstat(
148 lck_grp_attr_t *attr);
149
150 extern void lck_grp_attr_free(
151 lck_grp_attr_t *attr);
152
153 extern lck_grp_t *lck_grp_alloc_init(
154 const char* grp_name,
155 lck_grp_attr_t *attr);
156
157 __END_DECLS
158
159 #ifdef MACH_KERNEL_PRIVATE
160 extern void lck_grp_init(
161 lck_grp_t *grp,
162 const char* grp_name,
163 lck_grp_attr_t *attr);
164
165 extern void lck_grp_reference(
166 lck_grp_t *grp);
167
168 extern void lck_grp_deallocate(
169 lck_grp_t *grp);
170
171 extern void lck_grp_lckcnt_incr(
172 lck_grp_t *grp,
173 lck_type_t lck_type);
174
175 extern void lck_grp_lckcnt_decr(
176 lck_grp_t *grp,
177 lck_type_t lck_type);
178 #endif
179
180 __BEGIN_DECLS
181
182 extern void lck_grp_free(
183 lck_grp_t *grp);
184
185 __END_DECLS
186
187 #ifdef MACH_KERNEL_PRIVATE
188 typedef struct _lck_attr_ {
189 unsigned int lck_attr_val;
190 } lck_attr_t;
191
192 extern lck_attr_t LockDefaultLckAttr;
193
194 #define LCK_ATTR_NONE 0
195
196 #define LCK_ATTR_DEBUG 0x00000001
197 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
198
199 #else
200 typedef struct __lck_attr__ lck_attr_t;
201 #endif
202
203 #define LCK_ATTR_NULL (lck_attr_t *)0
204
205 __BEGIN_DECLS
206
207 extern lck_attr_t *lck_attr_alloc_init(
208 void);
209
210 extern void lck_attr_setdefault(
211 lck_attr_t *attr);
212
213 extern void lck_attr_setdebug(
214 lck_attr_t *attr);
215
216 extern void lck_attr_cleardebug(
217 lck_attr_t *attr);
218
219 #ifdef XNU_KERNEL_PRIVATE
220 extern void lck_attr_rw_shared_priority(
221 lck_attr_t *attr);
222 #endif
223
224 extern void lck_attr_free(
225 lck_attr_t *attr);
226
227 #define decl_lck_spin_data(class,name) class lck_spin_t name;
228
229 extern lck_spin_t *lck_spin_alloc_init(
230 lck_grp_t *grp,
231 lck_attr_t *attr);
232
233 extern void lck_spin_init(
234 lck_spin_t *lck,
235 lck_grp_t *grp,
236 lck_attr_t *attr);
237
238 extern void lck_spin_lock(
239 lck_spin_t *lck);
240
241 extern void lck_spin_unlock(
242 lck_spin_t *lck);
243
244 extern void lck_spin_destroy(
245 lck_spin_t *lck,
246 lck_grp_t *grp);
247
248 extern void lck_spin_free(
249 lck_spin_t *lck,
250 lck_grp_t *grp);
251
252 extern wait_result_t lck_spin_sleep(
253 lck_spin_t *lck,
254 lck_sleep_action_t lck_sleep_action,
255 event_t event,
256 wait_interrupt_t interruptible);
257
258 extern wait_result_t lck_spin_sleep_deadline(
259 lck_spin_t *lck,
260 lck_sleep_action_t lck_sleep_action,
261 event_t event,
262 wait_interrupt_t interruptible,
263 uint64_t deadline);
264
265 #ifdef KERNEL_PRIVATE
266
267 extern boolean_t lck_spin_try_lock( lck_spin_t *lck);
268
269 /* NOT SAFE: To be used only by kernel debugger to avoid deadlock. */
270 extern boolean_t kdp_lck_spin_is_acquired( lck_spin_t *lck);
271
272 struct _lck_mtx_ext_;
273 extern void lck_mtx_init_ext(lck_mtx_t *lck, struct _lck_mtx_ext_ *lck_ext,
274 lck_grp_t *grp, lck_attr_t *attr);
275
276 #endif
277
278
279 #define decl_lck_mtx_data(class,name) class lck_mtx_t name;
280
281 extern lck_mtx_t *lck_mtx_alloc_init(
282 lck_grp_t *grp,
283 lck_attr_t *attr);
284
285 extern void lck_mtx_init(
286 lck_mtx_t *lck,
287 lck_grp_t *grp,
288 lck_attr_t *attr);
289 extern void lck_mtx_lock(
290 lck_mtx_t *lck);
291
292 extern void lck_mtx_unlock(
293 lck_mtx_t *lck);
294
295 extern void lck_mtx_destroy(
296 lck_mtx_t *lck,
297 lck_grp_t *grp);
298
299 extern void lck_mtx_free(
300 lck_mtx_t *lck,
301 lck_grp_t *grp);
302
303 extern wait_result_t lck_mtx_sleep(
304 lck_mtx_t *lck,
305 lck_sleep_action_t lck_sleep_action,
306 event_t event,
307 wait_interrupt_t interruptible);
308
309 extern wait_result_t lck_mtx_sleep_deadline(
310 lck_mtx_t *lck,
311 lck_sleep_action_t lck_sleep_action,
312 event_t event,
313 wait_interrupt_t interruptible,
314 uint64_t deadline);
315
316 #ifdef KERNEL_PRIVATE
317
318 extern boolean_t lck_mtx_try_lock(
319 lck_mtx_t *lck);
320
321 extern void mutex_pause(uint32_t);
322
323 extern void lck_mtx_yield (
324 lck_mtx_t *lck);
325
326 extern boolean_t lck_mtx_try_lock_spin(
327 lck_mtx_t *lck);
328
329 extern void lck_mtx_lock_spin(
330 lck_mtx_t *lck);
331
332 extern boolean_t kdp_lck_mtx_lock_spin_is_acquired(
333 lck_mtx_t *lck);
334
335 extern void lck_mtx_convert_spin(
336 lck_mtx_t *lck);
337
338 extern void lck_mtx_lock_spin_always(
339 lck_mtx_t *lck);
340
341 extern boolean_t lck_mtx_try_lock_spin_always(
342 lck_mtx_t *lck);
343
344 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
345
346 extern void lck_spin_assert(
347 lck_spin_t *lck,
348 unsigned int type);
349
350 extern boolean_t kdp_lck_rw_lock_is_acquired_exclusive(
351 lck_rw_t *lck);
352
353 #endif /* KERNEL_PRIVATE */
354
355 extern void lck_mtx_assert(
356 lck_mtx_t *lck,
357 unsigned int type);
358
359 #if MACH_ASSERT
360 #define LCK_MTX_ASSERT(lck,type) lck_mtx_assert((lck),(type))
361 #else /* MACH_ASSERT */
362 #define LCK_MTX_ASSERT(lck,type)
363 #endif /* MACH_ASSERT */
364
365 #if DEBUG
366 #define LCK_MTX_ASSERT_DEBUG(lck,type) lck_mtx_assert((lck),(type))
367 #else /* DEBUG */
368 #define LCK_MTX_ASSERT_DEBUG(lck,type)
369 #endif /* DEBUG */
370
371 __END_DECLS
372
373 #define LCK_ASSERT_OWNED 1
374 #define LCK_ASSERT_NOTOWNED 2
375
376 #define LCK_MTX_ASSERT_OWNED LCK_ASSERT_OWNED
377 #define LCK_MTX_ASSERT_NOTOWNED LCK_ASSERT_NOTOWNED
378
379 #ifdef MACH_KERNEL_PRIVATE
380 extern void lck_mtx_lock_wait(
381 lck_mtx_t *lck,
382 thread_t holder);
383
384 extern int lck_mtx_lock_acquire(
385 lck_mtx_t *lck);
386
387 extern void lck_mtx_unlock_wakeup(
388 lck_mtx_t *lck,
389 thread_t holder);
390 extern void lck_mtx_unlockspin_wakeup(
391 lck_mtx_t *lck);
392
393 extern boolean_t lck_mtx_ilk_unlock(
394 lck_mtx_t *lck);
395
396 extern boolean_t lck_mtx_ilk_try_lock(
397 lck_mtx_t *lck);
398
399 #endif
400
401 #define decl_lck_rw_data(class,name) class lck_rw_t name;
402
403 typedef unsigned int lck_rw_type_t;
404
405 #define LCK_RW_TYPE_SHARED 0x01
406 #define LCK_RW_TYPE_EXCLUSIVE 0x02
407
408 #ifdef XNU_KERNEL_PRIVATE
409 #define LCK_RW_ASSERT_SHARED 0x01
410 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
411 #define LCK_RW_ASSERT_HELD 0x03
412 #define LCK_RW_ASSERT_NOTHELD 0x04
413 #endif
414
415 __BEGIN_DECLS
416
417 extern lck_rw_t *lck_rw_alloc_init(
418 lck_grp_t *grp,
419 lck_attr_t *attr);
420
421 extern void lck_rw_init(
422 lck_rw_t *lck,
423 lck_grp_t *grp,
424 lck_attr_t *attr);
425
426 extern void lck_rw_lock(
427 lck_rw_t *lck,
428 lck_rw_type_t lck_rw_type);
429
430 extern void lck_rw_unlock(
431 lck_rw_t *lck,
432 lck_rw_type_t lck_rw_type);
433
434 extern void lck_rw_lock_shared(
435 lck_rw_t *lck);
436
437 extern void lck_rw_unlock_shared(
438 lck_rw_t *lck);
439
440 extern void lck_rw_lock_exclusive(
441 lck_rw_t *lck);
442
443 extern void lck_rw_unlock_exclusive(
444 lck_rw_t *lck);
445
446 #ifdef XNU_KERNEL_PRIVATE
447 /*
448 * CAUTION
449 * read-write locks do not have a concept of ownership, so lck_rw_assert()
450 * merely asserts that someone is holding the lock, not necessarily the caller.
451 */
452 extern void lck_rw_assert(
453 lck_rw_t *lck,
454 unsigned int type);
455
456 extern void lck_rw_clear_promotion(
457 thread_t thread);
458 extern void lck_rw_set_promotion_locked(thread_t thread);
459 #endif
460
461 #ifdef KERNEL_PRIVATE
462
463 extern lck_rw_type_t lck_rw_done(
464 lck_rw_t *lck);
465 #endif
466
467 extern void lck_rw_destroy(
468 lck_rw_t *lck,
469 lck_grp_t *grp);
470
471 extern void lck_rw_free(
472 lck_rw_t *lck,
473 lck_grp_t *grp);
474
475 extern wait_result_t lck_rw_sleep(
476 lck_rw_t *lck,
477 lck_sleep_action_t lck_sleep_action,
478 event_t event,
479 wait_interrupt_t interruptible);
480
481 extern wait_result_t lck_rw_sleep_deadline(
482 lck_rw_t *lck,
483 lck_sleep_action_t lck_sleep_action,
484 event_t event,
485 wait_interrupt_t interruptible,
486 uint64_t deadline);
487
488 extern boolean_t lck_rw_lock_shared_to_exclusive(
489 lck_rw_t *lck);
490
491 extern void lck_rw_lock_exclusive_to_shared(
492 lck_rw_t *lck);
493
494 extern boolean_t lck_rw_try_lock(
495 lck_rw_t *lck,
496 lck_rw_type_t lck_rw_type);
497
498 #ifdef KERNEL_PRIVATE
499
500 extern boolean_t lck_rw_try_lock_shared(
501 lck_rw_t *lck);
502
503 extern boolean_t lck_rw_try_lock_exclusive(
504 lck_rw_t *lck);
505 #endif
506
507 __END_DECLS
508
509 #endif /* _KERN_LOCKS_H_ */