Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +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 | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | @file:JvmMultifileClass |
| 6 | @file:JvmName("JobKt") |
| 7 | |
| 8 | package kotlinx.coroutines.experimental |
| 9 | |
| 10 | import java.util.concurrent.* |
| 11 | |
| 12 | /** |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 13 | * Cancels a specified [future] when this job is cancelled. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 14 | * This is a shortcut for the following code with slightly more efficient implementation (one fewer object created). |
| 15 | * ``` |
| 16 | * invokeOnCompletion { future.cancel(false) } |
| 17 | * ``` |
Roman Elizarov | 27b8f45 | 2018-09-20 21:23:41 +0300 | [diff] [blame^] | 18 | * |
| 19 | * @suppress **This an internal API and should not be used from general code.** |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 20 | */ |
Roman Elizarov | 27b8f45 | 2018-09-20 21:23:41 +0300 | [diff] [blame^] | 21 | @InternalCoroutinesApi |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 22 | public fun Job.cancelFutureOnCompletion(future: Future<*>): DisposableHandle = |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 23 | invokeOnCompletion(handler = CancelFutureOnCompletion(this, future)) // TODO make it work only on cancellation as well? |
| 24 | |
| 25 | /** |
| 26 | * Cancels a specified [future] when this job is cancelled. |
| 27 | * This is a shortcut for the following code with slightly more efficient implementation (one fewer object created). |
| 28 | * ``` |
| 29 | * invokeOnCompletion { future.cancel(false) } |
| 30 | * ``` |
| 31 | */ |
| 32 | @Deprecated( |
| 33 | message = "Disposable handlers on regular completion are no longer supported", |
| 34 | replaceWith = ReplaceWith("cancelFutureOnCancellation(future)"), |
Vsevolod Tolstopyatov | 2cd27fb | 2018-06-07 13:22:17 +0300 | [diff] [blame] | 35 | level = DeprecationLevel.WARNING) |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 36 | public fun CancellableContinuation<*>.cancelFutureOnCompletion(future: Future<*>): DisposableHandle { |
| 37 | cancelFutureOnCancellation(future) |
| 38 | return NonDisposableHandle |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Cancels a specified [future] when this job is cancelled. |
| 43 | * This is a shortcut for the following code with slightly more efficient implementation (one fewer object created). |
| 44 | * ``` |
| 45 | * invokeOnCancellation { future.cancel(false) } |
| 46 | * ``` |
| 47 | */ |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 48 | public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) = |
| 49 | invokeOnCancellation(handler = CancelFutureOnCancel(future)) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 50 | |
| 51 | private class CancelFutureOnCompletion( |
| 52 | job: Job, |
| 53 | private val future: Future<*> |
| 54 | ) : JobNode<Job>(job) { |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 55 | override fun invoke(cause: Throwable?) { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 56 | // Don't interrupt when cancelling future on completion, because no one is going to reset this |
| 57 | // interruption flag and it will cause spurious failures elsewhere |
| 58 | future.cancel(false) |
| 59 | } |
| 60 | override fun toString() = "CancelFutureOnCompletion[$future]" |
| 61 | } |
| 62 | |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 63 | private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler() { |
| 64 | override fun invoke(cause: Throwable?) { |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 65 | // Don't interrupt when cancelling future on completion, because no one is going to reset this |
| 66 | // interruption flag and it will cause spurious failures elsewhere |
| 67 | future.cancel(false) |
| 68 | } |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 69 | override fun toString() = "CancelFutureOnCancel[$future]" |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 70 | } |