2 * Copyright (c) 2003-2019 Apple 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 <kern/lock_group.h>
38 #include <machine/locks.h>
40 #ifdef MACH_KERNEL_PRIVATE
41 #include <kern/queue.h>
43 extern void lck_mod_init(
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) */
55 #define LCK_SLEEP_SPIN 0x08 /* Reclaim the lock in spin mode (mutex only) */
56 #define LCK_SLEEP_PROMOTED_PRI 0x10 /* Sleep at a promoted priority */
57 #define LCK_SLEEP_SPIN_ALWAYS 0x20 /* Reclaim the lock in spin-always mode (mutex only) */
59 #define LCK_SLEEP_MASK 0x3f /* Valid actions */
61 typedef unsigned int lck_wake_action_t
;
63 #define LCK_WAKE_DEFAULT 0x00 /* If waiters are present, transfer their push to the wokenup thread */
64 #define LCK_WAKE_DO_NOT_TRANSFER_PUSH 0x01 /* Do not transfer waiters push when waking up */
66 #ifdef MACH_KERNEL_PRIVATE
67 typedef struct _lck_attr_
{
68 unsigned int lck_attr_val
;
71 extern lck_attr_t LockDefaultLckAttr
;
73 #define LCK_ATTR_NONE 0
75 #define LCK_ATTR_DEBUG 0x00000001
76 #define LCK_ATTR_RW_SHARED_PRIORITY 0x00010000
79 typedef struct __lck_attr__ lck_attr_t
;
82 #define LCK_ATTR_NULL (lck_attr_t *)NULL
86 extern lck_attr_t
*lck_attr_alloc_init(
89 extern void lck_attr_setdefault(
92 extern void lck_attr_setdebug(
95 extern void lck_attr_cleardebug(
98 #ifdef XNU_KERNEL_PRIVATE
106 } lck_ticket_internal
;
109 lck_ticket_internal tu
;
113 void lck_ticket_init(lck_ticket_t
*tlock
);
114 void lck_ticket_lock(lck_ticket_t
*tlock
);
115 void lck_ticket_unlock(lck_ticket_t
*tlock
);
116 void lck_ticket_assert_owned(lck_ticket_t
*tlock
);
118 extern void lck_attr_rw_shared_priority(
122 extern void lck_attr_free(
125 #define decl_lck_spin_data(class, name) class lck_spin_t name
127 extern lck_spin_t
*lck_spin_alloc_init(
131 extern void lck_spin_init(
136 extern void lck_spin_lock(
139 extern void lck_spin_lock_grp(
143 extern void lck_spin_unlock(
146 extern void lck_spin_destroy(
150 extern void lck_spin_free(
154 extern wait_result_t
lck_spin_sleep(
156 lck_sleep_action_t lck_sleep_action
,
158 wait_interrupt_t interruptible
);
160 extern wait_result_t
lck_spin_sleep_grp(
162 lck_sleep_action_t lck_sleep_action
,
164 wait_interrupt_t interruptible
,
167 extern wait_result_t
lck_spin_sleep_deadline(
169 lck_sleep_action_t lck_sleep_action
,
171 wait_interrupt_t interruptible
,
174 #ifdef KERNEL_PRIVATE
176 extern void lck_spin_lock_nopreempt( lck_spin_t
*lck
);
177 extern void lck_spin_lock_nopreempt_grp( lck_spin_t
*lck
, lck_grp_t
*grp
);
179 extern void lck_spin_unlock_nopreempt( lck_spin_t
*lck
);
181 extern boolean_t
lck_spin_try_lock_grp( lck_spin_t
*lck
, lck_grp_t
*grp
);
183 extern boolean_t
lck_spin_try_lock( lck_spin_t
*lck
);
185 extern boolean_t
lck_spin_try_lock_nopreempt( lck_spin_t
*lck
);
186 extern boolean_t
lck_spin_try_lock_nopreempt_grp( lck_spin_t
*lck
, lck_grp_t
*grp
);
188 /* NOT SAFE: To be used only by kernel debugger to avoid deadlock. */
189 extern boolean_t
kdp_lck_spin_is_acquired( lck_spin_t
*lck
);
191 struct _lck_mtx_ext_
;
192 extern void lck_mtx_init_ext(lck_mtx_t
*lck
, struct _lck_mtx_ext_
*lck_ext
,
193 lck_grp_t
*grp
, lck_attr_t
*attr
);
198 #define decl_lck_mtx_data(class, name) class lck_mtx_t name
200 extern lck_mtx_t
*lck_mtx_alloc_init(
204 extern void lck_mtx_init(
208 extern void lck_mtx_lock(
211 extern void lck_mtx_unlock(
214 extern void lck_mtx_destroy(
218 extern void lck_mtx_free(
222 extern wait_result_t
lck_mtx_sleep(
224 lck_sleep_action_t lck_sleep_action
,
226 wait_interrupt_t interruptible
);
228 extern wait_result_t
lck_mtx_sleep_deadline(
230 lck_sleep_action_t lck_sleep_action
,
232 wait_interrupt_t interruptible
,
235 #ifdef KERNEL_PRIVATE
237 * Name: lck_spin_sleep_with_inheritor
239 * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
240 * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
241 * be directed to the inheritor specified.
242 * An interruptible mode and deadline can be specified to return earlier from the wait.
245 * Arg1: lck_spin_t lock used to protect the sleep. The lock will be dropped while sleeping and reaquired before returning according to the sleep action specified.
246 * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK.
247 * Arg3: event to wait on.
248 * Arg4: thread to propagate the event push to.
249 * Arg5: interruptible flag for wait.
250 * Arg6: deadline for wait.
252 * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
253 * Lock will be dropped while waiting.
254 * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
255 * wakeup for the event is called.
257 * Returns: result of the wait.
259 extern wait_result_t
lck_spin_sleep_with_inheritor(lck_spin_t
*lock
, lck_sleep_action_t lck_sleep_action
, event_t event
, thread_t inheritor
, wait_interrupt_t interruptible
, uint64_t deadline
);
262 * Name: lck_mtx_sleep_with_inheritor
264 * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
265 * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
266 * be directed to the inheritor specified.
267 * An interruptible mode and deadline can be specified to return earlier from the wait.
270 * Arg1: lck_mtx_t lock used to protect the sleep. The lock will be dropped while sleeping and reaquired before returning according to the sleep action specified.
271 * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK, LCK_SLEEP_SPIN, LCK_SLEEP_SPIN_ALWAYS.
272 * Arg3: event to wait on.
273 * Arg4: thread to propagate the event push to.
274 * Arg5: interruptible flag for wait.
275 * Arg6: deadline for wait.
277 * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
278 * Lock will be dropped while waiting.
279 * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
280 * wakeup for the event is called.
282 * Returns: result of the wait.
284 extern wait_result_t
lck_mtx_sleep_with_inheritor(lck_mtx_t
*lock
, lck_sleep_action_t lck_sleep_action
, event_t event
, thread_t inheritor
, wait_interrupt_t interruptible
, uint64_t deadline
);
287 * Name: lck_mtx_sleep_with_inheritor
289 * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
290 * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
291 * be directed to the inheritor specified.
292 * An interruptible mode and deadline can be specified to return earlier from the wait.
295 * Arg1: lck_rw_t lock used to protect the sleep. The lock will be dropped while sleeping and reaquired before returning according to the sleep action specified.
296 * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_SHARED, LCK_SLEEP_EXCLUSIVE.
297 * Arg3: event to wait on.
298 * Arg4: thread to propagate the event push to.
299 * Arg5: interruptible flag for wait.
300 * Arg6: deadline for wait.
302 * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
303 * Lock will be dropped while waiting.
304 * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
305 * wakeup for the event is called.
307 * Returns: result of the wait.
309 extern wait_result_t
lck_rw_sleep_with_inheritor(lck_rw_t
*lock
, lck_sleep_action_t lck_sleep_action
, event_t event
, thread_t inheritor
, wait_interrupt_t interruptible
, uint64_t deadline
);
312 * Name: wakeup_one_with_inheritor
314 * Description: wake up one waiter for event if any. The thread woken up will be the one with the higher sched priority waiting on event.
315 * The push for the event will be transferred from the last inheritor to the woken up thread.
318 * Arg1: event to wake from.
319 * Arg2: wait result to pass to the woken up thread.
320 * Arg3: pointer for storing the thread wokenup.
322 * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
324 * Conditions: The new inheritor wokenup cannot run in user space until another inheritor is specified for the event or a
325 * wakeup for the event is called.
326 * A reference for the wokenup thread is acquired.
327 * NOTE: this cannot be called from interrupt context.
329 extern kern_return_t
wakeup_one_with_inheritor(event_t event
, wait_result_t result
, lck_wake_action_t action
, thread_t
*thread_wokenup
);
332 * Name: wakeup_all_with_inheritor
334 * Description: wake up all waiters waiting for event. The old inheritor will lose the push.
337 * Arg1: event to wake from.
338 * Arg2: wait result to pass to the woken up threads.
340 * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
342 * Conditions: NOTE: this cannot be called from interrupt context.
344 extern kern_return_t
wakeup_all_with_inheritor(event_t event
, wait_result_t result
);
347 * Name: change_sleep_inheritor
349 * Description: Redirect the push of the waiting threads of event to the new inheritor specified.
352 * Arg1: event to redirect the push.
353 * Arg2: new inheritor for event.
355 * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
357 * Conditions: In case of success, the new inheritor cannot run in user space until another inheritor is specified for the event or a
358 * wakeup for the event is called.
359 * NOTE: this cannot be called from interrupt context.
361 extern kern_return_t
change_sleep_inheritor(event_t event
, thread_t inheritor
);
366 typedef struct gate
{
367 uintptr_t gate_data
; // thread holder, interlock bit and waiter bit
368 struct turnstile
*turnstile
; // protected by the interlock bit
371 #define GATE_ILOCK_BIT 0
372 #define GATE_WAITERS_BIT 1
374 #define GATE_ILOCK (1 << GATE_ILOCK_BIT)
375 #define GATE_WAITERS (1 << GATE_WAITERS_BIT)
377 #define gate_ilock(gate) hw_lock_bit((hw_lock_bit_t*)(&(gate)->gate_data), GATE_ILOCK_BIT, LCK_GRP_NULL)
378 #define gate_iunlock(gate) hw_unlock_bit((hw_lock_bit_t*)(&(gate)->gate_data), GATE_ILOCK_BIT)
379 #define gate_has_waiters(state) ((state & GATE_WAITERS) != 0)
380 #define ordered_load_gate(gate) os_atomic_load(&(gate)->gate_data, compiler_acq_rel)
381 #define ordered_store_gate(gate, value) os_atomic_store(&(gate)->gate_data, value, compiler_acq_rel)
383 #define GATE_THREAD_MASK (~(uintptr_t)(GATE_ILOCK | GATE_WAITERS))
384 #define GATE_STATE_TO_THREAD(state) (thread_t)(state & GATE_THREAD_MASK)
385 #define GATE_THREAD_TO_STATE(thread) ((uintptr_t)thread)
388 * Possible gate_wait_result_t values.
390 typedef int gate_wait_result_t
;
391 #define GATE_HANDOFF 0
392 #define GATE_OPENED 1
393 #define GATE_TIMED_OUT 2
394 #define GATE_INTERRUPTED 3
397 * Gate flags used by gate_assert
399 #define GATE_ASSERT_CLOSED 0
400 #define GATE_ASSERT_OPEN 1
401 #define GATE_ASSERT_HELD 2
404 * Gate flags used by gate_handoff
406 #define GATE_HANDOFF_DEFAULT 0
407 #define GATE_HANDOFF_OPEN_IF_NO_WAITERS 1
409 #define GATE_EVENT(gate) ((event_t) gate)
410 #define EVENT_TO_GATE(event) ((gate_t *) event)
413 * Name: decl_lck_rw_gate_data
415 * Description: declares a gate variable with specified storage class.
416 * The gate itself will be stored in this variable and it is the caller's responsibility
417 * to ensure that this variable's memory is going to be accessible by all threads that will use
419 * Every gate function will require a pointer to this variable as parameter. The same pointer should
420 * be used in every thread.
422 * The variable needs to be initialized once with lck_rw_gate_init() and destroyed once with
423 * lck_rw_gate_destroy() when not needed anymore.
425 * The gate will be used in conjunction with a lck_rw_t.
428 * Arg1: storage class.
429 * Arg2: variable name.
431 #define decl_lck_rw_gate_data(class, name) class gate_t name
434 * Name: lck_rw_gate_init
436 * Description: initializes a variable declared with decl_lck_rw_gate_data.
439 * Arg1: lck_rw_t lock used to protect the gate.
440 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
442 extern void lck_rw_gate_init(lck_rw_t
*lock
, gate_t
*gate
);
445 * Name: lck_rw_gate_destroy
447 * Description: destroys a variable previously initialized.
450 * Arg1: lck_rw_t lock used to protect the gate.
451 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
453 extern void lck_rw_gate_destroy(lck_rw_t
*lock
, gate_t
*gate
);
456 * Name: lck_rw_gate_try_close
458 * Description: Tries to close the gate.
459 * In case of success the current thread will be set as
460 * the holder of the gate.
463 * Arg1: lck_rw_t lock used to protect the gate.
464 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
466 * Conditions: Lock must be held. Returns with the lock held.
469 * KERN_SUCCESS in case the gate was successfully closed. The current thread is the new holder
471 * A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
472 * to wake up possible waiters on the gate before returning to userspace.
473 * If the intent is to conditionally probe the gate before waiting, the lock must not be dropped
474 * between the calls to lck_rw_gate_try_close() and lck_rw_gate_wait().
476 * KERN_FAILURE in case the gate was already closed. Will panic if the current thread was already the holder of the gate.
477 * lck_rw_gate_wait() should be called instead if the intent is to unconditionally wait on this gate.
478 * The calls to lck_rw_gate_try_close() and lck_rw_gate_wait() should
479 * be done without dropping the lock that is protecting the gate in between.
481 extern kern_return_t
lck_rw_gate_try_close(lck_rw_t
*lock
, gate_t
*gate
);
484 * Name: lck_rw_gate_close
486 * Description: Closes the gate. The current thread will be set as
487 * the holder of the gate. Will panic if the gate is already closed.
488 * A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
489 * to wake up possible waiters on the gate before returning to userspace.
492 * Arg1: lck_rw_t lock used to protect the gate.
493 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
495 * Conditions: Lock must be held. Returns with the lock held.
496 * The gate must be open.
499 extern void lck_rw_gate_close(lck_rw_t
*lock
, gate_t
*gate
);
503 * Name: lck_rw_gate_open
505 * Description: Opens the gate and wakes up possible waiters.
508 * Arg1: lck_rw_t lock used to protect the gate.
509 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
511 * Conditions: Lock must be held. Returns with the lock held.
512 * The current thread must be the holder of the gate.
515 extern void lck_rw_gate_open(lck_rw_t
*lock
, gate_t
*gate
);
518 * Name: lck_rw_gate_handoff
520 * Description: Tries to transfer the ownership of the gate. The waiter with highest sched
521 * priority will be selected as the new holder of the gate, and woken up,
522 * with the gate remaining in the closed state throughout.
523 * If no waiters are present, the gate will be kept closed and KERN_NOT_WAITING
525 * GATE_HANDOFF_OPEN_IF_NO_WAITERS flag can be used to specify if the gate should be opened in
526 * case no waiters were found.
530 * Arg1: lck_rw_t lock used to protect the gate.
531 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
532 * Arg3: flags - GATE_HANDOFF_DEFAULT or GATE_HANDOFF_OPEN_IF_NO_WAITERS
534 * Conditions: Lock must be held. Returns with the lock held.
535 * The current thread must be the holder of the gate.
538 * KERN_SUCCESS in case one of the waiters became the new holder.
539 * KERN_NOT_WAITING in case there were no waiters.
542 extern kern_return_t
lck_rw_gate_handoff(lck_rw_t
*lock
, gate_t
*gate
, int flags
);
545 * Name: lck_rw_gate_steal
547 * Description: Set the current ownership of the gate. It sets the current thread as the
548 * new holder of the gate.
549 * A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
550 * to wake up possible waiters on the gate before returning to userspace.
551 * NOTE: the previous holder should not call lck_rw_gate_open() or lck_rw_gate_handoff()
556 * Arg1: lck_rw_t lock used to protect the gate.
557 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
559 * Conditions: Lock must be held. Returns with the lock held.
560 * The gate must be closed and the current thread must not already be the holder.
563 extern void lck_rw_gate_steal(lck_rw_t
*lock
, gate_t
*gate
);
566 * Name: lck_rw_gate_wait
568 * Description: Waits for the current thread to become the holder of the gate or for the
569 * gate to become open. An interruptible mode and deadline can be specified
570 * to return earlier from the wait.
573 * Arg1: lck_rw_t lock used to protect the gate.
574 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
575 * Arg3: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_SHARED, LCK_SLEEP_EXCLUSIVE.
576 * Arg3: interruptible flag for wait.
579 * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
580 * Lock will be dropped while waiting.
581 * The gate must be closed.
583 * Returns: Reason why the thread was woken up.
584 * GATE_HANDOFF - the current thread was handed off the ownership of the gate.
585 * A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
586 * to wake up possible waiters on the gate before returning to userspace.
587 * GATE_OPENED - the gate was opened by the holder.
588 * GATE_TIMED_OUT - the thread was woken up by a timeout.
589 * GATE_INTERRUPTED - the thread was interrupted while sleeping.
592 extern gate_wait_result_t
lck_rw_gate_wait(lck_rw_t
*lock
, gate_t
*gate
, lck_sleep_action_t lck_sleep_action
, wait_interrupt_t interruptible
, uint64_t deadline
);
595 * Name: lck_rw_gate_assert
597 * Description: asserts that the gate is in the specified state.
600 * Arg1: lck_rw_t lock used to protect the gate.
601 * Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
602 * Arg3: flags to specified assert type.
603 * GATE_ASSERT_CLOSED - the gate is currently closed
604 * GATE_ASSERT_OPEN - the gate is currently opened
605 * GATE_ASSERT_HELD - the gate is currently closed and the current thread is the holder
607 extern void lck_rw_gate_assert(lck_rw_t
*lock
, gate_t
*gate
, int flags
);
610 * Name: decl_lck_mtx_gate_data
612 * Description: declares a gate variable with specified storage class.
613 * The gate itself will be stored in this variable and it is the caller's responsibility
614 * to ensure that this variable's memory is going to be accessible by all threads that will use
616 * Every gate function will require a pointer to this variable as parameter. The same pointer should
617 * be used in every thread.
619 * The variable needs to be initialized once with lck_mtx_gate_init() and destroyed once with
620 * lck_mtx_gate_destroy() when not needed anymore.
622 * The gate will be used in conjunction with a lck_mtx_t.
625 * Arg1: storage class.
626 * Arg2: variable name.
628 #define decl_lck_mtx_gate_data(class, name) class gate_t name
631 * Name: lck_mtx_gate_init
633 * Description: initializes a variable declared with decl_lck_mtx_gate_data.
636 * Arg1: lck_mtx_t lock used to protect the gate.
637 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
639 extern void lck_mtx_gate_init(lck_mtx_t
*lock
, gate_t
*gate
);
642 * Name: lck_mtx_gate_destroy
644 * Description: destroys a variable previously initialized
647 * Arg1: lck_mtx_t lock used to protect the gate.
648 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
650 extern void lck_mtx_gate_destroy(lck_mtx_t
*lock
, gate_t
*gate
);
653 * Name: lck_mtx_gate_try_close
655 * Description: Tries to close the gate.
656 * In case of success the current thread will be set as
657 * the holder of the gate.
660 * Arg1: lck_mtx_t lock used to protect the gate.
661 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
663 * Conditions: Lock must be held. Returns with the lock held.
666 * KERN_SUCCESS in case the gate was successfully closed. The current thread is the new holder
668 * A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
669 * to wake up possible waiters on the gate before returning to userspace.
670 * If the intent is to conditionally probe the gate before waiting, the lock must not be dropped
671 * between the calls to lck_mtx_gate_try_close() and lck_mtx_gate_wait().
673 * KERN_FAILURE in case the gate was already closed. Will panic if the current thread was already the holder of the gate.
674 * lck_mtx_gate_wait() should be called instead if the intent is to unconditionally wait on this gate.
675 * The calls to lck_mtx_gate_try_close() and lck_mtx_gate_wait() should
676 * be done without dropping the lock that is protecting the gate in between.
678 extern kern_return_t
lck_mtx_gate_try_close(lck_mtx_t
*lock
, gate_t
*gate
);
681 * Name: lck_mtx_gate_close
683 * Description: Closes the gate. The current thread will be set as
684 * the holder of the gate. Will panic if the gate is already closed.
685 * A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
686 * to wake up possible waiters on the gate before returning to userspace.
689 * Arg1: lck_mtx_t lock used to protect the gate.
690 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
692 * Conditions: Lock must be held. Returns with the lock held.
693 * The gate must be open.
696 extern void lck_mtx_gate_close(lck_mtx_t
*lock
, gate_t
*gate
);
699 * Name: lck_mtx_gate_open
701 * Description: Opens of the gate and wakes up possible waiters.
704 * Arg1: lck_mtx_t lock used to protect the gate.
705 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
707 * Conditions: Lock must be held. Returns with the lock held.
708 * The current thread must be the holder of the gate.
711 extern void lck_mtx_gate_open(lck_mtx_t
*lock
, gate_t
*gate
);
714 * Name: lck_mtx_gate_handoff
716 * Description: Set the current ownership of the gate. The waiter with highest sched
717 * priority will be selected as the new holder of the gate, and woken up,
718 * with the gate remaining in the closed state throughout.
719 * If no waiters are present, the gate will be kept closed and KERN_NOT_WAITING
721 * OPEN_ON_FAILURE flag can be used to specify if the gate should be opened in
722 * case no waiters were found.
726 * Arg1: lck_mtx_t lock used to protect the gate.
727 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
728 * Arg3: flags - GATE_NO_FALGS or OPEN_ON_FAILURE
730 * Conditions: Lock must be held. Returns with the lock held.
731 * The current thread must be the holder of the gate.
734 * KERN_SUCCESS in case one of the waiters became the new holder.
735 * KERN_NOT_WAITING in case there were no waiters.
738 extern kern_return_t
lck_mtx_gate_handoff(lck_mtx_t
*lock
, gate_t
*gate
, int flags
);
741 * Name: lck_mtx_gate_steal
743 * Description: Steals the ownership of the gate. It sets the current thread as the
744 * new holder of the gate.
745 * A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
746 * to wake up possible waiters on the gate before returning to userspace.
747 * NOTE: the previous holder should not call lck_mtx_gate_open() or lck_mtx_gate_handoff()
752 * Arg1: lck_mtx_t lock used to protect the gate.
753 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
755 * Conditions: Lock must be held. Returns with the lock held.
756 * The gate must be closed and the current thread must not already be the holder.
759 extern void lck_mtx_gate_steal(lck_mtx_t
*lock
, gate_t
*gate
);
762 * Name: lck_mtx_gate_wait
764 * Description: Waits for the current thread to become the holder of the gate or for the
765 * gate to become open. An interruptible mode and deadline can be specified
766 * to return earlier from the wait.
769 * Arg1: lck_mtx_t lock used to protect the gate.
770 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
771 * Arg3: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK, LCK_SLEEP_SPIN, LCK_SLEEP_SPIN_ALWAYS.
772 * Arg3: interruptible flag for wait.
775 * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
776 * Lock will be dropped while waiting.
777 * The gate must be closed.
779 * Returns: Reason why the thread was woken up.
780 * GATE_HANDOFF - the current thread was handed off the ownership of the gate.
781 * A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
782 * to wake up possible waiters on the gate before returning to userspace.
783 * GATE_OPENED - the gate was opened by the holder.
784 * GATE_TIMED_OUT - the thread was woken up by a timeout.
785 * GATE_INTERRUPTED - the thread was interrupted while sleeping.
788 extern gate_wait_result_t
lck_mtx_gate_wait(lck_mtx_t
*lock
, gate_t
*gate
, lck_sleep_action_t lck_sleep_action
, wait_interrupt_t interruptible
, uint64_t deadline
);
791 * Name: lck_mtx_gate_assert
793 * Description: asserts that the gate is in the specified state.
796 * Arg1: lck_mtx_t lock used to protect the gate.
797 * Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
798 * Arg3: flags to specified assert type.
799 * GATE_ASSERT_CLOSED - the gate is currently closed
800 * GATE_ASSERT_OPEN - the gate is currently opened
801 * GATE_ASSERT_HELD - the gate is currently closed and the current thread is the holder
803 extern void lck_mtx_gate_assert(lck_mtx_t
*lock
, gate_t
*gate
, int flags
);
806 #endif //KERNEL_PRIVATE
808 #if DEVELOPMENT || DEBUG
809 #define FULL_CONTENDED 0
810 #define HALF_CONTENDED 1
811 #define MAX_CONDENDED 2
813 extern void erase_all_test_mtx_stats(void);
814 extern int get_test_mtx_stats_string(char* buffer
, int buffer_size
);
815 extern void lck_mtx_test_init(void);
816 extern void lck_mtx_test_lock(void);
817 extern void lck_mtx_test_unlock(void);
818 extern int lck_mtx_test_mtx_uncontended(int iter
, char* buffer
, int buffer_size
);
819 extern int lck_mtx_test_mtx_contended(int iter
, char* buffer
, int buffer_size
, int type
);
820 extern int lck_mtx_test_mtx_uncontended_loop_time(int iter
, char* buffer
, int buffer_size
);
821 extern int lck_mtx_test_mtx_contended_loop_time(int iter
, char* buffer
, int buffer_size
, int type
);
823 #ifdef KERNEL_PRIVATE
825 extern boolean_t
lck_mtx_try_lock(
828 extern void mutex_pause(uint32_t);
830 extern void lck_mtx_yield(
833 extern boolean_t
lck_mtx_try_lock_spin(
836 extern void lck_mtx_lock_spin(
839 extern boolean_t
kdp_lck_mtx_lock_spin_is_acquired(
842 extern void lck_mtx_convert_spin(
845 extern void lck_mtx_lock_spin_always(
848 extern boolean_t
lck_mtx_try_lock_spin_always(
851 #define lck_mtx_unlock_always(l) lck_mtx_unlock(l)
853 extern void lck_spin_assert(
857 extern boolean_t
kdp_lck_rw_lock_is_acquired_exclusive(
860 #endif /* KERNEL_PRIVATE */
862 extern void lck_mtx_assert(
867 #define LCK_MTX_ASSERT(lck, type) lck_mtx_assert((lck),(type))
868 #define LCK_SPIN_ASSERT(lck, type) lck_spin_assert((lck),(type))
869 #define LCK_RW_ASSERT(lck, type) lck_rw_assert((lck),(type))
870 #else /* MACH_ASSERT */
871 #define LCK_MTX_ASSERT(lck, type)
872 #define LCK_SPIN_ASSERT(lck, type)
873 #define LCK_RW_ASSERT(lck, type)
874 #endif /* MACH_ASSERT */
877 #define LCK_MTX_ASSERT_DEBUG(lck, type) lck_mtx_assert((lck),(type))
878 #define LCK_SPIN_ASSERT_DEBUG(lck, type) lck_spin_assert((lck),(type))
879 #define LCK_RW_ASSERT_DEBUG(lck, type) lck_rw_assert((lck),(type))
881 #define LCK_MTX_ASSERT_DEBUG(lck, type)
882 #define LCK_SPIN_ASSERT_DEBUG(lck, type)
883 #define LCK_RW_ASSERT_DEBUG(lck, type)
888 #define LCK_ASSERT_OWNED 1
889 #define LCK_ASSERT_NOTOWNED 2
891 #define LCK_MTX_ASSERT_OWNED LCK_ASSERT_OWNED
892 #define LCK_MTX_ASSERT_NOTOWNED LCK_ASSERT_NOTOWNED
894 #ifdef MACH_KERNEL_PRIVATE
896 extern void lck_mtx_lock_wait(
899 struct turnstile
**ts
);
901 extern int lck_mtx_lock_acquire(
903 struct turnstile
*ts
);
905 extern boolean_t
lck_mtx_unlock_wakeup(
909 extern boolean_t
lck_mtx_ilk_unlock(
912 extern boolean_t
lck_mtx_ilk_try_lock(
915 extern void lck_mtx_wakeup_adjust_pri(thread_t thread
, integer_t priority
);
919 #define decl_lck_rw_data(class, name) class lck_rw_t name
921 typedef unsigned int lck_rw_type_t
;
923 #define LCK_RW_TYPE_SHARED 0x01
924 #define LCK_RW_TYPE_EXCLUSIVE 0x02
926 #ifdef XNU_KERNEL_PRIVATE
927 #define LCK_RW_ASSERT_SHARED 0x01
928 #define LCK_RW_ASSERT_EXCLUSIVE 0x02
929 #define LCK_RW_ASSERT_HELD 0x03
930 #define LCK_RW_ASSERT_NOTHELD 0x04
935 extern lck_rw_t
*lck_rw_alloc_init(
939 extern void lck_rw_init(
944 extern void lck_rw_lock(
946 lck_rw_type_t lck_rw_type
);
948 extern void lck_rw_unlock(
950 lck_rw_type_t lck_rw_type
);
952 extern void lck_rw_lock_shared(
955 extern void lck_rw_unlock_shared(
958 extern boolean_t
lck_rw_lock_yield_shared(
960 boolean_t force_yield
);
962 extern void lck_rw_lock_exclusive(
965 extern void lck_rw_unlock_exclusive(
968 #ifdef XNU_KERNEL_PRIVATE
971 * read-write locks do not have a concept of ownership, so lck_rw_assert()
972 * merely asserts that someone is holding the lock, not necessarily the caller.
974 extern void lck_rw_assert(
978 extern void lck_rw_clear_promotion(thread_t thread
, uintptr_t trace_obj
);
979 extern void lck_rw_set_promotion_locked(thread_t thread
);
981 uintptr_t unslide_for_kdebug(void* object
);
982 #endif /* XNU_KERNEL_PRIVATE */
984 #ifdef KERNEL_PRIVATE
986 extern lck_rw_type_t
lck_rw_done(
990 extern void lck_rw_destroy(
994 extern void lck_rw_free(
998 extern wait_result_t
lck_rw_sleep(
1000 lck_sleep_action_t lck_sleep_action
,
1002 wait_interrupt_t interruptible
);
1004 extern wait_result_t
lck_rw_sleep_deadline(
1006 lck_sleep_action_t lck_sleep_action
,
1008 wait_interrupt_t interruptible
,
1011 extern boolean_t
lck_rw_lock_shared_to_exclusive(
1014 extern void lck_rw_lock_exclusive_to_shared(
1017 extern boolean_t
lck_rw_try_lock(
1019 lck_rw_type_t lck_rw_type
);
1021 #ifdef KERNEL_PRIVATE
1023 extern boolean_t
lck_rw_try_lock_shared(
1026 extern boolean_t
lck_rw_try_lock_exclusive(
1032 #endif /* _KERN_LOCKS_H_ */