Concurrency

Concurrency

Important

This page is under development. The concurrency model is being designed. Full documentation will be added once the design is finalized.

Kairo's concurrency system provides async/await, coroutines, and thread-level primitives. The following features are planned and referenced across the existing documentation:

Async/Await

The async modifier on functions and the await keyword for waiting on asynchronous results. See Functions for the async modifier.

Coroutines (yield)

Functions with a yield T return type are generators: they produce values cooperatively. The yield keyword suspends the function and produces a value to the caller. See Generators for the rules.

spawn

spawn f(args) starts an async function as a concurrent task and returns its future. await on the future joins the task; a task whose future is never awaited is detached. The runtime model (green threads, OS threads, or event loop) is being finalized. See Threading for how Tether analyzes what a task captures.

Atomic Types (atomic T)

Thread-safe wrapper type for lock-free operations. Referenced in Functions.

Thread-Local Variables

See Thread-Local Variables.

Synchronization Primitives

Mutexes, channels, and other coordination mechanisms will be documented here once the standard library concurrency API is finalized.

Custom Awaitables

Classes can define fn <T> op await(self, obj: std::forward<T>) -> T to customize the behavior of await when called on an instance. See Operators for the op await overload.

await async_fn()       // syntax sugar for a state machine
var fut = spawn some_async_fn()  // start a concurrent task
await fut                        // join it; never awaiting detaches it
yield some_value       // syntax sugar for a coroutine, function must have yield on return type

fn get_tokens() -> yield string {
    yield "token1"
    yield "token2"
}

op await is overloadable: fn op await (self) -> T for custom async types.

  • await async_fn() syntax sugar for state machine
  • spawn some_async_fn() starts a concurrent task and returns its future; never awaiting it detaches the task
  • yield some_value coroutine syntax, function must have -> yield T return type
  • fn op await (self) -> T overloadable for custom async types
  • atomic T thread-safe wrapper type

See also