blob: 8c8e9320439edb98fa2fb7cd91ad45263de4c84c [file] [log] [blame]
Roman Elizarov35d2c342017-07-20 14:54:39 +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
17package kotlinx.coroutines.experimental
18
19import java.util.concurrent.locks.LockSupport
20
21internal interface TimeSource {
22 fun nanoTime(): Long
23 fun trackTask(block: Runnable): Runnable
24 fun unTrackTask()
25 fun registerTimeLoopThread()
26 fun unregisterTimeLoopThread()
27 fun parkNanos(blocker: Any, nanos: Long) // should return immediately when nanos <= 0
28 fun unpark(thread: Thread)
29}
30
31internal object DefaultTimeSource : TimeSource {
32 override fun nanoTime(): Long = System.nanoTime()
33 override fun trackTask(block: Runnable): Runnable = block
34 override fun unTrackTask() {}
35 override fun registerTimeLoopThread() {}
36 override fun unregisterTimeLoopThread() {}
37
38 override fun parkNanos(blocker: Any, nanos: Long) {
39 LockSupport.parkNanos(blocker, nanos)
40 }
41
42 override fun unpark(thread: Thread) {
43 LockSupport.unpark(thread)
44 }
45}
46
47internal var timeSource: TimeSource = DefaultTimeSource