Skip to content

Commit c523a97

Browse files
committed
Add some internal documentation
1 parent 638a97c commit c523a97

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

kotlinx-coroutines-core/common/src/Delay.kt

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public interface Delay {
4848
* Schedules invocation of a specified [block] after a specified delay [timeMillis].
4949
* The resulting [DisposableHandle] can be used to [dispose][DisposableHandle.dispose] of this invocation
5050
* request if it is not needed anymore.
51+
*
52+
* [block] must execute quickly, be non-blocking, and must not throw any exceptions.
5153
*/
5254
public fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle =
5355
DefaultDelay.invokeOnTimeout(timeMillis, block, context)

kotlinx-coroutines-core/jvm/src/AbstractTimeSource.kt

+62
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,105 @@ internal inline fun mockTimeSource(source: AbstractTimeSource?) {
2727
timeSource = source
2828
}
2929

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+
*/
3036
@InlineOnly
3137
internal inline fun currentTimeMillis(): Long =
3238
timeSource?.currentTimeMillis() ?: System.currentTimeMillis()
3339

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+
*/
3444
@InlineOnly
3545
internal actual inline fun nanoTime(): Long =
3646
timeSource?.nanoTime() ?: System.nanoTime()
3747

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+
*/
3852
@InlineOnly
3953
internal inline fun wrapTask(block: Runnable): Runnable =
4054
timeSource?.wrapTask(block) ?: block
4155

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+
*/
4284
@InlineOnly
4385
internal inline fun trackTask() {
4486
timeSource?.trackTask()
4587
}
4688

89+
/**
90+
* Decrements the number of tasks not under our control. See [trackTask] for more details.
91+
*/
4792
@InlineOnly
4893
internal inline fun unTrackTask() {
4994
timeSource?.unTrackTask()
5095
}
5196

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+
*/
52104
@InlineOnly
53105
internal inline fun registerTimeLoopThread() {
54106
timeSource?.registerTimeLoopThread()
55107
}
56108

109+
/**
110+
* The opposite of [registerTimeLoopThread].
111+
*/
57112
@InlineOnly
58113
internal inline fun unregisterTimeLoopThread() {
59114
timeSource?.unregisterTimeLoopThread()
60115
}
61116

117+
/**
118+
* Waits for either the specified number of nanoseconds to pass or until [unpark] is called.
119+
*/
62120
@InlineOnly
63121
internal inline fun parkNanos(blocker: Any, nanos: Long) {
64122
timeSource?.parkNanos(blocker, nanos) ?: LockSupport.parkNanos(blocker, nanos)
65123
}
66124

125+
/**
126+
* Preliminarily unparks the specified thread that's currently parked in [parkNanos].
127+
* Can be called even before the thread is parked.
128+
*/
67129
@InlineOnly
68130
internal inline fun unpark(thread: Thread) {
69131
timeSource?.unpark(thread) ?: LockSupport.unpark(thread)

0 commit comments

Comments
 (0)