• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 @file:JvmMultifileClass
2 @file:JvmName("JobKt")
3 
4 package kotlinx.coroutines
5 
6 import java.util.concurrent.*
7 
8 /**
9  * Cancels a specified [future] when this job is cancelled.
10  * This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).
11  * ```
12  * invokeOnCancellation { if (it != null) future.cancel(false) }
13  * ```
14  */
15 // Warning since 1.9.0
16 @Deprecated(
17     "This function does not do what its name implies: it will not cancel the future if just cancel() was called.",
18     level = DeprecationLevel.WARNING,
19     replaceWith = ReplaceWith("this.invokeOnCancellation { future.cancel(false) }")
20 )
cancelFutureOnCancellationnull21 public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>): Unit =
22     invokeOnCancellation(handler = PublicCancelFutureOnCancel(future))
23 
24 private class PublicCancelFutureOnCancel(private val future: Future<*>) : CancelHandler {
25     override fun invoke(cause: Throwable?) {
26         // Don't interrupt when cancelling future on completion, because no one is going to reset this
27         // interruption flag and it will cause spurious failures elsewhere
28         if (cause != null)  future.cancel(false)
29     }
30     override fun toString() = "CancelFutureOnCancel[$future]"
31 }
32