@@ -23,8 +23,6 @@ import kotlin.native.concurrent.*
23
23
internal abstract class EventLoop : CoroutineDispatcher () {
24
24
/* *
25
25
* Counts the number of nested `runBlocking` and [Dispatchers.Unconfined] that use this event loop.
26
- * There are two 32-bit counters encoded in this 64-bit value, allowing to count [Dispatchers.Unconfined]
27
- * separately from `runBlocking`; see [delta] and its uses.
28
26
*/
29
27
private var useCount = 0L
30
28
@@ -53,9 +51,8 @@ internal abstract class EventLoop : CoroutineDispatcher() {
53
51
* (no check for performance reasons, may be added in the future).
54
52
*/
55
53
public open fun processNextEvent (): Long {
56
- val task = dequeueNextTask() ? : return Long .MAX_VALUE
57
- task.run ()
58
- return nextTime
54
+ if (! processUnconfinedEvent()) return Long .MAX_VALUE
55
+ return 0
59
56
}
60
57
61
58
protected open val isEmpty: Boolean get() = isUnconfinedQueueEmpty
@@ -67,20 +64,11 @@ internal abstract class EventLoop : CoroutineDispatcher() {
67
64
}
68
65
69
66
public fun processUnconfinedEvent (): Boolean {
70
- val task = dequeueUnconfinedTask() ? : return false
67
+ val queue = unconfinedQueue ? : return false
68
+ val task = queue.removeFirstOrNull() ? : return false
71
69
task.run ()
72
70
return true
73
71
}
74
-
75
- protected fun dequeueUnconfinedTask (): DispatchedTask <* >? =
76
- unconfinedQueue?.removeFirstOrNull()
77
-
78
- /* *
79
- * Get the next event in this event loop, if there is one.
80
- */
81
- protected open fun dequeueNextTask (): Runnable ? =
82
- dequeueUnconfinedTask()
83
-
84
72
/* *
85
73
* Returns `true` if the invoking `runBlocking(context) { ... }` that was passed this event loop in its context
86
74
* parameter should call [processNextEvent] for this event loop (otherwise, it will process thread-local one).
@@ -261,12 +249,9 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
261
249
}
262
250
}
263
251
264
- protected override fun dequeueNextTask (): Runnable ? {
265
- val unconfined = dequeueUnconfinedTask()
252
+ override fun processNextEvent (): Long {
266
253
// unconfined events take priority
267
- if (unconfined != null ) {
268
- return unconfined
269
- }
254
+ if (processUnconfinedEvent()) return 0
270
255
// queue all delayed tasks that are due to be executed
271
256
val delayed = _delayed .value
272
257
if (delayed != null && ! delayed.isEmpty) {
@@ -284,11 +269,11 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
284
269
}
285
270
}
286
271
// then process one event from queue
287
- return dequeue()
288
- }
289
-
290
- override fun processNextEvent (): Long {
291
- dequeueNextTask()?. run ()
272
+ val task = dequeue()
273
+ if (task != null ) {
274
+ task. run ()
275
+ return 0
276
+ }
292
277
return nextTime
293
278
}
294
279
0 commit comments