Skip to content

Commit 1c5efc8

Browse files
committed
Remove SOE avoidance in Mutex
1 parent 7ee722a commit 1c5efc8

File tree

1 file changed

+1
-51
lines changed
  • common/kotlinx-coroutines-core-common/src/sync

1 file changed

+1
-51
lines changed

common/kotlinx-coroutines-core-common/src/sync/Mutex.kt

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ private val UNLOCK_FAIL = Symbol("UNLOCK_FAIL")
119119
private val SELECT_SUCCESS = Symbol("SELECT_SUCCESS")
120120
private val LOCKED = Symbol("LOCKED")
121121
private val UNLOCKED = Symbol("UNLOCKED")
122-
private val RESUME_QUIESCENT = Symbol("RESUME_QUIESCENT")
123-
private val RESUME_ACTIVE = Symbol("RESUME_ACTIVE")
124122

125123
private val EmptyLocked = Empty(LOCKED)
126124
private val EmptyUnlocked = Empty(UNLOCKED)
@@ -136,9 +134,6 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2<Any?, Mutex> {
136134
// shared objects while we have no waiters
137135
private val _state = atomic<Any?>(if (locked) EmptyLocked else EmptyUnlocked)
138136

139-
// resumeNext is: RESUME_QUIESCENT | RESUME_ACTIVE | ResumeReq
140-
private val _resumeNext = atomic<Any>(RESUME_QUIESCENT)
141-
142137
public override val isLocked: Boolean get() {
143138
_state.loop { state ->
144139
when (state) {
@@ -332,16 +327,8 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2<Any?, Mutex> {
332327
} else {
333328
val token = (waiter as LockWaiter).tryResumeLockWaiter()
334329
if (token != null) {
335-
// successfully resumed waiter that now is holding the lock
336-
// we must immediately transfer ownership to the next waiter, because this coroutine
337-
// might try to lock it again after unlock returns do to StackOverflow avoidance code
338-
// and its attempts to take a lock must be queued.
339330
state.owner = waiter.owner ?: LOCKED
340-
// StackOverflow avoidance code
341-
if (startResumeNext(waiter, token)) {
342-
waiter.completeResumeLockWaiter(token)
343-
finishResumeNext()
344-
}
331+
waiter.completeResumeLockWaiter(token)
345332
return
346333
}
347334
}
@@ -351,43 +338,6 @@ internal class MutexImpl(locked: Boolean) : Mutex, SelectClause2<Any?, Mutex> {
351338
}
352339
}
353340

354-
private class ResumeReq(
355-
@JvmField val waiter: LockWaiter,
356-
@JvmField val token: Any
357-
)
358-
359-
private fun startResumeNext(waiter: LockWaiter, token: Any): Boolean {
360-
_resumeNext.loop { resumeNext ->
361-
when {
362-
resumeNext === RESUME_QUIESCENT -> {
363-
// this is never concurrent, because only one thread is holding mutex and trying to resume
364-
// next waiter, so no need to CAS here
365-
_resumeNext.value = RESUME_ACTIVE
366-
return true
367-
}
368-
resumeNext === RESUME_ACTIVE ->
369-
if (_resumeNext.compareAndSet(resumeNext, ResumeReq(waiter, token))) return false
370-
else -> error("Cannot happen")
371-
}
372-
}
373-
}
374-
375-
private fun finishResumeNext() {
376-
// also a resumption loop to fulfill requests of inner resume invokes
377-
_resumeNext.loop { resumeNext ->
378-
when {
379-
resumeNext === RESUME_ACTIVE ->
380-
if (_resumeNext.compareAndSet(resumeNext, RESUME_QUIESCENT)) return
381-
resumeNext is ResumeReq -> {
382-
// this is never concurrently, only one thread is finishing, so no need to CAS here
383-
_resumeNext.value = RESUME_ACTIVE
384-
resumeNext.waiter.completeResumeLockWaiter(resumeNext.token)
385-
}
386-
else -> error("Cannot happen")
387-
}
388-
}
389-
}
390-
391341
override fun toString(): String {
392342
_state.loop { state ->
393343
when (state) {

0 commit comments

Comments
 (0)