Standard library · Time and concurrency

ctx

A context for cancellation and deadlines, passed from the main piece of work down to the smaller ones.

import ctx
On this page
  1. Types
  2. Functions

A context tells long-running work when to stop: either someone called Cancel, or its deadline passed. Contexts form a tree: you start from Background and make a child each time with WithCancel, WithTimeout or WithDeadline.

Cancelling a context ends all its children, but not its parent. The work should check IsDone from time to time, or wait on it with Wait. Free each context with Free when you are done with it, children before parents.

Types

Context

struct Context
A context.
NameTypeDescription
statei64*The internal state: cancelled, the deadline and whether there is one.
parentContext*The parent context, or null for the root.
musync.MutexThe lock every context in one tree shares.
cvsync.CondVarThe condition variable that cancellation wakes.
is_rootbool = falseWhether this context is the root.
ctx-Context.salam
import ctx

func main:
    root := ctx.Background()
    cancellable := ctx.WithCancel(root)
    timed := ctx.WithTimeout(cancellable, 30)
    println ctx.IsDone(timed), "[" + ctx.Err(timed) + "]", ctx.Deadline(timed) > 0, ctx.Deadline(root)
    println ctx.Wait(timed, 10)
    println ctx.Wait(timed, 1000), ctx.Err(timed)
    ctx.Cancel(cancellable)
    println ctx.IsDone(cancellable), ctx.Err(cancellable), ctx.IsDone(root)
    past := ctx.WithDeadline(root, 1)
    println ctx.IsDone(past), ctx.Err(past)
    ctx.Free(past)
    ctx.Free(timed)
    ctx.Free(cancellable)
    ctx.Free(root)
end
Outputfalse [] true 0 false true deadline exceeded true cancelled false true deadline exceeded

Functions

Background

func Background(): Context
The root context, which never finishes by itself.

The example for this item is shown with the ones above: ctx-Context.salam

WithCancel

func WithCancel(parent: Context): Context
A child that ends when Cancel is called.

The example for this item is shown with the ones above: ctx-Context.salam

WithTimeout

func WithTimeout(parent: Context, ms: i64): Context
A child that ends ms milliseconds from now.

The example for this item is shown with the ones above: ctx-Context.salam

WithDeadline

func WithDeadline(parent: Context, at_ms: i64): Context
A child that ends at the instant at_ms, in milliseconds, as sync.NowMs gives it.

The example for this item is shown with the ones above: ctx-Context.salam

Cancel

func Cancel(c: Context)
Ends the context and all its children and wakes everything waiting.

The example for this item is shown with the ones above: ctx-Context.salam

IsDone

func IsDone(c: Context): bool
True when the context, or one of its parents, has been cancelled or has passed its deadline.

The example for this item is shown with the ones above: ctx-Context.salam

Err

func Err(c: Context): str
Why it finished: cancelled or deadline exceeded, or an empty string while it is still running.

The example for this item is shown with the ones above: ctx-Context.salam

Deadline

func Deadline(c: Context): i64
The nearest deadline in the context and its parents, in milliseconds, or zero when there is none.

The example for this item is shown with the ones above: ctx-Context.salam

Wait

func Wait(c: Context, ms: i64): bool
Waits at most ms milliseconds for the context to finish and says whether it did.

The example for this item is shown with the ones above: ctx-Context.salam

Free

func Free(c: Context)
Releases the context's memory.

The example for this item is shown with the ones above: ctx-Context.salam