From 747bde4aa6f923ddad0c9ac415b87053924c5a65 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 14 Apr 2023 00:18:21 +0200 Subject: [PATCH] added the concept of a process --- src/kernel/ps/Process.hx | 8 +++++ src/kernel/ps/ProcessHandle.hx | 56 +++++++++++++++++++++++++++++++++ src/kernel/ps/ProcessManager.hx | 11 +++++++ 3 files changed, 75 insertions(+) create mode 100644 src/kernel/ps/Process.hx create mode 100644 src/kernel/ps/ProcessHandle.hx create mode 100644 src/kernel/ps/ProcessManager.hx diff --git a/src/kernel/ps/Process.hx b/src/kernel/ps/Process.hx new file mode 100644 index 0000000..555cf84 --- /dev/null +++ b/src/kernel/ps/Process.hx @@ -0,0 +1,8 @@ +package kernel.ps; + +/** + Defines an independent process that can be run by the kernel. +**/ +interface Process { + public function run(handle: ProcessHandle): Void; +} diff --git a/src/kernel/ps/ProcessHandle.hx b/src/kernel/ps/ProcessHandle.hx new file mode 100644 index 0000000..933de50 --- /dev/null +++ b/src/kernel/ps/ProcessHandle.hx @@ -0,0 +1,56 @@ +package kernel.ps; + +import haxe.ds.ReadOnlyArray; +using tink.CoreApi; + +typedef HandleConfig = { + ?args: Array, + ?onWrite: Callback, + ?onExit: Callback, +} + +class ProcessHandle { + public var args(get,null): ReadOnlyArray; + + private final config:HandleConfig; + private final closeFuture: Future; + private var closeFutureResolev: Bool -> Void; + + @:allow(kernel.ps.ProcessManager) + private function new(config: HandleConfig) { + this.config = config; + this.closeFuture = new Future((trigger)->{ + this.closeFutureResolev = trigger; + return null; + }); + + if (this.config.onExit != null) { + this.closeFuture.handle(this.config.onExit); + } + } + + public function onExit(): Future { + return this.closeFuture; + } + + public function close(success: Bool = true): Void { + this.dispose(); + this.closeFutureResolev(success); + } + + public function write(message: String): Void { + this.config.onWrite.invoke(message); + } + + public function writeLine(message: String): Void { + this.write(message + "\n"); + } + + private function dispose() { + // TODO + } + + function get_args():ReadOnlyArray { + return this.config.args; + } +} diff --git a/src/kernel/ps/ProcessManager.hx b/src/kernel/ps/ProcessManager.hx new file mode 100644 index 0000000..1534659 --- /dev/null +++ b/src/kernel/ps/ProcessManager.hx @@ -0,0 +1,11 @@ +package kernel.ps; + +import kernel.ps.ProcessHandle.HandleConfig; + +class ProcessManager { + public static function run(process:Process, config: HandleConfig):ProcessHandle { + var handle = new ProcessHandle(config); + process.run(handle); + return handle; + } +}