Module lunatik.runner
Manages the execution and lifecycle of Lunatik scripts.
This module provides functionalities to run scripts as isolated runtimes, spawn them into separate kernel threads, and manage their state (start, stop, list, shutdown). It uses RCU-safe tables to store references to active runtimes and threads. In following descriptions, 'env' variable stands for 'lunatik._ENV'.
Functions
list () | Lists the names of all currently running scripts. |
run (script, ...) | Runs a Lunatik script in the current context. |
shutdown () | Shuts down all running scripts and their threads. |
spawn (script, ...) | Spawns a Lunatik script in a new kernel thread. |
startup () | Initializes the runner's internal state. |
stop (script) | Stops a running script and its associated thread, if any. |
Functions
- list ()
-
Lists the names of all currently running scripts.
Iterates over the
env.runtimes
RCU table to collect script names.Returns:
-
string
A comma-separated string of running script names, or an empty string if no scripts are running.
- run (script, ...)
-
Runs a Lunatik script in the current context.
Creates a new Lunatik runtime for the given script and registers it.
Throws an error if a script with the same name is already running.
Parameters:
- script string The path or name of the Lua script to run. The ".lua" extension will be trimmed.
- ... Additional arguments to pass to the script's main function.
Returns:
-
table
The created Lunatik runtime object.
Raises:
error if the script is already running. - shutdown ()
-
Shuts down all running scripts and their threads.
Iterates over
env.runtimes
and calls runner.stop for each script. - spawn (script, ...)
-
Spawns a Lunatik script in a new kernel thread.
First, it runs the script using runner.run, then creates a new kernel thread
to execute the runtime. The thread is named based on the script's filename.
The spawned script is expected to return a function, which will then be executed in the new thread.
Parameters:
- script string The path or name of the Lua script to spawn.
- ... Additional arguments to pass to the script's main function.
Returns:
-
userdata
The kernel thread object.
- startup ()
- Initializes the runner's internal state. Creates RCU-safe tables for storing runtimes and threads. This is typically called during Lunatik's initialization.
- stop (script)
-
Stops a running script and its associated thread, if any.
It attempts to stop the thread first, then the runtime.
Parameters:
- script string The name of the script to stop. The ".lua" extension will be trimmed.