Compare commits

...

3 Commits

Author SHA1 Message Date
e2916a213a added turtles to concepts 2024-01-22 23:37:02 +01:00
22509932c3 fixed pre-commit hook for deleted files 2024-01-22 23:36:41 +01:00
f109305f48 added bin info to other bins 2024-01-22 23:36:00 +01:00
15 changed files with 50 additions and 7 deletions

View File

@@ -120,3 +120,39 @@ In order for you program to be used it needs to be registered in the `BinStore`
You can imagine the whole runtime like the event loop is JS. The `EndOfLoop` class is used to register callbacks that are called at the end of the loop.
This is like the `setTimeout(0, callback)` in JS.
# Turtle Thread
Computercraft is mostly event-based. Listening to `Coroutine.yield()` waits for events with an option parameter for filtering events.
The problem with turtle arises when we call turtle functions like `move` or `place`. These functions wait internally for
the operation to finish with a `Coroutine.yield("turtle_response")` call. There is no way to prevent this. So in order to keep the main loop from
being blocked, we run all the turtle code in a separate thread.
There are no real threads in Lua, but we can create a coroutine for the turtles. Lua now switches between the main thread and the turtle thread.
Switching occurs when a `turtle_response` events is fired. This will be forwarded to the turtle thread. Once the turtle thread has run its code
and waits for the turtle operation to finish, we switch back to the main thread. The main thread is running normally until a `turtle_response` fires
and the circle repeats.
Be aware that no one is stopping you from running turtle commands in the main thread.
See `KernelEvents.hx` for more.
## Using Turtle threads
Example:
```haxe
// In the context of a process
if (!handle.claimTurtleMutex()) {
Log.warn("Failed to claim turtle thread");
return;
}
// Btw. no one is stopping you from calling this without claiming the mutex.
TurtleMutex.runInTThread(() -> {
while(true){
Turtle.turnLeft();
}
});
```

View File

@@ -8,7 +8,7 @@ set -e
# format
make format
git diff --name-only --cached | xargs -l git add
git diff --name-only --cached --diff-filter=d | xargs -l git add
# build
make build

View File

@@ -6,6 +6,7 @@ import kernel.peripherals.Peripherals.Peripheral;
using tink.CoreApi;
using Lambda;
@:build(macros.Binstore.includeBin("Disk", ["disk"]))
class Disk extends CLIAppBase {
public function new() {
registerSyncSubcommand("ls", (args) -> {

View File

@@ -7,6 +7,7 @@ import lib.Vec.Vec3;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("GPS", ["gps"]))
class GPS extends CLIAppBase {
public function new() {
registerSyncSubcommand("set", (args) -> {

View File

@@ -6,18 +6,13 @@ import kernel.ps.Process;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Hello world", ["hello"]))
class HelloWorld implements Process {
public function new() {}
public function run(handle:ProcessHandle) {
handle.write("Hello World!");
var c = new HelloWorldServiceRPC(0);
c.getNumber(2, 3).handle((res) -> {
Log.debug("Got number: " + res);
});
handle.close();
}
}

View File

@@ -3,6 +3,7 @@ package bin;
import kernel.ps.ProcessHandle;
import kernel.ps.Process;
@:build(macros.Binstore.includeBin("ID", ["id"]))
class ID implements Process {
public function new() {}

View File

@@ -3,6 +3,7 @@ package bin;
import kernel.KernelSettings;
import lib.CLIAppBase;
@:build(macros.Binstore.includeBin("Kernel settings", ["ksettings", "kset"]))
class KSettings extends CLIAppBase {
public function new() {
registerSyncSubcommand("get", (args) -> {

View File

@@ -10,6 +10,7 @@ import lib.ui.UIApp;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Log", ["log"]))
class KernelLog implements Process {
private var handle:ProcessHandle;
private var ctx:WindowContext;

View File

@@ -4,6 +4,7 @@ import kernel.ps.ProcessManager;
import kernel.ps.ProcessHandle;
import kernel.ps.Process;
@:build(macros.Binstore.includeBin("LSPS", ["lsps"]))
class LSPS implements Process {
public function new() {}

View File

@@ -6,6 +6,7 @@ import kernel.net.Routing;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Net", ["net"]))
class Net extends CLIAppBase {
public function new() {
registerSyncSubcommand("route", (args) -> {

View File

@@ -3,6 +3,7 @@ package bin;
import kernel.peripherals.Peripherals.Peripheral;
import lib.CLIAppBase;
@:build(macros.Binstore.includeBin("Peripheral", ["ph", "peripheral"]))
class Perf extends CLIAppBase {
public function new() {
registerSyncSubcommand("inspect", (args) -> {

View File

@@ -5,6 +5,7 @@ import kernel.peripherals.Peripherals.Peripheral;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Redstone", ["redstone", "rs"]))
class Redstone extends CLIAppBase {
public function new() {
registerSyncSubcommand("on", (args) -> {

View File

@@ -5,6 +5,7 @@ import lib.CLIAppBase;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Service", ["srv"]))
class Service extends CLIAppBase {
public function new() {
registerSyncSubcommand("start", (args) -> {

View File

@@ -10,6 +10,7 @@ import kernel.ui.WindowContext;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Terminal", ["terminal"]))
class Terminal implements Process {
private static inline final MAX_BACKLOG:Int = 100;

View File

@@ -4,6 +4,7 @@ import lib.CLIAppBase;
using tink.CoreApi;
@:build(macros.Binstore.includeBin("Turtle", ["turtle", "t"]))
class Turtle extends CLIAppBase {
public function new() {
registerSyncSubcommand("forward", (args) -> {