@@ -104,24 +104,26 @@ public abstract class CoroutineDispatcher :
104
104
105
105
// named class for ease of debugging, better stack-traces and optimize the number of anonymous classes
106
106
internal class DispatchTask <in T >(
107
- private val dispatched : DispatchedContinuation <T >,
107
+ private val continuation : Continuation <T >,
108
108
private val value : Any? , // T | Throwable
109
109
private val exception : Boolean ,
110
110
private val cancellable : Boolean
111
111
) : Runnable {
112
112
@Suppress(" UNCHECKED_CAST" )
113
113
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
+ }
120
122
}
121
123
}
122
124
123
125
override fun toString (): String =
124
- " DispatchTask[$value , cancellable=$cancellable , $dispatched ]"
126
+ " DispatchTask[$value , cancellable=$cancellable , $continuation ]"
125
127
}
126
128
127
129
internal class DispatchedContinuation <in T >(
@@ -131,15 +133,15 @@ internal class DispatchedContinuation<in T>(
131
133
override fun resume (value : T ) {
132
134
val context = continuation.context
133
135
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 ))
135
137
else
136
138
resumeUndispatched(value)
137
139
}
138
140
139
141
override fun resumeWithException (exception : Throwable ) {
140
142
val context = continuation.context
141
143
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 ))
143
145
else
144
146
resumeUndispatchedWithException(exception)
145
147
}
@@ -148,7 +150,7 @@ internal class DispatchedContinuation<in T>(
148
150
inline fun resumeCancellable (value : T ) {
149
151
val context = continuation.context
150
152
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 ))
152
154
else
153
155
resumeUndispatched(value)
154
156
}
@@ -157,7 +159,7 @@ internal class DispatchedContinuation<in T>(
157
159
inline fun resumeCancellableWithException (exception : Throwable ) {
158
160
val context = continuation.context
159
161
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 ))
161
163
else
162
164
resumeUndispatchedWithException(exception)
163
165
}
@@ -177,16 +179,9 @@ internal class DispatchedContinuation<in T>(
177
179
}
178
180
179
181
// used by "yield" implementation
180
- internal fun dispatchYield (job : Job ? , value : T ) {
182
+ internal fun dispatchYield (value : T ) {
181
183
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 ))
190
185
}
191
186
192
187
override fun toString (): String = " DispatchedContinuation[$dispatcher , $continuation ]"
0 commit comments