diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 70817a3..6c10a13 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -97,6 +97,7 @@ class KernelEvents { private static final onWebsocketSuccessTrigger:SignalTrigger<{url:String, handle:Any}> = Signal.trigger(); private static var stopLoop:Bool = false; + private static var turtleCoroutine:Coroutine; @:allow(kernel.Init) private static function init() { @@ -144,15 +145,19 @@ class KernelEvents { } } - private static function turtleLoop() { - var turtleCoroutine = Coroutine.create(runTurtleLoop); + @:allow(kernel.turtle.TurtleMutex) + private static function startTurtleCoroutine() { + turtleCoroutine = Coroutine.create(runTurtleLoop); Coroutine.resume(turtleCoroutine); + } + + private static function turtleLoop() { + startTurtleCoroutine(); while (true) { var eventData = pullEvents(); if (eventData[1] == "turtle_response" || eventData[1] == "tthread") { - Log.debug('Resuming turtle thread'); var result = Coroutine.resume(turtleCoroutine, TableTools.unpack(eventData)); if (!result.success) { @@ -170,7 +175,9 @@ class KernelEvents { private static function runTurtleLoop() { while (!stopLoop) { - Coroutine.yield(); + if ((TableTools.pack(Coroutine.yield()))[1] != "tthread") + continue; + if (stopLoop) continue; TurtleMutex.runThreadFunc(); diff --git a/src/kernel/turtle/TurtleMutex.hx b/src/kernel/turtle/TurtleMutex.hx index 7ea78c8..697a149 100644 --- a/src/kernel/turtle/TurtleMutex.hx +++ b/src/kernel/turtle/TurtleMutex.hx @@ -43,4 +43,9 @@ class TurtleMutex { threadFunc = func; OS.queueEvent("tthread"); } + + public static function stopTurtleThread() { + threadFunc = null; + KernelEvents.startTurtleCoroutine(); + } }