@@ -21,10 +21,17 @@ internal object UndispatchedEventLoop {
21
21
@JvmField
22
22
internal val threadLocalEventLoop = CommonThreadLocal { EventLoop () }
23
23
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 {
25
32
val eventLoop = threadLocalEventLoop.get()
26
33
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
28
35
if (doYield && eventLoop.queue.isEmpty) {
29
36
return false
30
37
}
@@ -234,11 +241,10 @@ internal interface DispatchedTask<in T> : Runnable {
234
241
}
235
242
}
236
243
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 ) {
239
246
run ()
240
247
}
241
- }
242
248
243
249
internal fun <T > DispatchedTask<T>.dispatch (mode : Int = MODE_CANCELLABLE ) {
244
250
val delegate = this .delegate
0 commit comments