blob: a413854c5fabe918a0357f3b40b1faf0303d1f05 [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)"),
Vsevolod Tolstopyatov80a29472018-04-17 16:02:02 +030044 level = DeprecationLevel.HIDDEN)
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030045public 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 Elizarovdbd9e1c2018-04-28 15:14:18 +030057public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) =
58 invokeOnCancellation(handler = CancelFutureOnCancel(future))
Roman Elizarovaa461cf2018-04-11 13:20:29 +030059
60private class CancelFutureOnCompletion(
61 job: Job,
62 private val future: Future<*>
63) : JobNode<Job>(job) {
Roman Elizarovdbd9e1c2018-04-28 15:14:18 +030064 override fun invoke(cause: Throwable?) {
Roman Elizarovaa461cf2018-04-11 13:20:29 +030065 // 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 Elizarovdbd9e1c2018-04-28 15:14:18 +030072private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler() {
73 override fun invoke(cause: Throwable?) {
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030074 // 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 Elizarovdbd9e1c2018-04-28 15:14:18 +030078 override fun toString() = "CancelFutureOnCancel[$future]"
Vsevolod Tolstopyatovf3a50132018-04-16 19:41:20 +030079}