From 14f7b6c6df4b630fbbac4b1b9d4911c941faaf29 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 8 Apr 2023 00:39:54 +0200 Subject: [PATCH] added shutdown --- src/kernel/Init.hx | 7 +++++-- src/kernel/KernelEvents.hx | 24 +++++++++++++++++++----- src/lib/HomeContext.hx | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 22ca02a..6850181 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -13,6 +13,10 @@ import kernel.peripherals.Peripherals.Peripheral; import kernel.net.Net; class Init { + + @:allow(kernel.KernelEvents) + private static var mainEvent:MainEvent; + public static function initKernel() { // Init singeltons here because haxe is confused about the order to create them. Log.instance = new Log(); @@ -44,9 +48,8 @@ class Init { FS.makeDir("/var/ns"); } - MainLoop.add(()->{ + Init.mainEvent = MainLoop.add(()->{ KernelEvents.instance.startEventLoop(); }); - } } diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 3a16297..7486941 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -1,11 +1,12 @@ package kernel; +import haxe.MainLoop; +import cc.OS; import kernel.log.Log; import lib.Pos; import cc.HTTP.HTTPResponse; import lua.TableTools; import lua.Coroutine; -import lib.Vec.Vec2; import haxe.Exception; using tink.CoreApi; @@ -95,6 +96,8 @@ class KernelEvents { private final onWebsocketMessageTrigger:SignalTrigger<{url:String, message:String, isBinary:Bool}> = Signal.trigger(); private final onWebsocketSuccessTrigger:SignalTrigger<{url:String, handle:Any}> = Signal.trigger(); + private var stopLoop:Bool = false; + @:allow(kernel.Init) private function new() { this.onAlarm = onAlarmTrigger.asSignal(); @@ -132,16 +135,27 @@ class KernelEvents { /** Start pulling events. Blocking. **/ - public function startEventLoop() { - while (true) { + @:allow(kernel.Init) + private function startEventLoop() { + while (!stopLoop) { var event:Table = pullEvents(); var eventName:String = event[1]; - - fireSignal(eventName,event); + try { + fireSignal(eventName,event); + }catch(e:Dynamic) { + Log.error('Error while handling event: $eventName: ${e}'); + } } } + public function shutdown() { + Log.info('Shutting down event loop'); + this.stopLoop = true; + MainTerm.instance.reset(); + Init.mainEvent.stop(); + } + private function pullEvents():Table { return cast TableTools.pack(Coroutine.yield(null)); } diff --git a/src/lib/HomeContext.hx b/src/lib/HomeContext.hx index 216c766..c6dee80 100644 --- a/src/lib/HomeContext.hx +++ b/src/lib/HomeContext.hx @@ -130,6 +130,7 @@ class HomeContext { children.push(new TextElement('Add Terminal', {onClick: this.addTerminal})); children.push(new TextElement('Add Log', {onClick: this.addLog})); + children.push(new TextElement('Exit', {onClick: KernelEvents.instance.shutdown})); renderer.setChildren(children);