added example turtle program

This commit is contained in:
Djeeberjr 2023-08-03 18:01:12 +02:00
parent 21387cd8e7
commit 0540cc465a
4 changed files with 49 additions and 2 deletions

31
src/bin/turtle/Patrol.hx Normal file
View File

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

View File

@ -1,5 +1,6 @@
package kernel.binstore; package kernel.binstore;
import bin.turtle.Patrol;
import bin.pathfinder.PFClient; import bin.pathfinder.PFClient;
import bin.ID; import bin.ID;
import bin.exporter.Res; import bin.exporter.Res;
@ -41,7 +42,8 @@ class BinStore {
{c: ResManager, name: "ResManager", aliases: ["resmanager", "resmgr"]}, {c: ResManager, name: "ResManager", aliases: ["resmanager", "resmgr"]},
{c: Res, name: "Res", aliases: ["res"]}, {c: Res, name: "Res", aliases: ["res"]},
{c: ID, name: "ID", aliases: ["id"]}, {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<Bin> { public static function getBinByName(name:String):Null<Bin> {

View File

@ -1,5 +1,6 @@
package kernel.ps; package kernel.ps;
import kernel.turtle.TurtleMutex;
import kernel.ps.ProcessManager.PID; import kernel.ps.ProcessManager.PID;
import kernel.ui.WindowContext; import kernel.ui.WindowContext;
import kernel.ui.WindowManager; import kernel.ui.WindowManager;
@ -104,7 +105,19 @@ class ProcessHandle {
this.deferFuncs.push(func); this.deferFuncs.push(func);
} }
function get_args():ReadOnlyArray<String> { public function get_args():ReadOnlyArray<String> {
return this.config.args; return this.config.args;
} }
public function claimTurtleMutex():Bool {
if (TurtleMutex.claim(this.pid)) {
this.addDeferFunc(() -> {
TurtleMutex.release(this.pid);
});
return true;
}
return false;
}
} }

View File

@ -24,6 +24,7 @@ class TurtleMutex {
private static function release(pid:PID) { private static function release(pid:PID) {
if (claimedPid == pid) { if (claimedPid == pid) {
claimedPid = -1; claimedPid = -1;
stopTurtleThread();
} }
} }