Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/sync/Mutex.kt (Kotlin)

Code before:
            }
        }
    }

    override fun toString(): String {
        _state.loop { state ->
            when (state) {
                is Empty -> return "Mutex[${state.locked}]"
                is OpDescriptor -> state.perform(this)
                is LockedQueue -> return "Mutex[${state.owner}]"
                else -> error("Illegal state $state")
            }
        }
    }

    private class LockedQueue(
        @JvmField var owner: Any
    ) : LockFreeLinkedListHead() {
        override fun toString(): String = "LockedQueue[$owner]"
    }

    private abstract inner class LockWaiter(
        @JvmField val owner: Any?
    ) : LockFreeLinkedListNode(), DisposableHandle {
        private val isTaken = atomic<Boolean>(false)
        fun take(): Boolean = isTaken.compareAndSet(false, true)
        final override fun dispose() { remove() }
        abstract fun tryResumeLockWaiter(): Boolean
        abstract fun completeResumeLockWaiter()
    }

    private inner class LockCont(
        owner: Any?,
        private val cont: CancellableContinuation<Unit>
    ) : LockWaiter(owner) {

        override fun tryResumeLockWaiter(): Boolean {
            if (!take()) return false
            return cont.tryResume(Unit, idempotent = null) {
                // if this continuation gets cancelled during dispatch to the caller, then release the lock
                unlock(owner)
            } != null
        }

        override fun completeResumeLockWaiter() = cont.completeResume(RESUME_TOKEN)
        override fun toString(): String = "LockCont[$owner, ${cont}] for ${this@MutexImpl}"
    }

    private inner class LockSelect<R>(
        owner: Any?,
        @JvmField val select: SelectInstance<R>,

Review comment (suggestion):
it's possible to avoid this field and use an atomic wrapper over the owner (then it has to be passed between `tryResumeLockWaiter` and `completeResumeLockWaiter`), but I found it way too harsh

Code after:
                else -> error("Illegal state $state")
            }
        }
    }

    override fun toString(): String {
        _state.loop { state ->
            when (state) {
                is Empty -> return "Mutex[${state.locked}]"
                is OpDescriptor -> state.perform(this)
                is LockedQueue -> return "Mutex[${state.owner}]"
                else -> error("Illegal state $state")
            }
        }
    }

    private class LockedQueue(
        @JvmField var owner: Any
    ) : LockFreeLinkedListHead() {
        override fun toString(): String = "LockedQueue[$owner]"
    }

    private abstract inner class LockWaiter(
        @JvmField val owner: Any?
    ) : LockFreeLinkedListNode(), DisposableHandle {
        private val isTaken = atomic<Boolean>(false)
        fun take(): Boolean = isTaken.compareAndSet(false, true)
        final override fun dispose() { remove() }
        abstract fun tryResumeLockWaiter(): Boolean
        abstract fun completeResumeLockWaiter()
    }

    private inner class LockCont(
        owner: Any?,
        private val cont: CancellableContinuation<Unit>
    ) : LockWaiter(owner) {

        override fun tryResumeLockWaiter(): Boolean {
            if (!take()) return false
            return cont.tryResume(Unit, idempotent = null) {
                // if this continuation gets cancelled during dispatch to the caller, then release the lock
                unlock(owner)
            } != null
        }

        override fun completeResumeLockWaiter() = cont.completeResume(RESUME_TOKEN)
        override fun toString(): String = "LockCont[$owner, ${cont}] for ${this@MutexImpl}"
    }

    private inner class LockSelect<R>(
        owner: Any?,