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.
do spawn {
log(error, "Hello, World!");
}
SchedMode
- Scheduler modesTaskResult
- Indicates the manner in which a task exited.SchedOpts
- Scheduler configuration optionsTaskBuilder
- The task builder type.TaskOpts
- Task configuration optionsof ::std::cmp::Eq for TaskResult
- Automatically derived.of ::std::cmp::Eq for SchedMode
- Automatically derived.for TaskBuilder
default_task_opts
- The default task optionsdeschedule
- Yield control to the task schedulerfailing
- True if the running task has failedrekillable
- Makes killable a task marked as unkillablespawn
- Creates and executes a new child taskspawn_indestructible
- Creates a child task that cannot be killed by linked failurespawn_sched
- * Creates a new task on a new or existing schedulerspawn_supervised
- Creates a child task supervised by the current onespawn_unlinked
- Creates a child task unlinked from the current onespawn_with
- Runs a task, while transferring ownership of one argument to the child.task
- Generate the base configuration for spawning a task, off of which more configuration methods can be chainedtry
- Execute a function in another task and return either the return value of the function or result::err.unkillable
- Temporarily make the task unkillablewith_task_name
- Read the name of the current task.SchedMode
Scheduler modes
DefaultScheduler
- Run task on the default scheduler
SingleThreaded
- All tasks run in the same OS thread
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.
Success
Failure
SchedOpts
pub struct SchedOpts {
mode: SchedMode,
}
Scheduler configuration options
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.
TaskOpts
pub struct TaskOpts {
linked: bool,
supervised: bool,
watched: bool,
indestructible: bool,
notify_chan: Option<Chan<TaskResult>>,
name: Option<~str>,
sched: SchedOpts,
stack_size: Option<uint>,
}
Task configuration options
linked - Propagate failure bidirectionally between child and parent. True by default. If both this and 'supervised' are false, then either task's failure will not affect the other ("unlinked").
supervised - Propagate failure unidirectionally from parent to child, but not from child to parent. False by default.
watched - Make parent task collect exit status notifications from child before reporting its own exit status. (This delays the parent task's death and cleanup until after all transitively watched children also exit.) True by default.
indestructible - Configures the task to ignore kill signals received from linked failure. This may cause process hangs during failure if not used carefully, but causes task blocking code paths (e.g. port recv() calls) to be faster by 2 atomic operations. False by default.
notify_chan - Enable lifecycle notifications on the given channel
name - A name for the task-to-be, for identification in failure messages.
sched - Specify the configuration of a new scheduler to create the task in
By default, every task is created in the same scheduler as its parent, where it is scheduled cooperatively with all other tasks in that scheduler. Some specialized applications may want more control over their scheduling, in which case they can be spawned into a new scheduler with the specific properties required.
This is of particular importance for libraries which want to call into foreign code that blocks. Without doing so in a different scheduler other tasks will be impeded or even blocked indefinitely.
::std::cmp::Eq
for TaskResult
Automatically derived.
eq
fn eq(&self, __arg_0: &TaskResult) -> ::bool
ne
fn ne(&self, __arg_0: &TaskResult) -> ::bool
::std::cmp::Eq
for SchedMode
Automatically derived.
eq
fn eq(&self, __arg_0: &SchedMode) -> ::bool
ne
fn ne(&self, __arg_0: &SchedMode) -> ::bool
TaskBuilder
unlinked
fn unlinked(&mut self)
Decouple the child task's failure from the parent's. If either fails, the other will not be killed.
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.
linked
fn linked(&mut self)
Link the child task's and parent task's failures. If either fails, the other will be killed.
watched
fn watched(&mut self)
Cause the parent task to collect the child's exit status (and that of all transitively-watched grandchildren) before reporting its own.
unwatched
fn unwatched(&mut self)
Allow the child task to outlive the parent task, at the possible cost of the parent reporting success even if the child task fails later.
indestructible
fn indestructible(&mut self)
Cause the child task to ignore any kill signals received from linked failure. This optimizes context switching, at the possible expense of process hangs in the case of unexpected failure.
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.
Fails if a future_result was already set for this task.
name
fn name(&mut self, name: ~str)
Name the task-to-be. Currently the name is used for identification only in failure messages.
sched_mode
fn sched_mode(&mut self, mode: SchedMode)
Configure a custom scheduler mode for the task.
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.
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.
When spawning into a new scheduler, the number of threads requested must be greater than zero.
spawn_with
fn spawn_with<A: Send>(&mut self, arg: A, f: ~fn(v: A))
Runs a task, while transferring ownership of one argument to the child.
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.
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.
Fails if a future_result was already set for this task.
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.
deschedule
fn deschedule()
Yield control to the task scheduler
failing
fn failing() -> bool
True if the running task has failed
rekillable
fn rekillable<U>(f: &fn() -> U) -> U
Makes killable a task marked as unkillable. This is meant to be used only nested in unkillable.
~ do task::unkillable { do task::rekillable { // Task is killable } // Task is unkillable again }
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)
.
spawn_indestructible
fn spawn_indestructible(f: ~fn())
Creates a child task that cannot be killed by linked failure. This causes its context-switch path to be faster by 2 atomic swap operations. (Note that this convenience wrapper still uses linked-failure, so the child's children will still be killable by the parent. For the fastest possible spawn mode, use task::task().unlinked().indestructible().spawn.)
spawn_sched
fn spawn_sched(mode: SchedMode, f: ~fn())
Creates a new task on a new or existing scheduler
greater than zero.
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.
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.
spawn_with
fn spawn_with<A: Send>(arg: A, f: ~fn(v: A))
Runs a task, while transferring ownership of one argument to the child.
This is useful for transferring ownership of noncopyables to another task.
This function is equivalent to task().spawn_with(arg, f)
.
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.
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.
unkillable
fn unkillable<U>(f: &fn() -> U) -> U
Temporarily make the task unkillable
do task::unkillable {
// detach / deschedule / destroy must all be called together
rustrt::rust_port_detach(po);
// This must not result in the current task being killed
task::deschedule();
rustrt::rust_port_destroy(po);
}
with_task_name
fn with_task_name<U>(blk: &fn(Option<&str>) -> U) -> U
Read the name of the current task.