2013-04-21 18:28:17 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
//! Language-level runtime services that should reasonably expected
|
|
|
|
//! to be available 'everywhere'. Local heaps, GC, unwinding,
|
|
|
|
//! local storage, and logging. Even a 'freestanding' Rust would likely want
|
|
|
|
//! to implement this.
|
|
|
|
|
2013-06-02 18:16:40 -05:00
|
|
|
use borrow;
|
2013-04-22 19:15:31 -05:00
|
|
|
use cast::transmute;
|
2013-05-24 21:35:29 -05:00
|
|
|
use libc::{c_void, uintptr_t};
|
|
|
|
use ptr;
|
|
|
|
use prelude::*;
|
2013-06-14 01:31:19 -05:00
|
|
|
use option::{Option, Some, None};
|
2013-05-19 17:45:39 -05:00
|
|
|
use rt::local::Local;
|
2013-05-07 17:57:15 -05:00
|
|
|
use rt::logging::StdErrLogger;
|
2013-05-24 21:35:29 -05:00
|
|
|
use super::local_heap::LocalHeap;
|
2013-06-14 14:17:56 -05:00
|
|
|
use rt::sched::{SchedHome, AnySched};
|
2013-06-14 01:31:19 -05:00
|
|
|
use rt::join_latch::JoinLatch;
|
2013-04-21 18:28:17 -05:00
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
pub struct Task {
|
2013-04-21 18:28:17 -05:00
|
|
|
heap: LocalHeap,
|
|
|
|
gc: GarbageCollector,
|
|
|
|
storage: LocalStorage,
|
2013-04-27 20:57:15 -05:00
|
|
|
logger: StdErrLogger,
|
2013-06-14 01:16:27 -05:00
|
|
|
unwinder: Unwinder,
|
2013-06-15 21:31:46 -05:00
|
|
|
home: Option<SchedHome>,
|
2013-06-14 01:31:19 -05:00
|
|
|
join_latch: Option<~JoinLatch>,
|
|
|
|
on_exit: Option<~fn(bool)>,
|
2013-04-22 14:54:03 -05:00
|
|
|
destroyed: bool
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GarbageCollector;
|
2013-04-22 14:54:03 -05:00
|
|
|
pub struct LocalStorage(*c_void, Option<~fn(*c_void)>);
|
2013-04-22 19:15:31 -05:00
|
|
|
|
|
|
|
pub struct Unwinder {
|
|
|
|
unwinding: bool,
|
|
|
|
}
|
2013-04-21 18:28:17 -05:00
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
impl Task {
|
2013-06-14 00:43:20 -05:00
|
|
|
pub fn new_root() -> Task {
|
2013-05-19 03:04:01 -05:00
|
|
|
Task {
|
2013-04-21 21:03:52 -05:00
|
|
|
heap: LocalHeap::new(),
|
2013-04-21 18:28:17 -05:00
|
|
|
gc: GarbageCollector,
|
2013-04-22 14:54:03 -05:00
|
|
|
storage: LocalStorage(ptr::null(), None),
|
2013-04-27 20:57:15 -05:00
|
|
|
logger: StdErrLogger,
|
2013-06-14 01:16:27 -05:00
|
|
|
unwinder: Unwinder { unwinding: false },
|
2013-06-15 21:31:46 -05:00
|
|
|
home: Some(AnySched),
|
2013-06-14 01:31:19 -05:00
|
|
|
join_latch: Some(JoinLatch::new_root()),
|
|
|
|
on_exit: None,
|
2013-04-23 17:11:28 -05:00
|
|
|
destroyed: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 00:43:20 -05:00
|
|
|
pub fn new_child(&mut self) -> Task {
|
2013-05-19 03:04:01 -05:00
|
|
|
Task {
|
2013-04-23 17:11:28 -05:00
|
|
|
heap: LocalHeap::new(),
|
|
|
|
gc: GarbageCollector,
|
|
|
|
storage: LocalStorage(ptr::null(), None),
|
2013-04-27 20:57:15 -05:00
|
|
|
logger: StdErrLogger,
|
2013-06-15 21:31:46 -05:00
|
|
|
home: Some(AnySched),
|
2013-06-14 01:16:27 -05:00
|
|
|
unwinder: Unwinder { unwinding: false },
|
2013-06-14 01:31:19 -05:00
|
|
|
join_latch: Some(self.join_latch.get_mut_ref().new_child()),
|
|
|
|
on_exit: None,
|
2013-04-22 14:54:03 -05:00
|
|
|
destroyed: false
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
|
2013-06-14 14:17:56 -05:00
|
|
|
pub fn give_home(&mut self, new_home: SchedHome) {
|
|
|
|
self.home = Some(new_home);
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:15:31 -05:00
|
|
|
pub fn run(&mut self, f: &fn()) {
|
|
|
|
// This is just an assertion that `run` was called unsafely
|
2013-05-19 03:04:01 -05:00
|
|
|
// and this instance of Task is still accessible.
|
2013-06-10 17:29:02 -05:00
|
|
|
do Local::borrow::<Task, ()> |task| {
|
2013-06-02 18:16:40 -05:00
|
|
|
assert!(borrow::ref_eq(task, self));
|
2013-04-22 19:15:31 -05:00
|
|
|
}
|
|
|
|
|
2013-06-14 01:16:27 -05:00
|
|
|
self.unwinder.try(f);
|
2013-04-22 19:15:31 -05:00
|
|
|
self.destroy();
|
2013-06-14 01:31:19 -05:00
|
|
|
|
|
|
|
// Wait for children. Possibly report the exit status.
|
|
|
|
let local_success = !self.unwinder.unwinding;
|
|
|
|
let join_latch = self.join_latch.swap_unwrap();
|
|
|
|
match self.on_exit {
|
|
|
|
Some(ref on_exit) => {
|
|
|
|
let success = join_latch.wait(local_success);
|
|
|
|
(*on_exit)(success);
|
2013-04-23 17:11:28 -05:00
|
|
|
}
|
|
|
|
None => {
|
2013-06-14 01:31:19 -05:00
|
|
|
join_latch.release(local_success);
|
2013-04-23 17:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 19:15:31 -05:00
|
|
|
}
|
|
|
|
|
2013-06-14 01:31:19 -05:00
|
|
|
/// must be called manually before finalization to clean up
|
2013-04-22 14:54:03 -05:00
|
|
|
/// thread-local resources. Some of the routines here expect
|
2013-05-19 03:04:01 -05:00
|
|
|
/// Task to be available recursively so this must be
|
|
|
|
/// called unsafely, without removing Task from
|
2013-04-22 14:54:03 -05:00
|
|
|
/// thread-local-storage.
|
2013-04-22 19:15:31 -05:00
|
|
|
fn destroy(&mut self) {
|
2013-04-22 14:54:03 -05:00
|
|
|
// This is just an assertion that `destroy` was called unsafely
|
2013-05-19 03:04:01 -05:00
|
|
|
// and this instance of Task is still accessible.
|
2013-06-10 17:29:02 -05:00
|
|
|
do Local::borrow::<Task, ()> |task| {
|
2013-06-02 18:16:40 -05:00
|
|
|
assert!(borrow::ref_eq(task, self));
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
|
|
|
match self.storage {
|
2013-04-23 17:16:04 -05:00
|
|
|
LocalStorage(ptr, Some(ref dtor)) => {
|
|
|
|
(*dtor)(ptr)
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
self.destroyed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
impl Drop for Task {
|
2013-04-22 14:54:03 -05:00
|
|
|
fn finalize(&self) { assert!(self.destroyed) }
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
|
2013-04-22 19:15:31 -05:00
|
|
|
// Just a sanity check to make sure we are catching a Rust-thrown exception
|
|
|
|
static UNWIND_TOKEN: uintptr_t = 839147;
|
|
|
|
|
|
|
|
impl Unwinder {
|
|
|
|
pub fn try(&mut self, f: &fn()) {
|
|
|
|
use sys::Closure;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let closure: Closure = transmute(f);
|
|
|
|
let code = transmute(closure.code);
|
|
|
|
let env = transmute(closure.env);
|
|
|
|
|
|
|
|
let token = rust_try(try_fn, code, env);
|
|
|
|
assert!(token == 0 || token == UNWIND_TOKEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern fn try_fn(code: *c_void, env: *c_void) {
|
|
|
|
unsafe {
|
|
|
|
let closure: Closure = Closure {
|
|
|
|
code: transmute(code),
|
|
|
|
env: transmute(env),
|
|
|
|
};
|
|
|
|
let closure: &fn() = transmute(closure);
|
|
|
|
closure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_try(f: *u8, code: *c_void, data: *c_void) -> uintptr_t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn begin_unwind(&mut self) -> ! {
|
|
|
|
self.unwinding = true;
|
|
|
|
unsafe {
|
|
|
|
rust_begin_unwind(UNWIND_TOKEN);
|
|
|
|
return transmute(());
|
|
|
|
}
|
|
|
|
extern {
|
|
|
|
fn rust_begin_unwind(token: uintptr_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:03:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use rt::test::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn local_heap() {
|
|
|
|
do run_in_newsched_task() {
|
|
|
|
let a = @5;
|
|
|
|
let b = a;
|
|
|
|
assert!(*a == 5);
|
|
|
|
assert!(*b == 5);
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tls() {
|
2013-05-08 08:02:21 -05:00
|
|
|
use local_data::*;
|
2013-04-22 14:54:03 -05:00
|
|
|
do run_in_newsched_task() {
|
|
|
|
unsafe {
|
|
|
|
fn key(_x: @~str) { }
|
|
|
|
local_data_set(key, @~"data");
|
|
|
|
assert!(*local_data_get(key).get() == ~"data");
|
2013-04-23 17:16:04 -05:00
|
|
|
fn key2(_x: @~str) { }
|
|
|
|
local_data_set(key2, @~"data");
|
|
|
|
assert!(*local_data_get(key2).get() == ~"data");
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 19:15:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwind() {
|
|
|
|
do run_in_newsched_task() {
|
2013-04-23 17:11:28 -05:00
|
|
|
let result = spawntask_try(||());
|
2013-04-22 19:15:31 -05:00
|
|
|
assert!(result.is_ok());
|
2013-04-23 17:11:28 -05:00
|
|
|
let result = spawntask_try(|| fail!());
|
2013-04-22 19:15:31 -05:00
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 20:24:37 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rng() {
|
|
|
|
do run_in_newsched_task() {
|
|
|
|
use rand::{rng, Rng};
|
2013-05-08 14:26:34 -05:00
|
|
|
let mut r = rng();
|
2013-05-06 20:24:37 -05:00
|
|
|
let _ = r.next();
|
|
|
|
}
|
|
|
|
}
|
2013-04-27 20:57:15 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn logging() {
|
|
|
|
do run_in_newsched_task() {
|
|
|
|
info!("here i am. logging in a newsched task");
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 01:12:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comm_oneshot() {
|
|
|
|
use comm::*;
|
|
|
|
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let (port, chan) = oneshot();
|
|
|
|
send_one(chan, 10);
|
|
|
|
assert!(recv_one(port) == 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comm_stream() {
|
|
|
|
use comm::*;
|
|
|
|
|
|
|
|
do run_in_newsched_task() {
|
2013-05-17 19:47:10 -05:00
|
|
|
let (port, chan) = stream();
|
2013-05-17 01:12:22 -05:00
|
|
|
chan.send(10);
|
|
|
|
assert!(port.recv() == 10);
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 01:31:19 -05:00
|
|
|
|
2013-06-20 20:26:56 -05:00
|
|
|
#[test]
|
|
|
|
fn comm_shared_chan() {
|
|
|
|
use comm::*;
|
|
|
|
|
|
|
|
do run_in_newsched_task() {
|
|
|
|
let (port, chan) = stream();
|
|
|
|
let chan = SharedChan::new(chan);
|
|
|
|
chan.send(10);
|
|
|
|
assert!(port.recv() == 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 01:31:19 -05:00
|
|
|
#[test]
|
|
|
|
fn linked_failure() {
|
|
|
|
do run_in_newsched_task() {
|
|
|
|
let res = do spawntask_try {
|
|
|
|
spawntask_random(|| fail!());
|
|
|
|
};
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 14:26:34 -05:00
|
|
|
}
|