-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCompletedExceptionally.kt
43 lines (37 loc) · 1.53 KB
/
CompletedExceptionally.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlinx.atomicfu.*
import kotlin.coroutines.*
import kotlin.jvm.*
internal fun <T> Result<T>.toState(): Any? =
if (isSuccess) getOrThrow() else CompletedExceptionally(exceptionOrNull()!!) // todo: need to do it better
/**
* Class for an internal state of a job that was cancelled (completed exceptionally).
*
* @param cause the exceptional completion cause. It's either original exceptional cause
* or artificial [CancellationException] if no cause was provided
*/
internal open class CompletedExceptionally(
@JvmField public val cause: Throwable
) {
override fun toString(): String = "$classSimpleName[$cause]"
}
/**
* A specific subclass of [CompletedExceptionally] for cancelled [AbstractContinuation].
*
* @param continuation the continuation that was cancelled.
* @param cause the exceptional completion cause. If `cause` is null, then a [CancellationException]
* if created on first access to [exception] property.
*/
internal class CancelledContinuation(
continuation: Continuation<*>,
cause: Throwable?,
handled: Boolean
) : CompletedExceptionally(cause ?: CancellationException("Continuation $continuation was cancelled normally")) {
private val resumed = atomic(false)
private val handled = atomic(handled)
fun makeResumed(): Boolean = resumed.compareAndSet(false, true)
fun makeHandled(): Boolean = handled.compareAndSet(false, true)
}