Questions on `Mutex.kt` ``` internal class MutexI...
# coroutines
d
Questions on
Mutex.kt
Copy code
internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2<Any?, Mutex> {
    // State is: Empty | LockedQueue | OpDescriptor
    // shared objects while we have no waiters
    private val _state = atomic<Any?>(if (locked) EmptyLocked else EmptyUnlocked)

    // resumeNext is: RESUME_QUIESCENT | RESUME_ACTIVE | ResumeReq
    private val _resumeNext = atomic<Any>(RESUME_QUIESCENT)

    public override val isLocked: Boolean get() {
        _state.loop { state ->
            when (state) {
                is Empty -> return state.locked !== UNLOCKED
                is LockedQueue -> return true
                is OpDescriptor -> state.perform(this) // help
                else -> error("Illegal state $state")
            }
        }
    }
1. Please, explain, what is meant by each of
Empty | LockedQueue | OpDescriptor
? 2. What is for
resumeNext is: RESUME_QUIESCENT | RESUME_ACTIVE | ResumeReq
? 3. What means the `help`comment in
is OpDescriptor -> state.perform(this) // help
? 😅
e
1 & 2 explain what possible types can be stored in the corresponding variable with declared type of
Any?
3. Read docs in
OpDescriptor
class and
AtomicOp
in the same file.
👌 1
d
Well, ok, gonna study the paper
I wanted to understand how the mutex is done, that is what questions 1&2 are about. Can not understand what
OpDescriptor
is for. Is there a popular preparation of Mutex?
e
You don’t have to understand
OpDescriptor
for that. It is only use when Mutex is a part of
select
, which you can ignore at first. Assume that
OpDescriptor
never happens there