Sync

From MemCP
Revision as of 08:29, 27 August 2026 by Carli (talk | contribs)
Jump to navigation Jump to search

Sync

The Sync module provides synchronization and concurrency functionality for the SCM programming language. This module includes:

  • Session management: Functions to create threadsafe key-value stores (newsession)
  • Context handling: Context management with session support (context)
  • Timing control: Functions to pause execution (sleep)
  • Function caching: One-time execution wrappers with result caching (once)
  • Thread synchronization: Mutex creation for serialized access (mutex)

These functions provide essential tools for managing concurrent operations, shared state, and synchronization in multi-threaded SCM programs.

← Back to Full SCM API documentation

newpromise

Creates a single-value promise cell (thread-safe via CAS spin-lock). Returns a tagPromise Scmer. (newpromise) allocates a [2]Scmer backing; (newpromise list) reuses an existing ≥2-element slice as backing with zero extra allocation. API: (p "value") reads current value (nil if pending), (p "value" v) resolves, (p "once" v) resolves once (panics if already fulfilled/failed), (p "once" v msg) resolves once with custom panic message, (p "state") returns state (nil/true/false), (p "fail") sets failed and clears the stored value, (p "fail" err) sets failed and stores err as payload.

Allowed number of parameters: 0–1

Parameters

  • list (any): optional: ≥2-element slice to use as backing (optional)

Returns

func(operation:string, value:any?, msg:string?) -> any

newsession

Creates a new session which is a threadsafe key-value store. Besides get/set/list, get_or_compute_scoped shares concurrent computation by a query-local handle.

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

func(key_or_operation:any?, value_scope_or_key:any?, key_or_producer:any?, scoped_producer:func?) -> any

with_session

Executes a function with the given session installed in the execution context, so storage operations can access the session's transaction state.

Allowed number of parameters: 2–2

Parameters

  • session (func(key:any?, value:any?) -> any): the session to install
  • fn (func): the function to execute

Returns

any

context

Context helper function. Each context also contains a session. (context func args) creates a new context and runs func in that context, (context "session") reads the session variable, (context "check") will check the liveliness of the context and otherwise throw an error

Allowed number of parameters: 0–10000

Parameters

  • args... (any): depends on the usage (variadic)

Returns

any

sleep

sleeps the amount of seconds

Allowed number of parameters: 1–1

Parameters

  • duration (number): number of seconds to sleep

Returns

bool

once

Creates a function wrapper that you can call multiple times but only gets executed once. The result value is cached and returned on a second call. You can add parameters to that resulting function that will be passed to the first run of the wrapped function.

Allowed number of parameters: 1–1

Parameters

  • f (func): function that produces the result value

Returns

func(args:any...) -> any

mutex

Creates a context-aware mutex. The return value serializes calls to parameterless functions and stops waiting when the current request is cancelled.

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

func(fn:func) -> any

numcpu

Returns the number of logical CPUs available for parallel execution

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

number

memstats

Returns memory statistics as a dict with keys: alloc, total_alloc, sys, heap_alloc, heap_sys (all in bytes)

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

dict

setTimeout

Schedules a callback to run after the given delay in milliseconds (fractional values allowed for sub-millisecond precision).

Allowed number of parameters: 2–10000

Parameters

  • callback (func(args:any...) -> any): function to execute once the timeout expires
  • milliseconds (number): milliseconds until execution
  • args... (any): optional arguments forwarded to the callback (variadic)

Returns

int

clearTimeout

Cancels a timeout created with setTimeout.

Allowed number of parameters: 1–1

Parameters

  • id (number): identifier returned by setTimeout

Returns

bool