Module: nupp.workers

Isolated worker threads.

Each worker owns a fresh LuaJIT state on a native thread. Values cross only as bounded string.buffer messages, so neither Lua heap, module state, globals, closures, userdata, nor cdata are shared.

Worker entry modules run from the same stamped payload as their spawner:

The entry obtains its endpoints independently:

A ready receive returns inline. A wait blocks efficiently in an ordinary program and suspends under an installed handler. Closing is cooperative: it wakes a worker blocked in Self:receive, but source that ignores the closed inbox can keep join and stop waiting indefinitely.

Module contents

Types

TypeKindDescription
ExitrecordHow a worker ended.
SelfrecordThe worker-side endpoints.
WorkerrecordA fresh Lua state running on a native thread.

Functions

FunctionKindDescription
currentfunctionReturns the endpoints installed in the current worker state.
Self:receivemethod
Self:sendmethod
Self:servemethod
spawnfunctionStarts entry in a fresh LuaJIT state.
Worker:callmethod
Worker:closemethod
Worker:joinmethod
Worker:receivemethod
Worker:sendmethod
Worker:stopmethod
Worker:tryReceivemethod

Types#

Exitrecord#

How a worker ended.

record workers.Exit
    succeeded: boolean
    status: integer
    error: string?
end

Fields

NameTypeDescription
succeededboolean

True when the entry module returned without an uncaught error.

statusinteger

Zero for a clean return and nonzero for a load or runtime failure.

errorstring?

The worker's load or runtime error, when it failed.

Selfrecord#

The worker-side endpoints.

record workers.Self
    inbox: Channel
    outbox: Channel
    receive: function(workers.Self): any?
    send: function(workers.Self, any)
    serve: function(workers.Self, function(any): any)
end

Methods

receive

Waits for and returns the next payload. Nil means the inbox closed and drained.

receive: function(workers.Self): any?
Arguments
NameTypeDescription
?workers.Self
Returns
TypeDescription
any?
send

Sends an ordinary message to the spawner.

send: function(workers.Self, any)
Arguments
NameTypeDescription
?workers.Self
?any
serve

Answers requests until the inbox closes.

serve: function(workers.Self, function(any): any)
Arguments
NameTypeDescription
?workers.Self
?function(any): any

Fields

NameTypeDescription
inboxChannel
outboxChannel

Workerrecord#

A fresh Lua state running on a native thread.

record workers.Worker
    _handle: any
    _inbox: Channel
    _outbox: Channel
    _closed: boolean
    _destroyed: boolean
    _exit: workers.Exit?
    _nextId: integer
    _pendingIds: {[integer]: boolean}
    _replies: {[integer]: Frame}
    _messages: {any}
    _firstMessage: integer
    _lastMessage: integer
    send: function(workers.Worker, any)
    tryReceive: function(workers.Worker): any?
    receive: function(workers.Worker, integer?): any?
    call: function(workers.Worker, any): any
    close: function(workers.Worker)
    join: function(workers.Worker): workers.Exit
    stop: function(workers.Worker): workers.Exit
end

Methods

send

Queues a copied value without waiting for capacity.

send: function(workers.Worker, any)
Arguments
NameTypeDescription
?workers.Worker
?any
tryReceive

Takes a ready ordinary message without waiting.

tryReceive: function(workers.Worker): any?
Arguments
NameTypeDescription
?workers.Worker
Returns
TypeDescription
any?
receive

Waits for an ordinary message, or up to timeoutMs when supplied.

receive: function(workers.Worker, integer?): any?
Arguments
NameTypeDescription
?workers.Worker
?integer?
Returns
TypeDescription
any?
call

Sends a request and waits for the matching reply.

call: function(workers.Worker, any): any
Arguments
NameTypeDescription
?workers.Worker
?any
Returns
TypeDescription
any
close

Closes the worker inbox. Nonblocking and idempotent.

close: function(workers.Worker)
Arguments
NameTypeDescription
?workers.Worker
join

Waits for the worker thread and records how it ended.

join: function(workers.Worker): workers.Exit
Arguments
NameTypeDescription
?workers.Worker
Returns
TypeDescription
workers.Exit
stop

Closes, joins, and releases the worker. Idempotent.

stop: function(workers.Worker): workers.Exit
Arguments
NameTypeDescription
?workers.Worker
Returns
TypeDescription
workers.Exit

Functions#

workers.currentfunction#

Returns the endpoints installed in the current worker state.

function workers.current(): workers.Self

Returns

TypeDescription
workers.Self

Raises

  • outside a worker state

workers.Self:receivemethod#

function workers.Self:receive(): any?

Returns

TypeDescription
any?

workers.Self:sendmethod#

function workers.Self:send(value: any): nil

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
nil

workers.Self:servemethod#

function workers.Self:serve(handler: function(any): any): nil

Arguments

NameTypeDescription
handlerfunction(any): any

Returns

TypeDescription
nil

workers.spawnfunction#

Starts entry in a fresh LuaJIT state.

function workers.spawn(entry: string): workers.Worker

Arguments

NameTypeDescription
entrystring

Returns

TypeDescription
workers.Worker

an owned worker that is stopped on every structured exit

workers.Worker:callmethod#

function workers.Worker:call(value: any): any

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
any

workers.Worker:closemethod#

function workers.Worker:close(): nil

Returns

TypeDescription
nil

workers.Worker:joinmethod#

function workers.Worker:join(): workers.Exit

Returns

TypeDescription
workers.Exit

workers.Worker:receivemethod#

function workers.Worker:receive(timeoutMs: integer?): any?

Arguments

NameTypeDescription
timeoutMsinteger?

Returns

TypeDescription
any?

workers.Worker:sendmethod#

function workers.Worker:send(value: any): nil

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
nil

workers.Worker:stopmethod#

function workers.Worker:stop(): workers.Exit

Returns

TypeDescription
workers.Exit

workers.Worker:tryReceivemethod#

function workers.Worker:tryReceive(): any?

Returns

TypeDescription
any?