+ if (thread->sched_flags & TH_SFLAG_RW_PROMOTED) {
+ sched_thread_unpromote_reason(thread, TH_SFLAG_RW_PROMOTED, trace_obj);
+ }
+
+ thread_unlock(thread);
+ splx(s);
+}
+
+/*
+ * Callout from context switch if the thread goes
+ * off core with a positive rwlock_count
+ *
+ * Called at splsched with the thread locked
+ */
+void
+lck_rw_set_promotion_locked(thread_t thread)
+{
+ if (LcksOpts & disLkRWPrio) {
+ return;
+ }
+
+ assert(thread->rwlock_count > 0);
+
+ if (!(thread->sched_flags & TH_SFLAG_RW_PROMOTED)) {
+ sched_thread_promote_reason(thread, TH_SFLAG_RW_PROMOTED, 0);
+ }
+}
+
+kern_return_t
+host_lockgroup_info(
+ host_t host,
+ lockgroup_info_array_t *lockgroup_infop,
+ mach_msg_type_number_t *lockgroup_infoCntp)
+{
+ lockgroup_info_t *lockgroup_info_base;
+ lockgroup_info_t *lockgroup_info;
+ vm_offset_t lockgroup_info_addr;
+ vm_size_t lockgroup_info_size;
+ vm_size_t lockgroup_info_vmsize;
+ lck_grp_t *lck_grp;
+ unsigned int i;
+ vm_map_copy_t copy;
+ kern_return_t kr;
+
+ if (host == HOST_NULL) {
+ return KERN_INVALID_HOST;
+ }
+
+ lck_mtx_lock(&lck_grp_lock);
+
+ lockgroup_info_size = lck_grp_cnt * sizeof(*lockgroup_info);
+ lockgroup_info_vmsize = round_page(lockgroup_info_size);
+ kr = kmem_alloc_pageable(ipc_kernel_map,
+ &lockgroup_info_addr, lockgroup_info_vmsize, VM_KERN_MEMORY_IPC);
+ if (kr != KERN_SUCCESS) {
+ lck_mtx_unlock(&lck_grp_lock);
+ return kr;
+ }
+
+ lockgroup_info_base = (lockgroup_info_t *) lockgroup_info_addr;
+ lck_grp = (lck_grp_t *)queue_first(&lck_grp_queue);
+ lockgroup_info = lockgroup_info_base;
+
+ for (i = 0; i < lck_grp_cnt; i++) {
+ lockgroup_info->lock_spin_cnt = lck_grp->lck_grp_spincnt;
+ lockgroup_info->lock_rw_cnt = lck_grp->lck_grp_rwcnt;
+ lockgroup_info->lock_mtx_cnt = lck_grp->lck_grp_mtxcnt;
+
+#if LOCK_STATS
+ lockgroup_info->lock_spin_held_cnt = lck_grp->lck_grp_stats.lgss_spin_held.lgs_count;
+ lockgroup_info->lock_spin_miss_cnt = lck_grp->lck_grp_stats.lgss_spin_miss.lgs_count;
+#endif /* LOCK_STATS */
+
+ // Historically on x86, held was used for "direct wait" and util for "held"
+ lockgroup_info->lock_mtx_util_cnt = lck_grp->lck_grp_stats.lgss_mtx_held.lgs_count;
+ lockgroup_info->lock_mtx_held_cnt = lck_grp->lck_grp_stats.lgss_mtx_direct_wait.lgs_count;
+ lockgroup_info->lock_mtx_miss_cnt = lck_grp->lck_grp_stats.lgss_mtx_miss.lgs_count;
+ lockgroup_info->lock_mtx_wait_cnt = lck_grp->lck_grp_stats.lgss_mtx_wait.lgs_count;
+
+ (void) strncpy(lockgroup_info->lockgroup_name, lck_grp->lck_grp_name, LOCKGROUP_MAX_NAME);
+
+ lck_grp = (lck_grp_t *)(queue_next((queue_entry_t)(lck_grp)));
+ lockgroup_info++;
+ }
+
+ *lockgroup_infoCntp = lck_grp_cnt;
+ lck_mtx_unlock(&lck_grp_lock);
+
+ if (lockgroup_info_size != lockgroup_info_vmsize) {
+ bzero((char *)lockgroup_info, lockgroup_info_vmsize - lockgroup_info_size);
+ }
+
+ kr = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)lockgroup_info_addr,
+ (vm_map_size_t)lockgroup_info_size, TRUE, ©);
+ assert(kr == KERN_SUCCESS);
+
+ *lockgroup_infop = (lockgroup_info_t *) copy;
+
+ return KERN_SUCCESS;
+}
+
+/*
+ * sleep_with_inheritor and wakeup_with_inheritor KPI
+ *
+ * Functions that allow to sleep on an event and use turnstile to propagate the priority of the sleeping threads to
+ * the latest thread specified as inheritor.
+ *
+ * The inheritor management is delegated to the caller, the caller needs to store a thread identifier to provide to this functions to specified upon whom
+ * direct the push. The inheritor cannot run in user space while holding a push from an event. Therefore is the caller responsibility to call a
+ * wakeup_with_inheritor from inheritor before running in userspace or specify another inheritor before letting the old inheritor run in userspace.
+ *
+ * sleep_with_inheritor requires to hold a locking primitive while invoked, but wakeup_with_inheritor and change_sleep_inheritor don't require it.
+ *
+ * Turnstile requires a non blocking primitive as interlock to synchronize the turnstile data structure manipulation, threfore sleep_with_inheritor, change_sleep_inheritor and
+ * wakeup_with_inheritor will require the same interlock to manipulate turnstiles.
+ * If sleep_with_inheritor is associated with a locking primitive that can block (like lck_mtx_t or lck_rw_t), an handoff to a non blocking primitive is required before
+ * invoking any turnstile operation.
+ *
+ * All functions will save the turnstile associated with the event on the turnstile kernel hash table and will use the the turnstile kernel hash table bucket
+ * spinlock as the turnstile interlock. Because we do not want to hold interrupt disabled while holding the bucket interlock a new turnstile kernel hash table
+ * is instantiated for this KPI to manage the hash without interrupt disabled.
+ * Also:
+ * - all events on the system that hash on the same bucket will contend on the same spinlock.
+ * - every event will have a dedicated wait_queue.
+ *
+ * Different locking primitives can be associated with sleep_with_inheritor as long as the primitive_lock() and primitive_unlock() functions are provided to
+ * sleep_with_inheritor_turnstile to perform the handoff with the bucket spinlock.
+ */
+
+kern_return_t
+wakeup_with_inheritor_and_turnstile_type(event_t event, turnstile_type_t type, wait_result_t result, bool wake_one, lck_wake_action_t action, thread_t *thread_wokenup)
+{
+ uint32_t index;
+ struct turnstile *ts = NULL;
+ kern_return_t ret = KERN_NOT_WAITING;
+ int priority;
+ thread_t wokeup;
+
+ /*
+ * the hash bucket spinlock is used as turnstile interlock
+ */
+ turnstile_hash_bucket_lock((uintptr_t)event, &index, type);
+
+ ts = turnstile_prepare((uintptr_t)event, NULL, TURNSTILE_NULL, type);
+
+ if (wake_one) {
+ if (action == LCK_WAKE_DEFAULT) {
+ priority = WAITQ_PROMOTE_ON_WAKE;
+ } else {
+ assert(action == LCK_WAKE_DO_NOT_TRANSFER_PUSH);
+ priority = WAITQ_ALL_PRIORITIES;
+ }
+
+ /*
+ * WAITQ_PROMOTE_ON_WAKE will call turnstile_update_inheritor
+ * if it finds a thread
+ */
+ wokeup = waitq_wakeup64_identify(&ts->ts_waitq, CAST_EVENT64_T(event), result, priority);
+ if (wokeup != NULL) {
+ if (thread_wokenup != NULL) {
+ *thread_wokenup = wokeup;
+ } else {
+ thread_deallocate_safe(wokeup);
+ }
+ ret = KERN_SUCCESS;
+ if (action == LCK_WAKE_DO_NOT_TRANSFER_PUSH) {
+ goto complete;
+ }
+ } else {
+ if (thread_wokenup != NULL) {
+ *thread_wokenup = NULL;
+ }
+ turnstile_update_inheritor(ts, TURNSTILE_INHERITOR_NULL, TURNSTILE_IMMEDIATE_UPDATE);
+ ret = KERN_NOT_WAITING;
+ }
+ } else {
+ ret = waitq_wakeup64_all(&ts->ts_waitq, CAST_EVENT64_T(event), result, WAITQ_ALL_PRIORITIES);
+ turnstile_update_inheritor(ts, TURNSTILE_INHERITOR_NULL, TURNSTILE_IMMEDIATE_UPDATE);
+ }
+
+ /*
+ * turnstile_update_inheritor_complete could be called while holding the interlock.
+ * In this case the new inheritor or is null, or is a thread that is just been woken up
+ * and have not blocked because it is racing with the same interlock used here
+ * after the wait.
+ * So there is no chain to update for the new inheritor.
+ *
+ * However unless the current thread is the old inheritor,
+ * old inheritor can be blocked and requires a chain update.
+ *
+ * The chain should be short because kernel turnstiles cannot have user turnstiles
+ * chained after them.
+ *
+ * We can anyway optimize this by asking turnstile to tell us
+ * if old inheritor needs an update and drop the lock
+ * just in that case.
+ */
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
+
+ turnstile_hash_bucket_lock((uintptr_t)NULL, &index, type);
+
+complete:
+ turnstile_complete((uintptr_t)event, NULL, NULL, type);
+
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ turnstile_cleanup();
+
+ return ret;
+}
+
+static wait_result_t
+sleep_with_inheritor_and_turnstile_type(event_t event,
+ thread_t inheritor,
+ wait_interrupt_t interruptible,
+ uint64_t deadline,
+ turnstile_type_t type,
+ void (^primitive_lock)(void),
+ void (^primitive_unlock)(void))
+{
+ wait_result_t ret;
+ uint32_t index;
+ struct turnstile *ts = NULL;
+
+ /*
+ * the hash bucket spinlock is used as turnstile interlock,
+ * lock it before releasing the primitive lock
+ */
+ turnstile_hash_bucket_lock((uintptr_t)event, &index, type);
+
+ primitive_unlock();
+
+ ts = turnstile_prepare((uintptr_t)event, NULL, TURNSTILE_NULL, type);
+
+ thread_set_pending_block_hint(current_thread(), kThreadWaitSleepWithInheritor);
+ /*
+ * We need TURNSTILE_DELAYED_UPDATE because we will call
+ * waitq_assert_wait64 after.
+ */
+ turnstile_update_inheritor(ts, inheritor, (TURNSTILE_DELAYED_UPDATE | TURNSTILE_INHERITOR_THREAD));
+
+ ret = waitq_assert_wait64(&ts->ts_waitq, CAST_EVENT64_T(event), interruptible, deadline);
+
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ /*
+ * Update new and old inheritor chains outside the interlock;
+ */
+ turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
+
+ if (ret == THREAD_WAITING) {
+ ret = thread_block(THREAD_CONTINUE_NULL);
+ }
+
+ turnstile_hash_bucket_lock((uintptr_t)NULL, &index, type);
+
+ turnstile_complete((uintptr_t)event, NULL, NULL, type);
+
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ turnstile_cleanup();
+
+ primitive_lock();
+
+ return ret;
+}
+
+kern_return_t
+change_sleep_inheritor_and_turnstile_type(event_t event,
+ thread_t inheritor,
+ turnstile_type_t type)
+{
+ uint32_t index;
+ struct turnstile *ts = NULL;
+ kern_return_t ret = KERN_SUCCESS;
+ /*
+ * the hash bucket spinlock is used as turnstile interlock
+ */
+ turnstile_hash_bucket_lock((uintptr_t)event, &index, type);
+
+ ts = turnstile_prepare((uintptr_t)event, NULL, TURNSTILE_NULL, type);
+
+ if (!turnstile_has_waiters(ts)) {
+ ret = KERN_NOT_WAITING;
+ }
+
+ /*
+ * We will not call an assert_wait later so use TURNSTILE_IMMEDIATE_UPDATE
+ */
+ turnstile_update_inheritor(ts, inheritor, (TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD));
+
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ /*
+ * update the chains outside the interlock
+ */
+ turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
+
+ turnstile_hash_bucket_lock((uintptr_t)NULL, &index, type);
+
+ turnstile_complete((uintptr_t)event, NULL, NULL, type);
+
+ turnstile_hash_bucket_unlock((uintptr_t)NULL, &index, type, 0);
+
+ turnstile_cleanup();
+
+ return ret;
+}
+
+typedef void (^void_block_void)(void);
+
+/*
+ * sleep_with_inheritor functions with lck_mtx_t as locking primitive.
+ */
+
+wait_result_t
+lck_mtx_sleep_with_inheritor_and_turnstile_type(lck_mtx_t *lock, lck_sleep_action_t lck_sleep_action, event_t event, thread_t inheritor, wait_interrupt_t interruptible, uint64_t deadline, turnstile_type_t type)
+{
+ LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+ if (lck_sleep_action & LCK_SLEEP_UNLOCK) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{;},
+ ^{lck_mtx_unlock(lock);});
+ } else if (lck_sleep_action & LCK_SLEEP_SPIN) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_mtx_lock_spin(lock);},
+ ^{lck_mtx_unlock(lock);});
+ } else if (lck_sleep_action & LCK_SLEEP_SPIN_ALWAYS) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_mtx_lock_spin_always(lock);},
+ ^{lck_mtx_unlock(lock);});
+ } else {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_mtx_lock(lock);},
+ ^{lck_mtx_unlock(lock);});
+ }
+}
+
+/*
+ * Name: lck_spin_sleep_with_inheritor
+ *
+ * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
+ * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
+ * be directed to the inheritor specified.
+ * An interruptible mode and deadline can be specified to return earlier from the wait.
+ *
+ * Args:
+ * 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.
+ * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK.
+ * Arg3: event to wait on.
+ * Arg4: thread to propagate the event push to.
+ * Arg5: interruptible flag for wait.
+ * Arg6: deadline for wait.
+ *
+ * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
+ * Lock will be dropped while waiting.
+ * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
+ * wakeup for the event is called.
+ *
+ * Returns: result of the wait.
+ */
+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)
+{
+ if (lck_sleep_action & LCK_SLEEP_UNLOCK) {
+ return sleep_with_inheritor_and_turnstile_type(event, inheritor,
+ interruptible, deadline, TURNSTILE_SLEEP_INHERITOR,
+ ^{}, ^{ lck_spin_unlock(lock); });
+ } else {
+ return sleep_with_inheritor_and_turnstile_type(event, inheritor,
+ interruptible, deadline, TURNSTILE_SLEEP_INHERITOR,
+ ^{ lck_spin_lock(lock); }, ^{ lck_spin_unlock(lock); });
+ }
+}
+
+/*
+ * Name: lck_mtx_sleep_with_inheritor
+ *
+ * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
+ * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
+ * be directed to the inheritor specified.
+ * An interruptible mode and deadline can be specified to return earlier from the wait.
+ *
+ * Args:
+ * 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.
+ * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK, LCK_SLEEP_SPIN, LCK_SLEEP_SPIN_ALWAYS.
+ * Arg3: event to wait on.
+ * Arg4: thread to propagate the event push to.
+ * Arg5: interruptible flag for wait.
+ * Arg6: deadline for wait.
+ *
+ * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
+ * Lock will be dropped while waiting.
+ * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
+ * wakeup for the event is called.
+ *
+ * Returns: result of the wait.
+ */
+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)
+{
+ return lck_mtx_sleep_with_inheritor_and_turnstile_type(lock, lck_sleep_action, event, inheritor, interruptible, deadline, TURNSTILE_SLEEP_INHERITOR);
+}
+
+/*
+ * sleep_with_inheritor functions with lck_rw_t as locking primitive.
+ */
+
+wait_result_t
+lck_rw_sleep_with_inheritor_and_turnstile_type(lck_rw_t *lock, lck_sleep_action_t lck_sleep_action, event_t event, thread_t inheritor, wait_interrupt_t interruptible, uint64_t deadline, turnstile_type_t type)
+{
+ __block lck_rw_type_t lck_rw_type = LCK_RW_TYPE_EXCLUSIVE;
+
+ LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+ if (lck_sleep_action & LCK_SLEEP_UNLOCK) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{;},
+ ^{lck_rw_type = lck_rw_done(lock);});
+ } else if (!(lck_sleep_action & (LCK_SLEEP_SHARED | LCK_SLEEP_EXCLUSIVE))) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_rw_lock(lock, lck_rw_type);},
+ ^{lck_rw_type = lck_rw_done(lock);});
+ } else if (lck_sleep_action & LCK_SLEEP_EXCLUSIVE) {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_rw_lock_exclusive(lock);},
+ ^{lck_rw_type = lck_rw_done(lock);});
+ } else {
+ return sleep_with_inheritor_and_turnstile_type(event,
+ inheritor,
+ interruptible,
+ deadline,
+ type,
+ ^{lck_rw_lock_shared(lock);},
+ ^{lck_rw_type = lck_rw_done(lock);});
+ }
+}
+
+/*
+ * Name: lck_rw_sleep_with_inheritor
+ *
+ * Description: deschedule the current thread and wait on the waitq associated with event to be woken up.
+ * While waiting, the sched priority of the waiting thread will contribute to the push of the event that will
+ * be directed to the inheritor specified.
+ * An interruptible mode and deadline can be specified to return earlier from the wait.
+ *
+ * Args:
+ * 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.
+ * Arg2: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_SHARED, LCK_SLEEP_EXCLUSIVE.
+ * Arg3: event to wait on.
+ * Arg4: thread to propagate the event push to.
+ * Arg5: interruptible flag for wait.
+ * Arg6: deadline for wait.
+ *
+ * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
+ * Lock will be dropped while waiting.
+ * The inheritor specified cannot run in user space until another inheritor is specified for the event or a
+ * wakeup for the event is called.
+ *
+ * Returns: result of the wait.
+ */
+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)
+{
+ return lck_rw_sleep_with_inheritor_and_turnstile_type(lock, lck_sleep_action, event, inheritor, interruptible, deadline, TURNSTILE_SLEEP_INHERITOR);
+}
+
+/*
+ * wakeup_with_inheritor functions are independent from the locking primitive.
+ */
+
+/*
+ * Name: wakeup_one_with_inheritor
+ *
+ * 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.
+ * The push for the event will be transferred from the last inheritor to the woken up thread if LCK_WAKE_DEFAULT is specified.
+ * If LCK_WAKE_DO_NOT_TRANSFER_PUSH is specified the push will not be transferred.
+ *
+ * Args:
+ * Arg1: event to wake from.
+ * Arg2: wait result to pass to the woken up thread.
+ * Arg3: wake flag. LCK_WAKE_DEFAULT or LCK_WAKE_DO_NOT_TRANSFER_PUSH.
+ * Arg4: pointer for storing the thread wokenup.
+ *
+ * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
+ *
+ * Conditions: The new inheritor wokenup cannot run in user space until another inheritor is specified for the event or a
+ * wakeup for the event is called.
+ * A reference for the wokenup thread is acquired.
+ * NOTE: this cannot be called from interrupt context.
+ */
+kern_return_t
+wakeup_one_with_inheritor(event_t event, wait_result_t result, lck_wake_action_t action, thread_t *thread_wokenup)
+{
+ return wakeup_with_inheritor_and_turnstile_type(event,
+ TURNSTILE_SLEEP_INHERITOR,
+ result,
+ TRUE,
+ action,
+ thread_wokenup);
+}
+
+/*
+ * Name: wakeup_all_with_inheritor
+ *
+ * Description: wake up all waiters waiting for event. The old inheritor will lose the push.
+ *
+ * Args:
+ * Arg1: event to wake from.
+ * Arg2: wait result to pass to the woken up threads.
+ *
+ * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
+ *
+ * Conditions: NOTE: this cannot be called from interrupt context.
+ */
+kern_return_t
+wakeup_all_with_inheritor(event_t event, wait_result_t result)
+{
+ return wakeup_with_inheritor_and_turnstile_type(event,
+ TURNSTILE_SLEEP_INHERITOR,
+ result,
+ FALSE,
+ 0,
+ NULL);
+}
+
+/*
+ * change_sleep_inheritor is independent from the locking primitive.
+ */
+
+/*
+ * Name: change_sleep_inheritor
+ *
+ * Description: Redirect the push of the waiting threads of event to the new inheritor specified.
+ *
+ * Args:
+ * Arg1: event to redirect the push.
+ * Arg2: new inheritor for event.
+ *
+ * Returns: KERN_NOT_WAITING if no threads were waiting, KERN_SUCCESS otherwise.
+ *
+ * Conditions: In case of success, the new inheritor cannot run in user space until another inheritor is specified for the event or a
+ * wakeup for the event is called.
+ * NOTE: this cannot be called from interrupt context.
+ */
+kern_return_t
+change_sleep_inheritor(event_t event, thread_t inheritor)
+{
+ return change_sleep_inheritor_and_turnstile_type(event,
+ inheritor,
+ TURNSTILE_SLEEP_INHERITOR);
+}
+
+void
+kdp_sleep_with_inheritor_find_owner(struct waitq * waitq, __unused event64_t event, thread_waitinfo_t * waitinfo)
+{
+ assert(waitinfo->wait_type == kThreadWaitSleepWithInheritor);
+ assert(waitq_is_turnstile_queue(waitq));
+ waitinfo->owner = 0;
+ waitinfo->context = 0;
+
+ if (waitq_held(waitq)) {
+ return;
+ }
+
+ struct turnstile *turnstile = waitq_to_turnstile(waitq);
+ assert(turnstile->ts_inheritor_flags & TURNSTILE_INHERITOR_THREAD);
+ waitinfo->owner = thread_tid(turnstile->ts_inheritor);
+}
+
+typedef void (*void_func_void)(void);
+
+static kern_return_t
+gate_try_close(gate_t *gate)
+{
+ uintptr_t state;
+ thread_t holder;
+ kern_return_t ret;
+ __assert_only bool waiters;
+ thread_t thread = current_thread();
+
+ if (os_atomic_cmpxchg(&gate->gate_data, 0, GATE_THREAD_TO_STATE(thread), acquire)) {
+ return KERN_SUCCESS;
+ }
+
+ gate_ilock(gate);
+ state = ordered_load_gate(gate);
+ holder = GATE_STATE_TO_THREAD(state);
+
+ if (holder == NULL) {
+ waiters = gate_has_waiters(state);
+ assert(waiters == FALSE);
+
+ state = GATE_THREAD_TO_STATE(current_thread());
+ state |= GATE_ILOCK;
+ ordered_store_gate(gate, state);
+ ret = KERN_SUCCESS;
+ } else {
+ if (holder == current_thread()) {
+ panic("Trying to close a gate already owned by current thread %p", current_thread());
+ }
+ ret = KERN_FAILURE;
+ }
+
+ gate_iunlock(gate);
+ return ret;
+}
+
+static void
+gate_close(gate_t* gate)
+{
+ uintptr_t state;
+ thread_t holder;
+ __assert_only bool waiters;
+ thread_t thread = current_thread();
+
+ if (os_atomic_cmpxchg(&gate->gate_data, 0, GATE_THREAD_TO_STATE(thread), acquire)) {
+ return;
+ }
+
+ gate_ilock(gate);
+ state = ordered_load_gate(gate);
+ holder = GATE_STATE_TO_THREAD(state);
+
+ if (holder != NULL) {
+ panic("Closing a gate already owned by %p from current thread %p", holder, current_thread());
+ }
+
+ waiters = gate_has_waiters(state);
+ assert(waiters == FALSE);
+
+ state = GATE_THREAD_TO_STATE(thread);
+ state |= GATE_ILOCK;
+ ordered_store_gate(gate, state);
+
+ gate_iunlock(gate);
+}
+
+static void
+gate_open_turnstile(gate_t *gate)
+{
+ struct turnstile *ts = NULL;
+
+ ts = turnstile_prepare((uintptr_t)gate, &gate->turnstile, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
+ waitq_wakeup64_all(&ts->ts_waitq, CAST_EVENT64_T(GATE_EVENT(gate)), THREAD_AWAKENED, WAITQ_ALL_PRIORITIES);
+ turnstile_update_inheritor(ts, TURNSTILE_INHERITOR_NULL, TURNSTILE_IMMEDIATE_UPDATE);
+ turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+ turnstile_complete((uintptr_t)gate, &gate->turnstile, NULL, TURNSTILE_KERNEL_MUTEX);
+ /*
+ * We can do the cleanup while holding the interlock.
+ * It is ok because:
+ * 1. current_thread is the previous inheritor and it is running
+ * 2. new inheritor is NULL.
+ * => No chain of turnstiles needs to be updated.
+ */
+ turnstile_cleanup();