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