1 package kotlinx.coroutines.channels
2
3 import kotlinx.coroutines.*
4 import kotlin.coroutines.*
5
6 internal open class ChannelCoroutine<E>(
7 parentContext: CoroutineContext,
8 protected val _channel: Channel<E>,
9 initParentJob: Boolean,
10 active: Boolean
<lambda>null11 ) : AbstractCoroutine<Unit>(parentContext, initParentJob, active), Channel<E> by _channel {
12
13 val channel: Channel<E> get() = this
14
15 @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x")
16 override fun cancel() {
17 cancelInternal(defaultCancellationException())
18 }
19
20 @Suppress("MULTIPLE_DEFAULTS_INHERITED_FROM_SUPERTYPES_DEPRECATION_WARNING") // do not remove the MULTIPLE_DEFAULTS suppression: required in K2
21 @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x")
22 final override fun cancel(cause: Throwable?): Boolean {
23 cancelInternal(defaultCancellationException())
24 return true
25 }
26
27 @Suppress("MULTIPLE_DEFAULTS_INHERITED_FROM_SUPERTYPES_DEPRECATION_WARNING") // do not remove the MULTIPLE_DEFAULTS suppression: required in K2
28 final override fun cancel(cause: CancellationException?) {
29 if (isCancelled) return // Do not create an exception if the coroutine (-> the channel) is already cancelled
30 cancelInternal(cause ?: defaultCancellationException())
31 }
32
33 override fun cancelInternal(cause: Throwable) {
34 val exception = cause.toCancellationException()
35 _channel.cancel(exception) // cancel the channel
36 cancelCoroutine(exception) // cancel the job
37 }
38 }
39