nupp.runtime.services.suspension

Scheduler integration contract for suspension API 1.

Setup imports service here before any consumer requires nupp.suspension. 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.

A subscription may resume synchronously or later. It must settle a Waiting only once and run returned cleanup when the wait ends, including cancellation. Source release and Installed.drop must not suspend. Nested installation restores the captured handler, and transparent handlers preserve delegation to the outer scheduler. create propagates the suspension context into a new coroutine.

Sources report readiness in priority order. Context.uses identifies sources needed by a wait, while canPark distinguishes schedulers that can yield from ones that must drive readiness themselves. Cancellation values must come from nupp.runtime.services.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 one 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.

Functions

FunctionKindDescription
destroyInstalledfunctionCanonical affine cleanup for a handler installation.

Values

ValueKindDescription
servicevariableCanonical suspension API 1 handle; setup selects before the public facade loads.

Types#

Contextrecord#

record Context
    source: function(Context, string, integer, function(): integer, (function(integer): integer)?): Source
    uses: function(Context, Source): nil
    canPark: function(Context): boolean
    handler: any
    associated: {Source}?
end

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

Methods

source#
source: function(Context, string, integer, function(): integer, (function(integer): integer)?): Source

Registers a source associated with this subscription.

Arguments
NameTypeDescription
?Context
?string
?integer
?function(): integer
?(function(integer): integer)?
Returns
TypeDescription
Source
uses#
uses: function(Context, Source): nil

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

Arguments
NameTypeDescription
?Context
?Source
Returns
TypeDescription
nil
canPark#
canPark: function(Context): boolean

Reports whether the current handler can park this subscription.

Arguments
NameTypeDescription
?Context
Returns
TypeDescription
boolean

Fields

handler#
handler: any

Provider-owned active handler state.

associated#
associated: {Source}?

Sources associated with this subscription, when explicitly tracked.

Createtype#

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

Coroutine construction preserving arbitrary argument and result packs.

Handlerrecord#

record Handler
    park: function(Handler, Waiting, function(): nil): nil
    canPark: function(Handler): boolean
    shutdown: function(Handler): nil
end

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

Methods

park#
park: function(Handler, Waiting, function(): nil): nil

Parks or drives this Waiting; the callback performs subscription cleanup.

Arguments
NameTypeDescription
?Handler
?Waiting
?function(): nil
Returns
TypeDescription
nil
canPark#
canPark: function(Handler): boolean

Reports whether this handler can park the current execution.

Arguments
NameTypeDescription
?Handler
Returns
TypeDescription
boolean
shutdown#
shutdown: function(Handler): nil

Releases scheduling state owned by this handler.

Arguments
NameTypeDescription
?Handler
Returns
TypeDescription
nil

Installedrecord#

record Installed
    drop: nosuspend function(takes self: Installed): nil
    release: function(self: Installed): nil
    co: any
    previous: any
    handler: Handler
    transparent: boolean
    restored: boolean = false
    released: boolean = false
    parks: any
end

Handler restoration state owned by one installation. Its affine drop restores the outer context and releases registration state.

Methods

drop#
drop: nosuspend function(takes self: Installed): nil

Canonical non-suspending affine cleanup for the installation.

Arguments
NameTypeDescription
takes selfInstalled
Returns
TypeDescription
nil
release#
release: function(self: Installed): nil

Restores the outer handler and releases this installation.

Arguments
NameTypeDescription
selfInstalled
Returns
TypeDescription
nil

Fields

co#
co: any

Coroutine whose handler is installed, or the provider's main-state key.

previous#
previous: any

Captured outer handler state restored by release.

handler#
handler: Handler

Handler supplied by the caller.

transparent#
transparent: boolean

Whether operations may delegate through to the outer context.

restored#
restored: boolean

Whether the outer handler has been restored.

released#
released: boolean

Whether installation cleanup has completed.

parks#
parks: any

Provider bookkeeping for waits owned by this installation.

Providerinterface#

interface Provider
    readonly source: function(
        name: string,
        priority: integer,
        poll: function(): integer,
        wait: (function(integer): integer)?,
        active: (function(Source): boolean)?
    ): Source
    readonly poll: function(): integer
    readonly suspend: Suspend
    readonly install: function(handler: Handler, transparent: boolean?): affine(Installed, destroyInstalled)
    readonly create: Create
    readonly handled: function(): boolean
    readonly delegatedCanPark: function(): function(): boolean
    readonly delegatedPark: function(): function(Waiting, function(): nil): nil
    readonly canSuspend: function(): boolean
end

Methods

source#
source: function(
    name: string,
    priority: integer,
    poll: function(): integer,
    wait: (function(integer): integer)?,
    active: (function(Source): boolean)?
): Source

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

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

Polls registered sources and returns the amount of progress reported.

Returns
TypeDescription
integer
install#
install: function(handler: Handler, transparent: boolean?): affine(Installed, destroyInstalled)

Installs a handler and returns affine restoration state.

Arguments
NameTypeDescription
handlerHandler
transparentboolean?
Returns
TypeDescription
affine(Installed, destroyInstalled)
handled#
handled: function(): boolean

Reports whether an installed handler owns the current suspension context.

Returns
TypeDescription
boolean
delegatedCanPark#
delegatedCanPark: function(): function(): boolean

Captures the outer handler's park-availability query.

Returns
TypeDescription
function(): boolean
delegatedPark#
delegatedPark: function(): function(Waiting, function(): nil): nil

Captures the outer handler's parking operation.

Returns
TypeDescription
function(Waiting, function(): nil): nil
canSuspend#
canSuspend: function(): boolean

Reports whether this execution context can perform a suspension.

Returns
TypeDescription
boolean

Fields

suspend#
suspend: Suspend

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

create#
create: Create

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

Sourcerecord#

record Source
    release: nosuspend function(Source): nil
    releaseImpl: function(Source): nil
    name: string
    priority: integer
    wait: (function(integer): integer)?
    active: (function(Source): boolean)?
    sequence: integer = 0
    poller: (function(): integer)?
    waiter: (function(integer): integer)?
    released: boolean = false
end

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

Methods

release#
release: nosuspend function(Source): nil

Unregisters this source without suspending; repeated calls are harmless.

Arguments
NameTypeDescription
?Source
Returns
TypeDescription
nil
releaseImpl#
releaseImpl: function(Source): nil

Provider callback used by the canonical release method.

Arguments
NameTypeDescription
?Source
Returns
TypeDescription
nil

Fields

name#
name: string

Diagnostic identity of the readiness source.

priority#
priority: integer

Readiness ordering key; smaller priorities are polled first.

wait#
wait: (function(integer): integer)?

Optional bounded blocking waiter receiving milliseconds and returning progress.

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

Optional predicate deciding whether the source has outstanding work.

sequence#
sequence: integer

Stable registration-order tie breaker used by the provider.

poller#
poller: (function(): integer)?

Provider-owned nonblocking readiness callback.

waiter#
waiter: (function(integer): integer)?

Provider-owned blocking readiness callback.

released#
released: boolean

Whether the registration has already been released.

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
    ready: function(Waiting): boolean
    onResume: function(Waiting, function(): nil): nil
    operation: string
    state: any
end

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

Methods

ready#
ready: function(Waiting): boolean

Reports whether the subscription has settled.

Arguments
NameTypeDescription
?Waiting
Returns
TypeDescription
boolean
onResume#
onResume: function(Waiting, function(): nil): nil

Registers a wake callback for the waiting scheduler.

Arguments
NameTypeDescription
?Waiting
?function(): nil
Returns
TypeDescription
nil

Fields

operation#
operation: string

Diagnostic operation name supplied to suspend.

state#

Provider-owned settlement and subscription state.

Functions#

destroyInstalledfunction#

function destroyInstalled(takes value: Installed): nil

Canonical affine cleanup for a handler installation.

Arguments

NameTypeDescription
takes valueInstalled

Returns

TypeDescription
nil

Values#

servicevariable#

const service: services.Service<Provider>

Canonical suspension API 1 handle; setup selects before the public facade loads.