@@ -13,53 +13,53 @@ private val UNDEFINED = Symbol("UNDEFINED")
13
13
14
14
@NativeThreadLocal
15
15
internal object UndispatchedEventLoop {
16
- data class State (
16
+ data class EventLoop (
17
17
@JvmField var isActive : Boolean = false ,
18
- @JvmField val threadLocalQueue : ArrayQueue <Runnable > = ArrayQueue ()
18
+ @JvmField val queue : ArrayQueue <Runnable > = ArrayQueue ()
19
19
)
20
20
21
21
@JvmField
22
- internal val state = CommonThreadLocal { State () }
22
+ internal val threadLocalEventLoop = CommonThreadLocal { EventLoop () }
23
23
24
24
inline fun execute (continuation : DispatchedContinuation <* >, contState : Any? , mode : Int , block : () -> Unit ) {
25
- val state = state .get()
26
- if (state .isActive) {
25
+ val eventLoop = threadLocalEventLoop .get()
26
+ if (eventLoop .isActive) {
27
27
continuation._state = contState
28
28
continuation.resumeMode = mode
29
- state.threadLocalQueue .addLast(continuation)
29
+ eventLoop.queue .addLast(continuation)
30
30
return
31
31
}
32
32
33
- runLoop(state , block)
33
+ runEventLoop(eventLoop , block)
34
34
}
35
35
36
36
fun resumeUndispatched (task : DispatchedTask <* >) {
37
- val state = state .get()
38
- if (state .isActive) {
39
- state.threadLocalQueue .addLast(task)
37
+ val eventLoop = threadLocalEventLoop .get()
38
+ if (eventLoop .isActive) {
39
+ eventLoop.queue .addLast(task)
40
40
return
41
41
}
42
42
43
- runLoop(state , { task.resume(task.delegate, MODE_UNDISPATCHED ) })
43
+ runEventLoop(eventLoop , { task.resume(task.delegate, MODE_UNDISPATCHED ) })
44
44
}
45
45
46
- inline fun runLoop ( state : State , block : () -> Unit ) {
46
+ inline fun runEventLoop ( eventLoop : EventLoop , block : () -> Unit ) {
47
47
try {
48
- state .isActive = true
48
+ eventLoop .isActive = true
49
49
block()
50
- while (! state.threadLocalQueue.isEmpty ) {
51
- val element = state.threadLocalQueue.removeFirst()
52
- element .run ()
50
+ while (true ) {
51
+ val nextEvent = eventLoop.queue.removeFirstOrNull() ? : return
52
+ nextEvent .run ()
53
53
}
54
54
} catch (e: Throwable ) {
55
55
/*
56
56
* This exception doesn't happen normally, only if user either submitted throwing runnable
57
57
* or if we have a bug in implementation. Anyway, reset state of the dispatcher to the initial.
58
58
*/
59
- state.threadLocalQueue .clear()
59
+ eventLoop.queue .clear()
60
60
throw DispatchException (" Unexpected exception in undispatched event loop, clearing pending tasks" , e)
61
61
} finally {
62
- state .isActive = false
62
+ eventLoop .isActive = false
63
63
}
64
64
}
65
65
}
0 commit comments