Skip to content

Async functions and tasks

An async function or async task can suspend: it ends the current run of the task that executes it and continues at the same point when that task is activated again. Its local variables keep their values and their addresses in between.

These pages call the task that executes a piece of async code its hardware task. For an async task, it is the task itself. For an async function, it is the task of the caller, or a worker task in an @await_parallel.

async fn name(parameters) ReturnType { ... }

An async function takes parameters and returns a value like any function. It can contain suspend, call other async functions with @await and @await_parallel, and call ordinary functions.

src/pe.csl
// Suspends once, then returns x + 1.
async fn next(x: u32) u32 {
@activate(@current_task());
suspend;
return x + 1;
}

Rules:

  • An async function can only be called from an async function or async task, and only as the argument of @await or @await_parallel: @await(next(1)). The call has no value of its own and cannot be stored or passed on.
  • An async function cannot be evaluated at comptime.
  • An async function cannot be extern.
  • An async function can have comptime parameters and can be declared in a module and called through the module, like other functions.
async task name() void { ... }

An async task is a task that can suspend. It is bound to a task ID with @bind_local_task in a comptime block, and started with @activate or by the completion of an asynchronous operation, like any task:

src/pe.csl
const main_task_id: local_task_id = @claim_local_task();
async task main_task() void {
@assert(@await(twice(1)) == 3);
@assert(@await(three_at_once()) == 13);
@assert(@await(spread(5)) == 18);
sys_mod.unblock_cmd_stream();
}
// Called by the host.
fn compute() void {
@coro_runtime_init();
@activate(main_task_id);
}
comptime {
@bind_local_task(main_task, main_task_id);
}

The task ID must be known at compile time, and the bind must be made at compile time. When an async task returns, its next activation starts it again from the beginning.

suspend;

suspend ends the current run of the hardware task that executes it. When the task is activated the next time, execution continues after the suspend.

suspend does not start anything that activates the task again. Before suspend, start an operation that activates the task when it completes, usually an asynchronous DSD operation with .activate = @current_task():

@fmovs(out_dsd, x_dsd, .{ .async = true, .activate = @current_task() });
suspend;

@activate(@current_task()) directly before suspend resumes the task as soon as other activated tasks have run.

If the operation completes before the task reaches suspend, the activation is kept: the task runs again as soon as the current run ends, and continues after the suspend right away.

Any activation of the task resumes it, whatever its cause. A task that is suspended must therefore not be activated for another purpose. A suspended task whose activation never comes is never resumed. A suspended task does not block other tasks of the PE: they run as usual while it waits.

suspend is only allowed in async functions and async tasks.

@current_task() local_task_id

Returns the ID of the hardware task that executes the call:

  • in an async task, the task itself;
  • in an async function called with @await, the task of the caller;
  • in a call of @await_parallel that runs on a worker task, the worker task.

Use it as the .activate target of an asynchronous operation before suspend, so that the operation resumes the code that started it.

@coro_runtime_init() void

Sets up the runtime that suspends and resumes async tasks. A program that binds an async task must call @coro_runtime_init() once, before the first async task is activated. Usually, the function that the host launches calls it before it activates the first task. csl-pp reports an error if a program binds an async task and never calls @coro_runtime_init() (see Troubleshooting), and a remark if a program calls it without binding an async task.

Local variables of async functions and tasks keep their values across suspend. They also keep their addresses, so an asynchronous DSD operation can read or write a local variable while the function is suspended. In the allreduce library, allreduce_nrm2 reduces a local variable buf that the DSD operations of several phases access through a pointer.

Each async task has a stack of its own for these variables; see Stacks.

Deadlocks between PEs are possible as in vanilla CSL. For example, two PEs that each wait for data from the other wait forever.