2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* 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!");
|
|
|
|
* }
|
|
|
|
* ~~~
|
|
|
|
*/
|
2012-11-18 19:56:50 -06:00
|
|
|
|
2013-02-25 15:23:16 -06:00
|
|
|
use cell::Cell;
|
2012-11-28 18:20:41 -06:00
|
|
|
use cmp::Eq;
|
2012-12-23 16:41:37 -06:00
|
|
|
use option;
|
2012-11-28 18:20:41 -06:00
|
|
|
use result::Result;
|
2013-02-02 05:10:12 -06:00
|
|
|
use comm::{stream, Chan, GenericChan, GenericPort, Port, SharedChan};
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use result;
|
2013-01-08 21:46:12 -06:00
|
|
|
use task::rt::{task_id, sched_id, rust_task};
|
2012-12-23 16:41:37 -06:00
|
|
|
use util;
|
2012-11-28 18:20:41 -06:00
|
|
|
use util::replace;
|
2012-11-18 19:56:50 -06:00
|
|
|
|
2012-11-28 18:20:41 -06:00
|
|
|
mod local_data_priv;
|
|
|
|
pub mod local_data;
|
|
|
|
pub mod rt;
|
2012-11-18 19:56:50 -06:00
|
|
|
pub mod spawn;
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-08 21:46:12 -06:00
|
|
|
/// A handle to a scheduler
|
2013-03-20 10:35:02 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-08 21:46:12 -06:00
|
|
|
pub enum Scheduler {
|
|
|
|
SchedulerHandle(sched_id)
|
|
|
|
}
|
|
|
|
|
2012-11-28 18:20:41 -06:00
|
|
|
/// A handle to a task
|
2013-03-20 10:35:02 -05:00
|
|
|
#[deriving(Eq)]
|
2012-11-28 18:20:41 -06:00
|
|
|
pub enum Task {
|
|
|
|
TaskHandle(task_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
pub enum TaskResult {
|
|
|
|
Success,
|
|
|
|
Failure,
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Eq for TaskResult {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &TaskResult) -> bool {
|
2012-11-28 18:20:41 -06:00
|
|
|
match ((*self), (*other)) {
|
|
|
|
(Success, Success) | (Failure, Failure) => true,
|
|
|
|
(Success, _) | (Failure, _) => false
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ne(&self, other: &TaskResult) -> bool { !(*self).eq(other) }
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scheduler modes
|
2013-03-20 10:35:02 -05:00
|
|
|
#[deriving(Eq)]
|
2012-11-28 18:20:41 -06:00
|
|
|
pub enum SchedMode {
|
2013-01-08 21:46:12 -06:00
|
|
|
/// Run task on the default scheduler
|
|
|
|
DefaultScheduler,
|
|
|
|
/// Run task on the current scheduler
|
|
|
|
CurrentScheduler,
|
|
|
|
/// Run task on a specific scheduler
|
|
|
|
ExistingScheduler(Scheduler),
|
|
|
|
/**
|
|
|
|
* Tasks are scheduled on the main OS thread
|
|
|
|
*
|
|
|
|
* The main OS thread is the thread used to launch the runtime which,
|
|
|
|
* in most cases, is the process's initial thread as created by the OS.
|
|
|
|
*/
|
|
|
|
PlatformThread,
|
2012-11-28 18:20:41 -06:00
|
|
|
/// All tasks run in the same OS thread
|
|
|
|
SingleThreaded,
|
|
|
|
/// Tasks are distributed among available CPUs
|
|
|
|
ThreadPerCore,
|
|
|
|
/// Each task runs in its own OS thread
|
|
|
|
ThreadPerTask,
|
|
|
|
/// Tasks are distributed among a fixed number of OS threads
|
|
|
|
ManualThreads(uint),
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scheduler configuration options
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * sched_mode - The operating mode of the scheduler
|
|
|
|
*
|
|
|
|
* * foreign_stack_size - The size of the foreign stack, in bytes
|
|
|
|
*
|
|
|
|
* Rust code runs on Rust-specific stacks. When Rust code calls foreign
|
|
|
|
* code (via functions in foreign modules) it switches to a typical, large
|
|
|
|
* stack appropriate for running code written in languages like C. By
|
|
|
|
* default these foreign stacks have unspecified size, but with this
|
|
|
|
* option their size can be precisely specified.
|
|
|
|
*/
|
2013-01-22 10:12:52 -06:00
|
|
|
pub struct SchedOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
mode: SchedMode,
|
2013-01-22 10:12:52 -06:00
|
|
|
foreign_stack_size: Option<uint>,
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Task configuration options
|
|
|
|
*
|
|
|
|
* # Fields
|
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
* * notify_chan - Enable lifecycle notifications on the given channel
|
|
|
|
*
|
|
|
|
* * 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.
|
|
|
|
*/
|
2013-01-22 10:12:52 -06:00
|
|
|
pub struct TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: bool,
|
|
|
|
supervised: bool,
|
|
|
|
mut notify_chan: Option<Chan<TaskResult>>,
|
2013-01-25 19:51:53 -06:00
|
|
|
sched: SchedOpts
|
2013-01-22 10:12:52 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The task builder type.
|
|
|
|
*
|
|
|
|
* Provides detailed control over the properties and behavior of new tasks.
|
|
|
|
*/
|
|
|
|
// NB: Builders are designed to be single-use because they do stateful
|
|
|
|
// things that get weird when reusing - e.g. if you create a result future
|
|
|
|
// it only applies to a single task, so then you have to maintain Some
|
|
|
|
// potentially tricky state to ensure that everything behaves correctly
|
|
|
|
// when you try to reuse the builder to spawn a new task. We'll just
|
|
|
|
// sidestep that whole issue by making builders uncopyable and making
|
|
|
|
// the run function move them in.
|
|
|
|
|
|
|
|
// FIXME (#3724): Replace the 'consumed' bit with move mode on self
|
2012-12-05 17:06:54 -06:00
|
|
|
pub struct TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
opts: TaskOpts,
|
2013-03-01 16:32:37 -06:00
|
|
|
gen_body: @fn(v: ~fn()) -> ~fn(),
|
2012-11-28 18:20:41 -06:00
|
|
|
can_not_copy: Option<util::NonCopyable>,
|
|
|
|
mut consumed: bool,
|
2012-12-05 17:06:54 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
pub fn task() -> TaskBuilder {
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
opts: default_task_opts(),
|
2013-02-15 02:51:28 -06:00
|
|
|
gen_body: |body| body, // Identity function
|
2012-11-28 18:20:41 -06:00
|
|
|
can_not_copy: None,
|
|
|
|
mut consumed: false,
|
2012-12-05 17:06:54 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)] // FIXME #3538
|
|
|
|
priv impl TaskBuilder {
|
2013-03-04 21:36:15 -06:00
|
|
|
fn consume(&self) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
if self.consumed {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"Cannot copy a task_builder"); // Fake move mode on self
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
self.consumed = true;
|
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: self.opts.linked,
|
|
|
|
supervised: self.opts.supervised,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
|
|
|
gen_body: self.gen_body,
|
|
|
|
can_not_copy: None,
|
2013-01-22 10:12:52 -06:00
|
|
|
consumed: false
|
2012-12-05 17:06:54 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
/**
|
|
|
|
* Decouple the child task's failure from the parent's. If either fails,
|
|
|
|
* the other will not be killed.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn unlinked(&self) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: false,
|
|
|
|
supervised: self.opts.supervised,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn supervised(&self) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: false,
|
|
|
|
supervised: true,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Link the child task's and parent task's failures. If either fails, the
|
|
|
|
* other will be killed.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn linked(&self) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: true,
|
|
|
|
supervised: false,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-07 16:38:38 -06:00
|
|
|
fn future_result(&self, blk: &fn(v: Port<TaskResult>)) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
// FIXME (#3725): Once linked failure and notification are
|
|
|
|
// handled in the library, I can imagine implementing this by just
|
|
|
|
// registering an arbitrary number of task::on_exit handlers and
|
|
|
|
// sending out messages.
|
|
|
|
|
|
|
|
if self.opts.notify_chan.is_some() {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"Can't set multiple future_results for one task!");
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the future and give it to the caller.
|
2012-12-11 14:26:41 -06:00
|
|
|
let (notify_pipe_po, notify_pipe_ch) = stream::<TaskResult>();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
blk(notify_pipe_po);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
// Reconfigure self to use a notify channel.
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: self.opts.linked,
|
|
|
|
supervised: self.opts.supervised,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: Some(notify_pipe_ch),
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
/// Configure a custom scheduler mode for the task.
|
2013-03-04 21:36:15 -06:00
|
|
|
fn sched_mode(&self, mode: SchedMode) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: self.opts.linked,
|
|
|
|
supervised: self.opts.supervised,
|
2013-02-15 02:51:28 -06:00
|
|
|
notify_chan: notify_chan,
|
2013-01-25 19:51:53 -06:00
|
|
|
sched: SchedOpts { mode: mode, foreign_stack_size: None}
|
2012-11-28 18:20:41 -06:00
|
|
|
},
|
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn add_wrapper(&self, wrapper: @fn(v: ~fn()) -> ~fn()) -> TaskBuilder {
|
2012-11-28 18:20:41 -06:00
|
|
|
let prev_gen_body = self.gen_body;
|
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
2012-12-05 17:06:54 -06:00
|
|
|
TaskBuilder {
|
2013-01-22 10:12:52 -06:00
|
|
|
opts: TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: self.opts.linked,
|
|
|
|
supervised: self.opts.supervised,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: self.opts.sched
|
|
|
|
},
|
2013-02-15 02:51:28 -06:00
|
|
|
gen_body: |body| { wrapper(prev_gen_body(body)) },
|
2012-11-28 18:20:41 -06:00
|
|
|
can_not_copy: None,
|
2012-12-05 17:06:54 -06:00
|
|
|
.. self.consume()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn spawn(&self, f: ~fn()) {
|
2012-11-28 18:20:41 -06:00
|
|
|
let notify_chan = replace(&mut self.opts.notify_chan, None);
|
|
|
|
let x = self.consume();
|
2013-01-22 10:12:52 -06:00
|
|
|
let opts = TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: x.opts.linked,
|
|
|
|
supervised: x.opts.supervised,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: notify_chan,
|
2012-11-28 18:20:41 -06:00
|
|
|
sched: x.opts.sched
|
|
|
|
};
|
2013-02-15 02:51:28 -06:00
|
|
|
spawn::spawn_raw(opts, (x.gen_body)(f));
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
/// Runs a task, while transfering ownership of one argument to the child.
|
2013-03-04 21:36:15 -06:00
|
|
|
fn spawn_with<A:Owned>(&self, arg: A, f: ~fn(v: A)) {
|
2013-02-25 15:23:16 -06:00
|
|
|
let arg = Cell(arg);
|
|
|
|
do self.spawn {
|
|
|
|
f(arg.take());
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-04 21:36:15 -06:00
|
|
|
fn try<T:Owned>(&self, f: ~fn() -> T) -> Result<T,()> {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream::<T>();
|
2012-11-28 18:20:41 -06:00
|
|
|
let mut result = None;
|
|
|
|
|
|
|
|
let fr_task_builder = self.future_result(|+r| {
|
2013-02-15 02:51:28 -06:00
|
|
|
result = Some(r);
|
2012-11-28 18:20:41 -06:00
|
|
|
});
|
2013-02-15 02:51:28 -06:00
|
|
|
do fr_task_builder.spawn || {
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(f());
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
match option::unwrap(result).recv() {
|
2013-01-22 14:38:08 -06:00
|
|
|
Success => result::Ok(po.recv()),
|
2012-11-28 18:20:41 -06:00
|
|
|
Failure => result::Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Task construction */
|
|
|
|
|
|
|
|
pub 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-22 10:12:52 -06:00
|
|
|
TaskOpts {
|
2012-11-28 18:20:41 -06:00
|
|
|
linked: true,
|
|
|
|
supervised: false,
|
2013-01-22 10:12:52 -06:00
|
|
|
notify_chan: None,
|
2013-01-25 19:51:53 -06:00
|
|
|
sched: SchedOpts {
|
2013-01-08 21:46:12 -06:00
|
|
|
mode: DefaultScheduler,
|
|
|
|
foreign_stack_size: None
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Spawn convenience functions */
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn spawn(f: ~fn()) {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* 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)`.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().spawn(f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn spawn_unlinked(f: ~fn()) {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* Creates a child task unlinked from the current one. If either this
|
|
|
|
* task or the child task fails, the other will not be killed.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().unlinked().spawn(f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn spawn_supervised(f: ~fn()) {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* Creates a child task unlinked from the current one. If either this
|
|
|
|
* task or the child task fails, the other will not be killed.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().supervised().spawn(f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn spawn_with<A:Owned>(arg: A, f: ~fn(v: A)) {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* 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)`.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().spawn_with(arg, f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn spawn_sched(mode: SchedMode, f: ~fn()) {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
2013-01-08 21:46:12 -06:00
|
|
|
* Creates a new task on a new or existing scheduler
|
|
|
|
|
|
|
|
* When there are no more tasks to execute the
|
2012-11-28 18:20:41 -06:00
|
|
|
* scheduler terminates.
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* In manual threads mode the number of threads requested must be
|
|
|
|
* greater than zero.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().sched_mode(mode).spawn(f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-03-01 16:32:37 -06:00
|
|
|
pub fn try<T:Owned>(f: ~fn() -> T) -> Result<T,()> {
|
2012-11-28 18:20:41 -06:00
|
|
|
/*!
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
task().supervised().try(f)
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Lifecycle functions */
|
|
|
|
|
|
|
|
pub fn yield() {
|
|
|
|
//! Yield control to the task scheduler
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let task_ = rt::rust_get_task();
|
|
|
|
let killed = rt::rust_task_yield(task_);
|
|
|
|
if killed && !failing() {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"killed");
|
2013-01-23 18:29:31 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn failing() -> bool {
|
|
|
|
//! True if the running task has failed
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
rt::rust_task_is_unwinding(rt::rust_get_task())
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_task() -> Task {
|
|
|
|
//! Get a handle to the running task
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
TaskHandle(rt::get_task_id())
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-01-08 21:46:12 -06:00
|
|
|
pub fn get_scheduler() -> Scheduler {
|
2013-01-25 19:51:53 -06:00
|
|
|
SchedulerHandle(unsafe { rt::rust_get_sched_id() })
|
2013-01-08 21:46:12 -06:00
|
|
|
}
|
|
|
|
|
2012-11-28 18:20:41 -06:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
* }
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-07 16:38:38 -06:00
|
|
|
pub unsafe fn unkillable<U>(f: &fn() -> U) -> U {
|
2012-11-28 18:20:41 -06:00
|
|
|
struct AllowFailure {
|
|
|
|
t: *rust_task,
|
2013-01-23 18:29:31 -06:00
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
rt::rust_task_allow_kill(self.t);
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn AllowFailure(t: *rust_task) -> AllowFailure{
|
|
|
|
AllowFailure {
|
|
|
|
t: t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let t = rt::rust_get_task();
|
|
|
|
let _allow_failure = AllowFailure(t);
|
|
|
|
rt::rust_task_inhibit_kill(t);
|
|
|
|
f()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The inverse of unkillable. Only ever to be used nested in unkillable().
|
2013-03-07 16:38:38 -06:00
|
|
|
pub unsafe fn rekillable<U>(f: &fn() -> U) -> U {
|
2012-11-28 18:20:41 -06:00
|
|
|
struct DisallowFailure {
|
|
|
|
t: *rust_task,
|
2013-01-23 18:29:31 -06:00
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
rt::rust_task_inhibit_kill(self.t);
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn DisallowFailure(t: *rust_task) -> DisallowFailure {
|
|
|
|
DisallowFailure {
|
|
|
|
t: t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let t = rt::rust_get_task();
|
|
|
|
let _allow_failure = DisallowFailure(t);
|
|
|
|
rt::rust_task_allow_kill(t);
|
|
|
|
f()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A stronger version of unkillable that also inhibits scheduling operations.
|
|
|
|
* For use with exclusive ARCs, which use pthread mutexes directly.
|
|
|
|
*/
|
2013-03-07 16:38:38 -06:00
|
|
|
pub unsafe fn atomically<U>(f: &fn() -> U) -> U {
|
2012-11-28 18:20:41 -06:00
|
|
|
struct DeferInterrupts {
|
|
|
|
t: *rust_task,
|
|
|
|
drop {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
rt::rust_task_allow_yield(self.t);
|
|
|
|
rt::rust_task_allow_kill(self.t);
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn DeferInterrupts(t: *rust_task) -> DeferInterrupts {
|
|
|
|
DeferInterrupts {
|
|
|
|
t: t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let t = rt::rust_get_task();
|
|
|
|
let _interrupts = DeferInterrupts(t);
|
|
|
|
rt::rust_task_inhibit_kill(t);
|
|
|
|
rt::rust_task_inhibit_yield(t);
|
|
|
|
f()
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_cant_dup_task_builder() {
|
|
|
|
let b = task().unlinked();
|
|
|
|
do b.spawn { }
|
|
|
|
// FIXME(#3724): For now, this is a -runtime- failure, because we haven't
|
|
|
|
// got move mode on self. When 3724 is fixed, this test should fail to
|
|
|
|
// compile instead, and should go in tests/compile-fail.
|
|
|
|
do b.spawn { } // b should have been consumed by the previous call
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following 8 tests test the following 2^3 combinations:
|
|
|
|
// {un,}linked {un,}supervised failure propagation {up,down}wards.
|
|
|
|
|
|
|
|
// !!! These tests are dangerous. If Something is buggy, they will hang, !!!
|
|
|
|
// !!! instead of exiting cleanly. This might wedge the buildbots. !!!
|
|
|
|
|
|
|
|
#[test] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_unlinked_unsup_no_fail_down() { // grandchild sends on a port
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream();
|
|
|
|
let ch = SharedChan(ch);
|
2012-11-28 18:20:41 -06:00
|
|
|
do spawn_unlinked {
|
2013-01-22 14:38:08 -06:00
|
|
|
let ch = ch.clone();
|
2012-11-28 18:20:41 -06:00
|
|
|
do spawn_unlinked {
|
|
|
|
// Give middle task a chance to fail-but-not-kill-us.
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(()); // If killed first, grandparent hangs.
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(); // Shouldn't kill either (grand)parent or (grand)child.
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_unlinked_unsup_no_fail_up() { // child unlinked fails
|
2013-02-11 21:26:38 -06:00
|
|
|
do spawn_unlinked { fail!(); }
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_unlinked_sup_no_fail_up() { // child unlinked fails
|
2013-02-11 21:26:38 -06:00
|
|
|
do spawn_supervised { fail!(); }
|
2012-11-28 18:20:41 -06:00
|
|
|
// Give child a chance to fail-but-not-kill-us.
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
|
|
|
}
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_unlinked_sup_fail_down() {
|
|
|
|
do spawn_supervised { loop { task::yield(); } }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(); // Shouldn't leave a child hanging around.
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_sup_fail_up() { // child fails; parent fails
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, _ch) = stream::<()>();
|
2012-11-28 18:20:41 -06:00
|
|
|
// Unidirectional "parenting" shouldn't override bidirectional linked.
|
|
|
|
// We have to cheat with opts - the interface doesn't support them because
|
|
|
|
// they don't make sense (redundant with task().supervised()).
|
|
|
|
let opts = {
|
|
|
|
let mut opts = default_task_opts();
|
|
|
|
opts.linked = true;
|
|
|
|
opts.supervised = true;
|
2013-02-15 02:51:28 -06:00
|
|
|
opts
|
2012-11-28 18:20:41 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let b0 = task();
|
2012-12-05 17:06:54 -06:00
|
|
|
let b1 = TaskBuilder {
|
2013-02-15 02:51:28 -06:00
|
|
|
opts: opts,
|
2012-11-28 18:20:41 -06:00
|
|
|
can_not_copy: None,
|
2012-12-05 17:30:00 -06:00
|
|
|
.. b0
|
2012-12-05 17:06:54 -06:00
|
|
|
};
|
2013-02-11 21:26:38 -06:00
|
|
|
do b1.spawn { fail!(); }
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv(); // We should get punted awake
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_sup_fail_down() { // parent fails; child fails
|
|
|
|
// We have to cheat with opts - the interface doesn't support them because
|
|
|
|
// they don't make sense (redundant with task().supervised()).
|
|
|
|
let opts = {
|
|
|
|
let mut opts = default_task_opts();
|
|
|
|
opts.linked = true;
|
|
|
|
opts.supervised = true;
|
2013-02-15 02:51:28 -06:00
|
|
|
opts
|
2012-11-28 18:20:41 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let b0 = task();
|
2012-12-05 17:06:54 -06:00
|
|
|
let b1 = TaskBuilder {
|
2013-02-15 02:51:28 -06:00
|
|
|
opts: opts,
|
2012-11-28 18:20:41 -06:00
|
|
|
can_not_copy: None,
|
2012-12-05 17:30:00 -06:00
|
|
|
.. b0
|
2012-12-05 17:06:54 -06:00
|
|
|
};
|
2012-11-28 18:20:41 -06:00
|
|
|
do b1.spawn { loop { task::yield(); } }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(); // *both* mechanisms would be wrong if this didn't kill the child
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_unsup_fail_up() { // child fails; parent fails
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, _ch) = stream::<()>();
|
2012-11-28 18:20:41 -06:00
|
|
|
// Default options are to spawn linked & unsupervised.
|
2013-02-11 21:26:38 -06:00
|
|
|
do spawn { fail!(); }
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv(); // We should get punted awake
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_unsup_fail_down() { // parent fails; child fails
|
|
|
|
// Default options are to spawn linked & unsupervised.
|
|
|
|
do spawn { loop { task::yield(); } }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_unsup_default_opts() { // parent fails; child fails
|
|
|
|
// Make sure the above test is the same as this one.
|
|
|
|
do task().linked().spawn { loop { task::yield(); } }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A couple bonus linked failure tests - testing for failure propagation even
|
|
|
|
// when the middle task exits successfully early before kill signals are sent.
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_failure_propagate_grandchild() {
|
|
|
|
// Middle task exits; does grandparent's failure propagate across the gap?
|
|
|
|
do spawn_supervised {
|
|
|
|
do spawn_supervised {
|
|
|
|
loop { task::yield(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_failure_propagate_secondborn() {
|
|
|
|
// First-born child exits; does parent's failure propagate to sibling?
|
|
|
|
do spawn_supervised {
|
|
|
|
do spawn { // linked
|
|
|
|
loop { task::yield(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_failure_propagate_nephew_or_niece() {
|
|
|
|
// Our sibling exits; does our failure propagate to sibling's child?
|
|
|
|
do spawn { // linked
|
|
|
|
do spawn_supervised {
|
|
|
|
loop { task::yield(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_linked_sup_propagate_sibling() {
|
|
|
|
// Middle sibling exits - does eldest's failure propagate to youngest?
|
|
|
|
do spawn { // linked
|
|
|
|
do spawn { // linked
|
|
|
|
loop { task::yield(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter::repeat(16) { task::yield(); }
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_run_basic() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream::<()>();
|
2012-11-28 18:20:41 -06:00
|
|
|
do task().spawn {
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(());
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
#[test]
|
|
|
|
struct Wrapper {
|
|
|
|
mut f: Option<Chan<()>>
|
|
|
|
}
|
|
|
|
|
2012-11-28 18:20:41 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_wrapper() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream::<()>();
|
2012-11-28 18:20:41 -06:00
|
|
|
let b0 = task();
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = Wrapper { f: Some(ch) };
|
2012-11-28 18:20:41 -06:00
|
|
|
let b1 = do b0.add_wrapper |body| {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = Wrapper { f: Some(ch.f.swap_unwrap()) };
|
2013-03-01 16:32:37 -06:00
|
|
|
let result: ~fn() = || {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = ch.f.swap_unwrap();
|
2012-11-28 18:20:41 -06:00
|
|
|
body();
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(());
|
2013-03-01 16:32:37 -06:00
|
|
|
};
|
|
|
|
result
|
2012-11-28 18:20:41 -06:00
|
|
|
};
|
|
|
|
do b1.spawn { }
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
fn test_future_result() {
|
|
|
|
let mut result = None;
|
2013-02-15 02:51:28 -06:00
|
|
|
do task().future_result(|+r| { result = Some(r); }).spawn { }
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(option::unwrap(result).recv() == Success);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
result = None;
|
|
|
|
do task().future_result(|+r|
|
2013-02-15 02:51:28 -06:00
|
|
|
{ result = Some(r); }).unlinked().spawn {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(option::unwrap(result).recv() == Failure);
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_back_to_the_future_result() {
|
|
|
|
let _ = task().future_result(util::ignore).future_result(util::ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try_success() {
|
|
|
|
match do try {
|
|
|
|
~"Success!"
|
|
|
|
} {
|
|
|
|
result::Ok(~"Success!") => (),
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
fn test_try_fail() {
|
|
|
|
match do try {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!()
|
2012-11-28 18:20:41 -06:00
|
|
|
} {
|
|
|
|
result::Err(()) => (),
|
2013-02-11 21:26:38 -06:00
|
|
|
result::Ok(()) => fail!()
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
fn test_spawn_sched_no_threads() {
|
|
|
|
do spawn_sched(ManualThreads(0u)) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spawn_sched() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream::<()>();
|
|
|
|
let ch = SharedChan(ch);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
fn f(i: int, ch: SharedChan<()>) {
|
2013-01-25 19:51:53 -06:00
|
|
|
let parent_sched_id = unsafe { rt::rust_get_sched_id() };
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
do spawn_sched(SingleThreaded) {
|
2013-01-25 19:51:53 -06:00
|
|
|
let child_sched_id = unsafe { rt::rust_get_sched_id() };
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parent_sched_id != child_sched_id);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
if (i == 0) {
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(());
|
2012-11-28 18:20:41 -06:00
|
|
|
} else {
|
2013-01-22 14:38:08 -06:00
|
|
|
f(i - 1, ch.clone());
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
f(10, ch);
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-01-08 21:46:12 -06:00
|
|
|
fn test_spawn_sched_childs_on_default_sched() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-08 21:46:12 -06:00
|
|
|
// Assuming tests run on the default scheduler
|
2013-01-25 19:51:53 -06:00
|
|
|
let default_id = unsafe { rt::rust_get_sched_id() };
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = Wrapper { f: Some(ch) };
|
2012-11-28 18:20:41 -06:00
|
|
|
do spawn_sched(SingleThreaded) {
|
2013-01-25 19:51:53 -06:00
|
|
|
let parent_sched_id = unsafe { rt::rust_get_sched_id() };
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = Wrapper { f: Some(ch.f.swap_unwrap()) };
|
2012-11-28 18:20:41 -06:00
|
|
|
do spawn {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ch = ch.f.swap_unwrap();
|
2013-01-25 19:51:53 -06:00
|
|
|
let child_sched_id = unsafe { rt::rust_get_sched_id() };
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parent_sched_id != child_sched_id);
|
|
|
|
fail_unless!(child_sched_id == default_id);
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(());
|
2012-11-28 18:20:41 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod testrt {
|
2013-03-06 21:09:17 -06:00
|
|
|
use libc;
|
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
#[nolink]
|
|
|
|
pub extern {
|
|
|
|
unsafe fn rust_dbg_lock_create() -> *libc::c_void;
|
|
|
|
unsafe fn rust_dbg_lock_destroy(lock: *libc::c_void);
|
|
|
|
unsafe fn rust_dbg_lock_lock(lock: *libc::c_void);
|
|
|
|
unsafe fn rust_dbg_lock_unlock(lock: *libc::c_void);
|
|
|
|
unsafe fn rust_dbg_lock_wait(lock: *libc::c_void);
|
|
|
|
unsafe fn rust_dbg_lock_signal(lock: *libc::c_void);
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spawn_sched_blocking() {
|
2013-01-11 00:36:54 -06:00
|
|
|
unsafe {
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-11 00:36:54 -06:00
|
|
|
// Testing that a task in one scheduler can block in foreign code
|
|
|
|
// without affecting other schedulers
|
|
|
|
for iter::repeat(20u) {
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
let (start_po, start_ch) = stream();
|
|
|
|
let (fin_po, fin_ch) = stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-11 00:36:54 -06:00
|
|
|
let lock = testrt::rust_dbg_lock_create();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-11 00:36:54 -06:00
|
|
|
do spawn_sched(SingleThreaded) {
|
2013-01-11 01:08:17 -06:00
|
|
|
unsafe {
|
|
|
|
testrt::rust_dbg_lock_lock(lock);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
start_ch.send(());
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-11 01:08:17 -06:00
|
|
|
// Block the scheduler thread
|
|
|
|
testrt::rust_dbg_lock_wait(lock);
|
|
|
|
testrt::rust_dbg_lock_unlock(lock);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
fin_ch.send(());
|
2013-01-11 01:08:17 -06:00
|
|
|
}
|
2013-01-11 00:36:54 -06:00
|
|
|
};
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-11 00:36:54 -06:00
|
|
|
// Wait until the other task has its lock
|
2013-01-22 14:38:08 -06:00
|
|
|
start_po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
fn pingpong(po: &Port<int>, ch: &Chan<int>) {
|
2013-01-11 00:36:54 -06:00
|
|
|
let mut val = 20;
|
|
|
|
while val > 0 {
|
2013-01-22 14:38:08 -06:00
|
|
|
val = po.recv();
|
|
|
|
ch.send(val - 1);
|
2013-01-11 00:36:54 -06:00
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
let (setup_po, setup_ch) = stream();
|
|
|
|
let (parent_po, parent_ch) = stream();
|
2013-01-11 00:36:54 -06:00
|
|
|
do spawn {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (child_po, child_ch) = stream();
|
|
|
|
setup_ch.send(child_ch);
|
|
|
|
pingpong(&child_po, &parent_ch);
|
2013-01-11 00:36:54 -06:00
|
|
|
};
|
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
let child_ch = setup_po.recv();
|
|
|
|
child_ch.send(20);
|
|
|
|
pingpong(&parent_po, &child_ch);
|
2013-01-11 00:36:54 -06:00
|
|
|
testrt::rust_dbg_lock_lock(lock);
|
|
|
|
testrt::rust_dbg_lock_signal(lock);
|
|
|
|
testrt::rust_dbg_lock_unlock(lock);
|
2013-01-22 14:38:08 -06:00
|
|
|
fin_po.recv();
|
2013-01-11 00:36:54 -06:00
|
|
|
testrt::rust_dbg_lock_destroy(lock);
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-03-01 16:32:37 -06:00
|
|
|
fn avoid_copying_the_body(spawnfn: &fn(v: ~fn())) {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (p, ch) = stream::<uint>();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
let x = ~1;
|
|
|
|
let x_in_parent = ptr::addr_of(&(*x)) as uint;
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawnfn || {
|
2012-11-28 18:20:41 -06:00
|
|
|
let x_in_child = ptr::addr_of(&(*x)) as uint;
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(x_in_child);
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
2013-01-22 14:38:08 -06:00
|
|
|
let x_in_child = p.recv();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(x_in_parent == x_in_child);
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_avoid_copying_the_body_spawn() {
|
|
|
|
avoid_copying_the_body(spawn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_avoid_copying_the_body_task_spawn() {
|
|
|
|
do avoid_copying_the_body |f| {
|
2013-02-15 02:51:28 -06:00
|
|
|
do task().spawn || {
|
2012-11-28 18:20:41 -06:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_avoid_copying_the_body_try() {
|
|
|
|
do avoid_copying_the_body |f| {
|
2013-02-15 02:51:28 -06:00
|
|
|
do try || {
|
2012-11-28 18:20:41 -06:00
|
|
|
f()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_avoid_copying_the_body_unlinked() {
|
|
|
|
do avoid_copying_the_body |f| {
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawn_unlinked || {
|
2012-11-28 18:20:41 -06:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_platform_thread() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
do task().sched_mode(PlatformThread).spawn {
|
2013-01-22 14:38:08 -06:00
|
|
|
ch.send(());
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
2013-01-22 14:38:08 -06:00
|
|
|
po.recv();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_unkillable() {
|
2013-01-22 14:38:08 -06:00
|
|
|
let (po, ch) = stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
// We want to do this after failing
|
|
|
|
do spawn_unlinked {
|
|
|
|
for iter::repeat(10) { yield() }
|
|
|
|
ch.send(());
|
|
|
|
}
|
|
|
|
|
|
|
|
do spawn {
|
|
|
|
yield();
|
|
|
|
// We want to fail after the unkillable task
|
|
|
|
// blocks on recv
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
do unkillable {
|
|
|
|
let p = ~0;
|
2013-02-15 02:51:28 -06:00
|
|
|
let pp: *uint = cast::transmute(p);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
// If we are killed here then the box will leak
|
|
|
|
po.recv();
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
let _p: ~int = cast::transmute(pp);
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can be killed
|
|
|
|
po.recv();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_unkillable_nested() {
|
2013-02-02 05:10:12 -06:00
|
|
|
let (po, ch) = comm::stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
// We want to do this after failing
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawn_unlinked || {
|
2012-11-28 18:20:41 -06:00
|
|
|
for iter::repeat(10) { yield() }
|
|
|
|
ch.send(());
|
|
|
|
}
|
|
|
|
|
|
|
|
do spawn {
|
|
|
|
yield();
|
|
|
|
// We want to fail after the unkillable task
|
|
|
|
// blocks on recv
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
do unkillable {
|
|
|
|
do unkillable {} // Here's the difference from the previous test.
|
|
|
|
let p = ~0;
|
2013-02-15 02:51:28 -06:00
|
|
|
let pp: *uint = cast::transmute(p);
|
2012-11-28 18:20:41 -06:00
|
|
|
|
|
|
|
// If we are killed here then the box will leak
|
|
|
|
po.recv();
|
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
let _p: ~int = cast::transmute(pp);
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can be killed
|
|
|
|
po.recv();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_atomically() {
|
|
|
|
unsafe { do atomically { yield(); } }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_atomically2() {
|
|
|
|
unsafe { do atomically { } } yield(); // shouldn't fail
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] #[should_fail] #[ignore(cfg(windows))]
|
|
|
|
fn test_atomically_nested() {
|
|
|
|
unsafe { do atomically { do atomically { } yield(); } }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_child_doesnt_ref_parent() {
|
|
|
|
// If the child refcounts the parent task, this will stack overflow when
|
|
|
|
// climbing the task tree to dereference each ancestor. (See #1789)
|
|
|
|
// (well, it would if the constant were 8000+ - I lowered it to be more
|
|
|
|
// valgrind-friendly. try this at home, instead..!)
|
|
|
|
const generations: uint = 16;
|
2013-03-01 16:32:37 -06:00
|
|
|
fn child_no(x: uint) -> ~fn() {
|
2012-11-28 18:20:41 -06:00
|
|
|
return || {
|
|
|
|
if x < generations {
|
|
|
|
task::spawn(child_no(x+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task::spawn(child_no(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sched_thread_per_core() {
|
2013-02-02 05:10:12 -06:00
|
|
|
let (port, chan) = comm::stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawn_sched(ThreadPerCore) || {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let cores = rt::rust_num_threads();
|
|
|
|
let reported_threads = rt::rust_sched_threads();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((cores as uint == reported_threads as uint));
|
2013-01-23 18:29:31 -06:00
|
|
|
chan.send(());
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
port.recv();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_spawn_thread_on_demand() {
|
2013-02-02 05:10:12 -06:00
|
|
|
let (port, chan) = comm::stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawn_sched(ManualThreads(2)) || {
|
2013-01-23 18:29:31 -06:00
|
|
|
unsafe {
|
|
|
|
let max_threads = rt::rust_sched_threads();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((max_threads as int == 2));
|
2013-01-23 18:29:31 -06:00
|
|
|
let running_threads = rt::rust_sched_current_nonlazy_threads();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((running_threads as int == 1));
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
let (port2, chan2) = comm::stream();
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-02-15 02:51:28 -06:00
|
|
|
do spawn_sched(CurrentScheduler) || {
|
2013-01-23 18:29:31 -06:00
|
|
|
chan2.send(());
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
let running_threads2 = rt::rust_sched_current_nonlazy_threads();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((running_threads2 as int == 2));
|
2012-11-28 18:20:41 -06:00
|
|
|
|
2013-01-23 18:29:31 -06:00
|
|
|
port2.recv();
|
|
|
|
chan.send(());
|
|
|
|
}
|
2012-11-28 18:20:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
port.recv();
|
|
|
|
}
|