Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 2 | * Copyright 2016-2020 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 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 8 | package kotlinx.coroutines |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 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 | * ``` |
Vsevolod Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame] | 18 | * |
Roman Elizarov | 27b8f45 | 2018-09-20 21:23:41 +0300 | [diff] [blame] | 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 = |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 23 | invokeOnCompletion(handler = CancelFutureOnCompletion(this, future)) // TODO make it work only on cancellation as well? |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 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 | * ``` |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 29 | * invokeOnCancellation { future.cancel(false) } |
| 30 | * ``` |
| 31 | */ |
Vsevolod Tolstopyatov | 9cbad7d | 2020-03-27 17:43:45 +0300 | [diff] [blame] | 32 | public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>): Unit = |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 33 | invokeOnCancellation(handler = CancelFutureOnCancel(future)) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 34 | |
| 35 | private class CancelFutureOnCompletion( |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 36 | job: Job, |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 37 | private val future: Future<*> |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 38 | ) : JobNode<Job>(job) { |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 39 | override fun invoke(cause: Throwable?) { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 40 | // Don't interrupt when cancelling future on completion, because no one is going to reset this |
| 41 | // interruption flag and it will cause spurious failures elsewhere |
| 42 | future.cancel(false) |
| 43 | } |
Aurimas Liutikas | 7b14046 | 2021-05-12 21:56:16 +0000 | [diff] [blame^] | 44 | override fun toString() = "CancelFutureOnCompletion[$future]" |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 45 | } |
| 46 | |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 47 | private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler() { |
| 48 | override fun invoke(cause: Throwable?) { |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 49 | // Don't interrupt when cancelling future on completion, because no one is going to reset this |
| 50 | // interruption flag and it will cause spurious failures elsewhere |
| 51 | future.cancel(false) |
| 52 | } |
Roman Elizarov | dbd9e1c | 2018-04-28 15:14:18 +0300 | [diff] [blame] | 53 | override fun toString() = "CancelFutureOnCancel[$future]" |
Vsevolod Tolstopyatov | f3a5013 | 2018-04-16 19:41:20 +0300 | [diff] [blame] | 54 | } |