From 21387cd8e736a34ce216fae8e328d7002de2dd78 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 2 Aug 2023 17:14:54 +0200 Subject: [PATCH] you can stop the turtle thread now --- src/kernel/KernelEvents.hx | 15 +++++++++++---- src/kernel/turtle/TurtleMutex.hx | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) 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(); + } }