Skip to content

Commit bf45576

Browse files
committed
Improve naming and documentation
1 parent c33ef61 commit bf45576

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ internal object UndispatchedEventLoop {
2121
@JvmField
2222
internal val threadLocalEventLoop = CommonThreadLocal { EventLoop() }
2323

24-
inline fun execute(continuation: DispatchedContinuation<*>, contState: Any?, mode: Int, doYield: Boolean = false, block: () -> Unit) : Boolean {
24+
/**
25+
* Executes given [block] as part of current event loop, updating related to block [continuation]
26+
* mode and state if continuation is not resumed immediately.
27+
* [doYield] indicates whether current continuation is yielding (to provide fast-path if event-loop is empty).
28+
* Returns `true` if execution of continuation was queued (trampolined) or `false` otherwise.
29+
*/
30+
inline fun execute(continuation: DispatchedContinuation<*>, contState: Any?, mode: Int,
31+
doYield: Boolean = false, block: () -> Unit) : Boolean {
2532
val eventLoop = threadLocalEventLoop.get()
2633
if (eventLoop.isActive) {
27-
// If we are yielding and queue is empty, yield should be a no-op
34+
// If we are yielding and queue is empty, we can bail out as part of fast path
2835
if (doYield && eventLoop.queue.isEmpty) {
2936
return false
3037
}
@@ -234,11 +241,10 @@ internal interface DispatchedTask<in T> : Runnable {
234241
}
235242
}
236243

237-
internal fun DispatchedContinuation<Unit>.yield(): Boolean {
238-
return UndispatchedEventLoop.execute(this, Unit, MODE_CANCELLABLE, true) {
244+
internal fun DispatchedContinuation<Unit>.yieldUndispatched(): Boolean =
245+
UndispatchedEventLoop.execute(this, Unit, MODE_CANCELLABLE, doYield = true) {
239246
run()
240247
}
241-
}
242248

243249
internal fun <T> DispatchedTask<T>.dispatch(mode: Int = MODE_CANCELLABLE) {
244250
val delegate = this.delegate

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public suspend fun yield(): Unit = suspendCoroutineUninterceptedOrReturn sc@ { u
2020
context.checkCompletion()
2121
val cont = uCont.intercepted() as? DispatchedContinuation<Unit> ?: return@sc Unit
2222
if (!cont.dispatcher.isDispatchNeeded(context)) {
23-
return@sc if (cont.yield()) COROUTINE_SUSPENDED else Unit
23+
return@sc if (cont.yieldUndispatched()) COROUTINE_SUSPENDED else Unit
2424
}
2525
cont.dispatchYield(Unit)
2626
COROUTINE_SUSPENDED

0 commit comments

Comments
 (0)