nupp.suspension.spi

Shared interfaces for scheduler integration.

Importing these declarations initializes no scheduler. The facade retains the selected functions. Source, Context, Waiting, Handler, and Installed are the shared types; providers initialize these declarations or satisfy their exact signatures without replacing their nominal identities.

Each record declares what its holder may read and nothing else. Whatever state an implementation needs beside that is its own, kept where callers cannot reach it, so two implementations may represent a source or an installation differently and neither has to reproduce the other's layout.

A subscription may resume synchronously or later. It must settle a Waiting only once and run returned cleanup when the wait ends, including cancellation. Waiting.onResume is settle-safe: registering a waker on a wait that has already settled runs it immediately. Source release and Installed close must not suspend. Nested installation restores the captured handler, and a driver installation preserves delegation to the outer scheduler. create propagates the suspension context into a new coroutine.

Sources report readiness in order, lowest first. Context.uses identifies sources needed by a wait. Whether a park is permitted here is canSuspend, which returns for the handler in force. Cancellation values must come from nupp.suspension.cancellation, which preserves recognition by tasks and worker providers. State belongs to one Lua state; registrations and handlers are not copied into worker lanes.

Module contents

Types

TypeKindDescription
ContextrecordSubscription context used to register or associate readiness sources.
CreatetypeCoroutine construction preserving arbitrary argument and result packs.
HandlerrecordScheduler integration operations with explicit Handler receivers.
InstalledrecordHandler restoration state owned by an installation.
Providerinterface
SourcerecordReadiness registration shared with callers and handlers.
SuspendtypeTyped subscription operation preserving the result type T.
WaitingrecordA single pending operation observed by a suspension handler.

Types#

Contextrecord#

record Context ...

Subscription context used to register or associate readiness sources. A subscription may use an existing Source or create a scoped one.

Members

NameKindDescription
sourcemethodRegisters a source associated with this subscription, as nupp.suspension.spi.Provider.source registers one outside any.
usesmethodAssociates an existing source with this wait without changing its identity.

sourcemethod#

source: function(
    self: Context,
    name: string,
    order: integer,
    poll: function(): integer,
    wait: (function(integer): integer)?,
    active: (function(source: Source): boolean)?
): Source

Registers a source associated with this subscription, as nupp.suspension.spi.Provider.source registers one outside any.

Arguments
NameTypeDescription
selfContext
namestring
orderinteger
pollfunction(): integer
wait(function(integer): integer)?
active(function(source: Source): boolean)?
Returns
TypeDescription
Source

usesmethod#

uses: function(self: Context, source: Source): nil

Associates an existing source with this wait without changing its identity.

Arguments
NameTypeDescription
selfContext
sourceSource
Returns
TypeDescription
nil

Createtype#

type Create = function<A..., R...>(body: function(A...): R...): thread

Coroutine construction preserving arbitrary argument and result packs.

Handlerrecord#

record Handler ...

Scheduler integration operations with explicit Handler receivers. park drives or yields a wait; shutdown releases handler-owned scheduling state.

Members

NameKindDescription
parkmethodParks or drives this Waiting; cleanup cancels the subscription.
canParkmethodReports whether this handler can park the current execution.
shutdownmethodReleases scheduling state owned by this handler.

parkmethod#

park: function(self: Handler, waiting: Waiting, cleanup: function(): nil): nil

Parks or drives this Waiting; cleanup cancels the subscription.

Arguments
NameTypeDescription
selfHandler
waitingWaiting
cleanupfunction(): nil
Returns
TypeDescription
nil

canParkmethod#

canPark: function(self: Handler): boolean

Reports whether this handler can park the current execution.

Arguments
NameTypeDescription
selfHandler
Returns
TypeDescription
boolean

shutdownmethod#

shutdown: function(self: Handler): nil

Releases scheduling state owned by this handler.

Arguments
NameTypeDescription
selfHandler
Returns
TypeDescription
nil

Installedrecord#

record Installed is nupp.Closeable ...

Handler restoration state owned by an installation. Closing it restores the outer context and releases registration state.

Members

NameKindDescription
closemethodRestores the outer handler and settles every park this installation accepted, without suspending.
handlerfieldHandler supplied by the caller.

closemethod#

close: @nosuspend function(takes self: Installed): nil

Restores the outer handler and settles every park this installation accepted, without suspending. The provider supplies it with the installation; a drain that fails leaves the installation to be closed again.

Arguments
NameTypeDescription
takes selfInstalled
Returns
TypeDescription
nil

handlerfield#

handler: Handler

Handler supplied by the caller.

Providerinterface#

interface Provider ...

Members

NameKindDescription
priorityfield
sourcemethodRegisters a named poll source with a poll order and optional blocking waiter.
pollmethodPolls registered sources and returns the amount of progress reported.
suspendfieldSubscribes once, then waits for a typed result through the active handler.
installmethodInstalls a host's handler and returns affine restoration state.
installDrivermethodInstalls a driver's handler, which stands in front of a host rather than being one: a task scope opened in a frame...
createfieldCreates a coroutine preserving the active suspension context and result pack.
handledmethodReports whether an installed handler owns the current suspension context.
canSuspendmethodReports whether this execution context can perform a suspension.
delegatedCanParkmethodCaptures the barrier in force here, for a driver about to displace it.
delegatedParkmethodCaptures how a wait is parked here, for a driver about to displace it.
derivemethodBuilds a Waiting that settles when base does or when ready returns true.
turnAvailablefieldWhether the aggregate running here may spend another coroutine activation before it owes its host a turn.
consumeTurnfieldRecords one coroutine activation against the current turn.
deferTurnfieldParks until the host has had its turn.

priorityfield#

priority: integer?
@readonly

sourcemethod#

source: function(
    name: string,
    order: integer,
    poll: function(): integer,
    wait: (function(integer): integer)?,
    active: (function(Source): boolean)?
): Source
@readonly

Registers a named poll source with a poll order and optional blocking waiter. The optional active predicate controls whether the source can make progress.

Arguments
NameTypeDescription
namestring
orderinteger
pollfunction(): integer
wait(function(integer): integer)?
active(function(Source): boolean)?
Returns
TypeDescription
Source

pollmethod#

poll: function(): integer
@readonly

Polls registered sources and returns the amount of progress reported.

Returns
TypeDescription
integer

suspendfield#

suspend: Suspend
@readonly

Subscribes once, then waits for a typed result through the active handler.

installmethod#

install: function(handler: Handler): affine(Installed)
@readonly

Installs a host's handler and returns affine restoration state.

Arguments
NameTypeDescription
handlerHandler
Returns
TypeDescription
affine(Installed)

installDrivermethod#

installDriver: function(handler: Handler): affine(Installed)
@readonly

Installs a driver's handler, which stands in front of a host rather than being one: a task scope opened in a frame installs one so the frame's own waits keep its children running, and it does not bound a host turn where no host is beneath it.

Arguments
NameTypeDescription
handlerHandler
Returns
TypeDescription
affine(Installed)

createfield#

@readonly

Creates a coroutine preserving the active suspension context and result pack.

handledmethod#

handled: function(): boolean
@readonly

Reports whether an installed handler owns the current suspension context.

Returns
TypeDescription
boolean

canSuspendmethod#

canSuspend: function(): boolean
@readonly

Reports whether this execution context can perform a suspension.

Returns
TypeDescription
boolean

delegatedCanParkmethod#

delegatedCanPark: function(): function(): boolean
@readonly

Captures the barrier in force here, for a driver about to displace it.

A driver's handler changes where a branch yields and must not quietly grant permission the handler it displaced refused, so a driver takes this before it installs and delegates canPark to it. Resolved when called rather than where captured, because a host enters and leaves barriers while a driver is live.

Returns
TypeDescription
function(): boolean

delegatedParkmethod#

delegatedPark: function(): function(Waiting, function(): nil): nil
@readonly

Captures how a wait is parked here, for a driver about to displace it.

A driver runs its own work while a frame waits and hands what it cannot satisfy to the handler it displaced, or to the blocking park where there is none. Captured like delegatedCanPark, and resolved when called.

Returns
TypeDescription
function(Waiting, function(): nil): nil

derivemethod#

derive: function(
    base: Waiting,
    ready: function(): boolean,
    arm: function(wake: function(): nil): nil
): Waiting
@readonly

Builds a Waiting that settles when base does or when ready returns true.

A driver parks outward on the result, so the park it hands to its host is woken both by the wait it was given and by its own work becoming runnable. arm registers the driver's own wakers and receives the same callback base is armed with, which runs at most once per registration. Whatever routing state base carries travels with the derived wait, so a blocking park on it reaches the readiness sources the original named.

Arguments
NameTypeDescription
baseWaiting
readyfunction(): boolean
armfunction(wake: function(): nil): nil
Returns
TypeDescription
Waiting

turnAvailablefield#

turnAvailable: (function(): boolean)?
@readonly

Whether the aggregate running here may spend another coroutine activation before it owes its host a turn.

Turn budgeting is an implementation's to offer: absent, the three leave the aggregate turn unbounded, and the facade supplies that reading. An implementation publishes all three or none.

consumeTurnfield#

consumeTurn: (function(): nil)?
@readonly

Records one coroutine activation against the current turn.

deferTurnfield#

deferTurn: (function(): nil)?
@readonly

Parks until the host has had its turn.

Sourcerecord#

record Source ...

Readiness registration shared with callers and handlers. release must be idempotent and non-suspending; provider state tracks its lifetime.

Members

NameKindDescription
releasemethodUnregisters this source without suspending; repeated calls are harmless.
namefieldDiagnostic identity of the readiness source.
orderfieldWhere in a poll pass this source runs; smaller orders are polled first.
waitfieldOptional bounded blocking waiter receiving milliseconds and returning progress.
activefieldOptional predicate deciding whether the source has outstanding work.

releasemethod#

release: @nosuspend function(self: Source): nil

Unregisters this source without suspending; repeated calls are harmless. The provider supplies it with the registration.

Arguments
NameTypeDescription
selfSource
Returns
TypeDescription
nil

namefield#

name: string

Diagnostic identity of the readiness source.

orderfield#

order: integer

Where in a poll pass this source runs; smaller orders are polled first.

waitfield#

wait: (function(integer): integer)?

Optional bounded blocking waiter receiving milliseconds and returning progress.

activefield#

active: (function(source: Source): boolean)?

Optional predicate deciding whether the source has outstanding work.

Suspendtype#

type Suspend = function<T>(operation: string, subscribe: function(function(T), Context): (function()?)): T

Typed subscription operation preserving the result type T. The subscription returns optional cleanup and may resume synchronously.

Waitingrecord#

record Waiting ...

A single pending operation observed by a suspension handler. ready is monotonic once resumed; onResume installs the handler's wake callback.

Members

NameKindDescription
readymethodReports whether the subscription has settled.
onResumemethodRegisters a wake callback for the waiting scheduler.
operationfieldDiagnostic operation name supplied to suspend.

readymethod#

ready: function(self: Waiting): boolean

Reports whether the subscription has settled.

Arguments
NameTypeDescription
selfWaiting
Returns
TypeDescription
boolean

onResumemethod#

onResume: function(self: Waiting, wake: function(): nil): nil

Registers a wake callback for the waiting scheduler.

Settle-safe: where the wait has already settled, wake is called before this returns rather than stored. A handler may therefore arm once and then yield until ready returns true, without a recheck loop around the arming.

Arguments
NameTypeDescription
selfWaiting
wakefunction(): nil
Returns
TypeDescription
nil

operationfield#

operation: string

Diagnostic operation name supplied to suspend.