Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.timeunit.TimeUnit |
| 8 | import java.io.* |
| 9 | import java.util.concurrent.* |
| 10 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 11 | |
| 12 | /** |
Marko Devcic | 26f4b9e | 2018-03-20 20:28:52 +0100 | [diff] [blame] | 13 | * [CoroutineDispatcher] that implements [Closeable] |
| 14 | */ |
| 15 | abstract class CloseableCoroutineDispatcher: CoroutineDispatcher(), Closeable |
| 16 | |
| 17 | /** |
| 18 | * Converts an instance of [ExecutorService] to an implementation of [CloseableCoroutineDispatcher]. |
| 19 | */ |
| 20 | public fun ExecutorService.asCoroutineDispatcher(): CloseableCoroutineDispatcher = |
| 21 | // we know that an implementation of Executor.asCoroutineDispatcher actually returns a closeable one |
| 22 | (this as Executor).asCoroutineDispatcher() as CloseableCoroutineDispatcher |
| 23 | |
| 24 | /** |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 25 | * Converts an instance of [Executor] to an implementation of [CoroutineDispatcher]. |
Roman Elizarov | 38b5ea1 | 2017-03-09 12:03:39 +0300 | [diff] [blame] | 26 | * @suppress **Deprecated**: Renamed to [asCoroutineDispatcher]. |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 27 | */ |
Roman Elizarov | 38b5ea1 | 2017-03-09 12:03:39 +0300 | [diff] [blame] | 28 | @Deprecated("Renamed to `asCoroutineDispatcher`", |
| 29 | replaceWith = ReplaceWith("asCoroutineDispatcher()")) |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 30 | public fun Executor.toCoroutineDispatcher(): CoroutineDispatcher = |
| 31 | ExecutorCoroutineDispatcher(this) |
| 32 | |
Roman Elizarov | 38b5ea1 | 2017-03-09 12:03:39 +0300 | [diff] [blame] | 33 | /** |
| 34 | * Converts an instance of [Executor] to an implementation of [CoroutineDispatcher]. |
| 35 | */ |
| 36 | public fun Executor.asCoroutineDispatcher(): CoroutineDispatcher = |
| 37 | ExecutorCoroutineDispatcher(this) |
| 38 | |
Roman Elizarov | f04f51d | 2017-04-18 15:28:35 +0300 | [diff] [blame] | 39 | private class ExecutorCoroutineDispatcher(override val executor: Executor) : ExecutorCoroutineDispatcherBase() |
| 40 | |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 41 | /** |
| 42 | * @suppress **This is unstable API and it is subject to change.** |
| 43 | */ |
Marko Devcic | 26f4b9e | 2018-03-20 20:28:52 +0100 | [diff] [blame] | 44 | public abstract class ExecutorCoroutineDispatcherBase : CloseableCoroutineDispatcher(), Delay { |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 45 | /** |
| 46 | * @suppress **This is unstable API and it is subject to change.** |
| 47 | */ |
| 48 | internal abstract val executor: Executor |
| 49 | |
Roman Elizarov | 92b0485 | 2017-07-11 15:11:58 +0300 | [diff] [blame] | 50 | override fun dispatch(context: CoroutineContext, block: Runnable) = |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 51 | try { executor.execute(timeSource.trackTask(block)) } |
| 52 | catch (e: RejectedExecutionException) { |
| 53 | timeSource.unTrackTask() |
| 54 | DefaultExecutor.execute(block) |
| 55 | } |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 56 | |
| 57 | override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { |
Roman Elizarov | 92b0485 | 2017-07-11 15:11:58 +0300 | [diff] [blame] | 58 | val timeout = |
| 59 | try { (executor as? ScheduledExecutorService) |
| 60 | ?.schedule(ResumeUndispatchedRunnable(this, continuation), time, unit) } |
| 61 | catch (e: RejectedExecutionException) { null } |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 62 | if (timeout != null) |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 63 | continuation.cancelFutureOnCancellation(timeout) |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 64 | else |
| 65 | DefaultExecutor.scheduleResumeAfterDelay(time, unit, continuation) |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 66 | } |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 67 | |
| 68 | override fun invokeOnTimeout(time: Long, unit: TimeUnit, block: Runnable): DisposableHandle { |
Roman Elizarov | 92b0485 | 2017-07-11 15:11:58 +0300 | [diff] [blame] | 69 | val timeout = |
| 70 | try { (executor as? ScheduledExecutorService) |
| 71 | ?.schedule(block, time, unit) } |
| 72 | catch (e: RejectedExecutionException) { null } |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 73 | if (timeout != null) |
| 74 | return DisposableFutureHandle(timeout) |
| 75 | else |
| 76 | return DefaultExecutor.invokeOnTimeout(time, unit, block) |
Roman Elizarov | daa1d9d | 2017-03-02 19:00:50 +0300 | [diff] [blame] | 77 | } |
Roman Elizarov | 30dd5c1 | 2017-04-11 13:50:03 +0300 | [diff] [blame] | 78 | |
Marko Devcic | 26f4b9e | 2018-03-20 20:28:52 +0100 | [diff] [blame] | 79 | override fun close() { |
| 80 | (executor as? ExecutorService)?.shutdown() |
| 81 | } |
| 82 | |
Roman Elizarov | 30dd5c1 | 2017-04-11 13:50:03 +0300 | [diff] [blame] | 83 | override fun toString(): String = executor.toString() |
Roman Elizarov | f04f51d | 2017-04-18 15:28:35 +0300 | [diff] [blame] | 84 | override fun equals(other: Any?): Boolean = other is ExecutorCoroutineDispatcherBase && other.executor === executor |
Roman Elizarov | 30dd5c1 | 2017-04-11 13:50:03 +0300 | [diff] [blame] | 85 | override fun hashCode(): Int = System.identityHashCode(executor) |
Roman Elizarov | 67891d8 | 2017-01-23 16:47:20 +0300 | [diff] [blame] | 86 | } |
| 87 | |
Roman Elizarov | f04f51d | 2017-04-18 15:28:35 +0300 | [diff] [blame] | 88 | private class ResumeUndispatchedRunnable( |
| 89 | private val dispatcher: CoroutineDispatcher, |
| 90 | private val continuation: CancellableContinuation<Unit> |
| 91 | ) : Runnable { |
| 92 | override fun run() { |
| 93 | with(continuation) { dispatcher.resumeUndispatched(Unit) } |
| 94 | } |
| 95 | } |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * An implementation of [DisposableHandle] that cancels the specified future on dispose. |
| 99 | * @suppress **This is unstable API and it is subject to change.** |
| 100 | */ |
| 101 | public class DisposableFutureHandle(private val future: Future<*>) : DisposableHandle { |
| 102 | override fun dispose() { |
| 103 | future.cancel(false) |
| 104 | } |
| 105 | override fun toString(): String = "DisposableFutureHandle[$future]" |
| 106 | } |