Task management.

An executing Rust program consists of a tree of tasks, each with their own stack, and sole ownership of their allocated heap data. Tasks communicate with each other using ports and channels.

When a task fails, that failure will propagate to its parent (the task that spawned it) and the parent will fail as well. The reverse is not true: when a parent task fails its children will continue executing. When the root (main) task fails, all tasks fail, and then so does the entire process.

Tasks may execute in parallel and are scheduled automatically by the runtime.

Example

do spawn {
    log(error, "Hello, World!");
}

Enum SchedMode

Scheduler modes

Variants

Enum Scheduler

A handle to a scheduler

Variants

Enum Task

A handle to a task

Variants

Enum TaskResult

Indicates the manner in which a task exited.

A task that completes without failing is considered to exit successfully. Supervised ancestors and linked siblings may yet fail after this task succeeds. Also note that in such a case, it may be nondeterministic whether linked failure or successful exit happen first.

If you wish for this result's delivery to block until all linked and/or children tasks complete, recommend using a result future.

Variants

Struct SchedOpts

pub struct SchedOpts {
    mode: SchedMode,
    foreign_stack_size: Option<uint>,
}

Scheduler configuration options

Fields

Struct TaskBuilder

pub struct TaskBuilder {
    opts: TaskOpts,
    gen_body: Option<~fn(v: ~fn()) -> ~fn()>,
    can_not_copy: Option<util::NonCopyable>,
    consumed: bool,
}

The task builder type.

Provides detailed control over the properties and behavior of new tasks.

Struct TaskOpts

pub struct TaskOpts {
    linked: bool,
    supervised: bool,
    notify_chan: Option<Chan<TaskResult>>,
    sched: SchedOpts,
}

Task configuration options

Fields

Implementation of ::std::cmp::Eq for Scheduler

Automatically derived.

Method eq

fn eq(&self, __arg_0: &Scheduler) -> ::bool

Method ne

fn ne(&self, __arg_0: &Scheduler) -> ::bool

Implementation of ::std::cmp::Eq for Task

Automatically derived.

Method eq

fn eq(&self, __arg_0: &Task) -> ::bool

Method ne

fn ne(&self, __arg_0: &Task) -> ::bool

Implementation of ::std::cmp::Eq for TaskResult

Automatically derived.

Method eq

fn eq(&self, __arg_0: &TaskResult) -> ::bool

Method ne

fn ne(&self, __arg_0: &TaskResult) -> ::bool

Implementation of ::std::cmp::Eq for SchedMode

Automatically derived.

Method eq

fn eq(&self, __arg_0: &SchedMode) -> ::bool

Method ne

fn ne(&self, __arg_0: &SchedMode) -> ::bool

Implementation for TaskBuilder

Method unlinked

fn unlinked(&mut self)

Decouple the child task's failure from the parent's. If either fails, the other will not be killed.

Method supervised

fn supervised(&mut self)

Unidirectionally link the child task's failure with the parent's. The child's failure will not kill the parent, but the parent's will kill the child.

Method linked

fn linked(&mut self)

Link the child task's and parent task's failures. If either fails, the other will be killed.

Method future_result

fn future_result(&mut self, blk: &fn(v: Port<TaskResult>))

Get a future representing the exit status of the task.

Taking the value of the future will block until the child task terminates. The future-receiving callback specified will be called before the task is spawned; as such, do not invoke .get() within the closure; rather, store it in an outer variable/list for later use.

Note that the future returning by this function is only useful for obtaining the value of the next task to be spawning with the builder. If additional tasks are spawned with the same builder then a new result future must be obtained prior to spawning each task.

Failure

Fails if a future_result was already set for this task.

Method sched_mode

fn sched_mode(&mut self, mode: SchedMode)

Configure a custom scheduler mode for the task.

Method add_wrapper

fn add_wrapper(&mut self, wrapper: ~fn(v: ~fn()) -> ~fn())

Add a wrapper to the body of the spawned task.

Before the task is spawned it is passed through a 'body generator' function that may perform local setup operations as well as wrap the task body in remote setup operations. With this the behavior of tasks can be extended in simple ways.

This function augments the current body generator with a new body generator by applying the task body which results from the existing body generator to the new body generator.

Method spawn

fn spawn(&mut self, f: ~fn())

Creates and executes a new child task

Sets up a new task with its own call stack and schedules it to run the provided unique closure. The task has the properties and behavior specified by the task_builder.

Failure

When spawning into a new scheduler, the number of threads requested must be greater than zero.

Method spawn_with

fn spawn_with<A: Send>(&mut self, arg: A, f: ~fn(v: A))

Runs a task, while transfering ownership of one argument to the child.

Method try

fn try<T: Send>(&mut self, f: ~fn() -> T) -> Result<T, ()>

Execute a function in another task and return either the return value of the function or result::err.

Return value

If the function executed successfully then try returns result::ok containing the value returned by the function. If the function fails then try returns result::err containing nil.

Failure

Fails if a future_result was already set for this task.

Function atomically

unsafe fn atomically<U>(f: &fn() -> U) -> U

A stronger version of unkillable that also inhibits scheduling operations. For use with exclusive ARCs, which use pthread mutexes directly.

Function default_task_opts

fn default_task_opts() -> TaskOpts

The default task options

By default all tasks are supervised by their parent, are spawned into the same scheduler, and do not post lifecycle notifications.

Function failing

fn failing() -> bool

True if the running task has failed

Function get_scheduler

fn get_scheduler() -> Scheduler

Function get_task

fn get_task() -> Task

Get a handle to the running task

Function rekillable

unsafe fn rekillable<U>(f: &fn() -> U) -> U

The inverse of unkillable. Only ever to be used nested in unkillable().

Function spawn

fn spawn(f: ~fn())

Creates and executes a new child task

Sets up a new task with its own call stack and schedules it to run the provided unique closure.

This function is equivalent to task().spawn(f).

Function spawn_sched

fn spawn_sched(mode: SchedMode, f: ~fn())

Function spawn_supervised

fn spawn_supervised(f: ~fn())

Creates a child task supervised by the current one. If the child task fails, the parent will not be killed, but if the parent fails, the child will be killed.

Function spawn_unlinked

fn spawn_unlinked(f: ~fn())

Creates a child task unlinked from the current one. If either this task or the child task fails, the other will not be killed.

Function spawn_with

fn spawn_with<A: Send>(arg: A, f: ~fn(v: A))

Runs a task, while transfering ownership of one argument to the child.

This is useful for transfering ownership of noncopyables to another task.

This function is equivalent to task().spawn_with(arg, f).

Function task

fn task() -> TaskBuilder

Generate the base configuration for spawning a task, off of which more configuration methods can be chained. For example, task().unlinked().spawn is equivalent to spawn_unlinked.

Function try

fn try<T: Send>(f: ~fn() -> T) -> Result<T, ()>

Execute a function in another task and return either the return value of the function or result::err.

This is equivalent to task().supervised().try.

Function unkillable

unsafe fn unkillable<U>(f: &fn() -> U) -> U

Temporarily make the task unkillable

Example

do task::unkillable {
    // detach / yield / destroy must all be called together
    rustrt::rust_port_detach(po);
    // This must not result in the current task being killed
    task::yield();
    rustrt::rust_port_destroy(po);
}

Function yield

fn yield()

Yield control to the task scheduler