Skip to content
CryoCryo home
StdlibConcurrency

future

The runtime half of Cryo's async support. async and await are a compile-time transformation — an async function becomes a state machine, and calling it builds a struct rather than running the body. Nothing happens until something polls it, and this module is what does the polling.

public module future::poll;
public module future::waker;
public module future::traits;
public module future::ready;
public module future::reactor;
public module future::blocking;
public module future::executor;
public module future::timer;
public module future::combinator;

The module is named future because async is a reserved keyword. See Asynchronous Programming for the language side. A plain import std::future; brings in the whole module.

FileWhat it holdsImport
traitsFutureimport std::future::traits;
pollPoll<T>import std::future::poll;
wakerContext, Wakerimport std::future::waker;
readyReady<T>, PendingThenReady<T> — futures for testsimport std::future::ready;
executorExecutor, JoinHandle, JoinErrorimport std::future::executor;
combinatorFutures::join / try_join / select / timeoutimport std::future::combinator;
timerSleepimport std::future::timer;
reactorReactor, the I/O readiness driverimport std::future::reactor;
blockingBlockingPool, BlockingJob — off-executor workimport std::future::blocking;

How the pieces fit

A Future is polled with a Context, which carries a Waker. Returning Pending means "not yet — I have arranged to be woken"; returning Ready(v) means the value is available and the future is done. The executor owns the tasks and the worker threads that poll them; a parked task is woken by whichever of the two drivers it registered with — the reactor for descriptor readiness and timers, or the blocking pool for a call the OS cannot report readiness on. Everything in net, and the async halves of process and io, is a leaf future built on those two.

Cancellation has no mechanism of its own: dropping a parked future runs its Drop, which is where a Sleep disarms its timer and an I/O future releases its registration. See cancellation is a drop.