Skip to content

Commit 01886b4

Browse files
committed
Remove MODE_DIRECT resume mode
Now, there are only three resume modes that are used by DispatchedTask and the corresponding constants are moved to DispatchedTask.kt file.
1 parent e33c68a commit 01886b4

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

kotlinx-coroutines-core/common/src/Builders.common.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ public suspend fun <T> withContext(
142142
newContext.checkCompletion()
143143
// FAST PATH #1 -- new context is the same as the old one
144144
if (newContext === oldContext) {
145-
val coroutine = ScopeCoroutine(newContext, uCont) // MODE_DIRECT
145+
val coroutine = ScopeCoroutine(newContext, uCont)
146146
return@sc coroutine.startUndispatchedOrReturn(coroutine, block)
147147
}
148148
// FAST PATH #2 -- the new dispatcher is the same as the old one (something else changed)
149149
// `equals` is used by design (see equals implementation is wrapper context like ExecutorCoroutineDispatcher)
150150
if (newContext[ContinuationInterceptor] == oldContext[ContinuationInterceptor]) {
151-
val coroutine = UndispatchedCoroutine(newContext, uCont) // MODE_UNDISPATCHED
151+
val coroutine = UndispatchedCoroutine(newContext, uCont)
152152
// There are changes in the context, so this thread needs to be updated
153153
withCoroutineContext(newContext, null) {
154154
return@sc coroutine.startUndispatchedOrReturn(coroutine, block)
155155
}
156156
}
157157
// SLOW PATH -- use new dispatcher
158-
val coroutine = DispatchedCoroutine(newContext, uCont) // MODE_CANCELLABLE
158+
val coroutine = DispatchedCoroutine(newContext, uCont)
159159
coroutine.initParentJob()
160160
block.startCoroutineCancellable(coroutine, coroutine)
161161
coroutine.getResult()

kotlinx-coroutines-core/common/src/ResumeMode.kt

-13
This file was deleted.

kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import kotlinx.coroutines.internal.*
88
import kotlin.coroutines.*
99
import kotlin.jvm.*
1010

11+
@PublishedApi internal const val MODE_ATOMIC_DEFAULT = 0 // schedule non-cancellable dispatch for suspendCoroutine
12+
@PublishedApi internal const val MODE_CANCELLABLE = 1 // schedule cancellable dispatch for suspendCancellableCoroutine
13+
@PublishedApi internal const val MODE_UNDISPATCHED = 2 // when the thread is right, but need to mark it with current coroutine
14+
15+
internal val Int.isCancellableMode get() = this == MODE_CANCELLABLE
16+
internal val Int.isDispatchedMode get() = this == MODE_ATOMIC_DEFAULT || this == MODE_CANCELLABLE
17+
1118
internal abstract class DispatchedTask<in T>(
1219
@JvmField public var resumeMode: Int
1320
) : SchedulerTask() {
@@ -89,7 +96,7 @@ internal abstract class DispatchedTask<in T>(
8996
}
9097
}
9198

92-
internal fun <T> DispatchedTask<T>.dispatch(mode: Int = MODE_CANCELLABLE) {
99+
internal fun <T> DispatchedTask<T>.dispatch(mode: Int) {
93100
val delegate = this.delegate
94101
if (mode.isDispatchedMode && delegate is DispatchedContinuation<*> && mode.isCancellableMode == resumeMode.isCancellableMode) {
95102
// dispatch directly using this instance's Runnable implementation
@@ -125,7 +132,6 @@ internal fun <T> DispatchedTask<T>.resume(delegate: Continuation<T>, useMode: In
125132
when (useMode) {
126133
MODE_ATOMIC_DEFAULT -> delegate.resumeWith(result)
127134
MODE_CANCELLABLE -> delegate.resumeCancellableWith(result)
128-
MODE_DIRECT -> ((delegate as? DispatchedContinuation)?.continuation ?: delegate).resumeWith(result)
129135
MODE_UNDISPATCHED -> (delegate as DispatchedContinuation).resumeUndispatchedWith(result)
130136
else -> error("Invalid mode $useMode")
131137
}

kotlinx-coroutines-core/common/src/selects/Select.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public interface SelectInstance<in R> {
128128
/**
129129
* Returns completion continuation of this select instance.
130130
* This select instance must be _selected_ first.
131-
* All resumption through this instance happen _directly_ without going through dispatcher ([MODE_DIRECT]).
131+
* All resumption through this instance happen _directly_ without going through dispatcher.
132132
*/
133133
public val completion: Continuation<R>
134134

@@ -271,7 +271,7 @@ internal class SelectBuilderImpl<in R>(
271271
}
272272
}
273273

274-
// Resumes in MODE_DIRECT
274+
// Resumes in direct mode, without going through dispatcher. Should be called in the same context.
275275
override fun resumeWith(result: Result<R>) {
276276
doResume({ result.toState() }) {
277277
if (result.isFailure) {
@@ -282,7 +282,7 @@ internal class SelectBuilderImpl<in R>(
282282
}
283283
}
284284

285-
// Resumes in MODE_CANCELLABLE
285+
// Resumes in MODE_CANCELLABLE, can be called from an arbitrary context
286286
override fun resumeSelectCancellableWithException(exception: Throwable) {
287287
doResume({ CompletedExceptionally(exception) }) {
288288
uCont.intercepted().resumeCancellableWith(Result.failure(exception))

0 commit comments

Comments
 (0)