added shutdown

This commit is contained in:
Djeeberjr 2023-04-08 00:39:54 +02:00
parent f8f5f5e5c7
commit 14f7b6c6df
3 changed files with 25 additions and 7 deletions

View File

@ -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();
});
}
}

View File

@ -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,15 +135,26 @@ class KernelEvents {
/**
Start pulling events. Blocking.
**/
public function startEventLoop() {
while (true) {
@:allow(kernel.Init)
private function startEventLoop() {
while (!stopLoop) {
var event:Table<Int, Dynamic> = pullEvents();
var eventName:String = event[1];
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<Int, Dynamic> {
return cast TableTools.pack(Coroutine.yield(null));

View File

@ -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);