Module thread

Kernel thread primitives.

This library provides support for creating and managing kernel threads from Lua. It allows running Lua scripts, encapsulated in Lunatik runtime environments, within dedicated kernel threads.

Class thread

thread:stop (self) Stops a running kernel thread.
thread:task (self) Retrieves information about the kernel task associated with the thread.

thread

current () Gets a thread object representing the current kernel task.
run (runtime, name) Creates and starts a new kernel thread to run a Lua task.
shouldstop () Checks if the current thread has been signaled to stop.


Class thread

Represents a kernel thread object. This is a userdata object returned by thread.run() or thread.current(). It encapsulates a kernel struct task_struct and, if created by thread.run(), the Lunatik runtime environment associated with the thread's Lua task.
thread:stop (self)
Stops a running kernel thread. Signals the specified thread to stop and waits for it to exit. This calls kthread_stop() on the underlying kernel thread.

Parameters:

  • self thread The thread object to stop.

Returns:

    nil

Usage:

    -- Assuming 'my_thread' is an object returned by thread.run()
    my_thread:stop()
thread:task (self)
Retrieves information about the kernel task associated with the thread.

Parameters:

  • self thread The thread object.

Returns:

    table A table containing task information with the following fields:

Usage:

    local t_info = my_thread:task()
    print("Thread PID:", t_info.pid)
    print("Command:", t_info.command)
    if t_info.cpu then print("Running on CPU:", t_info.cpu) end

thread

current ()
Gets a thread object representing the current kernel task. Note: If the current task was not created by thread.run(), the returned thread object will not have an associated Lunatik runtime.

Returns:

    thread A thread object for the current task.

Usage:

    local current_task_as_thread = thread.current()
run (runtime, name)
Creates and starts a new kernel thread to run a Lua task. The Lua task is defined by a function returned from the script loaded into the provided runtime environment. The new thread begins execution by resuming this function. The runtime environment must be sleepable.

Parameters:

  • runtime runtime A Lunatik runtime object. The script associated with this runtime (e.g., loaded via lunatik.runtime("path/to/script.lua")) must return a function. This function will be executed in the new kernel thread.
  • name string A descriptive name for the kernel thread (e.g., as shown in ps or top).

Returns:

    thread A new thread object representing the created kernel thread.

Raises:

Error if the runtime is not sleepable, if memory allocation fails, or if kthread_run fails.

See also:

Usage:

    -- main_script.lua
    local lunatik = require("lunatik")
    local thread = require("thread")
    -- Assume "worker_script.lua" returns a function: function() print("worker running") while not thread.shouldstop() do linux.schedule(1000) end print("worker stopped") end
    local worker_rt = lunatik.runtime("worker_script.lua")
    local new_thread = thread.run(worker_rt, "my_lua_worker")
shouldstop ()
Checks if the current thread has been signaled to stop. This function should be called periodically within a thread's main loop to allow for graceful termination when thrd:stop() is invoked. It wraps the kernel's kthread_should_stop().

Returns:

    boolean true if the thread should stop, false otherwise.

See also:

Usage:

    -- Inside a function returned by a script passed to thread.run():
    while not thread.shouldstop() do
      -- do work
      linux.schedule(100) -- example: yield/sleep
    end
    print("Thread is stopping.")
generated by LDoc 1.5.0 Last updated 2025-06-27 17:53:55