Lunatik

threadModule

Kernel thread primitives.

Thread objects are created in a process runtime alone: in a softirq or hardirq runtime thread.run has no runtime object to take. shouldstop answers false outside a kernel thread, and stop on a thread already stopped logs a warning and returns true.

Usage

-- body.lua, run with `lunatik spawn body`
local thread = require("thread")
local linux  = require("linux")

local function body()
	while not thread.shouldstop() do
		linux.schedule(100)
	end
end
return body

Class thread

thread:stop () Stops a running kernel thread.
thread:task () Returns a task object for the kernel task associated with the thread.

thread

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 in detail

Represents a kernel thread object.
thread:stop ()
Stops a running kernel thread. Signals the thread to stop and waits for it to exit. A body waiting for a runtime’s lock, or for the lock a shared object’s method takes, leaves that wait with “EINTR”. A to-be-closed variable holding the thread stops it the same way, and so does the end of the runtime that called thread.run, which keeps the thread until a stop called there, as thread.run says. A stop made while another stop of the same thread waits for it returns at once, before the thread has exited.

Returns

    boolean false if the body it stopped raised, true otherwise, for a thread already stopped or being stopped too

Raises

“not allowed from the runtime itself” from under the lock of the thread’s runtime, the contexts runtime:stop names, the thread’s own body among them, where the stop would wait on a body that runs under that lock; “EINTR” if the stop of the calling kernel thread, or a fatal signal to any other task, ends its wait for the thread’s lock

Usage

my_thread:stop()
thread:task ()
Returns a task object for the kernel task associated with the thread. The thread holds a reference to it, so the object stays readable after the body returned, reporting the task as it ended. stop releases that reference: the object returned once a stop began, before the thread has exited too, has no task, and its methods raise “closed object”.

Returns

    task

Usage

local t = my_thread:task()
print(t:pid(), t:comm())

thread in detail

run (runtime, name, ...)
Creates and starts a new kernel thread to run a Lua task. The runtime must be sleepable; the script it loaded must return a function, which becomes the thread body, called with the objects given here. The thread holds a reference to the runtime until stop, or the end of the runtime that called thread.run, releases it, even once its body has returned, so a runtime that keeps starting threads stops each one whose body returned.

Parameters

  • runtime runtime A sleepable Lunatik runtime whose script returns a function.
  • name string A descriptive name for the kernel thread.
  • ... Lunatik objects passed to the thread body.

Returns

    thread the thread, which this call keeps for the runtime that calls it: dropping it stops nothing, and the thread runs until its body returns, stop or the end of that runtime, which stops it as stop does; an end made under the lock of the thread’s runtime, as one the thread’s own body makes, leaves the thread to run until its body returns.

Raises

“not allowed while the runtime closes” from a finalizer that runs at its close; “not allowed before the runtime is armed” from a script body; “IRQ runtime cannot spawn threads” for a softirq or hardirq runtime; “stopped runtime”; “invalid object” or “cannot share SINGLE object” for a value passed to the body; “couldn’t pass the thread arguments”; the kernel’s errno if it cannot create the thread; “not allowed from the runtime itself” from under the runtime’s own lock, the contexts runtime:stop names, where passing the arguments would wait on it; “EINTR” if the stop of the calling kernel thread, or a fatal signal to any other task, ends its wait for the runtime’s lock.

See also

shouldstop ()
Checks if the current thread has been signaled to stop.

Returns

    boolean true if the thread should stop, false otherwise.

See also

Usage

while not thread.shouldstop() do
  linux.schedule(100)
end