Skip to content

Commit 56edf29

Browse files
authored
Merge pull request #23 from scala-native/avoid-loop-starvation
Avoid libuv loop starvation run all ready callbacks
2 parents 26f4698 + 5d91cf6 commit 56edf29

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

core/loop.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.scalanative.loop
22
import scala.scalanative.unsafe._
3+
import scala.annotation.tailrec
34

45
object EventLoop {
56
import LibUV._, LibUVConstants._
@@ -20,14 +21,21 @@ object EventLoop {
2021
* - we check if they generated IO calls on
2122
* the event loop
2223
* - If it's the case we run libuv's event loop
23-
* using UV_RUN_ONCE that blocks only once
24+
* using UV_RUN_ONCE until there are callbacks
25+
* to execute
2426
* - We run the default execution context again
2527
* in case the callbacks generated new Futures
2628
*/
2729
def run(): Unit = {
30+
@tailrec
31+
def runUv(): Unit = {
32+
val res = uv_run(loop, UV_RUN_ONCE)
33+
if(res != 0) runUv()
34+
}
35+
2836
scala.scalanative.runtime.loop()
2937
while (uv_loop_alive(loop) != 0) {
30-
uv_run(loop, UV_RUN_ONCE)
38+
runUv()
3139
scala.scalanative.runtime.loop()
3240
}
3341
uv_loop_close(loop)

0 commit comments

Comments
 (0)