]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/locks.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / kern / locks.c
index 04106709bb2315ba99b6ef74a3b6e4aab8953cb9..78aee369cc6eb3b890350fec66e17eff3b9d7f5f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  *
@@ -54,7 +54,6 @@
  * the rights to redistribute these changes.
  */
 
-#define ATOMIC_PRIVATE 1
 #define LOCK_PRIVATE 1
 
 #include <mach_ldebug.h>
 #define ALIGN_TEST(p, t) do{}while(0)
 #endif
 
-/* Silence the volatile to _Atomic cast warning */
-#define ATOMIC_CAST(t, p) ((_Atomic t*)(uintptr_t)(p))
-
-/* Enforce program order of loads and stores. */
-#define ordered_load(target, type) \
-               __c11_atomic_load((_Atomic type *)(target), memory_order_relaxed)
-#define ordered_store(target, type, value) \
-               __c11_atomic_store((_Atomic type *)(target), value, memory_order_relaxed)
-
-#define ordered_load_hw(lock)                   ordered_load(&(lock)->lock_data, uintptr_t)
-#define ordered_store_hw(lock, value)   ordered_store(&(lock)->lock_data, uintptr_t, (value))
-
 #define NOINLINE                __attribute__((noinline))
 
+#define ordered_load_hw(lock)          os_atomic_load(&(lock)->lock_data, compiler_acq_rel)
+#define ordered_store_hw(lock, value)  os_atomic_store(&(lock)->lock_data, (value), compiler_acq_rel)
+
 
 queue_head_t     lck_grp_queue;
 unsigned int     lck_grp_cnt;
 
-decl_lck_mtx_data(, lck_grp_lock)
+decl_lck_mtx_data(, lck_grp_lock);
 static lck_mtx_ext_t lck_grp_lock_ext;
 
 SECURITY_READ_ONLY_LATE(boolean_t) spinlock_timeout_panic = TRUE;
@@ -175,7 +165,7 @@ lck_mod_init(
                LockCompatGroup.lck_grp_attr |= LCK_GRP_ATTR_TIME_STAT;
        }
 
-       LockCompatGroup.lck_grp_refcnt = 1;
+       os_ref_init(&LockCompatGroup.lck_grp_refcnt, NULL);
 
        enqueue_tail(&lck_grp_queue, (queue_entry_t)&LockCompatGroup);
        lck_grp_cnt = 1;
@@ -228,7 +218,7 @@ void
 lck_grp_attr_setstat(
        lck_grp_attr_t  *attr)
 {
-       (void)hw_atomic_or(&attr->grp_attr_val, LCK_GRP_ATTR_STAT);
+       os_atomic_or(&attr->grp_attr_val, LCK_GRP_ATTR_STAT, relaxed);
 }
 
 
@@ -307,7 +297,7 @@ lck_grp_init(lck_grp_t * grp, const char * grp_name, lck_grp_attr_t * attr)
 #endif /* LOCK_STATS */
        }
 
-       grp->lck_grp_refcnt = 1;
+       os_ref_init(&grp->lck_grp_refcnt, NULL);
 
        lck_mtx_lock(&lck_grp_lock);
        enqueue_tail(&lck_grp_queue, (queue_entry_t)grp);
@@ -339,7 +329,7 @@ void
 lck_grp_reference(
        lck_grp_t       *grp)
 {
-       (void)hw_atomic_add(&grp->lck_grp_refcnt, 1);
+       os_ref_retain(&grp->lck_grp_refcnt);
 }
 
 
@@ -351,9 +341,11 @@ void
 lck_grp_deallocate(
        lck_grp_t       *grp)
 {
-       if (hw_atomic_sub(&grp->lck_grp_refcnt, 1) == 0) {
-               kfree(grp, sizeof(lck_grp_t));
+       if (os_ref_release(&grp->lck_grp_refcnt) != 0) {
+               return;
        }
+
+       kfree(grp, sizeof(lck_grp_t));
 }
 
 /*
@@ -381,7 +373,7 @@ lck_grp_lckcnt_incr(
                return panic("lck_grp_lckcnt_incr(): invalid lock type: %d\n", lck_type);
        }
 
-       (void)hw_atomic_add(lckcnt, 1);
+       os_atomic_inc(lckcnt, relaxed);
 }
 
 /*
@@ -411,7 +403,7 @@ lck_grp_lckcnt_decr(
                return;
        }
 
-       updated = (int)hw_atomic_sub(lckcnt, 1);
+       updated = os_atomic_dec(lckcnt, relaxed);
        assert(updated >= 0);
 }
 
@@ -467,7 +459,7 @@ void
 lck_attr_setdebug(
        lck_attr_t      *attr)
 {
-       (void)hw_atomic_or(&attr->lck_attr_val, LCK_ATTR_DEBUG);
+       os_atomic_or(&attr->lck_attr_val, LCK_ATTR_DEBUG, relaxed);
 }
 
 /*
@@ -477,7 +469,7 @@ void
 lck_attr_cleardebug(
        lck_attr_t      *attr)
 {
-       (void)hw_atomic_and(&attr->lck_attr_val, ~LCK_ATTR_DEBUG);
+       os_atomic_andnot(&attr->lck_attr_val, LCK_ATTR_DEBUG, relaxed);
 }
 
 
@@ -488,7 +480,7 @@ void
 lck_attr_rw_shared_priority(
        lck_attr_t      *attr)
 {
-       (void)hw_atomic_or(&attr->lck_attr_val, LCK_ATTR_RW_SHARED_PRIORITY);
+       os_atomic_or(&attr->lck_attr_val, LCK_ATTR_RW_SHARED_PRIORITY, relaxed);
 }
 
 
@@ -513,6 +505,31 @@ hw_lock_init(hw_lock_t lock)
        ordered_store_hw(lock, 0);
 }
 
+#if     __SMP__
+static inline bool
+hw_lock_trylock_contended(hw_lock_t lock, uintptr_t newval)
+{
+#if OS_ATOMIC_USE_LLSC
+       uintptr_t oldval;
+       os_atomic_rmw_loop(&lock->lock_data, oldval, newval, acquire, {
+               if (oldval != 0) {
+                       wait_for_event(); // clears the monitor so we don't need give_up()
+                       return false;
+               }
+       });
+       return true;
+#else // !OS_ATOMIC_USE_LLSC
+#if OS_ATOMIC_HAS_LLSC
+       uintptr_t oldval = os_atomic_load_exclusive(&lock->lock_data, relaxed);
+       if (oldval != 0) {
+               wait_for_event(); // clears the monitor so we don't need give_up()
+               return false;
+       }
+#endif // OS_ATOMIC_HAS_LLSC
+       return os_atomic_cmpxchg(&lock->lock_data, 0, newval, acquire);
+#endif // !OS_ATOMIC_USE_LLSC
+}
+
 /*
  *     Routine: hw_lock_lock_contended
  *
@@ -520,8 +537,6 @@ hw_lock_init(hw_lock_t lock)
  *     timeout is in mach_absolute_time ticks. Called with
  *     preemption disabled.
  */
-
-#if     __SMP__
 static unsigned int NOINLINE
 hw_lock_lock_contended(hw_lock_t lock, uintptr_t data, uint64_t timeout, boolean_t do_panic LCK_GRP_ARG(lck_grp_t *grp))
 {
@@ -551,8 +566,7 @@ hw_lock_lock_contended(hw_lock_t lock, uintptr_t data, uint64_t timeout, boolean
                                continue;
                        }
 #endif
-                       if (atomic_compare_exchange(&lock->lock_data, 0, data,
-                           memory_order_acquire_smp, TRUE)) {
+                       if (hw_lock_trylock_contended(lock, data)) {
 #if CONFIG_DTRACE || LOCK_STATS
                                if (__improbable(stat_enabled)) {
                                        lck_grp_spin_update_spin(lock LCK_GRP_ARG(grp), mach_absolute_time() - begin);
@@ -578,6 +592,42 @@ hw_lock_lock_contended(hw_lock_t lock, uintptr_t data, uint64_t timeout, boolean
 }
 #endif  // __SMP__
 
+void *
+hw_wait_while_equals(void **address, void *current)
+{
+#if     __SMP__
+       void *v;
+       uint64_t end = 0;
+
+       for (;;) {
+               for (int i = 0; i < LOCK_SNOOP_SPINS; i++) {
+                       cpu_pause();
+#if OS_ATOMIC_HAS_LLSC
+                       v = os_atomic_load_exclusive(address, relaxed);
+                       if (__probable(v != current)) {
+                               os_atomic_clear_exclusive();
+                               return v;
+                       }
+                       wait_for_event();
+#else
+                       v = os_atomic_load(address, relaxed);
+                       if (__probable(v != current)) {
+                               return v;
+                       }
+#endif // OS_ATOMIC_HAS_LLSC
+               }
+               if (end == 0) {
+                       end = ml_get_timebase() + LOCK_PANIC_TIMEOUT;
+               } else if (ml_get_timebase() >= end) {
+                       panic("Wait while equals timeout @ *%p == %p", address, v);
+               }
+       }
+#else // !__SMP__
+       panic("Value at %p is %p", address, current);
+       __builtin_unreachable();
+#endif // !__SMP__
+}
+
 static inline void
 hw_lock_lock_internal(hw_lock_t lock, thread_t thread LCK_GRP_ARG(lck_grp_t *grp))
 {
@@ -585,14 +635,12 @@ hw_lock_lock_internal(hw_lock_t lock, thread_t thread LCK_GRP_ARG(lck_grp_t *grp
 
        state = LCK_MTX_THREAD_TO_STATE(thread) | PLATFORM_LCK_ILOCK;
 #if     __SMP__
-
 #if     LOCK_PRETEST
        if (ordered_load_hw(lock)) {
                goto contended;
        }
 #endif  // LOCK_PRETEST
-       if (atomic_compare_exchange(&lock->lock_data, 0, state,
-           memory_order_acquire_smp, TRUE)) {
+       if (hw_lock_trylock_contended(lock, state)) {
                goto end;
        }
 #if     LOCK_PRETEST
@@ -659,14 +707,12 @@ int
        disable_preemption_for_thread(thread);
        state = LCK_MTX_THREAD_TO_STATE(thread) | PLATFORM_LCK_ILOCK;
 #if     __SMP__
-
 #if     LOCK_PRETEST
        if (ordered_load_hw(lock)) {
                goto contended;
        }
 #endif  // LOCK_PRETEST
-       if (atomic_compare_exchange(&lock->lock_data, 0, state,
-           memory_order_acquire_smp, TRUE)) {
+       if (hw_lock_trylock_contended(lock, state)) {
                success = 1;
                goto end;
        }
@@ -704,8 +750,8 @@ hw_lock_try_internal(hw_lock_t lock, thread_t thread LCK_GRP_ARG(lck_grp_t *grp)
                goto failed;
        }
 #endif  // LOCK_PRETEST
-       success = atomic_compare_exchange(&lock->lock_data, 0, LCK_MTX_THREAD_TO_STATE(thread) | PLATFORM_LCK_ILOCK,
-           memory_order_acquire_smp, FALSE);
+       success = os_atomic_cmpxchg(&lock->lock_data, 0,
+           LCK_MTX_THREAD_TO_STATE(thread) | PLATFORM_LCK_ILOCK, acquire);
 #else
        if (lock->lock_data == 0) {
                lock->lock_data = LCK_MTX_THREAD_TO_STATE(thread) | PLATFORM_LCK_ILOCK;
@@ -754,7 +800,7 @@ int
 static inline void
 hw_lock_unlock_internal(hw_lock_t lock)
 {
-       __c11_atomic_store((_Atomic uintptr_t *)&lock->lock_data, 0, memory_order_release_smp);
+       os_atomic_store(&lock->lock_data, 0, release);
 #if __arm__ || __arm64__
        // ARM tests are only for open-source exclusion
        set_event();
@@ -790,6 +836,198 @@ hw_lock_held(hw_lock_t lock)
        return ordered_load_hw(lock) != 0;
 }
 
+#if     __SMP__
+static unsigned int
+hw_lock_bit_to_contended(hw_lock_bit_t *lock, uint32_t mask, uint32_t timeout LCK_GRP_ARG(lck_grp_t *grp));
+#endif
+
+static inline unsigned int
+hw_lock_bit_to_internal(hw_lock_bit_t *lock, unsigned int bit, uint32_t timeout LCK_GRP_ARG(lck_grp_t *grp))
+{
+       unsigned int success = 0;
+       uint32_t        mask = (1 << bit);
+#if     !__SMP__
+       uint32_t        state;
+#endif
+
+#if     __SMP__
+       if (__improbable(!hw_atomic_test_and_set32(lock, mask, mask, memory_order_acquire, FALSE))) {
+               success = hw_lock_bit_to_contended(lock, mask, timeout LCK_GRP_ARG(grp));
+       } else {
+               success = 1;
+       }
+#else   // __SMP__
+       (void)timeout;
+       state = ordered_load_bit(lock);
+       if (!(mask & state)) {
+               ordered_store_bit(lock, state | mask);
+               success = 1;
+       }
+#endif  // __SMP__
+
+       if (success) {
+               lck_grp_spin_update_held(lock LCK_GRP_ARG(grp));
+       }
+
+       return success;
+}
+
+unsigned
+int
+(hw_lock_bit_to)(hw_lock_bit_t * lock, unsigned int bit, uint32_t timeout LCK_GRP_ARG(lck_grp_t *grp))
+{
+       _disable_preemption();
+       return hw_lock_bit_to_internal(lock, bit, timeout LCK_GRP_ARG(grp));
+}
+
+#if     __SMP__
+static unsigned int NOINLINE
+hw_lock_bit_to_contended(hw_lock_bit_t *lock, uint32_t mask, uint32_t timeout LCK_GRP_ARG(lck_grp_t *grp))
+{
+       uint64_t        end = 0;
+       int             i;
+#if CONFIG_DTRACE || LOCK_STATS
+       uint64_t begin = 0;
+       boolean_t stat_enabled = lck_grp_spin_spin_enabled(lock LCK_GRP_ARG(grp));
+#endif /* CONFIG_DTRACE || LOCK_STATS */
+
+#if LOCK_STATS || CONFIG_DTRACE
+       if (__improbable(stat_enabled)) {
+               begin = mach_absolute_time();
+       }
+#endif /* LOCK_STATS || CONFIG_DTRACE */
+       for (;;) {
+               for (i = 0; i < LOCK_SNOOP_SPINS; i++) {
+                       // Always load-exclusive before wfe
+                       // This grabs the monitor and wakes up on a release event
+                       if (hw_atomic_test_and_set32(lock, mask, mask, memory_order_acquire, TRUE)) {
+                               goto end;
+                       }
+               }
+               if (end == 0) {
+                       end = ml_get_timebase() + timeout;
+               } else if (ml_get_timebase() >= end) {
+                       break;
+               }
+       }
+       return 0;
+end:
+#if CONFIG_DTRACE || LOCK_STATS
+       if (__improbable(stat_enabled)) {
+               lck_grp_spin_update_spin(lock LCK_GRP_ARG(grp), mach_absolute_time() - begin);
+       }
+       lck_grp_spin_update_miss(lock LCK_GRP_ARG(grp));
+#endif /* CONFIG_DTRACE || LCK_GRP_STAT */
+
+       return 1;
+}
+#endif  // __SMP__
+
+void
+(hw_lock_bit)(hw_lock_bit_t * lock, unsigned int bit LCK_GRP_ARG(lck_grp_t *grp))
+{
+       if (hw_lock_bit_to(lock, bit, LOCK_PANIC_TIMEOUT, LCK_GRP_PROBEARG(grp))) {
+               return;
+       }
+#if     __SMP__
+       panic("hw_lock_bit(): timed out (%p)", lock);
+#else
+       panic("hw_lock_bit(): interlock held (%p)", lock);
+#endif
+}
+
+void
+(hw_lock_bit_nopreempt)(hw_lock_bit_t * lock, unsigned int bit LCK_GRP_ARG(lck_grp_t *grp))
+{
+       if (__improbable(get_preemption_level() == 0)) {
+               panic("Attempt to take no-preempt bitlock %p in preemptible context", lock);
+       }
+       if (hw_lock_bit_to_internal(lock, bit, LOCK_PANIC_TIMEOUT LCK_GRP_ARG(grp))) {
+               return;
+       }
+#if     __SMP__
+       panic("hw_lock_bit_nopreempt(): timed out (%p)", lock);
+#else
+       panic("hw_lock_bit_nopreempt(): interlock held (%p)", lock);
+#endif
+}
+
+unsigned
+int
+(hw_lock_bit_try)(hw_lock_bit_t * lock, unsigned int bit LCK_GRP_ARG(lck_grp_t *grp))
+{
+       uint32_t        mask = (1 << bit);
+#if     !__SMP__
+       uint32_t        state;
+#endif
+       boolean_t       success = FALSE;
+
+       _disable_preemption();
+#if     __SMP__
+       // TODO: consider weak (non-looping) atomic test-and-set
+       success = hw_atomic_test_and_set32(lock, mask, mask, memory_order_acquire, FALSE);
+#else
+       state = ordered_load_bit(lock);
+       if (!(mask & state)) {
+               ordered_store_bit(lock, state | mask);
+               success = TRUE;
+       }
+#endif  // __SMP__
+       if (!success) {
+               _enable_preemption();
+       }
+
+       if (success) {
+               lck_grp_spin_update_held(lock LCK_GRP_ARG(grp));
+       }
+
+       return success;
+}
+
+static inline void
+hw_unlock_bit_internal(hw_lock_bit_t *lock, unsigned int bit)
+{
+       uint32_t        mask = (1 << bit);
+#if     !__SMP__
+       uint32_t        state;
+#endif
+
+#if     __SMP__
+       os_atomic_andnot(lock, mask, release);
+#if __arm__
+       set_event();
+#endif
+#else   // __SMP__
+       state = ordered_load_bit(lock);
+       ordered_store_bit(lock, state & ~mask);
+#endif  // __SMP__
+#if CONFIG_DTRACE
+       LOCKSTAT_RECORD(LS_LCK_SPIN_UNLOCK_RELEASE, lock, bit);
+#endif
+}
+
+/*
+ *     Routine:        hw_unlock_bit
+ *
+ *             Release spin-lock. The second parameter is the bit number to test and set.
+ *             Decrement the preemption level.
+ */
+void
+hw_unlock_bit(hw_lock_bit_t * lock, unsigned int bit)
+{
+       hw_unlock_bit_internal(lock, bit);
+       _enable_preemption();
+}
+
+void
+hw_unlock_bit_nopreempt(hw_lock_bit_t * lock, unsigned int bit)
+{
+       if (__improbable(get_preemption_level() == 0)) {
+               panic("Attempt to release no-preempt bitlock %p in preemptible context", lock);
+       }
+       hw_unlock_bit_internal(lock, bit);
+}
+
 /*
  * Routine:    lck_spin_sleep
  */
@@ -983,37 +1221,9 @@ lck_mtx_sleep_deadline(
  * The lock owner is always promoted to the max priority of all its waiters.
  * Max priority is capped at MAXPRI_PROMOTE.
  *
- * lck_mtx_pri being set implies that the lock owner is promoted to at least lck_mtx_pri
- *      This prevents the thread from dropping in priority while holding a mutex
- *      (note: Intel locks currently don't do this, to avoid thread lock churn)
- *
- * thread->promotions has a +1 for every mutex currently promoting the thread
- * and 1 for was_promoted_on_wakeup being set.
- * TH_SFLAG_PROMOTED is set on a thread whenever it has any promotions
- * from any mutex (i.e. thread->promotions != 0)
- *
- * was_promoted_on_wakeup is set on a thread which is woken up by a mutex when
- * it raises the priority of the woken thread to match lck_mtx_pri.
- * It can be set for multiple iterations of wait, fail to acquire, re-wait, etc
- * was_promoted_on_wakeup being set always implies a +1 promotions count.
- *
  * The last waiter is not given a promotion when it wakes up or acquires the lock.
  * When the last waiter is waking up, a new contender can always come in and
  * steal the lock without having to wait for the last waiter to make forward progress.
- *
- * lck_mtx_waiters has a +1 for every waiter currently between wait and acquire
- * This prevents us from asserting that every wakeup wakes up a thread.
- * This also causes excess thread_wakeup calls in the unlock path.
- * It can only be fooled into thinking there are more waiters than are
- * actually blocked, not less.
- * It does allows us to reduce the complexity of the lock state.
- *
- * This also means that a starved bg thread as the last waiter could end up
- * keeping the lock in the contended state for a long period of time, which
- * may keep lck_mtx_pri artificially high for a very long time even though
- * it is not participating or blocking anyone else.
- * Intel locks don't have this problem because they can go uncontended
- * as soon as there are no blocked threads involved.
  */
 
 /*
@@ -1034,9 +1244,10 @@ lck_mtx_sleep_deadline(
 void
 lck_mtx_lock_wait(
        lck_mtx_t                       *lck,
-       thread_t                        holder)
+       thread_t                        holder,
+       struct turnstile                **ts)
 {
-       thread_t                self = current_thread();
+       thread_t                thread = current_thread();
        lck_mtx_t               *mutex;
        __kdebug_only uintptr_t trace_lck = unslide_for_kdebug(lck);
 
@@ -1057,64 +1268,27 @@ lck_mtx_lock_wait(
        KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS, LCK_MTX_LCK_WAIT_CODE) | DBG_FUNC_START,
            trace_lck, (uintptr_t)thread_tid(thread), 0, 0, 0);
 
-       spl_t s = splsched();
-       thread_lock(holder);
-
-       assert_promotions_invariant(holder);
-
-       if ((holder->sched_flags & TH_SFLAG_DEPRESS) == 0) {
-               assert(holder->sched_pri >= mutex->lck_mtx_pri);
-       }
-
-       integer_t priority = self->sched_pri;
-       priority = MAX(priority, self->base_pri);
-       priority = MAX(priority, BASEPRI_DEFAULT);
-       priority = MIN(priority, MAXPRI_PROMOTE);
-
-       if (mutex->lck_mtx_pri == 0) {
-               /* This is the first promotion for this mutex */
-               if (holder->promotions++ == 0) {
-                       /* This is the first promotion for holder */
-                       sched_thread_promote_to_pri(holder, priority, trace_lck);
-               } else {
-                       /* Holder was previously promoted due to a different mutex, raise to match this one */
-                       sched_thread_update_promotion_to_pri(holder, priority, trace_lck);
-               }
-       } else {
-               /* Holder was previously promoted due to this mutex, check if the pri needs to go up */
-               sched_thread_update_promotion_to_pri(holder, priority, trace_lck);
-       }
-
-       assert(holder->promotions > 0);
-       assert(holder->promotion_priority >= priority);
-
-       if ((holder->sched_flags & TH_SFLAG_DEPRESS) == 0) {
-               assert(holder->sched_pri >= mutex->lck_mtx_pri);
-       }
-
-       assert_promotions_invariant(holder);
-
-       thread_unlock(holder);
-       splx(s);
+       assert(thread->waiting_for_mutex == NULL);
+       thread->waiting_for_mutex = mutex;
+       mutex->lck_mtx_waiters++;
 
-       if (mutex->lck_mtx_pri < priority) {
-               mutex->lck_mtx_pri = priority;
+       if (*ts == NULL) {
+               *ts = turnstile_prepare((uintptr_t)mutex, NULL, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
        }
 
-       if (self->waiting_for_mutex == NULL) {
-               self->waiting_for_mutex = mutex;
-               mutex->lck_mtx_waiters++;
-       }
+       struct turnstile *turnstile = *ts;
+       thread_set_pending_block_hint(thread, kThreadWaitKernelMutex);
+       turnstile_update_inheritor(turnstile, holder, (TURNSTILE_DELAYED_UPDATE | TURNSTILE_INHERITOR_THREAD));
 
-       assert(self->waiting_for_mutex == mutex);
+       waitq_assert_wait64(&turnstile->ts_waitq, CAST_EVENT64_T(LCK_MTX_EVENT(mutex)), THREAD_UNINT | THREAD_WAIT_NOREPORT_USER, TIMEOUT_WAIT_FOREVER);
 
-       thread_set_pending_block_hint(self, kThreadWaitKernelMutex);
-       assert_wait(LCK_MTX_EVENT(mutex), THREAD_UNINT | THREAD_WAIT_NOREPORT_USER);
        lck_mtx_ilk_unlock(mutex);
 
+       turnstile_update_inheritor_complete(turnstile, TURNSTILE_INTERLOCK_NOT_HELD);
+
        thread_block(THREAD_CONTINUE_NULL);
 
-       assert(mutex->lck_mtx_waiters > 0);
+       thread->waiting_for_mutex = NULL;
 
        KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS, LCK_MTX_LCK_WAIT_CODE) | DBG_FUNC_END, 0, 0, 0, 0, 0);
 #if     CONFIG_DTRACE
@@ -1146,11 +1320,11 @@ lck_mtx_lock_wait(
  */
 int
 lck_mtx_lock_acquire(
-       lck_mtx_t               *lck)
+       lck_mtx_t               *lck,
+       struct turnstile        *ts)
 {
        thread_t                thread = current_thread();
        lck_mtx_t               *mutex;
-       integer_t               priority;
 
        if (lck->lck_mtx_tag != LCK_MTX_TAG_INDIRECT) {
                mutex = lck;
@@ -1158,79 +1332,19 @@ lck_mtx_lock_acquire(
                mutex = &lck->lck_mtx_ptr->lck_mtx;
        }
 
-       /*
-        * If waiting_for_mutex is set, then this thread was previously blocked waiting on this lock
-        * If it's un-set, then this thread stole the lock from another waiter.
-        */
-       if (thread->waiting_for_mutex == mutex) {
-               assert(mutex->lck_mtx_waiters > 0);
-
-               thread->waiting_for_mutex = NULL;
-               mutex->lck_mtx_waiters--;
-       }
-
        assert(thread->waiting_for_mutex == NULL);
 
        if (mutex->lck_mtx_waiters > 0) {
-               priority = mutex->lck_mtx_pri;
-       } else {
-               /* I was the last waiter, so the mutex is no longer promoted or contended */
-               mutex->lck_mtx_pri = 0;
-               priority = 0;
-       }
-
-       if (priority || thread->was_promoted_on_wakeup) {
-               __kdebug_only uintptr_t trace_lck = unslide_for_kdebug(lck);
-
-               /*
-                * Note: was_promoted_on_wakeup can happen for multiple wakeups in a row without
-                * an intervening acquire if a thread keeps failing to acquire the lock
-                *
-                * If priority is true but not promoted on wakeup,
-                * then this is a lock steal of a promoted mutex, so it needs a ++ of promotions.
-                *
-                * If promoted on wakeup is true, but priority is not,
-                * then this is the last owner, and the last owner does not need a promotion.
-                */
-
-               spl_t s = splsched();
-               thread_lock(thread);
-
-               assert_promotions_invariant(thread);
-
-               if (thread->was_promoted_on_wakeup) {
-                       assert(thread->promotions > 0);
+               if (ts == NULL) {
+                       ts = turnstile_prepare((uintptr_t)mutex, NULL, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
                }
 
-               if (priority) {
-                       if (thread->promotions++ == 0) {
-                               /* This is the first promotion for holder */
-                               sched_thread_promote_to_pri(thread, priority, trace_lck);
-                       } else {
-                               /*
-                                * Holder was previously promoted due to a different mutex, raise to match this one
-                                * Or, this thread was promoted on wakeup but someone else later contended on mutex
-                                * at higher priority before we got here
-                                */
-                               sched_thread_update_promotion_to_pri(thread, priority, trace_lck);
-                       }
-               }
-
-               if (thread->was_promoted_on_wakeup) {
-                       thread->was_promoted_on_wakeup = 0;
-                       if (--thread->promotions == 0) {
-                               sched_thread_unpromote(thread, trace_lck);
-                       }
-               }
-
-               assert_promotions_invariant(thread);
-
-               if (priority && (thread->sched_flags & TH_SFLAG_DEPRESS) == 0) {
-                       assert(thread->sched_pri >= priority);
-               }
+               turnstile_update_inheritor(ts, thread, (TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD));
+               turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+       }
 
-               thread_unlock(thread);
-               splx(s);
+       if (ts != NULL) {
+               turnstile_complete((uintptr_t)mutex, NULL, NULL, TURNSTILE_KERNEL_MUTEX);
        }
 
        return mutex->lck_mtx_waiters;
@@ -1243,11 +1357,10 @@ lck_mtx_lock_acquire(
  *
  * Called with the interlock locked.
  *
- * TODO: the 'waiters' flag does not indicate waiters exist on the waitqueue,
- * it indicates waiters exist between wait and acquire.
- * This means that here we may do extra unneeded wakeups.
+ * NOTE: callers should call turnstile_clenup after
+ * dropping the interlock.
  */
-void
+boolean_t
 lck_mtx_unlock_wakeup(
        lck_mtx_t                       *lck,
        thread_t                        holder)
@@ -1255,6 +1368,8 @@ lck_mtx_unlock_wakeup(
        thread_t                thread = current_thread();
        lck_mtx_t               *mutex;
        __kdebug_only uintptr_t trace_lck = unslide_for_kdebug(lck);
+       struct turnstile *ts;
+       kern_return_t did_wake;
 
        if (lck->lck_mtx_tag != LCK_MTX_TAG_INDIRECT) {
                mutex = lck;
@@ -1270,88 +1385,29 @@ lck_mtx_unlock_wakeup(
            trace_lck, (uintptr_t)thread_tid(thread), 0, 0, 0);
 
        assert(mutex->lck_mtx_waiters > 0);
-       assert(thread->was_promoted_on_wakeup == 0);
        assert(thread->waiting_for_mutex == NULL);
 
-       /*
-        * The waiters count does not precisely match the number of threads on the waitqueue,
-        * therefore we cannot assert that we actually wake up a thread here
-        */
+       ts = turnstile_prepare((uintptr_t)mutex, NULL, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
+
        if (mutex->lck_mtx_waiters > 1) {
-               thread_wakeup_one_with_pri(LCK_MTX_EVENT(lck), lck->lck_mtx_pri);
+               /* WAITQ_PROMOTE_ON_WAKE will call turnstile_update_inheritor on the wokenup thread */
+               did_wake = waitq_wakeup64_one(&ts->ts_waitq, CAST_EVENT64_T(LCK_MTX_EVENT(mutex)), THREAD_AWAKENED, WAITQ_PROMOTE_ON_WAKE);
        } else {
-               thread_wakeup_one(LCK_MTX_EVENT(lck));
+               did_wake = waitq_wakeup64_one(&ts->ts_waitq, CAST_EVENT64_T(LCK_MTX_EVENT(mutex)), THREAD_AWAKENED, WAITQ_ALL_PRIORITIES);
+               turnstile_update_inheritor(ts, NULL, TURNSTILE_IMMEDIATE_UPDATE);
        }
+       assert(did_wake == KERN_SUCCESS);
 
-       /* When mutex->lck_mtx_pri is set, it means means I as the owner have a promotion. */
-       if (mutex->lck_mtx_pri) {
-               spl_t s = splsched();
-               thread_lock(thread);
-
-               assert(thread->promotions > 0);
-
-               assert_promotions_invariant(thread);
+       turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+       turnstile_complete((uintptr_t)mutex, NULL, NULL, TURNSTILE_KERNEL_MUTEX);
 
-               if (--thread->promotions == 0) {
-                       sched_thread_unpromote(thread, trace_lck);
-               }
-
-               assert_promotions_invariant(thread);
-
-               thread_unlock(thread);
-               splx(s);
-       }
+       mutex->lck_mtx_waiters--;
 
        KERNEL_DEBUG(MACHDBG_CODE(DBG_MACH_LOCKS, LCK_MTX_UNLCK_WAKEUP_CODE) | DBG_FUNC_END, 0, 0, 0, 0, 0);
-}
-
-/*
- * Callout from the waitqueue code from inside thread_wakeup_one_with_pri
- * At splsched, thread is pulled from waitq, still locked, not on runqueue yet
- *
- * We always make sure to set the promotion flag, even if the thread is already at this priority,
- * so that it doesn't go down.
- */
-void
-lck_mtx_wakeup_adjust_pri(thread_t thread, integer_t priority)
-{
-       assert(priority <= MAXPRI_PROMOTE);
-       assert(thread->waiting_for_mutex != NULL);
-
-       __kdebug_only uintptr_t trace_lck = unslide_for_kdebug(thread->waiting_for_mutex);
-
-       assert_promotions_invariant(thread);
-
-       if (thread->was_promoted_on_wakeup) {
-               /* Thread was previously promoted, but contended again */
-               sched_thread_update_promotion_to_pri(thread, priority, trace_lck);
-               return;
-       }
-
-       if (thread->promotions > 0 && priority <= thread->promotion_priority) {
-               /*
-                * Thread is already promoted to the right level, no need to do more
-                * I can draft off of another promotion here, which is OK
-                * because I know the thread will soon run acquire to get its own promotion
-                */
-               assert((thread->sched_flags & TH_SFLAG_PROMOTED) == TH_SFLAG_PROMOTED);
-               return;
-       }
-
-       thread->was_promoted_on_wakeup = 1;
-
-       if (thread->promotions++ == 0) {
-               /* This is the first promotion for this thread */
-               sched_thread_promote_to_pri(thread, priority, trace_lck);
-       } else {
-               /* Holder was previously promoted due to a different mutex, raise to match this one */
-               sched_thread_update_promotion_to_pri(thread, priority, trace_lck);
-       }
 
-       assert_promotions_invariant(thread);
+       return mutex->lck_mtx_waiters > 0;
 }
 
-
 /*
  * Routine:     mutex_pause
  *
@@ -1703,56 +1759,1495 @@ host_lockgroup_info(
 }
 
 /*
- * Atomic primitives, prototyped in kern/simple_lock.h
- * Noret versions are more efficient on some architectures
+ * 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.
  */
 
-uint32_t
-hw_atomic_add(volatile uint32_t *dest, uint32_t delt)
+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)
 {
-       ALIGN_TEST(dest, uint32_t);
-       return __c11_atomic_fetch_add(ATOMIC_CAST(uint32_t, dest), delt, memory_order_relaxed) + delt;
-}
+       uint32_t index;
+       struct turnstile *ts = NULL;
+       kern_return_t ret = KERN_NOT_WAITING;
+       int priority;
+       thread_t wokeup;
 
-uint32_t
-hw_atomic_sub(volatile uint32_t *dest, uint32_t delt)
-{
-       ALIGN_TEST(dest, uint32_t);
-       return __c11_atomic_fetch_sub(ATOMIC_CAST(uint32_t, dest), delt, memory_order_relaxed) - delt;
-}
+       /*
+        * the hash bucket spinlock is used as turnstile interlock
+        */
+       turnstile_hash_bucket_lock((uintptr_t)event, &index, type);
 
-uint32_t
-hw_atomic_or(volatile uint32_t *dest, uint32_t mask)
-{
-       ALIGN_TEST(dest, uint32_t);
-       return __c11_atomic_fetch_or(ATOMIC_CAST(uint32_t, dest), mask, memory_order_relaxed) | mask;
-}
+       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();
+}
+
+static void
+gate_open(gate_t *gate)
+{
+       uintptr_t state;
+       thread_t holder;
+       bool waiters;
+       thread_t thread = current_thread();
+
+       if (os_atomic_cmpxchg(&gate->gate_data, GATE_THREAD_TO_STATE(thread), 0, release)) {
+               return;
+       }
+
+       gate_ilock(gate);
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+       waiters = gate_has_waiters(state);
+
+       if (holder != thread) {
+               panic("Opening gate owned by %p from current thread %p", holder, thread);
+       }
+
+       if (waiters) {
+               gate_open_turnstile(gate);
+       }
+
+       state = GATE_ILOCK;
+       ordered_store_gate(gate, state);
+
+       gate_iunlock(gate);
+}
+
+static kern_return_t
+gate_handoff_turnstile(gate_t *gate,
+    int flags,
+    thread_t *thread_woken_up,
+    bool *waiters)
+{
+       struct turnstile *ts = NULL;
+       kern_return_t ret = KERN_FAILURE;
+       thread_t hp_thread;
+
+       ts = turnstile_prepare((uintptr_t)gate, &gate->turnstile, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
+       /*
+        * Wake up the higest priority thread waiting on the gate
+        */
+       hp_thread = waitq_wakeup64_identify(&ts->ts_waitq, CAST_EVENT64_T(GATE_EVENT(gate)), THREAD_AWAKENED, WAITQ_PROMOTE_ON_WAKE);
+
+       if (hp_thread != NULL) {
+               /*
+                * In this case waitq_wakeup64_identify has called turnstile_update_inheritor for us
+                */
+               turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+               *thread_woken_up = hp_thread;
+               *waiters = turnstile_has_waiters(ts);
+               /*
+                * Note: hp_thread is the new holder and the new inheritor.
+                * In case there are no more waiters, it doesn't need to be the inheritor
+                * and it shouldn't be it by the time it finishes the wait, so that its next open or
+                * handoff can go through the fast path.
+                * We could set the inheritor to NULL here, or the new holder itself can set it
+                * on its way back from the sleep. In the latter case there are more chanses that
+                * new waiters will come by, avoiding to do the opearation at all.
+                */
+               ret = KERN_SUCCESS;
+       } else {
+               /*
+                * waiters can have been woken up by an interrupt and still not
+                * have updated gate->waiters, so we couldn't find them on the waitq.
+                * Update the inheritor to NULL here, so that the current thread can return to userspace
+                * indipendently from when the interrupted waiters will finish the wait.
+                */
+               if (flags == GATE_HANDOFF_OPEN_IF_NO_WAITERS) {
+                       turnstile_update_inheritor(ts, TURNSTILE_INHERITOR_NULL, TURNSTILE_IMMEDIATE_UPDATE);
+                       turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+               }
+               // there are no waiters.
+               ret = KERN_NOT_WAITING;
+       }
+
+       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 or it is a just wokenup thread that will race acquiring the lock
+        *    of the gate before trying to sleep.
+        * => No chain of turnstiles needs to be updated.
+        */
+       turnstile_cleanup();
+
+       return ret;
+}
+
+static kern_return_t
+gate_handoff(gate_t *gate,
+    int flags)
+{
+       kern_return_t ret;
+       thread_t new_holder = NULL;
+       uintptr_t state;
+       thread_t holder;
+       bool waiters;
+       thread_t thread = current_thread();
+
+       assert(flags == GATE_HANDOFF_OPEN_IF_NO_WAITERS || flags == GATE_HANDOFF_DEFAULT);
+
+       if (flags == GATE_HANDOFF_OPEN_IF_NO_WAITERS) {
+               if (os_atomic_cmpxchg(&gate->gate_data, GATE_THREAD_TO_STATE(thread), 0, release)) {
+                       //gate opened but there were no waiters, so return KERN_NOT_WAITING.
+                       return KERN_NOT_WAITING;
+               }
+       }
+
+       gate_ilock(gate);
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+       waiters = gate_has_waiters(state);
+
+       if (holder != current_thread()) {
+               panic("Handing off gate owned by %p from current thread %p", holder, current_thread());
+       }
+
+       if (waiters) {
+               ret = gate_handoff_turnstile(gate, flags, &new_holder, &waiters);
+               if (ret == KERN_SUCCESS) {
+                       state = GATE_THREAD_TO_STATE(new_holder);
+                       if (waiters) {
+                               state |= GATE_WAITERS;
+                       }
+               } else {
+                       if (flags == GATE_HANDOFF_OPEN_IF_NO_WAITERS) {
+                               state = 0;
+                       }
+               }
+       } else {
+               if (flags == GATE_HANDOFF_OPEN_IF_NO_WAITERS) {
+                       state = 0;
+               }
+               ret = KERN_NOT_WAITING;
+       }
+       state |= GATE_ILOCK;
+       ordered_store_gate(gate, state);
+
+       gate_iunlock(gate);
+
+       if (new_holder) {
+               thread_deallocate(new_holder);
+       }
+       return ret;
+}
+
+static void_func_void
+gate_steal_turnstile(gate_t *gate,
+    thread_t new_inheritor)
+{
+       struct turnstile *ts = NULL;
+
+       ts = turnstile_prepare((uintptr_t)gate, &gate->turnstile, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
+
+       turnstile_update_inheritor(ts, new_inheritor, (TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD));
+       turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
+       turnstile_complete((uintptr_t)gate, &gate->turnstile, NULL, TURNSTILE_KERNEL_MUTEX);
+
+       /*
+        * turnstile_cleanup might need to update the chain of the old holder.
+        * This operation should happen without the turnstile interlock held.
+        */
+       return turnstile_cleanup;
+}
+
+static void
+gate_steal(gate_t *gate)
+{
+       uintptr_t state;
+       thread_t holder;
+       thread_t thread = current_thread();
+       bool waiters;
+
+       void_func_void func_after_interlock_unlock;
+
+       gate_ilock(gate);
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+       waiters = gate_has_waiters(state);
+
+       assert(holder != NULL);
+       state = GATE_THREAD_TO_STATE(thread) | GATE_ILOCK;
+       if (waiters) {
+               state |= GATE_WAITERS;
+               ordered_store_gate(gate, state);
+               func_after_interlock_unlock = gate_steal_turnstile(gate, thread);
+               gate_iunlock(gate);
+
+               func_after_interlock_unlock();
+       } else {
+               ordered_store_gate(gate, state);
+               gate_iunlock(gate);
+       }
+}
+
+static void_func_void
+gate_wait_turnstile(gate_t *gate,
+    wait_interrupt_t interruptible,
+    uint64_t deadline,
+    thread_t holder,
+    wait_result_t* wait,
+    bool* waiters)
+{
+       struct turnstile *ts;
+       uintptr_t state;
+
+       ts = turnstile_prepare((uintptr_t)gate, &gate->turnstile, TURNSTILE_NULL, TURNSTILE_KERNEL_MUTEX);
+
+       turnstile_update_inheritor(ts, holder, (TURNSTILE_DELAYED_UPDATE | TURNSTILE_INHERITOR_THREAD));
+       waitq_assert_wait64(&ts->ts_waitq, CAST_EVENT64_T(GATE_EVENT(gate)), interruptible, deadline);
+
+       gate_iunlock(gate);
+
+       turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
+
+       *wait = thread_block(THREAD_CONTINUE_NULL);
+
+       gate_ilock(gate);
+
+       *waiters = turnstile_has_waiters(ts);
+
+       if (!*waiters) {
+               /*
+                * We want to enable the fast path as soon as we see that there are no more waiters.
+                * On the fast path the holder will not do any turnstile operations.
+                * Set the inheritor as NULL here.
+                *
+                * NOTE: if it was an open operation that woke this thread up, the inheritor has
+                * already been set to NULL.
+                */
+               state = ordered_load_gate(gate);
+               holder = GATE_STATE_TO_THREAD(state);
+               if (holder &&
+                   ((*wait != THREAD_AWAKENED) ||     // thread interrupted or timedout
+                   holder == current_thread())) {     // thread was woken up and it is the new holder
+                       turnstile_update_inheritor(ts, TURNSTILE_INHERITOR_NULL, TURNSTILE_IMMEDIATE_UPDATE);
+                       turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
+               }
+       }
+
+       turnstile_complete((uintptr_t)gate, &gate->turnstile, NULL, TURNSTILE_KERNEL_MUTEX);
+
+       /*
+        * turnstile_cleanup might need to update the chain of the old holder.
+        * This operation should happen without the turnstile primitive interlock held.
+        */
+       return turnstile_cleanup;
+}
+
+static gate_wait_result_t
+gate_wait(gate_t* gate,
+    wait_interrupt_t interruptible,
+    uint64_t deadline,
+    void (^primitive_unlock)(void),
+    void (^primitive_lock)(void))
+{
+       gate_wait_result_t ret;
+       void_func_void func_after_interlock_unlock;
+       wait_result_t wait_result;
+       uintptr_t state;
+       thread_t holder;
+       bool waiters;
+
+
+       gate_ilock(gate);
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+
+       if (holder == NULL) {
+               panic("Trying to wait on open gate thread %p gate %p", current_thread(), gate);
+       }
+
+       state |= GATE_WAITERS;
+       ordered_store_gate(gate, state);
+
+       /*
+        * Release the primitive lock before any
+        * turnstile operation. Turnstile
+        * does not support a blocking primitive as
+        * interlock.
+        *
+        * In this way, concurrent threads will be
+        * able to acquire the primitive lock
+        * but still will wait for me through the
+        * gate interlock.
+        */
+       primitive_unlock();
+
+       func_after_interlock_unlock = gate_wait_turnstile(    gate,
+           interruptible,
+           deadline,
+           holder,
+           &wait_result,
+           &waiters);
+
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+
+       switch (wait_result) {
+       case THREAD_INTERRUPTED:
+       case THREAD_TIMED_OUT:
+               assert(holder != current_thread());
+
+               if (waiters) {
+                       state |= GATE_WAITERS;
+               } else {
+                       state &= ~GATE_WAITERS;
+               }
+               ordered_store_gate(gate, state);
+
+               if (wait_result == THREAD_INTERRUPTED) {
+                       ret = GATE_INTERRUPTED;
+               } else {
+                       ret = GATE_TIMED_OUT;
+               }
+               break;
+       default:
+               /*
+                * Note it is possible that even if the gate was handed off to
+                * me, someone called gate_steal() before I woke up.
+                *
+                * As well as it is possible that the gate was opened, but someone
+                * closed it while I was waking up.
+                *
+                * In both cases we return GATE_OPENED, as the gate was opened to me
+                * at one point, it is the caller responsibility to check again if
+                * the gate is open.
+                */
+               if (holder == current_thread()) {
+                       ret = GATE_HANDOFF;
+               } else {
+                       ret = GATE_OPENED;
+               }
+               break;
+       }
+
+       gate_iunlock(gate);
+
+       /*
+        * turnstile func that needs to be executed without
+        * holding the primitive interlock
+        */
+       func_after_interlock_unlock();
+
+       primitive_lock();
+
+       return ret;
+}
+static void
+gate_assert(gate_t *gate, int flags)
+{
+       uintptr_t state;
+       thread_t holder;
+
+       gate_ilock(gate);
+       state = ordered_load_gate(gate);
+       holder = GATE_STATE_TO_THREAD(state);
+
+       switch (flags) {
+       case GATE_ASSERT_CLOSED:
+               assert(holder != NULL);
+               break;
+       case GATE_ASSERT_OPEN:
+               assert(holder == NULL);
+               break;
+       case GATE_ASSERT_HELD:
+               assert(holder == current_thread());
+               break;
+       default:
+               panic("invalid %s flag %d", __func__, flags);
+       }
+
+       gate_iunlock(gate);
+}
+
+static void
+gate_init(gate_t *gate)
+{
+       gate->gate_data = 0;
+       gate->turnstile = NULL;
+}
+
+static void
+gate_destroy(__assert_only gate_t *gate)
+{
+       assert(gate->gate_data == 0);
+       assert(gate->turnstile == NULL);
+}
+
+/*
+ * Name: lck_rw_gate_init
+ *
+ * Description: initializes a variable declared with decl_lck_rw_gate_data.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ */
+void
+lck_rw_gate_init(lck_rw_t *lock, gate_t *gate)
+{
+       (void) lock;
+       gate_init(gate);
+}
+
+/*
+ * Name: lck_rw_gate_destroy
+ *
+ * Description: destroys a variable previously initialized.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ */
+void
+lck_rw_gate_destroy(lck_rw_t *lock, gate_t *gate)
+{
+       (void) lock;
+       gate_destroy(gate);
+}
+
+/*
+ * Name: lck_rw_gate_try_close
+ *
+ * Description: Tries to close the gate.
+ *              In case of success the current thread will be set as
+ *              the holder of the gate.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *
+ * Returns:
+ *          KERN_SUCCESS in case the gate was successfully closed. The current thread is the new holder
+ *          of the gate.
+ *          A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
+ *          to wake up possible waiters on the gate before returning to userspace.
+ *          If the intent is to conditionally probe the gate before waiting, the lock must not be dropped
+ *          between the calls to lck_rw_gate_try_close() and lck_rw_gate_wait().
+ *
+ *          KERN_FAILURE in case the gate was already closed. Will panic if the current thread was already the holder of the gate.
+ *          lck_rw_gate_wait() should be called instead if the intent is to unconditionally wait on this gate.
+ *          The calls to lck_rw_gate_try_close() and lck_rw_gate_wait() should
+ *          be done without dropping the lock that is protecting the gate in between.
+ */
+int
+lck_rw_gate_try_close(__assert_only lck_rw_t *lock, gate_t *gate)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       return gate_try_close(gate);
+}
+
+/*
+ * Name: lck_rw_gate_close
+ *
+ * Description: Closes the gate. The current thread will be set as
+ *              the holder of the gate. Will panic if the gate is already closed.
+ *              A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
+ *              to wake up possible waiters on the gate before returning to userspace.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The gate must be open.
+ *
+ */
+void
+lck_rw_gate_close(__assert_only lck_rw_t *lock, gate_t *gate)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       return gate_close(gate);
+}
+
+/*
+ * Name: lck_rw_gate_open
+ *
+ * Description: Opens the gate and wakes up possible waiters.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The current thread must be the holder of the gate.
+ *
+ */
+void
+lck_rw_gate_open(__assert_only lck_rw_t *lock, gate_t *gate)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       gate_open(gate);
+}
+
+/*
+ * Name: lck_rw_gate_handoff
+ *
+ * Description: Tries to transfer the ownership of the gate. The waiter with highest sched
+ *              priority will be selected as the new holder of the gate, and woken up,
+ *              with the gate remaining in the closed state throughout.
+ *              If no waiters are present, the gate will be kept closed and KERN_NOT_WAITING
+ *              will be returned.
+ *              GATE_HANDOFF_OPEN_IF_NO_WAITERS flag can be used to specify if the gate should be opened in
+ *              case no waiters were found.
+ *
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *   Arg3: flags - GATE_HANDOFF_DEFAULT or GATE_HANDOFF_OPEN_IF_NO_WAITERS
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The current thread must be the holder of the gate.
+ *
+ * Returns:
+ *          KERN_SUCCESS in case one of the waiters became the new holder.
+ *          KERN_NOT_WAITING in case there were no waiters.
+ *
+ */
+kern_return_t
+lck_rw_gate_handoff(__assert_only lck_rw_t *lock, gate_t *gate, int flags)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       return gate_handoff(gate, flags);
+}
+
+/*
+ * Name: lck_rw_gate_steal
+ *
+ * Description: Set the current ownership of the gate. It sets the current thread as the
+ *              new holder of the gate.
+ *              A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
+ *              to wake up possible waiters on the gate before returning to userspace.
+ *              NOTE: the previous holder should not call lck_rw_gate_open() or lck_rw_gate_handoff()
+ *              anymore.
+ *
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The gate must be closed and the current thread must not already be the holder.
+ *
+ */
+void
+lck_rw_gate_steal(__assert_only lck_rw_t *lock, gate_t *gate)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       gate_steal(gate);
+}
+
+/*
+ * Name: lck_rw_gate_wait
+ *
+ * Description: Waits for the current thread to become the holder of the gate or for the
+ *              gate to become open. An interruptible mode and deadline can be specified
+ *              to return earlier from the wait.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *   Arg3: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_SHARED, LCK_SLEEP_EXCLUSIVE.
+ *   Arg3: interruptible flag for wait.
+ *   Arg4: deadline
+ *
+ * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
+ *             Lock will be dropped while waiting.
+ *             The gate must be closed.
+ *
+ * Returns: Reason why the thread was woken up.
+ *          GATE_HANDOFF - the current thread was handed off the ownership of the gate.
+ *                         A matching lck_rw_gate_open() or lck_rw_gate_handoff() needs to be called later on
+ *                         to wake up possible waiters on the gate before returning to userspace.
+ *          GATE_OPENED - the gate was opened by the holder.
+ *          GATE_TIMED_OUT - the thread was woken up by a timeout.
+ *          GATE_INTERRUPTED - the thread was interrupted while sleeping.
+ *
+ */
+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)
+{
+       __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 gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_rw_type = lck_rw_done(lock);},
+                          ^{;});
+       } else if (!(lck_sleep_action & (LCK_SLEEP_SHARED | LCK_SLEEP_EXCLUSIVE))) {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_rw_type = lck_rw_done(lock);},
+                          ^{lck_rw_lock(lock, lck_rw_type);});
+       } else if (lck_sleep_action & LCK_SLEEP_EXCLUSIVE) {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_rw_type = lck_rw_done(lock);},
+                          ^{lck_rw_lock_exclusive(lock);});
+       } else {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_rw_type = lck_rw_done(lock);},
+                          ^{lck_rw_lock_shared(lock);});
+       }
+}
+
+/*
+ * Name: lck_rw_gate_assert
+ *
+ * Description: asserts that the gate is in the specified state.
+ *
+ * Args:
+ *   Arg1: lck_rw_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_rw_gate_data.
+ *   Arg3: flags to specified assert type.
+ *         GATE_ASSERT_CLOSED - the gate is currently closed
+ *         GATE_ASSERT_OPEN - the gate is currently opened
+ *         GATE_ASSERT_HELD - the gate is currently closed and the current thread is the holder
+ */
+void
+lck_rw_gate_assert(__assert_only lck_rw_t *lock, gate_t *gate, int flags)
+{
+       LCK_RW_ASSERT(lock, LCK_RW_ASSERT_HELD);
+
+       gate_assert(gate, flags);
+       return;
+}
+
+/*
+ * Name: lck_mtx_gate_init
+ *
+ * Description: initializes a variable declared with decl_lck_mtx_gate_data.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ */
+void
+lck_mtx_gate_init(lck_mtx_t *lock, gate_t *gate)
+{
+       (void) lock;
+       gate_init(gate);
+}
+
+/*
+ * Name: lck_mtx_gate_destroy
+ *
+ * Description: destroys a variable previously initialized
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ */
+void
+lck_mtx_gate_destroy(lck_mtx_t *lock, gate_t *gate)
+{
+       (void) lock;
+       gate_destroy(gate);
+}
+
+/*
+ * Name: lck_mtx_gate_try_close
+ *
+ * Description: Tries to close the gate.
+ *              In case of success the current thread will be set as
+ *              the holder of the gate.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *
+ * Returns:
+ *          KERN_SUCCESS in case the gate was successfully closed. The current thread is the new holder
+ *          of the gate.
+ *          A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
+ *          to wake up possible waiters on the gate before returning to userspace.
+ *          If the intent is to conditionally probe the gate before waiting, the lock must not be dropped
+ *          between the calls to lck_mtx_gate_try_close() and lck_mtx_gate_wait().
+ *
+ *          KERN_FAILURE in case the gate was already closed. Will panic if the current thread was already the holder of the gate.
+ *          lck_mtx_gate_wait() should be called instead if the intent is to unconditionally wait on this gate.
+ *          The calls to lck_mtx_gate_try_close() and lck_mtx_gate_wait() should
+ *          be done without dropping the lock that is protecting the gate in between.
+ */
+int
+lck_mtx_gate_try_close(__assert_only lck_mtx_t *lock, gate_t *gate)
+{
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       return gate_try_close(gate);
+}
+
+/*
+ * Name: lck_mtx_gate_close
+ *
+ * Description: Closes the gate. The current thread will be set as
+ *              the holder of the gate. Will panic if the gate is already closed.
+ *              A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
+ *              to wake up possible waiters on the gate before returning to userspace.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The gate must be open.
+ *
+ */
 void
-hw_atomic_or_noret(volatile uint32_t *dest, uint32_t mask)
+lck_mtx_gate_close(__assert_only lck_mtx_t *lock, gate_t *gate)
 {
-       ALIGN_TEST(dest, uint32_t);
-       __c11_atomic_fetch_or(ATOMIC_CAST(uint32_t, dest), mask, memory_order_relaxed);
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       return gate_close(gate);
+}
+
+/*
+ * Name: lck_mtx_gate_open
+ *
+ * Description: Opens of the gate and wakes up possible waiters.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The current thread must be the holder of the gate.
+ *
+ */
+void
+lck_mtx_gate_open(__assert_only lck_mtx_t *lock, gate_t *gate)
+{
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       gate_open(gate);
 }
 
-uint32_t
-hw_atomic_and(volatile uint32_t *dest, uint32_t mask)
+/*
+ * Name: lck_mtx_gate_handoff
+ *
+ * Description: Set the current ownership of the gate. The waiter with highest sched
+ *              priority will be selected as the new holder of the gate, and woken up,
+ *              with the gate remaining in the closed state throughout.
+ *              If no waiters are present, the gate will be kept closed and KERN_NOT_WAITING
+ *              will be returned.
+ *              OPEN_ON_FAILURE flag can be used to specify if the gate should be opened in
+ *              case no waiters were found.
+ *
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *   Arg3: flags - GATE_NO_FALGS or OPEN_ON_FAILURE
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The current thread must be the holder of the gate.
+ *
+ * Returns:
+ *          KERN_SUCCESS in case one of the waiters became the new holder.
+ *          KERN_NOT_WAITING in case there were no waiters.
+ *
+ */
+kern_return_t
+lck_mtx_gate_handoff(__assert_only lck_mtx_t *lock, gate_t *gate, int flags)
 {
-       ALIGN_TEST(dest, uint32_t);
-       return __c11_atomic_fetch_and(ATOMIC_CAST(uint32_t, dest), mask, memory_order_relaxed) & mask;
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       return gate_handoff(gate, flags);
 }
 
+/*
+ * Name: lck_mtx_gate_steal
+ *
+ * Description: Steals the ownership of the gate. It sets the current thread as the
+ *              new holder of the gate.
+ *              A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
+ *              to wake up possible waiters on the gate before returning to userspace.
+ *              NOTE: the previous holder should not call lck_mtx_gate_open() or lck_mtx_gate_handoff()
+ *              anymore.
+ *
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *
+ * Conditions: Lock must be held. Returns with the lock held.
+ *             The gate must be closed and the current thread must not already be the holder.
+ *
+ */
 void
-hw_atomic_and_noret(volatile uint32_t *dest, uint32_t mask)
+lck_mtx_gate_steal(__assert_only lck_mtx_t *lock, gate_t *gate)
+{
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       gate_steal(gate);
+}
+
+/*
+ * Name: lck_mtx_gate_wait
+ *
+ * Description: Waits for the current thread to become the holder of the gate or for the
+ *              gate to become open. An interruptible mode and deadline can be specified
+ *              to return earlier from the wait.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *   Arg3: sleep action. LCK_SLEEP_DEFAULT, LCK_SLEEP_UNLOCK, LCK_SLEEP_SPIN, LCK_SLEEP_SPIN_ALWAYS.
+ *   Arg3: interruptible flag for wait.
+ *   Arg4: deadline
+ *
+ * Conditions: Lock must be held. Returns with the lock held according to the sleep action specified.
+ *             Lock will be dropped while waiting.
+ *             The gate must be closed.
+ *
+ * Returns: Reason why the thread was woken up.
+ *          GATE_HANDOFF - the current thread was handed off the ownership of the gate.
+ *                         A matching lck_mtx_gate_open() or lck_mtx_gate_handoff() needs to be called later on
+ *                         to wake up possible waiters on the gate before returning to userspace.
+ *          GATE_OPENED - the gate was opened by the holder.
+ *          GATE_TIMED_OUT - the thread was woken up by a timeout.
+ *          GATE_INTERRUPTED - the thread was interrupted while sleeping.
+ *
+ */
+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)
 {
-       ALIGN_TEST(dest, uint32_t);
-       __c11_atomic_fetch_and(ATOMIC_CAST(uint32_t, dest), mask, memory_order_relaxed);
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       if (lck_sleep_action & LCK_SLEEP_UNLOCK) {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_mtx_unlock(lock);},
+                          ^{;});
+       } else if (lck_sleep_action & LCK_SLEEP_SPIN) {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_mtx_unlock(lock);},
+                          ^{lck_mtx_lock_spin(lock);});
+       } else if (lck_sleep_action & LCK_SLEEP_SPIN_ALWAYS) {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_mtx_unlock(lock);},
+                          ^{lck_mtx_lock_spin_always(lock);});
+       } else {
+               return gate_wait(gate,
+                          interruptible,
+                          deadline,
+                          ^{lck_mtx_unlock(lock);},
+                          ^{lck_mtx_lock(lock);});
+       }
 }
 
-uint32_t
-hw_compare_and_store(uint32_t oldval, uint32_t newval, volatile uint32_t *dest)
+/*
+ * Name: lck_mtx_gate_assert
+ *
+ * Description: asserts that the gate is in the specified state.
+ *
+ * Args:
+ *   Arg1: lck_mtx_t lock used to protect the gate.
+ *   Arg2: pointer to the gate data declared with decl_lck_mtx_gate_data.
+ *   Arg3: flags to specified assert type.
+ *         GATE_ASSERT_CLOSED - the gate is currently closed
+ *         GATE_ASSERT_OPEN - the gate is currently opened
+ *         GATE_ASSERT_HELD - the gate is currently closed and the current thread is the holder
+ */
+void
+lck_mtx_gate_assert(__assert_only lck_mtx_t *lock, gate_t *gate, int flags)
 {
-       ALIGN_TEST(dest, uint32_t);
-       return __c11_atomic_compare_exchange_strong(ATOMIC_CAST(uint32_t, dest), &oldval, newval,
-                  memory_order_acq_rel_smp, memory_order_relaxed);
+       LCK_MTX_ASSERT(lock, LCK_MTX_ASSERT_OWNED);
+
+       gate_assert(gate, flags);
 }