6 An executing Rust program consists of a tree of tasks, each with their own
7 stack, and sole ownership of their allocated heap data. Tasks communicate
8 with each other using ports and channels.
10 When a task fails, that failure will propagate to its parent (the task
11 that spawned it) and the parent will fail as well. The reverse is not
12 true: when a parent task fails its children will continue executing. When
13 the root (main) task fails, all tasks fail, and then so does the entire
16 A task may remove itself from this failure propagation mechanism by
17 calling the <unsupervise> function, after which failure will only
18 result in the termination of that task.
20 Tasks may execute in parallel and are scheduled automatically by the runtime.
25 > log(debug, "Hello, World!");
29 import cast = unsafe::reinterpret_cast;
38 export task_notification;
48 export spawn_joinable;
49 export spawn_connected;
51 export connected_task;
53 #[abi = "rust-intrinsic"]
55 // these must run on the Rust stack so that they can swap stacks etc:
56 fn task_sleep(task: *rust_task, time_in_us: uint, &killed: bool);
60 fnptr: c::intptr_t, envptr: c::intptr_t
63 #[link_name = "rustrt"]
66 // these can run on the C stack:
69 fn get_task_id() -> task_id;
70 fn rust_get_task() -> *rust_task;
72 fn new_task() -> task_id;
73 fn drop_task(task_id: *rust_task);
74 fn get_task_pointer(id: task_id) -> *rust_task;
76 fn migrate_alloc(alloc: *u8, target: task_id);
78 fn start_task(id: task, closure: *rust_closure);
85 mutable notify_enabled: int,
86 mutable notify_chan: comm::chan<task_notification>,
87 mutable stack_ptr: *u8};
89 resource rust_task_ptr(task: *rust_task) { rustrt::drop_task(task); }
103 Creates and executes a new child task
105 Sets up a new task with its own call stack and schedules it to be
106 executed. Upon execution, the closure `f()` will be invoked.
110 f - A function to execute in the new task
114 A handle to the new task
116 fn spawn(+f: sendfn()) -> task {
120 fn spawn_inner(-f: sendfn(),
121 notify: option<comm::chan<task_notification>>) -> task unsafe {
122 let closure: *rust_closure = unsafe::reinterpret_cast(ptr::addr_of(f));
123 #debug("spawn: closure={%x,%x}", (*closure).fnptr, (*closure).envptr);
124 let id = rustrt::new_task();
126 // set up notifications if they are enabled.
127 option::may(notify) {|c|
128 let task_ptr <- rust_task_ptr(rustrt::get_task_pointer(id));
129 (**task_ptr).notify_enabled = 1;
130 (**task_ptr).notify_chan = c;
133 rustrt::start_task(id, closure);
141 A task that sends notification upon termination
143 type joinable_task = (task, comm::port<task_notification>);
145 fn spawn_joinable(+f: sendfn()) -> joinable_task {
146 let notify_port = comm::port();
147 let notify_chan = comm::chan(notify_port);
148 let task = spawn_inner(f, some(notify_chan));
149 ret (task, notify_port);
151 resource notify_rsrc(data: (comm::chan<task_notification>,
153 @mutable task_result)) {
154 let (chan, task, tr) = data;
155 let msg = exit(task, *tr);
156 comm::send(chan, msg);
159 let notify_port = comm::port();
160 let notify_chan = comm::chan(notify_port);
161 let g = sendfn[copy notify_chan; move f]() {
162 let this_task = rustrt::get_task_id();
163 let result = @mutable tr_failure;
164 let _rsrc = notify_rsrc((notify_chan, this_task, result));
166 *result = tr_success; // rsrc will fire msg when fn returns
169 ret (task, notify_port);
176 Indicates the manner in which a task exited
179 /* Variant: tr_success */
181 /* Variant: tr_failure */
186 Tag: task_notification
188 Message sent upon task exit to indicate normal or abnormal termination
190 tag task_notification {
192 exit(task, task_result);
198 The prototype for a connected child task function. Such a function will be
199 supplied with a channel to send messages to the parent and a port to receive
200 messages from the parent. The type parameter `ToCh` is the type for messages
201 sent from the parent to the child and `FrCh` is the type for messages sent
202 from the child to the parent. */
203 type connected_fn<ToCh, FrCh> = sendfn(comm::port<ToCh>, comm::chan<FrCh>);
208 The result type of <spawn_connected>
210 type connected_task<ToCh, FrCh> = {
211 from_child: comm::port<FrCh>,
212 to_child: comm::chan<ToCh>,
217 Function: spawn_connected
219 Spawns a child task along with a port/channel for exchanging messages
220 with the parent task. The type `ToCh` represents messages sent to the child
221 and `FrCh` messages received from the child.
225 f - the child function to execute
229 The new child task along with the port to receive messages and the channel
232 fn spawn_connected<ToCh:send, FrCh:send>(+f: connected_fn<ToCh, FrCh>)
233 -> connected_task<ToCh,FrCh> {
234 let from_child_port = comm::port::<FrCh>();
235 let from_child_chan = comm::chan(from_child_port);
236 let get_to_child_port = comm::port::<comm::chan<ToCh>>();
237 let get_to_child_chan = comm::chan(get_to_child_port);
238 let child_task = spawn(sendfn[move f]() {
239 let to_child_port = comm::port::<ToCh>();
240 comm::send(get_to_child_chan, comm::chan(to_child_port));
241 f(to_child_port, from_child_chan);
243 let to_child_chan = comm::recv(get_to_child_port);
244 ret {from_child: from_child_port,
245 to_child: to_child_chan,
249 /* Section: Operations */
254 Retreives a handle to the currently executing task
256 fn get_task() -> task { rustrt::get_task_id() }
261 Hints the scheduler to yield this task for a specified ammount of time.
265 time_in_us - maximum number of microseconds to yield control for
267 fn sleep(time_in_us: uint) {
268 let task = rustrt::rust_get_task();
270 // FIXME: uncomment this when extfmt is moved to core
272 // #debug("yielding for %u us", time_in_us);
273 rusti::task_sleep(task, time_in_us, killed);
282 Yield control to the task scheduler
284 The scheduler may schedule another task to execute.
286 fn yield() { sleep(1u) }
291 Wait for a child task to exit
293 The child task must have been spawned with <spawn_joinable>, which
294 produces a notification port that the child uses to communicate its
299 A task_result indicating whether the task terminated normally or failed
301 fn join(task_port: joinable_task) -> task_result {
302 let (id, port) = task_port;
303 alt comm::recv::<task_notification>(port) {
308 // FIXME: uncomment this when extfmt is moved to core
310 // fail #fmt["join received id %d, expected %d", _id, id]
318 Function: unsupervise
320 Detaches this task from its parent in the task tree
322 An unsupervised task will not propagate its failure up the task tree
324 fn unsupervise() { ret sys::unsupervise(); }
329 Pins the current task and future child tasks to a single scheduler thread
331 fn pin() { rustrt::pin_task(); }
336 Unpin the current task and future child tasks
338 fn unpin() { rustrt::unpin_task(); }
343 // indent-tabs-mode: nil
345 // buffer-file-coding-system: utf-8-unix