-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTimeSource.kt
39 lines (32 loc) · 1.19 KB
/
TimeSource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.experimental
import java.util.concurrent.locks.LockSupport
internal interface TimeSource {
fun currentTimeMillis(): Long
fun nanoTime(): Long
fun wrapTask(block: Runnable): Runnable
fun trackTask()
fun unTrackTask()
fun registerTimeLoopThread()
fun unregisterTimeLoopThread()
fun parkNanos(blocker: Any, nanos: Long) // should return immediately when nanos <= 0
fun unpark(thread: Thread)
}
internal object DefaultTimeSource : TimeSource {
override fun currentTimeMillis(): Long = System.currentTimeMillis()
override fun nanoTime(): Long = System.nanoTime()
override fun wrapTask(block: Runnable): Runnable = block
override fun trackTask() {}
override fun unTrackTask() {}
override fun registerTimeLoopThread() {}
override fun unregisterTimeLoopThread() {}
override fun parkNanos(blocker: Any, nanos: Long) {
LockSupport.parkNanos(blocker, nanos)
}
override fun unpark(thread: Thread) {
LockSupport.unpark(thread)
}
}
internal var timeSource: TimeSource = DefaultTimeSource