Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Vsevolod Tolstopyatov | 26b86e9 | 2018-06-07 15:24:31 +0300 | [diff] [blame] | 5 | @file:JvmMultifileClass |
| 6 | @file:JvmName("ChannelsKt") |
| 7 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 8 | package kotlinx.coroutines.experimental.channels |
| 9 | |
| 10 | import kotlinx.coroutines.experimental.* |
| 11 | |
| 12 | // -------- Operations on SendChannel -------- |
| 13 | |
| 14 | /** |
| 15 | * Adds [element] into to this channel, **blocking** the caller while this channel [Channel.isFull], |
| 16 | * or throws exception if the channel [Channel.isClosedForSend] (see [Channel.close] for details). |
| 17 | * |
| 18 | * This is a way to call [Channel.send] method inside a blocking code using [runBlocking], |
| 19 | * so this function should not be used from coroutine. |
| 20 | */ |
| 21 | public fun <E> SendChannel<E>.sendBlocking(element: E) { |
| 22 | // fast path |
| 23 | if (offer(element)) |
| 24 | return |
| 25 | // slow path |
| 26 | runBlocking { |
| 27 | send(element) |
| 28 | } |
| 29 | } |