Cross-platform scheduler, context, timer, IPv4, and TCP helpers. The selected backend is linux/sync, macos/sync, or windows/sync.
Scheduling and timing
spawn
(callable out -- context) Creates, schedules, and returns one Context for callable with output schema out.- The
callableis invoked with no stack inputs. For()asout, it must return no objects; otherwise it must return one object matching the requested output schema. - A wrong-schema single output reports
context return schema mismatch. Blockcallables run through a call boundary.- A mutable callable view is moved into the new context. An immutable callable view is borrowed; keep the callable and anything it borrows alive until execution finishes.
- The callable body begins when scheduled, not before
spawnreturns.
Known issue: Linux: direct Text outputs currently lose their contents when retrieved from the Context, despite matching the requested schema; return an owned String instead, for example ["hello" toString] String spawn.
yield
(--) Allows another ready context to run and later resumes the current context.- Linux: a positive timed wait installs a cancellation callback, but
yielddoes not. Canceling a context suspended byyield, including a sleep that takes the zero-duration yield path, can terminate withinvalid cancelation function. Do not treatyieldas a safely cancellable timed wait. - macOS:
yieldenqueues and dispatches without installing a cancellation callback; its default callback reportsinvalid cancelation function. - Windows:
yieldenqueues and dispatches without installing a cancellation callback; its default callback reportsinvalid cancelation function.
canceled?
(-- canceled) Reports whether the current context has been canceled.sleepFor
(duration --) Waits for the requested duration unless canceled; cancellation can resume the current context early.- Linux:
durationis seconds inReal64. Fractional durations passed tosleepFororsleepUntilare truncated to whole nanoseconds; a duration that truncates to zero yields without a timer. The scheduler can resume later than the requested time. - A zero duration yields without installing a timer.
- An already-canceled context skips a supported nonnegative sleep. Duration validation precedes this check; cancellation does not make a negative duration valid.
- The duration must be nonnegative. Checked violations report
negative sleep duration; a known violation can fail compilation. Run-time assertions requireDEBUG. - Linux: Disabling assertions does not make a negative duration supported and can instead cause a fatal native-timer failure.
- macOS: The input is cast to
Real64before validation. Below one second it truncates to nanoseconds; at least one second uses truncated microseconds. The duration assertion precedes the cancellation-sensitive wait path. - Windows: The duration assertion precedes the cancellation-sensitive wait path; do not infer the macOS input conversions for this backend.
Known issue: Linux: canceling a context suspended in a zero-duration sleepFor (or a duration that truncates to zero nanoseconds) terminates the process with invalid cancelation function, exit 2; positive timed waits cancel correctly.
sleepUntil
(time --) Waits until the requested scheduler time unless canceled.- Linux:
timeuses the elapsed-time scale inReal64seconds returned bygetTime, not a calendar or Unix timestamp. A past deadline takes the zero-duration path. - Linux: fractional durations passed to
sleepUntilare truncated to whole nanoseconds; a duration that truncates to zero yields without a timer. The scheduler can resume later than the requested time. - An already-canceled context returns without waiting.
- macOS: Past times take the zero-duration path, and the wait uses one-shot
keventtimer behavior. - Windows: The earliest pending timer supplies the timer timeout; ready contexts and I/O completions can resume first. A timer deadline does not guarantee immediate execution or global resumption order.
- Linux: Uses
CLOCK_BOOTTIME. - macOS: Uses
CLOCK_MONOTONIC. - Windows: Derives elapsed
Real64seconds fromQueryPerformanceCounterandQueryPerformanceFrequency.
Fiber stack setting
SL_FIBER_STACK_SIZE is an optional build definition; for example, -D SL_FIBER_STACK_SIZE=131072. It is not defined as a public sync getter. The default usable/requested stack size is 65536 bytes.
- Linux: The usable size must be positive and a multiple of 65536, or the checked failure is
invalid fiber stack size. Separate 65536-byte guard regions are added below and above it. - macOS: The usable size must be positive and a multiple of 16384; separate 16384-byte guards are added on both sides.
- Windows: The value is passed as the reserve-size argument to
CreateFiberEx. This backend does not apply the Linux/macOS positivity or multiple assertion; native creation failure is reported separately.
The context model is cooperative: a running context continues until it calls yield, waits, sleeps, performs a blocking wrapper operation, or finishes. A waiting context resumes when its wait completes or is canceled; readiness does not guarantee immediate execution or global resumption order.
Network helpers
connectTcp
(address port -- connection result) Creates a TCP connection to the host-order IPv4 address and port.addressis a host-orderNat32;portis a host-orderNat16. No hostname resolution is performed.- Success returns a live
TcpConnectionand an empty ownedString. Failure returns an invalid connection and a nonempty owned errorString. - The connection wrapper is owned, movable, and noncopyable.
- Cancellation before a successful result returns the error
String"canceled". - Immediate native success is accepted, but it does not guarantee that
connectTcpreturns without yielding; the backend can still wait for completion notification.
listenTcp
(address port -- acceptor result) Creates a TCP listener for the host-order IPv4 address and port.addressis a host-orderNat32;portis a host-orderNat16. No hostname resolution is performed.- Success returns a live
TcpAcceptorand an empty ownedString. Failure returns an invalid acceptor and a nonempty owned errorString. - The acceptor wrapper is owned, movable, and noncopyable.
- Cancellation before a successful result returns the error
String"canceled". - Linux:
EPOLLERRandEPOLLHUPwake a parked accept or I/O wait so the operation reports its result; one-shot registrations do not repeatedly dispatch an idle error or hangup. - Windows: Listeners set
SO_EXCLUSIVEADDRUSEand use a backlog of 128.
macOS backend
- macOS: Read, write, and timer waits use
kqueueand one-shotkeventregistrations. - macOS: Cancellation deletes the corresponding registration before the waiting fiber resumes; operations that return a result
Stringreport"canceled". - macOS: Fiber stacks use
mmapwith 16384-byte lower and upper guard regions protected bymprotect.
Result and ownership model
- A successful
spawnreturns aContexthandle that owns the spawned context state. The handle remains valid until moved from or destroyed; wait, get, and cancellation require a valid handle. Destruction waits for completion and releases the stored result and context state. - Context is movable, not copyable.
getandwaitrequire a mutable handle. For non-Meta In-place outputs,getreturns a reference to the context-owned result; copy or move that result withnewbefore releasing its owner if it must survive. Meta outputs are returned as values;()returns no object. - Cancellation marks the context and invokes its active cancellation callback. It does not skip or terminate the callable body, and it does not discard a completed output. Check
canceled?in the body when deciding whether to continue or return a result.
Failure behavior
Linux: ordinary TCP setup failures return a nonempty String such as bind failed, result=<errno> or connect failed, result=<errno>. The suffix is a numeric native error value. Cancellation returns canceled.
Timer, scheduler, clock, and fiber-creation failures can instead print a fatal diagnostic and terminate; these operations return no error record. The Linux diagnostics are listed below; only the FATAL prefixes are followed by a native error number.
| Operation | Linux diagnostic prefix |
|---|---|
| getTime | FATAL: clock_gettime failed, result= |
| sleep timer creation | FATAL: timerfd_create failed, result=FATAL: epoll_ctl failed, result= |
| timer programming / wait registration | FATAL: timerfd_settime failed, result=FATAL: epoll_ctl failed, result= |
| scheduling / switching | FATAL: swapcontext failed, result=FATAL: epoll_wait failed, result= |
| fiber allocation | invalid fiber stack sizeFATAL: getcontext failed, result=FATAL: mmap failed, result=FATAL: mprotect failed, result= |
Preconditions can also report invalid cancelation function or invalid fiber stack size.
- macOS: Sleep registration reports
FATAL: [sleepFor] kevent failed, result=. - Windows: Dispatch reports
FATAL: GetQueuedCompletionStatusEx failed, result=; fiber creation reportsFATAL: CreateFiberEx failed, result=.
Examples
IPv4 formatting and spawn
"String" use
"control" use
"sync/sync" use
{} Int32 {} [
0xC0A86465n32 ipv4ToString print
LF print
context: [("done" LF) printList] () spawn;
@context.wait
0
] "main" exportFunction
Expected Output
192.168.100.101
done
See also
- sync/Context: Spawned context handle with waiting, output retrieval, and cancellation.
- sync/ContextGroup: Group of spawned contexts with shared waiting and cancellation.
- sync/Event: Persistent event state with clear, set, wait, wake, and wakeOne.
- sync/Signal: Wait queue with wake and wakeOne operations.
- sync/TcpAcceptor: Listening TCP acceptor with address reporting and accepted connection creation.
- sync/TcpConnection: Connected TCP stream with buffered read, string read, write, and shutdown operations.
- String: UTF-8 string views, owned strings, formatting helpers, and text conversion utilities.