2012-09-27 04:00:13 +02:00
|
|
|
% Rust Tasks and Communication Tutorial
|
|
|
|
|
|
|
|
# Introduction
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
The designers of Rust designed the language from the ground up to support pervasive
|
2012-10-02 00:41:21 +02:00
|
|
|
and safe concurrency through lightweight, memory-isolated tasks and
|
|
|
|
message passing.
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
Rust tasks are not the same as traditional threads: rather, they are more like
|
|
|
|
_green threads_. The Rust runtime system schedules tasks cooperatively onto a
|
|
|
|
small number of operating system threads. Because tasks are significantly
|
|
|
|
cheaper to create than traditional threads, Rust can create hundreds of
|
|
|
|
thousands of concurrent tasks on a typical 32-bit system.
|
|
|
|
|
|
|
|
Tasks provide failure isolation and recovery. When an exception occurs in Rust
|
2013-02-01 03:24:09 +01:00
|
|
|
code (as a result of an explicit call to `fail!()`, an assertion failure, or
|
2012-10-10 01:14:55 +02:00
|
|
|
another invalid operation), the runtime system destroys the entire
|
|
|
|
task. Unlike in languages such as Java and C++, there is no way to `catch` an
|
|
|
|
exception. Instead, tasks may monitor each other for failure.
|
|
|
|
|
|
|
|
Rust tasks have dynamically sized stacks. A task begins its life with a small
|
|
|
|
amount of stack space (currently in the low thousands of bytes, depending on
|
|
|
|
platform), and acquires more stack as needed. Unlike in languages such as C, a
|
|
|
|
Rust task cannot run off the end of the stack. However, tasks do have a stack
|
|
|
|
budget. If a Rust task exceeds its stack budget, then it will fail safely:
|
|
|
|
with a checked exception.
|
|
|
|
|
|
|
|
Tasks use Rust's type system to provide strong memory safety guarantees. In
|
|
|
|
particular, the type system guarantees that tasks cannot share mutable state
|
|
|
|
with each other. Tasks communicate with each other by transferring _owned_
|
|
|
|
data through the global _exchange heap_.
|
|
|
|
|
|
|
|
This tutorial explains the basics of tasks and communication in Rust,
|
|
|
|
explores some typical patterns in concurrent Rust code, and finally
|
|
|
|
discusses some of the more unusual synchronization types in the standard
|
2012-09-30 04:21:12 +02:00
|
|
|
library.
|
|
|
|
|
2012-10-08 06:29:26 +02:00
|
|
|
> ***Warning:*** This tutorial is incomplete
|
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
## A note about the libraries
|
2012-09-30 04:21:12 +02:00
|
|
|
|
|
|
|
While Rust's type system provides the building blocks needed for safe
|
|
|
|
and efficient tasks, all of the task functionality itself is implemented
|
|
|
|
in the core and standard libraries, which are still under development
|
2012-10-02 00:41:21 +02:00
|
|
|
and do not always present a consistent interface.
|
2012-09-30 04:21:12 +02:00
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
In particular, there are currently two independent modules that provide a
|
|
|
|
message passing interface to Rust code: `core::comm` and `core::pipes`.
|
|
|
|
`core::comm` is an older, less efficient system that is being phased out in
|
|
|
|
favor of `pipes`. At some point, we will remove the existing `core::comm` API
|
|
|
|
and move the user-facing portions of `core::pipes` to `core::comm`. In this
|
|
|
|
tutorial, we discuss `pipes` and ignore the `comm` API.
|
2012-09-30 04:21:12 +02:00
|
|
|
|
|
|
|
For your reference, these are the standard modules involved in Rust
|
2012-10-10 01:14:55 +02:00
|
|
|
concurrency at this writing.
|
2012-09-30 04:21:12 +02:00
|
|
|
|
|
|
|
* [`core::task`] - All code relating to tasks and task scheduling
|
|
|
|
* [`core::comm`] - The deprecated message passing API
|
|
|
|
* [`core::pipes`] - The new message passing infrastructure and API
|
|
|
|
* [`std::comm`] - Higher level messaging types based on `core::pipes`
|
2012-10-04 22:28:45 +02:00
|
|
|
* [`std::sync`] - More exotic synchronization tools, including locks
|
2012-10-10 01:14:55 +02:00
|
|
|
* [`std::arc`] - The ARC (atomic reference counted) type, for safely sharing
|
|
|
|
immutable data
|
2012-09-30 04:29:28 +02:00
|
|
|
* [`std::par`] - Some basic tools for implementing parallel algorithms
|
2012-09-30 04:21:12 +02:00
|
|
|
|
|
|
|
[`core::task`]: core/task.html
|
|
|
|
[`core::comm`]: core/comm.html
|
|
|
|
[`core::pipes`]: core/pipes.html
|
|
|
|
[`std::comm`]: std/comm.html
|
|
|
|
[`std::sync`]: std/sync.html
|
|
|
|
[`std::arc`]: std/arc.html
|
2012-09-30 04:29:28 +02:00
|
|
|
[`std::par`]: std/par.html
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
# Basics
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
The programming interface for creating and managing tasks lives
|
|
|
|
in the `task` module of the `core` library, and is thus available to all
|
|
|
|
Rust code by default. At its simplest, creating a task is a matter of
|
|
|
|
calling the `spawn` function with a closure argument. `spawn` executes the
|
|
|
|
closure in the new task.
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::io::println;
|
|
|
|
use core::task::spawn;
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
// Print something profound in a different task using a named function
|
|
|
|
fn print_message() { println("I am running in a different task!"); }
|
|
|
|
spawn(print_message);
|
|
|
|
|
|
|
|
// Print something more profound in a different task using a lambda expression
|
|
|
|
spawn( || println("I am also running in a different task!") );
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
// The canonical way to spawn is using `do` notation
|
2012-09-23 00:33:50 +02:00
|
|
|
do spawn {
|
2012-10-02 00:41:21 +02:00
|
|
|
println("I too am running in a different task!");
|
2012-09-23 00:33:50 +02:00
|
|
|
}
|
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
In Rust, there is nothing special about creating tasks: a task is not a
|
|
|
|
concept that appears in the language semantics. Instead, Rust's type system
|
|
|
|
provides all the tools necessary to implement safe concurrency: particularly,
|
|
|
|
_owned types_. The language leaves the implementation details to the core
|
|
|
|
library.
|
2012-10-02 00:41:21 +02:00
|
|
|
|
|
|
|
The `spawn` function has a very simple type signature: `fn spawn(f:
|
|
|
|
~fn())`. Because it accepts only owned closures, and owned closures
|
2012-10-10 01:14:55 +02:00
|
|
|
contain only owned data, `spawn` can safely move the entire closure
|
2012-10-02 00:41:21 +02:00
|
|
|
and all its associated state into an entirely different task for
|
2012-10-10 01:14:55 +02:00
|
|
|
execution. Like any closure, the function passed to `spawn` may capture
|
2012-10-02 00:41:21 +02:00
|
|
|
an environment that it carries across tasks.
|
|
|
|
|
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::io::println;
|
|
|
|
# use core::task::spawn;
|
2012-10-02 00:41:21 +02:00
|
|
|
# fn generate_task_number() -> int { 0 }
|
|
|
|
// Generate some state locally
|
|
|
|
let child_task_number = generate_task_number();
|
|
|
|
|
|
|
|
do spawn {
|
|
|
|
// Capture it in the remote task
|
|
|
|
println(fmt!("I am child number %d", child_task_number));
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
By default, the scheduler multiplexes tasks across the available cores, running
|
|
|
|
in parallel. Thus, on a multicore machine, running the following code
|
2012-10-02 00:41:21 +02:00
|
|
|
should interleave the output in vaguely random order.
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::io::print;
|
|
|
|
# use core::task::spawn;
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
for int::range(0, 20) |child_task_number| {
|
|
|
|
do spawn {
|
|
|
|
print(fmt!("I am child number %d\n", child_task_number));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
## Communication
|
|
|
|
|
|
|
|
Now that we have spawned a new task, it would be nice if we could
|
|
|
|
communicate with it. Recall that Rust does not have shared mutable
|
|
|
|
state, so one task may not manipulate variables owned by another task.
|
|
|
|
Instead we use *pipes*.
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
A pipe is simply a pair of endpoints: one for sending messages and another for
|
|
|
|
receiving messages. Pipes are low-level communication building-blocks and so
|
|
|
|
come in a variety of forms, each one appropriate for a different use case. In
|
|
|
|
what follows, we cover the most commonly used varieties.
|
2012-10-02 00:41:21 +02:00
|
|
|
|
|
|
|
The simplest way to create a pipe is to use the `pipes::stream`
|
2012-12-12 22:38:19 +01:00
|
|
|
function to create a `(Port, Chan)` pair. In Rust parlance, a *channel*
|
2012-10-10 01:14:55 +02:00
|
|
|
is a sending endpoint of a pipe, and a *port* is the receiving
|
|
|
|
endpoint. Consider the following example of calculating two results
|
|
|
|
concurrently:
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
use core::task::spawn;
|
|
|
|
use core::comm::{stream, Port, Chan};
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-12-12 22:38:19 +01:00
|
|
|
let (port, chan): (Port<int>, Chan<int>) = stream();
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn || {
|
2012-09-23 00:33:50 +02:00
|
|
|
let result = some_expensive_computation();
|
|
|
|
chan.send(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
some_other_expensive_computation();
|
|
|
|
let result = port.recv();
|
|
|
|
# fn some_expensive_computation() -> int { 42 }
|
|
|
|
# fn some_other_expensive_computation() {}
|
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
Let's examine this example in detail. First, the `let` statement creates a
|
|
|
|
stream for sending and receiving integers (the left-hand side of the `let`,
|
|
|
|
`(chan, port)`, is an example of a *destructuring let*: the pattern separates
|
|
|
|
a tuple into its component parts).
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::comm::{stream, Chan, Port};
|
2012-12-12 22:38:19 +01:00
|
|
|
let (port, chan): (Port<int>, Chan<int>) = stream();
|
2012-09-23 00:33:50 +02:00
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
The child task will use the channel to send data to the parent task,
|
2012-10-04 22:28:45 +02:00
|
|
|
which will wait to receive the data on the port. The next statement
|
2012-10-02 00:41:21 +02:00
|
|
|
spawns the child task.
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::spawn;
|
|
|
|
# use core::comm::{stream, Port, Chan};
|
2012-09-23 00:33:50 +02:00
|
|
|
# fn some_expensive_computation() -> int { 42 }
|
2012-12-12 22:38:19 +01:00
|
|
|
# let (port, chan) = stream();
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn || {
|
2012-09-23 00:33:50 +02:00
|
|
|
let result = some_expensive_computation();
|
|
|
|
chan.send(result);
|
|
|
|
}
|
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
Notice that the creation of the task closure transfers `chan` to the child
|
|
|
|
task implicitly: the closure captures `chan` in its environment. Both `Chan`
|
|
|
|
and `Port` are sendable types and may be captured into tasks or otherwise
|
|
|
|
transferred between them. In the example, the child task runs an expensive
|
|
|
|
computation, then sends the result over the captured channel.
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
Finally, the parent continues with some other expensive
|
|
|
|
computation, then waits for the child's result to arrive on the
|
2012-10-02 00:41:21 +02:00
|
|
|
port:
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::comm::{stream, Port, Chan};
|
2012-09-23 00:33:50 +02:00
|
|
|
# fn some_other_expensive_computation() {}
|
2012-12-12 22:38:19 +01:00
|
|
|
# let (port, chan) = stream::<int>();
|
2012-09-23 00:33:50 +02:00
|
|
|
# chan.send(0);
|
|
|
|
some_other_expensive_computation();
|
|
|
|
let result = port.recv();
|
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
The `Port` and `Chan` pair created by `stream` enables efficient communication
|
|
|
|
between a single sender and a single receiver, but multiple senders cannot use
|
|
|
|
a single `Chan`, and multiple receivers cannot use a single `Port`. What if our
|
2013-01-21 07:48:47 +01:00
|
|
|
example needed to compute multiple results across a number of tasks? The
|
2012-10-10 01:14:55 +02:00
|
|
|
following program is ill-typed:
|
2012-10-02 03:03:09 +02:00
|
|
|
|
|
|
|
~~~ {.xfail-test}
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::{spawn};
|
|
|
|
# use core::comm::{stream, Port, Chan};
|
2012-10-02 03:03:09 +02:00
|
|
|
# fn some_expensive_computation() -> int { 42 }
|
2012-12-12 22:38:19 +01:00
|
|
|
let (port, chan) = stream();
|
2012-10-02 03:03:09 +02:00
|
|
|
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn {
|
2012-10-02 03:03:09 +02:00
|
|
|
chan.send(some_expensive_computation());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ERROR! The previous spawn statement already owns the channel,
|
|
|
|
// so the compiler will not allow it to be captured again
|
|
|
|
do spawn {
|
|
|
|
chan.send(some_expensive_computation());
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Instead we can use a `SharedChan`, a type that allows a single
|
|
|
|
`Chan` to be shared by multiple senders.
|
2012-10-02 00:41:21 +02:00
|
|
|
|
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::spawn;
|
|
|
|
use core::comm::{stream, SharedChan};
|
2012-10-02 00:41:21 +02:00
|
|
|
|
2012-12-12 22:38:19 +01:00
|
|
|
let (port, chan) = stream();
|
2013-02-15 11:44:18 +01:00
|
|
|
let chan = SharedChan(chan);
|
2012-10-02 00:41:21 +02:00
|
|
|
|
|
|
|
for uint::range(0, 3) |init_val| {
|
|
|
|
// Create a new channel handle to distribute to the child task
|
|
|
|
let child_chan = chan.clone();
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn {
|
2012-10-02 00:41:21 +02:00
|
|
|
child_chan.send(some_expensive_computation(init_val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = port.recv() + port.recv() + port.recv();
|
|
|
|
# fn some_expensive_computation(_i: uint) -> int { 42 }
|
|
|
|
~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
Here we transfer ownership of the channel into a new `SharedChan` value. Like
|
|
|
|
`Chan`, `SharedChan` is a non-copyable, owned type (sometimes also referred to
|
|
|
|
as an *affine* or *linear* type). Unlike with `Chan`, though, the programmer
|
|
|
|
may duplicate a `SharedChan`, with the `clone()` method. A cloned
|
|
|
|
`SharedChan` produces a new handle to the same channel, allowing multiple
|
|
|
|
tasks to send data to a single port. Between `spawn`, `stream` and
|
|
|
|
`SharedChan`, we have enough tools to implement many useful concurrency
|
|
|
|
patterns.
|
2012-10-02 00:41:21 +02:00
|
|
|
|
|
|
|
Note that the above `SharedChan` example is somewhat contrived since
|
|
|
|
you could also simply use three `stream` pairs, but it serves to
|
2012-10-10 01:14:55 +02:00
|
|
|
illustrate the point. For reference, written with multiple streams, it
|
2012-10-02 00:41:21 +02:00
|
|
|
might look like the example below.
|
|
|
|
|
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::spawn;
|
|
|
|
# use core::comm::{stream, Port, Chan};
|
2012-10-02 00:41:21 +02:00
|
|
|
|
2012-10-02 03:03:09 +02:00
|
|
|
// Create a vector of ports, one for each child task
|
2012-10-02 00:41:21 +02:00
|
|
|
let ports = do vec::from_fn(3) |init_val| {
|
2012-12-12 22:38:19 +01:00
|
|
|
let (port, chan) = stream();
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn {
|
2012-10-02 00:41:21 +02:00
|
|
|
chan.send(some_expensive_computation(init_val));
|
|
|
|
}
|
2013-02-15 11:44:18 +01:00
|
|
|
port
|
2012-10-02 00:41:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Wait on each port, accumulating the results
|
|
|
|
let result = ports.foldl(0, |accum, port| *accum + port.recv() );
|
|
|
|
# fn some_expensive_computation(_i: uint) -> int { 42 }
|
|
|
|
~~~
|
|
|
|
|
2012-10-02 03:42:11 +02:00
|
|
|
# Handling task failure
|
|
|
|
|
2013-02-01 03:24:09 +01:00
|
|
|
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro
|
|
|
|
(which can also be written with an error string as an argument: `fail!(
|
|
|
|
~reason)`) and the `assert` construct (which effectively calls `fail!()` if a
|
2012-10-10 01:14:55 +02:00
|
|
|
boolean expression is false) are both ways to raise exceptions. When a task
|
|
|
|
raises an exception the task unwinds its stack---running destructors and
|
|
|
|
freeing memory along the way---and then exits. Unlike exceptions in C++,
|
|
|
|
exceptions in Rust are unrecoverable within a single task: once a task fails,
|
|
|
|
there is no way to "catch" the exception.
|
2012-10-02 03:42:11 +02:00
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
All tasks are, by default, _linked_ to each other. That means that the fates
|
|
|
|
of all tasks are intertwined: if one fails, so do all the others.
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::spawn;
|
2012-10-02 03:42:11 +02:00
|
|
|
# fn do_some_work() { loop { task::yield() } }
|
|
|
|
# do task::try {
|
|
|
|
// Create a child task that fails
|
2013-02-15 02:33:16 +01:00
|
|
|
do spawn { fail!() }
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
// This will also fail because the task we spawned failed
|
|
|
|
do_some_work();
|
|
|
|
# };
|
|
|
|
~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
While it isn't possible for a task to recover from failure, tasks may notify
|
|
|
|
each other of failure. The simplest way of handling task failure is with the
|
|
|
|
`try` function, which is similar to `spawn`, but immediately blocks waiting
|
|
|
|
for the child task to finish. `try` returns a value of type `Result<int,
|
|
|
|
()>`. `Result` is an `enum` type with two variants: `Ok` and `Err`. In this
|
|
|
|
case, because the type arguments to `Result` are `int` and `()`, callers can
|
|
|
|
pattern-match on a result to check whether it's an `Ok` result with an `int`
|
|
|
|
field (representing a successful result) or an `Err` result (representing
|
|
|
|
termination with an error).
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
~~~
|
|
|
|
# fn some_condition() -> bool { false }
|
|
|
|
# fn calculate_result() -> int { 0 }
|
|
|
|
let result: Result<int, ()> = do task::try {
|
|
|
|
if some_condition() {
|
|
|
|
calculate_result()
|
|
|
|
} else {
|
2013-02-15 02:33:16 +01:00
|
|
|
fail!(~"oops!");
|
2012-10-02 03:42:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
assert result.is_err();
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Unlike `spawn`, the function spawned using `try` may return a value,
|
|
|
|
which `try` will dutifully propagate back to the caller in a [`Result`]
|
|
|
|
enum. If the child task terminates successfully, `try` will
|
|
|
|
return an `Ok` result; if the child task fails, `try` will return
|
|
|
|
an `Error` result.
|
|
|
|
|
|
|
|
[`Result`]: core/result.html
|
|
|
|
|
|
|
|
> ***Note:*** A failed task does not currently produce a useful error
|
2012-10-10 01:14:55 +02:00
|
|
|
> value (`try` always returns `Err(())`). In the
|
|
|
|
> future, it may be possible for tasks to intercept the value passed to
|
2013-02-01 03:24:09 +01:00
|
|
|
> `fail!()`.
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
TODO: Need discussion of `future_result` in order to make failure
|
|
|
|
modes useful.
|
|
|
|
|
|
|
|
But not all failure is created equal. In some cases you might need to
|
|
|
|
abort the entire program (perhaps you're writing an assert which, if
|
|
|
|
it trips, indicates an unrecoverable logic error); in other cases you
|
|
|
|
might want to contain the failure at a certain boundary (perhaps a
|
|
|
|
small piece of input from the outside world, which you happen to be
|
|
|
|
processing in parallel, is malformed and its processing task can't
|
2012-10-10 01:14:55 +02:00
|
|
|
proceed). Hence, you will need different _linked failure modes_.
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
## Failure modes
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
By default, task failure is _bidirectionally linked_, which means that if
|
2013-02-15 02:33:16 +01:00
|
|
|
either task fails, it kills the other one.
|
2012-10-02 03:42:11 +02:00
|
|
|
|
|
|
|
~~~
|
|
|
|
# fn sleep_forever() { loop { task::yield() } }
|
|
|
|
# do task::try {
|
|
|
|
do task::spawn {
|
|
|
|
do task::spawn {
|
2013-02-15 02:33:16 +01:00
|
|
|
fail!(); // All three tasks will fail.
|
2012-10-02 03:42:11 +02:00
|
|
|
}
|
|
|
|
sleep_forever(); // Will get woken up by force, then fail
|
|
|
|
}
|
|
|
|
sleep_forever(); // Will get woken up by force, then fail
|
|
|
|
# };
|
|
|
|
~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
If you want parent tasks to be able to kill their children, but do not want a
|
2013-02-15 02:33:16 +01:00
|
|
|
parent to fail automatically if one of its child task fails, you can call
|
2012-10-02 03:42:11 +02:00
|
|
|
`task::spawn_supervised` for _unidirectionally linked_ failure. The
|
|
|
|
function `task::try`, which we saw previously, uses `spawn_supervised`
|
|
|
|
internally, with additional logic to wait for the child task to finish
|
|
|
|
before returning. Hence:
|
|
|
|
|
|
|
|
~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::comm::{stream, Chan, Port};
|
|
|
|
# use core::task::{spawn, try};
|
2012-10-02 03:42:11 +02:00
|
|
|
# fn sleep_forever() { loop { task::yield() } }
|
|
|
|
# do task::try {
|
2012-12-12 22:38:19 +01:00
|
|
|
let (receiver, sender): (Port<int>, Chan<int>) = stream();
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn { // Bidirectionally linked
|
2012-10-02 03:42:11 +02:00
|
|
|
// Wait for the supervised child task to exist.
|
|
|
|
let message = receiver.recv();
|
|
|
|
// Kill both it and the parent task.
|
|
|
|
assert message != 42;
|
|
|
|
}
|
2013-02-15 11:44:18 +01:00
|
|
|
do try { // Unidirectionally linked
|
2012-10-02 03:42:11 +02:00
|
|
|
sender.send(42);
|
|
|
|
sleep_forever(); // Will get woken up by force
|
|
|
|
}
|
|
|
|
// Flow never reaches here -- parent task was killed too.
|
|
|
|
# };
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Supervised failure is useful in any situation where one task manages
|
|
|
|
multiple fallible child tasks, and the parent task can recover
|
2012-10-10 01:14:55 +02:00
|
|
|
if any child fails. On the other hand, if the _parent_ (supervisor) fails,
|
2012-10-02 03:42:11 +02:00
|
|
|
then there is nothing the children can do to recover, so they should
|
|
|
|
also fail.
|
|
|
|
|
|
|
|
Supervised task failure propagates across multiple generations even if
|
|
|
|
an intermediate generation has already exited:
|
|
|
|
|
|
|
|
~~~
|
|
|
|
# fn sleep_forever() { loop { task::yield() } }
|
|
|
|
# fn wait_for_a_while() { for 1000.times { task::yield() } }
|
|
|
|
# do task::try::<int> {
|
|
|
|
do task::spawn_supervised {
|
|
|
|
do task::spawn_supervised {
|
|
|
|
sleep_forever(); // Will get woken up by force, then fail
|
|
|
|
}
|
|
|
|
// Intermediate task immediately exits
|
|
|
|
}
|
|
|
|
wait_for_a_while();
|
2013-02-15 02:33:16 +01:00
|
|
|
fail!(); // Will kill grandchild even if child has already exited
|
2012-10-02 03:42:11 +02:00
|
|
|
# };
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Finally, tasks can be configured to not propagate failure to each
|
|
|
|
other at all, using `task::spawn_unlinked` for _isolated failure_.
|
|
|
|
|
|
|
|
~~~
|
2013-01-10 19:59:58 +01:00
|
|
|
# fn random() -> uint { 100 }
|
|
|
|
# fn sleep_for(i: uint) { for i.times { task::yield() } }
|
2012-10-02 03:42:11 +02:00
|
|
|
# do task::try::<()> {
|
|
|
|
let (time1, time2) = (random(), random());
|
|
|
|
do task::spawn_unlinked {
|
|
|
|
sleep_for(time2); // Won't get forced awake
|
2013-02-15 02:33:16 +01:00
|
|
|
fail!();
|
2012-10-02 03:42:11 +02:00
|
|
|
}
|
|
|
|
sleep_for(time1); // Won't get forced awake
|
2013-02-15 02:33:16 +01:00
|
|
|
fail!();
|
2012-10-02 03:42:11 +02:00
|
|
|
// It will take MAX(time1,time2) for the program to finish.
|
|
|
|
# };
|
|
|
|
~~~
|
|
|
|
|
2012-10-02 00:41:21 +02:00
|
|
|
## Creating a task with a bi-directional communication path
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
A very common thing to do is to spawn a child task where the parent
|
|
|
|
and child both need to exchange messages with each other. The
|
|
|
|
function `std::comm::DuplexStream()` supports this pattern. We'll
|
2012-10-10 01:14:55 +02:00
|
|
|
look briefly at how to use it.
|
2012-09-23 00:33:50 +02:00
|
|
|
|
2012-12-13 04:17:31 +01:00
|
|
|
To see how `DuplexStream()` works, we will create a child task
|
2012-10-10 01:14:55 +02:00
|
|
|
that repeatedly receives a `uint` message, converts it to a string, and sends
|
|
|
|
the string in response. The child terminates when it receives `0`.
|
2012-09-23 00:33:50 +02:00
|
|
|
Here is the function that implements the child task:
|
|
|
|
|
|
|
|
~~~~
|
|
|
|
# use std::comm::DuplexStream;
|
|
|
|
fn stringifier(channel: &DuplexStream<~str, uint>) {
|
|
|
|
let mut value: uint;
|
|
|
|
loop {
|
|
|
|
value = channel.recv();
|
2013-02-02 16:40:42 +01:00
|
|
|
channel.send(uint::to_str(value));
|
2012-10-10 01:14:55 +02:00
|
|
|
if value == 0 { break; }
|
2012-09-23 00:33:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
The implementation of `DuplexStream` supports both sending and
|
|
|
|
receiving. The `stringifier` function takes a `DuplexStream` that can
|
|
|
|
send strings (the first type parameter) and receive `uint` messages
|
|
|
|
(the second type parameter). The body itself simply loops, reading
|
|
|
|
from the channel and then sending its response back. The actual
|
2012-10-10 01:14:55 +02:00
|
|
|
response itself is simply the stringified version of the received value,
|
2012-09-23 00:33:50 +02:00
|
|
|
`uint::to_str(value)`.
|
|
|
|
|
|
|
|
Here is the code for the parent task:
|
|
|
|
|
|
|
|
~~~~
|
2013-03-01 19:44:43 +01:00
|
|
|
# use core::task::spawn;
|
2012-09-23 00:33:50 +02:00
|
|
|
# use std::comm::DuplexStream;
|
|
|
|
# fn stringifier(channel: &DuplexStream<~str, uint>) {
|
|
|
|
# let mut value: uint;
|
|
|
|
# loop {
|
|
|
|
# value = channel.recv();
|
2013-02-02 16:40:42 +01:00
|
|
|
# channel.send(uint::to_str(value));
|
2012-09-23 00:33:50 +02:00
|
|
|
# if value == 0u { break; }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# fn main() {
|
|
|
|
|
|
|
|
let (from_child, to_child) = DuplexStream();
|
|
|
|
|
2013-02-15 11:44:18 +01:00
|
|
|
do spawn {
|
2012-09-23 00:33:50 +02:00
|
|
|
stringifier(&to_child);
|
|
|
|
};
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
from_child.send(22);
|
2012-09-23 00:33:50 +02:00
|
|
|
assert from_child.recv() == ~"22";
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
from_child.send(23);
|
|
|
|
from_child.send(0);
|
2012-09-23 00:33:50 +02:00
|
|
|
|
|
|
|
assert from_child.recv() == ~"23";
|
|
|
|
assert from_child.recv() == ~"0";
|
|
|
|
|
|
|
|
# }
|
|
|
|
~~~~
|
|
|
|
|
2012-10-10 01:14:55 +02:00
|
|
|
The parent task first calls `DuplexStream` to create a pair of bidirectional
|
|
|
|
endpoints. It then uses `task::spawn` to create the child task, which captures
|
|
|
|
one end of the communication channel. As a result, both parent and child can
|
|
|
|
send and receive data to and from the other.
|
2012-10-02 00:41:21 +02:00
|
|
|
|