From c77525761e20bcafbd862334c1f5a6b6f73c2e98 Mon Sep 17 00:00:00 2001 From: Francesco Vasco Date: Mon, 29 Oct 2018 11:26:22 +0100 Subject: [PATCH] Release block on LazyXxxCoroutine start --- .../src/Builders.common.kt | 12 ++++++++++-- .../src/channels/Broadcast.kt | 6 +++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/common/kotlinx-coroutines-core-common/src/Builders.common.kt b/common/kotlinx-coroutines-core-common/src/Builders.common.kt index b077b41d51..6b34cad7c2 100644 --- a/common/kotlinx-coroutines-core-common/src/Builders.common.kt +++ b/common/kotlinx-coroutines-core-common/src/Builders.common.kt @@ -104,9 +104,13 @@ private open class DeferredCoroutine( private class LazyDeferredCoroutine( parentContext: CoroutineContext, - private val block: suspend CoroutineScope.() -> T + block: suspend CoroutineScope.() -> T ) : DeferredCoroutine(parentContext, active = false) { + private var block: (suspend CoroutineScope.() -> T)? = block + override fun onStart() { + val block = checkNotNull(this.block) { "Already started" } + this.block = null block.startCoroutineCancellable(this, this) } } @@ -161,9 +165,13 @@ private open class StandaloneCoroutine( private class LazyStandaloneCoroutine( parentContext: CoroutineContext, - private val block: suspend CoroutineScope.() -> Unit + block: suspend CoroutineScope.() -> Unit ) : StandaloneCoroutine(parentContext, active = false) { + private var block: (suspend CoroutineScope.() -> Unit)? = block + override fun onStart() { + val block = checkNotNull(this.block) { "Already started" } + this.block = null block.startCoroutineCancellable(this, this) } } diff --git a/common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt b/common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt index 00df04fde1..8da21138a4 100644 --- a/common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt +++ b/common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt @@ -106,8 +106,10 @@ private open class BroadcastCoroutine( private class LazyBroadcastCoroutine( parentContext: CoroutineContext, channel: BroadcastChannel, - private val block: suspend ProducerScope.() -> Unit + block: suspend ProducerScope.() -> Unit ) : BroadcastCoroutine(parentContext, channel, active = false) { + private var block: (suspend ProducerScope.() -> Unit)? = block + override fun openSubscription(): ReceiveChannel { // open subscription _first_ val subscription = _channel.openSubscription() @@ -117,6 +119,8 @@ private class LazyBroadcastCoroutine( } override fun onStart() { + val block = checkNotNull(this.block) { "Already started" } + this.block = null block.startCoroutineCancellable(this, this) } }