blob: 6909b9929a5955c09e8686d6d8279089a79a9ecd [file] [log] [blame]
Roman Elizarovaa461cf2018-04-11 13:20:29 +03001/*
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
20package kotlinx.coroutines.experimental
21
22import java.util.concurrent.*
23
24/**
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030025 * Cancels a specified [future] when this job is cancelled.
Roman Elizarovaa461cf2018-04-11 13:20:29 +030026 * 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 */
31public fun Job.cancelFutureOnCompletion(future: Future<*>): DisposableHandle =
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030032 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)"),
44 level = DeprecationLevel.WARNING)
45public 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 */
57public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) {
58 if (this is AbstractContinuation<*>) {
59 invokeOnCancellation(handler = CancelFutureOnCancellation(this, future))
60 } else {
61 // Fallback if someone else implement CancellableContinuation
62 invokeOnCancellation { future.cancel(false) }
63 }
64}
Roman Elizarovaa461cf2018-04-11 13:20:29 +030065
66private class CancelFutureOnCompletion(
67 job: Job,
68 private val future: Future<*>
69) : JobNode<Job>(job) {
70 override fun invoke(reason: Throwable?) {
71 // Don't interrupt when cancelling future on completion, because no one is going to reset this
72 // interruption flag and it will cause spurious failures elsewhere
73 future.cancel(false)
74 }
75 override fun toString() = "CancelFutureOnCompletion[$future]"
76}
77
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030078private class CancelFutureOnCancellation(
79 continuation: CancellableContinuation<*>,
80 private val future: Future<*>
81) : CancellationHandlerImpl<AbstractContinuation<*>>(continuation as AbstractContinuation<*>) {
82
83 override fun invoke(reason: Throwable?) {
84 // Don't interrupt when cancelling future on completion, because no one is going to reset this
85 // interruption flag and it will cause spurious failures elsewhere
86 future.cancel(false)
87 }
88 override fun toString() = "CancelFutureOnCancellation[$future]"
89}