Skip to content

Commit b72c568

Browse files
committed
Fix event loop, use dedicated WorkStealing API instead of unsafe and incorrect reverse-engineering of class strucutre
1 parent 55e26fe commit b72c568

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

core/src/main/scala/scala/scalanative/loop/Eventloop.scala

+4-21
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,14 @@ object EventLoop {
1111
val loop: LibUV.Loop = uv_default_loop()
1212

1313
// Schedule loop execution after main ends
14-
scalanative.concurrent.NativeExecutionContext.queue.execute(
15-
new Runnable {
16-
def run(): Unit = EventLoop.run()
17-
}
18-
)
19-
20-
// Reference to the private queue of scala.scalanative.runtime.ExecutionContext
21-
private val queue: mutable.ListBuffer[Runnable] = {
22-
val executionContextPtr =
23-
fromRawPtr[Byte](castObjectToRawPtr(NativeExecutionContext))
24-
val queuePtr = !((executionContextPtr + 8).asInstanceOf[Ptr[Ptr[Byte]]])
25-
castRawPtrToObject(toRawPtr(queuePtr))
26-
.asInstanceOf[mutable.ListBuffer[Runnable]]
27-
}
14+
NativeExecutionContext.queue.execute { () => EventLoop.run() }
2815

2916
def run(): Unit = {
17+
// scala.scalanative package private queue containing WorkStealing API
18+
val queue = NativeExecutionContext.queueInternal
3019
while (uv_loop_alive(loop) != 0 || queue.nonEmpty) {
3120
while (queue.nonEmpty) {
32-
val runnable = queue.remove(0)
33-
try {
34-
runnable.run()
35-
} catch {
36-
case t: Throwable =>
37-
NativeExecutionContext.queue.reportFailure(t)
38-
}
21+
queue.stealWork(1)
3922
uv_run(loop, UV_RUN_NOWAIT)
4023
}
4124
uv_run(loop, UV_RUN_ONCE)

0 commit comments

Comments
 (0)