Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.41.0

0.41.0 / src / std / interop / worker_entry.lucb

#==============================================================================================
#
#   worker_entry - A worker's factory and runtime hooks
#
#   DESCRIPTION:
#       A named factory that runs once on the new thread and returns its local handler, with no
#       retained source-thread state.
#
#==============================================================================================

## A named factory and its runtime hooks, with no retained source-thread state.
## The factory runs exactly once on the new thread and returns its local handler.
## Compilers supply the managed entry adapter; Base factories use native().
pub struct WorkerEntry[C, M, R]:
    ## The factory's context pointer, borrowed for the worker's life.
    pub let context: void*?
    ## Runs the factory on the new thread, yielding its handler or a failure.
    pub let open: func(void*?, C) -> Outcome[Callback[M, R]]
    ## The runtime hook run as the worker starts.
    pub let enter: func() -> unit
    ## The runtime hook run as the worker ends.
    pub let leave: func() -> unit

    ## A worker entry from a plain Base factory.
    pub static func native(factory: func(C) -> Outcome[Callback[M, R]]) -> WorkerEntry[C, M, R]:
        return WorkerEntry[C, M, R]((void*)factory, WorkerEntry[C, M, R].open_native,
                                    ownership.enter, ownership.finish_task)

    static func open_native(context: void*?, configuration: C) -> Outcome[Callback[M, R]]:
        let factory = (func(C) -> Outcome[Callback[M, R]])(context else trap("a worker has no factory"))
        return factory(configuration)

local var active_worker_cancellation: sync.Cancellation*? = none

## Native operations called by a worker use this signal for interruptible socket
## waits. The pointer is borrowed on this worker until its factory/handler returns;
## it must not be retained outside the worker's lifetime or reset by application code.
pub func worker_cancellation() -> sync.Cancellation*:
    return active_worker_cancellation else trap("this operation requires an application worker")