2014-01-16 02:24:42 +08:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-12-12 17:54:53 -08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Tasks implemented on top of OS threads
|
|
|
|
//!
|
|
|
|
//! This module contains the implementation of the 1:1 threading module required
|
|
|
|
//! by rust tasks. This implements the necessary API traits laid out by std::rt
|
|
|
|
//! in order to spawn new tasks and deschedule the current task.
|
|
|
|
|
2014-03-03 01:01:13 +01:00
|
|
|
use std::any::Any;
|
2013-12-12 17:54:53 -08:00
|
|
|
use std::cast;
|
2014-03-05 10:35:30 -08:00
|
|
|
use std::rt::bookkeeping;
|
2013-12-12 17:54:53 -08:00
|
|
|
use std::rt::env;
|
|
|
|
use std::rt::local::Local;
|
|
|
|
use std::rt::rtio;
|
2014-03-05 10:35:30 -08:00
|
|
|
use std::rt::stack;
|
2014-02-10 14:49:56 -08:00
|
|
|
use std::rt::task::{Task, BlockedTask, SendMessage};
|
2013-12-12 17:54:53 -08:00
|
|
|
use std::rt::thread::Thread;
|
|
|
|
use std::rt;
|
2013-12-13 16:26:02 -08:00
|
|
|
use std::task::TaskOpts;
|
2014-02-15 15:01:00 +11:00
|
|
|
use std::unstable::mutex::NativeMutex;
|
2013-12-12 17:54:53 -08:00
|
|
|
|
|
|
|
use io;
|
|
|
|
use task;
|
|
|
|
|
|
|
|
/// Creates a new Task which is ready to execute as a 1:1 task.
|
2014-01-07 13:39:55 +11:00
|
|
|
pub fn new(stack_bounds: (uint, uint)) -> ~Task {
|
2013-12-12 17:54:53 -08:00
|
|
|
let mut task = ~Task::new();
|
2014-01-07 13:39:55 +11:00
|
|
|
let mut ops = ops();
|
|
|
|
ops.stack_bounds = stack_bounds;
|
2014-03-08 18:21:49 -08:00
|
|
|
task.put_runtime(ops);
|
2014-01-04 00:06:13 -08:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ops() -> ~Ops {
|
|
|
|
~Ops {
|
2014-02-15 15:01:00 +11:00
|
|
|
lock: unsafe { NativeMutex::new() },
|
2013-12-15 22:19:34 -08:00
|
|
|
awoken: false,
|
2013-12-27 17:50:16 -08:00
|
|
|
io: io::IoFactory::new(),
|
2014-01-07 13:39:55 +11:00
|
|
|
// these *should* get overwritten
|
|
|
|
stack_bounds: (0, 0),
|
2014-01-04 00:06:13 -08:00
|
|
|
}
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:38:57 -08:00
|
|
|
/// Spawns a function with the default configuration
|
2014-04-07 13:30:48 -07:00
|
|
|
pub fn spawn(f: proc():Send) {
|
2013-12-13 16:26:02 -08:00
|
|
|
spawn_opts(TaskOpts::new(), f)
|
2013-12-12 21:38:57 -08:00
|
|
|
}
|
|
|
|
|
2013-12-12 17:54:53 -08:00
|
|
|
/// Spawns a new task given the configuration options and a procedure to run
|
|
|
|
/// inside the task.
|
2014-04-07 13:30:48 -07:00
|
|
|
pub fn spawn_opts(opts: TaskOpts, f: proc():Send) {
|
2013-12-12 17:54:53 -08:00
|
|
|
let TaskOpts {
|
2014-01-06 10:26:11 -08:00
|
|
|
notify_chan, name, stack_size,
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-08 22:11:44 -08:00
|
|
|
stderr, stdout,
|
2013-12-12 17:54:53 -08:00
|
|
|
} = opts;
|
|
|
|
|
2014-01-04 00:06:13 -08:00
|
|
|
let mut task = ~Task::new();
|
2013-12-12 17:54:53 -08:00
|
|
|
task.name = name;
|
2014-01-06 10:26:11 -08:00
|
|
|
task.stderr = stderr;
|
|
|
|
task.stdout = stdout;
|
2013-12-12 17:54:53 -08:00
|
|
|
match notify_chan {
|
2014-02-10 14:49:56 -08:00
|
|
|
Some(chan) => { task.death.on_exit = Some(SendMessage(chan)); }
|
2013-12-12 17:54:53 -08:00
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stack = stack_size.unwrap_or(env::min_stack());
|
|
|
|
let task = task;
|
2014-01-04 00:06:13 -08:00
|
|
|
let ops = ops();
|
2013-12-12 17:54:53 -08:00
|
|
|
|
2014-01-04 12:02:02 -08:00
|
|
|
// Note that this increment must happen *before* the spawn in order to
|
|
|
|
// guarantee that if this task exits it will always end up waiting for the
|
|
|
|
// spawned task to exit.
|
2014-01-16 02:24:42 +08:00
|
|
|
bookkeeping::increment();
|
2014-01-04 12:02:02 -08:00
|
|
|
|
2013-12-12 17:54:53 -08:00
|
|
|
// Spawning a new OS thread guarantees that __morestack will never get
|
|
|
|
// triggered, but we must manually set up the actual stack bounds once this
|
|
|
|
// function starts executing. This raises the lower limit by a bit because
|
|
|
|
// by the time that this function is executing we've already consumed at
|
|
|
|
// least a little bit of stack (we don't know the exact byte address at
|
|
|
|
// which our stack started).
|
|
|
|
Thread::spawn_stack(stack, proc() {
|
|
|
|
let something_around_the_top_of_the_stack = 1;
|
|
|
|
let addr = &something_around_the_top_of_the_stack as *int;
|
2014-01-04 00:06:13 -08:00
|
|
|
let my_stack = addr as uint;
|
2013-12-12 17:54:53 -08:00
|
|
|
unsafe {
|
|
|
|
stack::record_stack_bounds(my_stack - stack + 1024, my_stack);
|
|
|
|
}
|
2014-01-04 00:06:13 -08:00
|
|
|
let mut ops = ops;
|
2014-01-07 13:39:55 +11:00
|
|
|
ops.stack_bounds = (my_stack - stack + 1024, my_stack);
|
2013-12-12 17:54:53 -08:00
|
|
|
|
2013-12-15 00:42:21 -08:00
|
|
|
let mut f = Some(f);
|
2014-01-04 00:06:13 -08:00
|
|
|
let mut task = task;
|
2014-03-08 18:21:49 -08:00
|
|
|
task.put_runtime(ops);
|
2014-01-30 14:28:28 -08:00
|
|
|
let t = task.run(|| { f.take_unwrap()() });
|
|
|
|
drop(t);
|
2014-01-16 02:24:42 +08:00
|
|
|
bookkeeping::decrement();
|
2013-12-12 17:54:53 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This structure is the glue between channels and the 1:1 scheduling mode. This
|
|
|
|
// structure is allocated once per task.
|
|
|
|
struct Ops {
|
2014-02-15 15:01:00 +11:00
|
|
|
lock: NativeMutex, // native synchronization
|
2013-12-27 17:50:16 -08:00
|
|
|
awoken: bool, // used to prevent spurious wakeups
|
|
|
|
io: io::IoFactory, // local I/O factory
|
2014-01-04 00:06:13 -08:00
|
|
|
|
|
|
|
// This field holds the known bounds of the stack in (lo, hi) form. Not all
|
|
|
|
// native tasks necessarily know their precise bounds, hence this is
|
|
|
|
// optional.
|
2014-01-07 13:39:55 +11:00
|
|
|
stack_bounds: (uint, uint),
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl rt::Runtime for Ops {
|
|
|
|
fn yield_now(~self, mut cur_task: ~Task) {
|
|
|
|
// put the task back in TLS and then invoke the OS thread yield
|
2014-03-08 18:21:49 -08:00
|
|
|
cur_task.put_runtime(self);
|
2013-12-12 17:54:53 -08:00
|
|
|
Local::put(cur_task);
|
|
|
|
Thread::yield_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_yield(~self, mut cur_task: ~Task) {
|
|
|
|
// just put the task back in TLS, on OS threads we never need to
|
|
|
|
// opportunistically yield b/c the OS will do that for us (preemption)
|
2014-03-08 18:21:49 -08:00
|
|
|
cur_task.put_runtime(self);
|
2013-12-12 17:54:53 -08:00
|
|
|
Local::put(cur_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap(~self) -> ~Any {
|
|
|
|
self as ~Any
|
|
|
|
}
|
|
|
|
|
2014-01-07 13:39:55 +11:00
|
|
|
fn stack_bounds(&self) -> (uint, uint) { self.stack_bounds }
|
2014-01-04 00:06:13 -08:00
|
|
|
|
2014-01-16 19:58:42 -08:00
|
|
|
fn can_block(&self) -> bool { true }
|
|
|
|
|
2013-12-12 17:54:53 -08:00
|
|
|
// This function gets a little interesting. There are a few safety and
|
|
|
|
// ownership violations going on here, but this is all done in the name of
|
|
|
|
// shared state. Additionally, all of the violations are protected with a
|
|
|
|
// mutex, so in theory there are no races.
|
|
|
|
//
|
|
|
|
// The first thing we need to do is to get a pointer to the task's internal
|
|
|
|
// mutex. This address will not be changing (because the task is allocated
|
|
|
|
// on the heap). We must have this handle separately because the task will
|
|
|
|
// have its ownership transferred to the given closure. We're guaranteed,
|
|
|
|
// however, that this memory will remain valid because *this* is the current
|
|
|
|
// task's execution thread.
|
|
|
|
//
|
|
|
|
// The next weird part is where ownership of the task actually goes. We
|
|
|
|
// relinquish it to the `f` blocking function, but upon returning this
|
|
|
|
// function needs to replace the task back in TLS. There is no communication
|
|
|
|
// from the wakeup thread back to this thread about the task pointer, and
|
|
|
|
// there's really no need to. In order to get around this, we cast the task
|
|
|
|
// to a `uint` which is then used at the end of this function to cast back
|
|
|
|
// to a `~Task` object. Naturally, this looks like it violates ownership
|
|
|
|
// semantics in that there may be two `~Task` objects.
|
|
|
|
//
|
|
|
|
// The fun part is that the wakeup half of this implementation knows to
|
|
|
|
// "forget" the task on the other end. This means that the awakening half of
|
|
|
|
// things silently relinquishes ownership back to this thread, but not in a
|
|
|
|
// way that the compiler can understand. The task's memory is always valid
|
|
|
|
// for both tasks because these operations are all done inside of a mutex.
|
|
|
|
//
|
|
|
|
// You'll also find that if blocking fails (the `f` function hands the
|
|
|
|
// BlockedTask back to us), we will `cast::forget` the handles. The
|
|
|
|
// reasoning for this is the same logic as above in that the task silently
|
|
|
|
// transfers ownership via the `uint`, not through normal compiler
|
|
|
|
// semantics.
|
2013-12-15 22:19:34 -08:00
|
|
|
//
|
|
|
|
// On a mildly unrelated note, it should also be pointed out that OS
|
|
|
|
// condition variables are susceptible to spurious wakeups, which we need to
|
|
|
|
// be ready for. In order to accomodate for this fact, we have an extra
|
|
|
|
// `awoken` field which indicates whether we were actually woken up via some
|
|
|
|
// invocation of `reawaken`. This flag is only ever accessed inside the
|
|
|
|
// lock, so there's no need to make it atomic.
|
2013-12-12 17:54:53 -08:00
|
|
|
fn deschedule(mut ~self, times: uint, mut cur_task: ~Task,
|
|
|
|
f: |BlockedTask| -> Result<(), BlockedTask>) {
|
2013-12-15 22:19:34 -08:00
|
|
|
let me = &mut *self as *mut Ops;
|
2014-03-08 18:21:49 -08:00
|
|
|
cur_task.put_runtime(self);
|
2013-12-12 17:54:53 -08:00
|
|
|
|
|
|
|
unsafe {
|
2014-02-21 22:40:53 +11:00
|
|
|
let cur_task_dupe = &*cur_task as *Task;
|
2013-12-12 17:54:53 -08:00
|
|
|
let task = BlockedTask::block(cur_task);
|
|
|
|
|
|
|
|
if times == 1 {
|
2014-03-22 00:45:41 -07:00
|
|
|
let guard = (*me).lock.lock();
|
2013-12-15 22:19:34 -08:00
|
|
|
(*me).awoken = false;
|
2013-12-12 17:54:53 -08:00
|
|
|
match f(task) {
|
2013-12-15 22:19:34 -08:00
|
|
|
Ok(()) => {
|
|
|
|
while !(*me).awoken {
|
2014-02-13 17:17:50 +11:00
|
|
|
guard.wait();
|
2013-12-15 22:19:34 -08:00
|
|
|
}
|
|
|
|
}
|
2013-12-12 17:54:53 -08:00
|
|
|
Err(task) => { cast::forget(task.wake()); }
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-14 07:06:14 -07:00
|
|
|
let iter = task.make_selectable(times);
|
2014-03-22 00:45:41 -07:00
|
|
|
let guard = (*me).lock.lock();
|
2013-12-15 22:19:34 -08:00
|
|
|
(*me).awoken = false;
|
2014-04-14 07:06:14 -07:00
|
|
|
|
|
|
|
// Apply the given closure to all of the "selectable tasks",
|
|
|
|
// bailing on the first one that produces an error. Note that
|
|
|
|
// care must be taken such that when an error is occurred, we
|
|
|
|
// may not own the task, so we may still have to wait for the
|
|
|
|
// task to become available. In other words, if task.wake()
|
|
|
|
// returns `None`, then someone else has ownership and we must
|
|
|
|
// wait for their signal.
|
|
|
|
match iter.map(f).filter_map(|a| a.err()).next() {
|
|
|
|
None => {}
|
|
|
|
Some(task) => {
|
|
|
|
match task.wake() {
|
|
|
|
Some(task) => {
|
|
|
|
cast::forget(task);
|
|
|
|
(*me).awoken = true;
|
|
|
|
}
|
|
|
|
None => {}
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
}
|
2014-04-14 07:06:14 -07:00
|
|
|
}
|
|
|
|
while !(*me).awoken {
|
2014-02-13 17:17:50 +11:00
|
|
|
guard.wait();
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// re-acquire ownership of the task
|
2014-02-21 22:40:53 +11:00
|
|
|
cur_task = cast::transmute(cur_task_dupe);
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// put the task back in TLS, and everything is as it once was.
|
|
|
|
Local::put(cur_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See the comments on `deschedule` for why the task is forgotten here, and
|
|
|
|
// why it's valid to do so.
|
2014-01-16 19:58:42 -08:00
|
|
|
fn reawaken(mut ~self, mut to_wake: ~Task) {
|
2013-12-12 17:54:53 -08:00
|
|
|
unsafe {
|
2013-12-15 22:19:34 -08:00
|
|
|
let me = &mut *self as *mut Ops;
|
2014-03-08 18:21:49 -08:00
|
|
|
to_wake.put_runtime(self);
|
2013-12-12 17:54:53 -08:00
|
|
|
cast::forget(to_wake);
|
2014-03-22 00:45:41 -07:00
|
|
|
let guard = (*me).lock.lock();
|
2013-12-15 22:19:34 -08:00
|
|
|
(*me).awoken = true;
|
2014-02-13 17:17:50 +11:00
|
|
|
guard.signal();
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 13:30:48 -07:00
|
|
|
fn spawn_sibling(~self, mut cur_task: ~Task, opts: TaskOpts, f: proc():Send) {
|
2014-03-08 18:21:49 -08:00
|
|
|
cur_task.put_runtime(self);
|
2013-12-12 17:54:53 -08:00
|
|
|
Local::put(cur_task);
|
|
|
|
|
2013-12-12 21:38:57 -08:00
|
|
|
task::spawn_opts(opts, f);
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> {
|
2013-12-27 17:50:16 -08:00
|
|
|
Some(rtio::LocalIo::new(&mut self.io as &mut rtio::IoFactory))
|
2013-12-12 17:54:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 00:02:54 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::rt::local::Local;
|
|
|
|
use std::rt::task::Task;
|
|
|
|
use std::task;
|
2013-12-13 19:33:28 -08:00
|
|
|
use std::task::TaskOpts;
|
2013-12-13 00:02:54 -08:00
|
|
|
use super::{spawn, spawn_opts, Ops};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
tx.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_fail() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel::<()>();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let _tx = tx;
|
2013-12-13 00:02:54 -08:00
|
|
|
fail!()
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
std: Make std::comm return types consistent
There are currently a number of return values from the std::comm methods, not
all of which are necessarily completely expressive:
Sender::try_send(t: T) -> bool
This method currently doesn't transmit back the data `t` if the send fails
due to the other end having disconnected. Additionally, this shares the name
of the synchronous try_send method, but it differs in semantics in that it
only has one failure case, not two (the buffer can never be full).
SyncSender::try_send(t: T) -> TrySendResult<T>
This method accurately conveys all possible information, but it uses a
custom type to the std::comm module with no convenience methods on it.
Additionally, if you want to inspect the result you're forced to import
something from `std::comm`.
SyncSender::send_opt(t: T) -> Option<T>
This method uses Some(T) as an "error value" and None as a "success value",
but almost all other uses of Option<T> have Some/None the other way
Receiver::try_recv(t: T) -> TryRecvResult<T>
Similarly to the synchronous try_send, this custom return type is lacking in
terms of usability (no convenience methods).
With this number of drawbacks in mind, I believed it was time to re-work the
return types of these methods. The new API for the comm module is:
Sender::send(t: T) -> ()
Sender::send_opt(t: T) -> Result<(), T>
SyncSender::send(t: T) -> ()
SyncSender::send_opt(t: T) -> Result<(), T>
SyncSender::try_send(t: T) -> Result<(), TrySendError<T>>
Receiver::recv() -> T
Receiver::recv_opt() -> Result<T, ()>
Receiver::try_recv() -> Result<T, TryRecvError>
The notable changes made are:
* Sender::try_send => Sender::send_opt. This renaming brings the semantics in
line with the SyncSender::send_opt method. An asychronous send only has one
failure case, unlike the synchronous try_send method which has two failure
cases (full/disconnected).
* Sender::send_opt returns the data back to the caller if the send is guaranteed
to fail. This method previously returned `bool`, but then it was unable to
retrieve the data if the data was guaranteed to fail to send. There is still a
race such that when `Ok(())` is returned the data could still fail to be
received, but that's inherent to an asynchronous channel.
* Result is now the basis of all return values. This not only adds lots of
convenience methods to all return values for free, but it also means that you
can inspect the return values with no extra imports (Ok/Err are in the
prelude). Additionally, it's now self documenting when something failed or not
because the return value has "Err" in the name.
Things I'm a little uneasy about:
* The methods send_opt and recv_opt are not returning options, but rather
results. I felt more strongly that Option was the wrong return type than the
_opt prefix was wrong, and I coudn't think of a much better name for these
methods. One possible way to think about them is to read the _opt suffix as
"optionally".
* Result<T, ()> is often better expressed as Option<T>. This is only applicable
to the recv_opt() method, but I thought it would be more consistent for
everything to return Result rather than one method returning an Option.
Despite my two reasons to feel uneasy, I feel much better about the consistency
in return values at this point, and I think the only real open question is if
there's a better suffix for {send,recv}_opt.
Closes #11527
2014-04-10 10:53:49 -07:00
|
|
|
assert_eq!(rx.recv_opt(), Err(()));
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_opts() {
|
2013-12-13 16:26:02 -08:00
|
|
|
let mut opts = TaskOpts::new();
|
2014-02-07 16:36:59 -08:00
|
|
|
opts.name = Some("test".into_maybe_owned());
|
2013-12-13 00:02:54 -08:00
|
|
|
opts.stack_size = Some(20 * 4096);
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
opts.notify_chan = Some(tx);
|
2013-12-13 00:02:54 -08:00
|
|
|
spawn_opts(opts, proc() {});
|
2014-03-09 14:58:32 -07:00
|
|
|
assert!(rx.recv().is_ok());
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_opts_fail() {
|
2013-12-13 16:26:02 -08:00
|
|
|
let mut opts = TaskOpts::new();
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
opts.notify_chan = Some(tx);
|
2013-12-13 00:02:54 -08:00
|
|
|
spawn_opts(opts, proc() { fail!() });
|
2014-03-09 14:58:32 -07:00
|
|
|
assert!(rx.recv().is_err());
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn yield_test() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-01-30 11:20:34 +11:00
|
|
|
for _ in range(0, 10) { task::deschedule(); }
|
2014-03-09 14:58:32 -07:00
|
|
|
tx.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spawn_children() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx2, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx3, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
tx3.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
|
|
|
tx2.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
|
|
|
tx1.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spawn_inherits() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-01-26 23:01:20 -05:00
|
|
|
spawn(proc() {
|
|
|
|
spawn(proc() {
|
2013-12-13 00:02:54 -08:00
|
|
|
let mut task: ~Task = Local::take();
|
|
|
|
match task.maybe_take_runtime::<Ops>() {
|
|
|
|
Some(ops) => {
|
2014-03-08 18:21:49 -08:00
|
|
|
task.put_runtime(ops);
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
None => fail!(),
|
|
|
|
}
|
|
|
|
Local::put(task);
|
2014-03-09 14:58:32 -07:00
|
|
|
tx.send(());
|
2014-01-26 23:01:20 -05:00
|
|
|
});
|
|
|
|
});
|
2014-03-09 14:58:32 -07:00
|
|
|
rx.recv();
|
2013-12-13 00:02:54 -08:00
|
|
|
}
|
|
|
|
}
|