blob: 255b7d278126671a6b696aeff3873739b4d63a18 [file] [log] [blame]
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlinx.coroutines.experimental
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit
import kotlin.coroutines.experimental.startCoroutine
private val KEEP_ALIVE = java.lang.Long.getLong("kotlinx.coroutines.ScheduledExecutor.keepAlive", 1000L)
internal val scheduledExecutor by lazy<ScheduledExecutorService> {
ScheduledThreadPoolExecutor(1) { r ->
Thread(r, "kotlinx.coroutines.ScheduledExecutor").apply { isDaemon = true }
}.apply {
setKeepAliveTime(KEEP_ALIVE, TimeUnit.MILLISECONDS)
allowCoreThreadTimeOut(true)
// "setRemoveOnCancelPolicy" is available only since JDK7, so try it via reflection
try {
val m = this::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.javaPrimitiveType)
m.invoke(this, true)
} catch (ex: Throwable) { /* ignore */ }
}
}
/**
* Runs a given suspending block of code inside a coroutine with a specified timeout and throws
* [CancellationException] if timeout was exceeded.
*/
suspend fun <T> withTimeout(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS, block: suspend () -> T): T {
require(time >= 0) { "Timeout time $time cannot be negative" }
if (time <= 0L) throw CancellationException("Timed out immediately")
return suspendCancellableCoroutine { cont: CancellableContinuation<T> ->
// schedule cancellation of this continuation on time
val timeout = scheduledExecutor.schedule({
// create an exception with a specific text
cont.cancel(CancellationException("Timed out waiting for $time $unit"))
}, time, unit)
cont.cancelFutureOnCompletion(timeout)
// restart block in a separate coroutine using cancellable context of this continuation,
block.startCoroutine(cont)
}
}