Skip to content

Commit 2a93646

Browse files
committed
Refactor DispatchTask to reuse it for dispatchYield
1 parent e824eaa commit 2a93646

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineDispatcher.kt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,26 @@ public abstract class CoroutineDispatcher :
104104

105105
// named class for ease of debugging, better stack-traces and optimize the number of anonymous classes
106106
internal class DispatchTask<in T>(
107-
private val dispatched: DispatchedContinuation<T>,
107+
private val continuation: Continuation<T>,
108108
private val value: Any?, // T | Throwable
109109
private val exception: Boolean,
110110
private val cancellable: Boolean
111111
) : Runnable {
112112
@Suppress("UNCHECKED_CAST")
113113
override fun run() {
114-
val job = if (cancellable) dispatched.context[Job] else null
115-
when {
116-
job != null && job.isCancelledOrCompleted ->
117-
dispatched.resumeUndispatchedWithException(job.getCompletionException())
118-
exception -> dispatched.resumeUndispatchedWithException(value as Throwable)
119-
else -> dispatched.resumeUndispatched(value as T)
114+
val context = continuation.context
115+
val job = if (cancellable) context[Job] else null
116+
withCoroutineContext(context) {
117+
when {
118+
job != null && job.isCancelledOrCompleted -> continuation.resumeWithException(job.getCompletionException())
119+
exception -> continuation.resumeWithException(value as Throwable)
120+
else -> continuation.resume(value as T)
121+
}
120122
}
121123
}
122124

123125
override fun toString(): String =
124-
"DispatchTask[$value, cancellable=$cancellable, $dispatched]"
126+
"DispatchTask[$value, cancellable=$cancellable, $continuation]"
125127
}
126128

127129
internal class DispatchedContinuation<in T>(
@@ -131,15 +133,15 @@ internal class DispatchedContinuation<in T>(
131133
override fun resume(value: T) {
132134
val context = continuation.context
133135
if (dispatcher.isDispatchNeeded(context))
134-
dispatcher.dispatch(context, DispatchTask(this, value, exception = false, cancellable = false))
136+
dispatcher.dispatch(context, DispatchTask(continuation, value, exception = false, cancellable = false))
135137
else
136138
resumeUndispatched(value)
137139
}
138140

139141
override fun resumeWithException(exception: Throwable) {
140142
val context = continuation.context
141143
if (dispatcher.isDispatchNeeded(context))
142-
dispatcher.dispatch(context, DispatchTask(this, exception, exception = true, cancellable = false))
144+
dispatcher.dispatch(context, DispatchTask(continuation, exception, exception = true, cancellable = false))
143145
else
144146
resumeUndispatchedWithException(exception)
145147
}
@@ -148,7 +150,7 @@ internal class DispatchedContinuation<in T>(
148150
inline fun resumeCancellable(value: T) {
149151
val context = continuation.context
150152
if (dispatcher.isDispatchNeeded(context))
151-
dispatcher.dispatch(context, DispatchTask(this, value, exception = false, cancellable = true))
153+
dispatcher.dispatch(context, DispatchTask(continuation, value, exception = false, cancellable = true))
152154
else
153155
resumeUndispatched(value)
154156
}
@@ -157,7 +159,7 @@ internal class DispatchedContinuation<in T>(
157159
inline fun resumeCancellableWithException(exception: Throwable) {
158160
val context = continuation.context
159161
if (dispatcher.isDispatchNeeded(context))
160-
dispatcher.dispatch(context, DispatchTask(this, exception, exception = true, cancellable = true))
162+
dispatcher.dispatch(context, DispatchTask(continuation, exception, exception = true, cancellable = true))
161163
else
162164
resumeUndispatchedWithException(exception)
163165
}
@@ -177,16 +179,9 @@ internal class DispatchedContinuation<in T>(
177179
}
178180

179181
// used by "yield" implementation
180-
internal fun dispatchYield(job: Job?, value: T) {
182+
internal fun dispatchYield(value: T) {
181183
val context = continuation.context
182-
dispatcher.dispatch(context, Runnable {
183-
withCoroutineContext(context) {
184-
if (job != null && job.isCancelledOrCompleted)
185-
continuation.resumeWithException(job.getCompletionException())
186-
else
187-
continuation.resume(value)
188-
}
189-
})
184+
dispatcher.dispatch(context, DispatchTask(continuation, value,false, true))
190185
}
191186

192187
override fun toString(): String = "DispatchedContinuation[$dispatcher, $continuation]"

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Yield.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ suspend fun yield(): Unit = suspendCoroutineOrReturn sc@ { cont ->
3333
if (job != null && job.isCancelledOrCompleted) throw job.getCompletionException()
3434
if (cont !is DispatchedContinuation<Unit>) return@sc Unit
3535
if (!cont.dispatcher.isDispatchNeeded(context)) return@sc Unit
36-
cont.dispatchYield(job, Unit)
36+
cont.dispatchYield(Unit)
3737
COROUTINE_SUSPENDED
3838
}

0 commit comments

Comments
 (0)