1 /*
2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 package kotlinx.coroutines.channels
6
7 import kotlinx.coroutines.*
8 import kotlin.coroutines.*
9
10 internal open class ChannelCoroutine<E>(
11 parentContext: CoroutineContext,
12 protected val _channel: Channel<E>,
13 initParentJob: Boolean,
14 active: Boolean
<lambda>null15 ) : AbstractCoroutine<Unit>(parentContext, initParentJob, active), Channel<E> by _channel {
16
17 val channel: Channel<E> get() = this
18
19 @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x")
20 override fun cancel() {
21 cancelInternal(defaultCancellationException())
22 }
23
24 @Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.2.0, binary compatibility with versions <= 1.1.x")
25 final override fun cancel(cause: Throwable?): Boolean {
26 cancelInternal(defaultCancellationException())
27 return true
28 }
29
30 final override fun cancel(cause: CancellationException?) {
31 if (isCancelled) return // Do not create an exception if the coroutine (-> the channel) is already cancelled
32 cancelInternal(cause ?: defaultCancellationException())
33 }
34
35 override fun cancelInternal(cause: Throwable) {
36 val exception = cause.toCancellationException()
37 _channel.cancel(exception) // cancel the channel
38 cancelCoroutine(exception) // cancel the job
39 }
40 }
41