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