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