@@ -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
243249internal fun <T > DispatchedTask<T>.dispatch (mode : Int = MODE_CANCELLABLE ) {
244250 val delegate = this .delegate
0 commit comments