All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.AbstractTimeSource.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
/*
 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

// Need InlineOnly for efficient bytecode on Android
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "NOTHING_TO_INLINE")

package kotlinx.coroutines

import java.util.concurrent.locks.*
import kotlin.internal.InlineOnly

internal abstract class AbstractTimeSource {
    abstract fun currentTimeMillis(): Long
    abstract fun nanoTime(): Long
    abstract fun wrapTask(block: Runnable): Runnable
    abstract fun trackTask()
    abstract fun unTrackTask()
    abstract fun registerTimeLoopThread()
    abstract fun unregisterTimeLoopThread()
    abstract fun parkNanos(blocker: Any, nanos: Long) // should return immediately when nanos <= 0
    abstract fun unpark(thread: Thread)
}

// For tests only
// @JvmField: Don't use JvmField here to enable R8 optimizations via "assumenosideeffects"
internal var timeSource: AbstractTimeSource? = null

@InlineOnly
internal inline fun currentTimeMillis(): Long =
    timeSource?.currentTimeMillis() ?: System.currentTimeMillis()

@InlineOnly
internal actual inline fun nanoTime(): Long =
    timeSource?.nanoTime() ?: System.nanoTime()

@InlineOnly
internal inline fun wrapTask(block: Runnable): Runnable =
    timeSource?.wrapTask(block) ?: block

@InlineOnly
internal inline fun trackTask() {
    timeSource?.trackTask()
}

@InlineOnly
internal inline fun unTrackTask() {
    timeSource?.unTrackTask()
}

@InlineOnly
internal inline fun registerTimeLoopThread() {
    timeSource?.registerTimeLoopThread()
}

@InlineOnly
internal inline fun unregisterTimeLoopThread() {
    timeSource?.unregisterTimeLoopThread()
}

@InlineOnly
internal inline fun parkNanos(blocker: Any, nanos: Long) {
    timeSource?.parkNanos(blocker, nanos) ?: LockSupport.parkNanos(blocker, nanos)
}

@InlineOnly
internal inline fun unpark(thread: Thread) {
    timeSource?.unpark(thread) ?: LockSupport.unpark(thread)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy