Sync

From MemCP
Revision as of 11:59, 28 August 2026 by Wikiservice (talk | contribs) (Refresh MemCP documentation: accuracy, operational guidance, performance profile and maintained API reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Sync

Generated from MemCP commit c42e19eba on 27 August 2026. See Full SCM API documentation.

The Sync module provides explicit coordination for state that must cross functional scopes or concurrent tasks. It includes:

  • thread-safe sessions and contextual values;
  • promises and one-time publication;
  • mutex and serialized execution helpers;
  • shared computation and cache maps;
  • timing, waiting, and lifecycle coordination.

Prefer ordinary immutable values when no sharing is required. Use these primitives to make ownership, failure, and publication explicit rather than relying on outer-scope mutation, which this Scheme dialect does not provide.

newpromise

Creates a thread-safe promise that lets parallel work publish one result for other code to inspect. Use it for shared initialization, asynchronous results, or ensuring that only the first successful producer resolves a value. Read with (promise "value"), inspect completion with (promise "state"), resolve with (promise "value" value), resolve exactly once with (promise "once" value), and mark failure with (promise "fail" error).

Allowed number of parameters: 0–1

Parameters

  • storage (list<any>): optional existing two-item list used to hold the promise state; most callers omit this (optional)
    • slot (any): promise state or value slot

Returns

  • promise (func): operation-based accessor for reading, resolving, or failing the promise
    • Parameters
      • operation (string): one of: value, state, fail, once
      • value (any): value to store (for value/once/fail) (optional)
      • message (string): optional error message used when once finds an already completed promise (optional)
    • Returns
      • result (any): stored value, state flag, or operation result

newsession

Creates a thread-safe key-value session. Call it without arguments to list values, with a key to read, with a key and value to store, or with get_or_compute_scoped, a scope, a key, and a producer to share one concurrent computation.

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

  • session (func): session accessor accepting exactly zero, one, two, or four arguments
    • Parameters
      • key_or_operation (any): key, or get_or_compute_scoped (optional)
      • value_or_scope (any): value to store, or scope for get_or_compute_scoped (optional)
      • scoped_key (any): cache key used by get_or_compute_scoped (optional)
      • scoped_producer (func): producer used only by the four-argument get_or_compute_scoped form (optional)
        • Returns
          • value (any): computed value cached for the scope and key
    • Returns
      • result (any): value list, stored value, retrieved value, or shared computed value

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): the session to install
    • Parameters
      • key (any) (optional)
      • value (any) (optional)
    • Returns
      • value (any)
  • fn (func): the function to execute
    • Returns
      • value (any)

Returns

  • value (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

  • value (any)

sleep

sleeps the amount of seconds

Allowed number of parameters: 1–1

Parameters

  • duration (number): number of seconds to sleep

Returns

  • value (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
    • Parameters
      • argument (any) (variadic)
    • Returns
      • result (any)

Returns

  • once_wrapper (func): calls the wrapped function once and returns its cached result thereafter
    • Parameters
      • args (any): arguments forwarded to the wrapped function on first call (variadic)
    • Returns
      • result (any): result cached from the first call

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

  • locked (func): executes one parameterless function while holding the mutex
    • Parameters
      • fn (func): parameterless function to execute under the lock
        • Returns
          • result (any)
    • Returns
      • result (any): result returned by the protected function

numcpu

Returns the number of logical CPUs available for parallel execution

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

  • value (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

  • value (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): function to execute once the timeout expires
    • Parameters
      • args (any) (variadic)
    • Returns
      • value (any)
  • milliseconds (number): milliseconds until execution
  • args... (any): optional arguments forwarded to the callback (variadic)

Returns

  • value (int)

clearTimeout

Cancels a timeout created with setTimeout.

Allowed number of parameters: 1–1

Parameters

  • id (number): identifier returned by setTimeout

Returns

  • value (bool)