@@ -255,37 +255,9 @@ internal open class CancellableContinuationImpl<in T>(
255
255
}
256
256
}
257
257
258
- private fun checkCancellation (): Job ? {
259
- // Don't need to check for non-reusable continuations, handle is already installed
260
- if (! resumeMode.isReusableMode) return null
261
- val parent: Job ?
262
- if (parentHandle == null ) {
263
- // No parent -- no postponed and no async cancellations
264
- parent = context[Job ] ? : return null
265
- /*
266
- * Rare slow-path: parent handle is not yet installed in reusable CC,
267
- * but parent is cancelled. Just let already existing machinery to figure everything out
268
- * and advance state machine for us.
269
- */
270
- if (parent.isCancelled) {
271
- installParentHandleReusable(parent)
272
- return parent
273
- }
274
- } else {
275
- // Parent handle is not null, no need to lookup it
276
- parent = null
277
- }
278
- return parent
279
- }
280
-
281
258
@PublishedApi
282
259
internal fun getResult (): Any? {
283
260
val isReusable = isReusable()
284
- /*
285
- * Check postponed or async cancellation for reusable continuations.
286
- * Returns job to avoid looking it up twice
287
- */
288
- val parentJob = checkCancellation()
289
261
// trySuspend may fail either if 'block' has resumed/cancelled a continuation
290
262
// or we got async cancellation from parent.
291
263
if (trySuspend()) {
@@ -295,7 +267,7 @@ internal open class CancellableContinuationImpl<in T>(
295
267
* so CC could be properly resumed on parent cancellation.
296
268
*/
297
269
if (parentHandle == null ) {
298
- installParentHandleReusable(parentJob )
270
+ installParentHandleReusable()
299
271
} else if (isReusable) {
300
272
releaseClaimedReusableContinuation()
301
273
}
@@ -321,14 +293,13 @@ internal open class CancellableContinuationImpl<in T>(
321
293
return getSuccessfulResult(state)
322
294
}
323
295
324
- private fun installParentHandleReusable (parent : Job ? ) {
325
- if ( parent == null ) return // don't do anything without parent or if completed
296
+ private fun installParentHandleReusable () {
297
+ val parent = context[ Job ] ? : return // don't do anything without parent or if completed
326
298
// Install the handle
327
- val handle = parent.invokeOnCompletion(
299
+ parentHandle = parent.invokeOnCompletion(
328
300
onCancelling = true ,
329
301
handler = ChildContinuation (this ).asHandler
330
302
)
331
- parentHandle = handle
332
303
/*
333
304
* Finally release the continuation after installing the handle. If we were successful, then
334
305
* do nothing, it's ok to reuse the instance now.
@@ -338,7 +309,8 @@ internal open class CancellableContinuationImpl<in T>(
338
309
}
339
310
340
311
private fun releaseClaimedReusableContinuation () {
341
- val cancellationCause = (delegate as DispatchedContinuation <* >).tryReleaseClaimedContinuation(this ) ? : return
312
+ // Cannot be casted if e.g. invoked from `installParentHandleReusable` for context without dispatchers, but with Job in it
313
+ val cancellationCause = (delegate as ? DispatchedContinuation <* >)?.tryReleaseClaimedContinuation(this ) ? : return
342
314
parentHandle?.let {
343
315
it.dispose()
344
316
parentHandle = NonDisposableHandle
0 commit comments