@@ -27,43 +27,105 @@ internal inline fun mockTimeSource(source: AbstractTimeSource?) {
27
27
timeSource = source
28
28
}
29
29
30
+ /* *
31
+ * The current system time in milliseconds.
32
+ *
33
+ * This is only used for automatically-generated tests in place of [System.currentTimeMillis],
34
+ * it is not used in production code.
35
+ */
30
36
@InlineOnly
31
37
internal inline fun currentTimeMillis (): Long =
32
38
timeSource?.currentTimeMillis() ? : System .currentTimeMillis()
33
39
40
+ /* *
41
+ * The reading from a high-precision monotonic clock used for measuring time intervals.
42
+ * Logically equivalent to [kotlin.time.TimeSource.Monotonic.markNow].
43
+ */
34
44
@InlineOnly
35
45
internal actual inline fun nanoTime (): Long =
36
46
timeSource?.nanoTime() ? : System .nanoTime()
37
47
48
+ /* *
49
+ * Calls [trackTask] and returns a wrapped version of the given [block] that calls [unTrackTask] after it completes.
50
+ * Is optimized to just returning [block] if [trackTask] and [unTrackTask] are no-ops.
51
+ */
38
52
@InlineOnly
39
53
internal inline fun wrapTask (block : Runnable ): Runnable =
40
54
timeSource?.wrapTask(block) ? : block
41
55
56
+ /* *
57
+ * Increments the number of tasks not under our control.
58
+ *
59
+ * Virtual time source uses this to decide whether to jump to the moment of virtual time when the next sleeping thread
60
+ * should wake up.
61
+ * If there are some uncontrollable tasks, it will not jump to the moment of the next sleeping thread,
62
+ * because the uncontrollable tasks may change the shared state in a way that affects the sleeping thread.
63
+ *
64
+ * Example:
65
+ *
66
+ * ```
67
+ * thread { // controlled thread
68
+ * while (true) {
69
+ * if (sharedState == 42) {
70
+ * break
71
+ * }
72
+ * Thread.sleep(1000)
73
+ * }
74
+ * }
75
+ *
76
+ * thread { // uncontrolled thread
77
+ * sharedState = 42
78
+ * }
79
+ * ```
80
+ *
81
+ * If the second thread is not tracked, the first thread effectively enters a spin loop until the second thread
82
+ * physically changes the shared state.
83
+ */
42
84
@InlineOnly
43
85
internal inline fun trackTask () {
44
86
timeSource?.trackTask()
45
87
}
46
88
89
+ /* *
90
+ * Decrements the number of tasks not under our control. See [trackTask] for more details.
91
+ */
47
92
@InlineOnly
48
93
internal inline fun unTrackTask () {
49
94
timeSource?.unTrackTask()
50
95
}
51
96
97
+ /* *
98
+ * Increases the registered number of nested loops of the form
99
+ * `while (nanoTime() < deadline) { parkNanos(deadline - nanoTime()) }` running in the current thread.
100
+ *
101
+ * While at least one such loop is running, other threads are allowed to call [unpark] on the current thread
102
+ * and wake it up. Before [registerTimeLoopThread] is called, [unpark] is not guaranteed to have any effect.
103
+ */
52
104
@InlineOnly
53
105
internal inline fun registerTimeLoopThread () {
54
106
timeSource?.registerTimeLoopThread()
55
107
}
56
108
109
+ /* *
110
+ * The opposite of [registerTimeLoopThread].
111
+ */
57
112
@InlineOnly
58
113
internal inline fun unregisterTimeLoopThread () {
59
114
timeSource?.unregisterTimeLoopThread()
60
115
}
61
116
117
+ /* *
118
+ * Waits for either the specified number of nanoseconds to pass or until [unpark] is called.
119
+ */
62
120
@InlineOnly
63
121
internal inline fun parkNanos (blocker : Any , nanos : Long ) {
64
122
timeSource?.parkNanos(blocker, nanos) ? : LockSupport .parkNanos(blocker, nanos)
65
123
}
66
124
125
+ /* *
126
+ * Preliminarily unparks the specified thread that's currently parked in [parkNanos].
127
+ * Can be called even before the thread is parked.
128
+ */
67
129
@InlineOnly
68
130
internal inline fun unpark (thread : Thread ) {
69
131
timeSource?.unpark(thread) ? : LockSupport .unpark(thread)
0 commit comments