diff --git a/src/bin/turtle/Patrol.hx b/src/bin/turtle/Patrol.hx new file mode 100644 index 0000000..7b9cbd0 --- /dev/null +++ b/src/bin/turtle/Patrol.hx @@ -0,0 +1,31 @@ +package bin.turtle; + +import kernel.turtle.TurtleMutex; +import kernel.ps.ProcessHandle; +import kernel.ps.Process; + +class Patrol implements Process { + private var handle:ProcessHandle; + + public function new() {} + + public function run(handle:ProcessHandle) { + this.handle = handle; + + if (!handle.claimTurtleMutex()) { + handle.writeLine("Failed to claim turtle mutex"); + handle.close(); + } + + handle.writeLine("Patroling"); + + TurtleMutex.runInTThread(() -> { + while (true) { + kernel.turtle.Turtle.forward(); + kernel.turtle.Turtle.forward(); + kernel.turtle.Turtle.forward(); + kernel.turtle.Turtle.turnLeft(); + } + }); + } +} diff --git a/src/kernel/binstore/BinStore.hx b/src/kernel/binstore/BinStore.hx index 325b6a1..04ec1a0 100644 --- a/src/kernel/binstore/BinStore.hx +++ b/src/kernel/binstore/BinStore.hx @@ -1,5 +1,6 @@ package kernel.binstore; +import bin.turtle.Patrol; import bin.pathfinder.PFClient; import bin.ID; import bin.exporter.Res; @@ -41,7 +42,8 @@ class BinStore { {c: ResManager, name: "ResManager", aliases: ["resmanager", "resmgr"]}, {c: Res, name: "Res", aliases: ["res"]}, {c: ID, name: "ID", aliases: ["id"]}, - {c: PFClient, name: "PFClient", aliases: ["pfclient"]} + {c: PFClient, name: "PFClient", aliases: ["pfclient"]}, + {c: Patrol, name: "Patrol", aliases: ["patrol"]} ]; public static function getBinByName(name:String):Null { diff --git a/src/kernel/ps/ProcessHandle.hx b/src/kernel/ps/ProcessHandle.hx index b83bd7f..082c847 100644 --- a/src/kernel/ps/ProcessHandle.hx +++ b/src/kernel/ps/ProcessHandle.hx @@ -1,5 +1,6 @@ package kernel.ps; +import kernel.turtle.TurtleMutex; import kernel.ps.ProcessManager.PID; import kernel.ui.WindowContext; import kernel.ui.WindowManager; @@ -104,7 +105,19 @@ class ProcessHandle { this.deferFuncs.push(func); } - function get_args():ReadOnlyArray { + public function get_args():ReadOnlyArray { return this.config.args; } + + public function claimTurtleMutex():Bool { + if (TurtleMutex.claim(this.pid)) { + this.addDeferFunc(() -> { + TurtleMutex.release(this.pid); + }); + + return true; + } + + return false; + } } diff --git a/src/kernel/turtle/TurtleMutex.hx b/src/kernel/turtle/TurtleMutex.hx index 697a149..67204b4 100644 --- a/src/kernel/turtle/TurtleMutex.hx +++ b/src/kernel/turtle/TurtleMutex.hx @@ -24,6 +24,7 @@ class TurtleMutex { private static function release(pid:PID) { if (claimedPid == pid) { claimedPid = -1; + stopTurtleThread(); } }