Lunatik

completionModule

Lua bindings for kernel completion mechanisms.

This library allows Lua scripts to create, signal, and wait on kernel completion objects.

Task completion is a synchronization mechanism used to coordinate the execution of multiple threads. It allows threads to wait for a specific event to occur before proceeding, ensuring certain tasks are complete.

Class completion

completion:complete () Signals a completion.
completion:wait ([timeout]) Waits for a completion to be signaled.

completion

new () Creates a new kernel completion object.

Class completion in detail

Represents a kernel completion object. This is a userdata object returned by completion.new(), wrapping a struct completion used to wait for and signal an event.
completion:complete ()
Signals a completion. This wakes up one task waiting on this completion object. Corresponds to the kernel’s complete() function. It may be called from any runtime.

Returns

    nil

See also

Usage

-- Assuming 'c' is a completion object returned by completion.new()
c:complete()
completion:wait ([timeout])
Waits for a completion to be signaled. This function will block the current Lua runtime until the completion is signaled, an optional timeout occurs, or the wait is interrupted. The Lunatik runtime invoking this method must be sleepable. Corresponds to the kernel’s wait_for_completion_interruptible_timeout().

Parameters

  • timeout integer Optional timeout in milliseconds, from 0 to 231 - 1. If omitted or nil, waits indefinitely. (optional)

Returns

    boolean true if the completion was signaled, false if the timeout elapsed first.

Raises

“out of bounds” for a timeout outside its range; ERESTARTSYS when a signal or the stop of the thread that waits interrupts the wait; “runtime context mismatch” from a softirq or hardirq runtime, its script body included

See also

Usage

-- Assuming 'c' is a completion object
if c:wait(1000) then -- Wait for up to 1 second
  print("Completion received!")
else
  print("Timed out")
end

completion in detail

new ()
Creates a new kernel completion object. Initializes a struct completion and returns it wrapped as a Lua userdata object of type completion.

Returns

    completion A new completion object.

Usage

local c = completion.new()