1
1
/*
2
- * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2
+ * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
5
+ // Need InlineOnly for efficient bytecode on Android
6
+ @file:Suppress(" INVISIBLE_REFERENCE" , " INVISIBLE_MEMBER" , " NOTHING_TO_INLINE" )
7
+
5
8
package kotlinx.coroutines
6
9
7
- import java.util.concurrent.locks.LockSupport
10
+ import java.util.concurrent.locks.*
11
+ import kotlin.internal.InlineOnly
8
12
9
13
internal interface TimeSource {
10
14
fun currentTimeMillis (): Long
@@ -18,22 +22,48 @@ internal interface TimeSource {
18
22
fun unpark (thread : Thread )
19
23
}
20
24
21
- internal object DefaultTimeSource : TimeSource {
22
- override fun currentTimeMillis (): Long = System .currentTimeMillis()
23
- override fun nanoTime (): Long = System .nanoTime()
24
- override fun wrapTask (block : Runnable ): Runnable = block
25
- override fun trackTask () {}
26
- override fun unTrackTask () {}
27
- override fun registerTimeLoopThread () {}
28
- override fun unregisterTimeLoopThread () {}
29
-
30
- override fun parkNanos (blocker : Any , nanos : Long ) {
31
- LockSupport .parkNanos(blocker, nanos)
32
- }
33
-
34
- override fun unpark (thread : Thread ) {
35
- LockSupport .unpark(thread)
36
- }
25
+ // For tests only
26
+ // @JvmField: Don't use JvmField here to enable R8 optimizations via "assumenosideeffects"
27
+ internal var timeSource: TimeSource ? = null
28
+
29
+ @InlineOnly
30
+ internal inline fun currentTimeMillis (): Long =
31
+ timeSource?.currentTimeMillis() ? : System .currentTimeMillis()
32
+
33
+ @InlineOnly
34
+ internal inline fun nanoTime (): Long =
35
+ timeSource?.nanoTime() ? : System .nanoTime()
36
+
37
+ @InlineOnly
38
+ internal inline fun wrapTask (block : Runnable ): Runnable =
39
+ timeSource?.wrapTask(block) ? : block
40
+
41
+ @InlineOnly
42
+ internal inline fun trackTask () {
43
+ timeSource?.trackTask()
44
+ }
45
+
46
+ @InlineOnly
47
+ internal inline fun unTrackTask () {
48
+ timeSource?.unTrackTask()
37
49
}
38
50
39
- internal var timeSource: TimeSource = DefaultTimeSource
51
+ @InlineOnly
52
+ internal inline fun registerTimeLoopThread () {
53
+ timeSource?.registerTimeLoopThread()
54
+ }
55
+
56
+ @InlineOnly
57
+ internal inline fun unregisterTimeLoopThread () {
58
+ timeSource?.unregisterTimeLoopThread()
59
+ }
60
+
61
+ @InlineOnly
62
+ internal inline fun parkNanos (blocker : Any , nanos : Long ) {
63
+ timeSource?.parkNanos(blocker, nanos) ? : LockSupport .parkNanos(blocker, nanos)
64
+ }
65
+
66
+ @InlineOnly
67
+ internal inline fun unpark (thread : Thread ) {
68
+ timeSource?.unpark(thread) ? : LockSupport .unpark(thread)
69
+ }
0 commit comments