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-06-22 03:09:06 -05:00
|
|
|
use cleanup;
|
2013-08-10 22:06:39 -05:00
|
|
|
use local_data;
|
2013-05-24 21:35:29 -05:00
|
|
|
use libc::{c_void, uintptr_t};
|
|
|
|
use prelude::*;
|
2013-06-14 01:31:19 -05:00
|
|
|
use option::{Option, Some, None};
|
2013-08-11 20:54:23 -05:00
|
|
|
use rt::borrowck;
|
|
|
|
use rt::borrowck::BorrowRecord;
|
2013-08-05 14:43:33 -05:00
|
|
|
use rt::env;
|
2013-07-01 22:24:24 -05:00
|
|
|
use rt::kill::Death;
|
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-26 18:41:00 -05:00
|
|
|
use rt::sched::{Scheduler, SchedHandle};
|
|
|
|
use rt::stack::{StackSegment, StackPool};
|
|
|
|
use rt::context::Context;
|
2013-08-03 16:43:16 -05:00
|
|
|
use unstable::finally::Finally;
|
2013-07-16 12:42:12 -05:00
|
|
|
use task::spawn::Taskgroup;
|
2013-06-26 18:41:00 -05:00
|
|
|
use cell::Cell;
|
2013-10-05 14:01:58 -05:00
|
|
|
use send_str::SendStr;
|
2013-04-21 18:28:17 -05:00
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// The Task struct represents all state associated with a rust
|
|
|
|
// task. There are at this point two primary "subtypes" of task,
|
|
|
|
// however instead of using a subtype we just have a "task_type" field
|
|
|
|
// in the struct. This contains a pointer to another struct that holds
|
|
|
|
// the type-specific state.
|
|
|
|
|
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-07-16 12:42:12 -05:00
|
|
|
taskgroup: Option<Taskgroup>,
|
2013-07-01 22:24:24 -05:00
|
|
|
death: Death,
|
2013-06-26 18:41:00 -05:00
|
|
|
destroyed: bool,
|
2013-10-05 14:01:58 -05:00
|
|
|
name: Option<SendStr>,
|
2013-07-29 15:34:08 -05:00
|
|
|
coroutine: Option<Coroutine>,
|
2013-07-19 16:25:05 -05:00
|
|
|
sched: Option<~Scheduler>,
|
2013-08-11 20:54:23 -05:00
|
|
|
task_type: TaskType,
|
|
|
|
// Dynamic borrowck debugging info
|
|
|
|
borrow_list: Option<~[BorrowRecord]>
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TaskType {
|
2013-08-18 00:59:46 -05:00
|
|
|
GreenTask(Option<SchedHome>),
|
2013-07-19 16:25:05 -05:00
|
|
|
SchedTask
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
/// A coroutine is nothing more than a (register context, stack) pair.
|
2013-06-26 18:41:00 -05:00
|
|
|
pub struct Coroutine {
|
|
|
|
/// The segment of stack on which the task is currently running or
|
|
|
|
/// if the task is blocked, on which the task will resume
|
|
|
|
/// execution.
|
2013-08-12 15:54:38 -05:00
|
|
|
current_stack_segment: StackSegment,
|
2013-06-26 18:41:00 -05:00
|
|
|
/// Always valid if the task is alive and not running.
|
|
|
|
saved_context: Context
|
|
|
|
}
|
|
|
|
|
2013-08-16 00:41:28 -05:00
|
|
|
/// Some tasks have a dedicated home scheduler that they must run on.
|
2013-06-26 18:41:00 -05:00
|
|
|
pub enum SchedHome {
|
|
|
|
AnySched,
|
|
|
|
Sched(SchedHandle)
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GarbageCollector;
|
2013-08-10 22:06:39 -05:00
|
|
|
pub struct LocalStorage(Option<local_data::Map>);
|
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-26 18:41:00 -05:00
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// A helper to build a new task using the dynamically found
|
|
|
|
// scheduler and task. Only works in GreenTask context.
|
2013-08-05 15:10:08 -05:00
|
|
|
pub fn build_homed_child(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
|
2013-07-19 16:25:05 -05:00
|
|
|
let f = Cell::new(f);
|
|
|
|
let home = Cell::new(home);
|
2013-08-08 13:38:10 -05:00
|
|
|
do Local::borrow |running_task: &mut Task| {
|
2013-07-19 16:25:05 -05:00
|
|
|
let mut sched = running_task.sched.take_unwrap();
|
|
|
|
let new_task = ~running_task.new_child_homed(&mut sched.stack_pool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size,
|
2013-07-19 16:25:05 -05:00
|
|
|
home.take(),
|
|
|
|
f.take());
|
|
|
|
running_task.sched = Some(sched);
|
|
|
|
new_task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:10:08 -05:00
|
|
|
pub fn build_child(stack_size: Option<uint>, f: ~fn()) -> ~Task {
|
|
|
|
Task::build_homed_child(stack_size, f, AnySched)
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 15:10:08 -05:00
|
|
|
pub fn build_homed_root(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
|
2013-07-19 16:25:05 -05:00
|
|
|
let f = Cell::new(f);
|
|
|
|
let home = Cell::new(home);
|
2013-08-08 13:38:10 -05:00
|
|
|
do Local::borrow |running_task: &mut Task| {
|
2013-07-19 16:25:05 -05:00
|
|
|
let mut sched = running_task.sched.take_unwrap();
|
|
|
|
let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size,
|
|
|
|
home.take(),
|
|
|
|
f.take());
|
2013-07-19 16:25:05 -05:00
|
|
|
running_task.sched = Some(sched);
|
|
|
|
new_task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:10:08 -05:00
|
|
|
pub fn build_root(stack_size: Option<uint>, f: ~fn()) -> ~Task {
|
|
|
|
Task::build_homed_root(stack_size, f, AnySched)
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_sched_task() -> Task {
|
|
|
|
Task {
|
|
|
|
heap: LocalHeap::new(),
|
|
|
|
gc: GarbageCollector,
|
2013-08-10 22:06:39 -05:00
|
|
|
storage: LocalStorage(None),
|
2013-07-19 16:25:05 -05:00
|
|
|
logger: StdErrLogger,
|
|
|
|
unwinder: Unwinder { unwinding: false },
|
|
|
|
taskgroup: None,
|
|
|
|
death: Death::new(),
|
|
|
|
destroyed: false,
|
2013-07-29 15:34:08 -05:00
|
|
|
coroutine: Some(Coroutine::empty()),
|
2013-08-01 17:08:51 -05:00
|
|
|
name: None,
|
2013-07-19 16:25:05 -05:00
|
|
|
sched: None,
|
2013-08-11 20:54:23 -05:00
|
|
|
task_type: SchedTask,
|
|
|
|
borrow_list: None
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 18:41:00 -05:00
|
|
|
pub fn new_root(stack_pool: &mut StackPool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size: Option<uint>,
|
2013-06-26 18:41:00 -05:00
|
|
|
start: ~fn()) -> Task {
|
2013-08-05 15:10:08 -05:00
|
|
|
Task::new_root_homed(stack_pool, stack_size, AnySched, start)
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_child(&mut self,
|
|
|
|
stack_pool: &mut StackPool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size: Option<uint>,
|
2013-06-26 18:41:00 -05:00
|
|
|
start: ~fn()) -> Task {
|
2013-08-05 15:10:08 -05:00
|
|
|
self.new_child_homed(stack_pool, stack_size, AnySched, start)
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_root_homed(stack_pool: &mut StackPool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size: Option<uint>,
|
2013-06-26 18:41:00 -05:00
|
|
|
home: SchedHome,
|
|
|
|
start: ~fn()) -> 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-08-10 22:06:39 -05:00
|
|
|
storage: LocalStorage(None),
|
2013-04-27 20:57:15 -05:00
|
|
|
logger: StdErrLogger,
|
2013-06-14 01:16:27 -05:00
|
|
|
unwinder: Unwinder { unwinding: false },
|
2013-07-12 21:45:19 -05:00
|
|
|
taskgroup: None,
|
2013-07-01 22:24:24 -05:00
|
|
|
death: Death::new(),
|
2013-06-26 18:41:00 -05:00
|
|
|
destroyed: false,
|
2013-07-30 18:20:59 -05:00
|
|
|
name: None,
|
2013-08-05 15:10:08 -05:00
|
|
|
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
|
2013-07-19 16:25:05 -05:00
|
|
|
sched: None,
|
2013-08-18 00:59:46 -05:00
|
|
|
task_type: GreenTask(Some(home)),
|
2013-08-11 20:54:23 -05:00
|
|
|
borrow_list: None
|
2013-04-23 17:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 18:41:00 -05:00
|
|
|
pub fn new_child_homed(&mut self,
|
|
|
|
stack_pool: &mut StackPool,
|
2013-08-05 15:10:08 -05:00
|
|
|
stack_size: Option<uint>,
|
2013-06-26 18:41:00 -05:00
|
|
|
home: SchedHome,
|
|
|
|
start: ~fn()) -> Task {
|
2013-05-19 03:04:01 -05:00
|
|
|
Task {
|
2013-04-23 17:11:28 -05:00
|
|
|
heap: LocalHeap::new(),
|
|
|
|
gc: GarbageCollector,
|
2013-08-10 22:06:39 -05:00
|
|
|
storage: LocalStorage(None),
|
2013-04-27 20:57:15 -05:00
|
|
|
logger: StdErrLogger,
|
2013-06-14 01:16:27 -05:00
|
|
|
unwinder: Unwinder { unwinding: false },
|
2013-07-12 21:45:19 -05:00
|
|
|
taskgroup: None,
|
2013-07-01 22:24:24 -05:00
|
|
|
// FIXME(#7544) make watching optional
|
|
|
|
death: self.death.new_child(),
|
2013-06-26 18:41:00 -05:00
|
|
|
destroyed: false,
|
2013-07-30 18:20:59 -05:00
|
|
|
name: None,
|
2013-08-05 15:10:08 -05:00
|
|
|
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
|
2013-07-19 16:25:05 -05:00
|
|
|
sched: None,
|
2013-08-18 00:59:46 -05:00
|
|
|
task_type: GreenTask(Some(home)),
|
2013-08-11 20:54:23 -05:00
|
|
|
borrow_list: None
|
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) {
|
2013-07-19 16:25:05 -05:00
|
|
|
match self.task_type {
|
|
|
|
GreenTask(ref mut home) => {
|
2013-08-18 00:59:46 -05:00
|
|
|
*home = Some(new_home);
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
SchedTask => {
|
|
|
|
rtabort!("type error: used SchedTask as GreenTask");
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 14:17:56 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
pub fn take_unwrap_home(&mut self) -> SchedHome {
|
|
|
|
match self.task_type {
|
|
|
|
GreenTask(ref mut home) => {
|
|
|
|
let out = home.take_unwrap();
|
2013-08-18 00:59:46 -05:00
|
|
|
return out;
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
SchedTask => {
|
|
|
|
rtabort!("type error: used SchedTask as GreenTask");
|
|
|
|
}
|
2013-04-22 19:15:31 -05:00
|
|
|
}
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, f: &fn()) {
|
2013-09-27 19:02:31 -05:00
|
|
|
rtdebug!("run called on task: {}", borrow::to_uint(self));
|
2013-04-22 19:15:31 -05:00
|
|
|
|
2013-08-03 16:43:16 -05:00
|
|
|
// The only try/catch block in the world. Attempt to run the task's
|
|
|
|
// client-specified code and catch any failures.
|
|
|
|
do self.unwinder.try {
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2013-08-03 16:43:16 -05:00
|
|
|
// Run the task main function, then do some cleanup.
|
|
|
|
do f.finally {
|
2013-08-10 22:06:39 -05:00
|
|
|
// First, destroy task-local storage. This may run user dtors.
|
|
|
|
//
|
2013-08-05 02:32:46 -05:00
|
|
|
// FIXME #8302: Dear diary. I'm so tired and confused.
|
|
|
|
// There's some interaction in rustc between the box
|
|
|
|
// annihilator and the TLS dtor by which TLS is
|
|
|
|
// accessed from annihilated box dtors *after* TLS is
|
|
|
|
// destroyed. Somehow setting TLS back to null, as the
|
|
|
|
// old runtime did, makes this work, but I don't currently
|
|
|
|
// understand how. I would expect that, if the annihilator
|
|
|
|
// reinvokes TLS while TLS is uninitialized, that
|
|
|
|
// TLS would be reinitialized but never destroyed,
|
|
|
|
// but somehow this works. I have no idea what's going
|
|
|
|
// on but this seems to make things magically work. FML.
|
2013-08-10 22:06:39 -05:00
|
|
|
//
|
|
|
|
// (added after initial comment) A possible interaction here is
|
|
|
|
// that the destructors for the objects in TLS themselves invoke
|
|
|
|
// TLS, or possibly some destructors for those objects being
|
|
|
|
// annihilated invoke TLS. Sadly these two operations seemed to
|
|
|
|
// be intertwined, and miraculously work for now...
|
|
|
|
self.storage.take();
|
2013-08-05 02:32:46 -05:00
|
|
|
|
2013-08-03 16:43:16 -05:00
|
|
|
// Destroy remaining boxes. Also may run user dtors.
|
|
|
|
unsafe { cleanup::annihilate(); }
|
2013-04-23 17:16:04 -05:00
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
2013-06-22 03:09:06 -05:00
|
|
|
|
2013-08-11 20:54:23 -05:00
|
|
|
// Cleanup the dynamic borrowck debugging info
|
|
|
|
borrowck::clear_task_borrow_list();
|
|
|
|
|
2013-08-12 13:54:09 -05:00
|
|
|
// NB. We pass the taskgroup into death so that it can be dropped while
|
|
|
|
// the unkillable counter is set. This is necessary for when the
|
|
|
|
// taskgroup destruction code drops references on KillHandles, which
|
|
|
|
// might require using unkillable (to synchronize with an unwrapper).
|
2013-08-03 16:43:16 -05:00
|
|
|
self.death.collect_failure(!self.unwinder.unwinding, self.taskgroup.take());
|
2013-04-22 14:54:03 -05:00
|
|
|
self.destroyed = true;
|
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// New utility functions for homes.
|
2013-06-26 18:41:00 -05:00
|
|
|
|
|
|
|
pub fn is_home_no_tls(&self, sched: &~Scheduler) -> bool {
|
2013-07-19 16:25:05 -05:00
|
|
|
match self.task_type {
|
2013-08-18 00:59:46 -05:00
|
|
|
GreenTask(Some(AnySched)) => { false }
|
|
|
|
GreenTask(Some(Sched(SchedHandle { sched_id: ref id, _}))) => {
|
2013-06-26 18:41:00 -05:00
|
|
|
*id == sched.sched_id()
|
|
|
|
}
|
2013-07-19 16:25:05 -05:00
|
|
|
GreenTask(None) => {
|
|
|
|
rtabort!("task without home");
|
|
|
|
}
|
|
|
|
SchedTask => {
|
|
|
|
// Awe yea
|
|
|
|
rtabort!("type error: expected: GreenTask, found: SchedTask");
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn homed(&self) -> bool {
|
2013-07-19 16:25:05 -05:00
|
|
|
match self.task_type {
|
2013-08-18 00:59:46 -05:00
|
|
|
GreenTask(Some(AnySched)) => { false }
|
|
|
|
GreenTask(Some(Sched(SchedHandle { _ }))) => { true }
|
2013-07-19 16:25:05 -05:00
|
|
|
GreenTask(None) => {
|
|
|
|
rtabort!("task without home");
|
|
|
|
}
|
|
|
|
SchedTask => {
|
|
|
|
rtabort!("type error: expected: GreenTask, found: SchedTask");
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// Grab both the scheduler and the task from TLS and check if the
|
|
|
|
// task is executing on an appropriate scheduler.
|
|
|
|
pub fn on_appropriate_sched() -> bool {
|
2013-08-08 13:38:10 -05:00
|
|
|
do Local::borrow |task: &mut Task| {
|
2013-07-19 16:25:05 -05:00
|
|
|
let sched_id = task.sched.get_ref().sched_id();
|
|
|
|
let sched_run_anything = task.sched.get_ref().run_anything;
|
|
|
|
match task.task_type {
|
2013-08-18 00:59:46 -05:00
|
|
|
GreenTask(Some(AnySched)) => {
|
2013-07-19 16:25:05 -05:00
|
|
|
rtdebug!("anysched task in sched check ****");
|
|
|
|
sched_run_anything
|
|
|
|
}
|
2013-08-18 00:59:46 -05:00
|
|
|
GreenTask(Some(Sched(SchedHandle { sched_id: ref id, _ }))) => {
|
2013-07-19 16:25:05 -05:00
|
|
|
rtdebug!("homed task in sched check ****");
|
|
|
|
*id == sched_id
|
|
|
|
}
|
|
|
|
GreenTask(None) => {
|
|
|
|
rtabort!("task without home");
|
|
|
|
}
|
|
|
|
SchedTask => {
|
|
|
|
rtabort!("type error: expected: GreenTask, found: SchedTask");
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 18:41:00 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 14:54:03 -05:00
|
|
|
}
|
|
|
|
|
2013-05-19 03:04:01 -05:00
|
|
|
impl Drop for Task {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-09-27 19:02:31 -05:00
|
|
|
rtdebug!("called drop for a task: {}", borrow::to_uint(self));
|
2013-08-04 01:28:47 -05:00
|
|
|
rtassert!(self.destroyed)
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
2013-04-21 18:28:17 -05:00
|
|
|
}
|
|
|
|
|
2013-06-26 18:41:00 -05:00
|
|
|
// Coroutines represent nothing more than a context and a stack
|
|
|
|
// segment.
|
|
|
|
|
|
|
|
impl Coroutine {
|
|
|
|
|
2013-08-05 15:10:08 -05:00
|
|
|
pub fn new(stack_pool: &mut StackPool, stack_size: Option<uint>, start: ~fn()) -> Coroutine {
|
|
|
|
let stack_size = match stack_size {
|
|
|
|
Some(size) => size,
|
|
|
|
None => env::min_stack()
|
|
|
|
};
|
2013-06-26 18:41:00 -05:00
|
|
|
let start = Coroutine::build_start_wrapper(start);
|
2013-08-05 14:43:33 -05:00
|
|
|
let mut stack = stack_pool.take_segment(stack_size);
|
2013-06-26 18:41:00 -05:00
|
|
|
let initial_context = Context::new(start, &mut stack);
|
|
|
|
Coroutine {
|
|
|
|
current_stack_segment: stack,
|
|
|
|
saved_context: initial_context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
pub fn empty() -> Coroutine {
|
|
|
|
Coroutine {
|
|
|
|
current_stack_segment: StackSegment::new(0),
|
|
|
|
saved_context: Context::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 18:41:00 -05:00
|
|
|
fn build_start_wrapper(start: ~fn()) -> ~fn() {
|
|
|
|
let start_cell = Cell::new(start);
|
|
|
|
let wrapper: ~fn() = || {
|
|
|
|
// First code after swap to this new context. Run our
|
|
|
|
// cleanup job.
|
|
|
|
unsafe {
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// Again - might work while safe, or it might not.
|
2013-08-08 13:38:10 -05:00
|
|
|
do Local::borrow |sched: &mut Scheduler| {
|
2013-08-15 21:46:23 -05:00
|
|
|
sched.run_cleanup_job();
|
2013-07-19 16:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// To call the run method on a task we need a direct
|
|
|
|
// reference to it. The task is in TLS, so we can
|
|
|
|
// simply unsafe_borrow it to get this reference. We
|
|
|
|
// need to still have the task in TLS though, so we
|
|
|
|
// need to unsafe_borrow.
|
2013-08-08 13:38:10 -05:00
|
|
|
let task: *mut Task = Local::unsafe_borrow();
|
2013-06-26 18:41:00 -05:00
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
do (*task).run {
|
2013-06-26 18:41:00 -05:00
|
|
|
// N.B. Removing `start` from the start wrapper
|
|
|
|
// closure by emptying a cell is critical for
|
|
|
|
// correctness. The ~Task pointer, and in turn the
|
|
|
|
// closure used to initialize the first call
|
|
|
|
// frame, is destroyed in the scheduler context,
|
|
|
|
// not task context. So any captured closures must
|
|
|
|
// not contain user-definable dtors that expect to
|
|
|
|
// be in task context. By moving `start` out of
|
|
|
|
// the closure, all the user code goes our of
|
|
|
|
// scope while the task is still running.
|
|
|
|
let start = start_cell.take();
|
|
|
|
start();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-07-19 16:25:05 -05:00
|
|
|
// We remove the sched from the Task in TLS right now.
|
2013-08-08 13:38:10 -05:00
|
|
|
let sched: ~Scheduler = Local::take();
|
2013-07-19 16:25:05 -05:00
|
|
|
// ... allowing us to give it away when performing a
|
|
|
|
// scheduling operation.
|
|
|
|
sched.terminate_current_task()
|
2013-06-26 18:41:00 -05:00
|
|
|
};
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy coroutine and try to reuse stack segment.
|
2013-07-29 15:34:08 -05:00
|
|
|
pub fn recycle(self, stack_pool: &mut StackPool) {
|
2013-06-26 18:41:00 -05:00
|
|
|
match self {
|
2013-07-29 15:34:08 -05:00
|
|
|
Coroutine { current_stack_segment, _ } => {
|
2013-06-26 18:41:00 -05:00
|
|
|
stack_pool.give_segment(current_stack_segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()) {
|
2013-07-21 19:20:52 -05:00
|
|
|
use unstable::raw::Closure;
|
2013-04-22 19:15:31 -05:00
|
|
|
|
|
|
|
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]
|
2013-08-21 08:31:02 -05:00
|
|
|
fn rust_try(f: extern "C" fn(*c_void, *c_void),
|
|
|
|
code: *c_void,
|
|
|
|
data: *c_void) -> uintptr_t;
|
2013-04-22 19:15:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn begin_unwind(&mut self) -> ! {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
|
2013-04-22 19:15:31 -05:00
|
|
|
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-07-11 00:14:40 -05:00
|
|
|
use local_data;
|
2013-04-22 14:54:03 -05:00
|
|
|
do run_in_newsched_task() {
|
2013-09-17 01:34:40 -05:00
|
|
|
local_data_key!(key: @~str)
|
2013-07-12 03:38:44 -05:00
|
|
|
local_data::set(key, @~"data");
|
2013-09-20 01:08:47 -05:00
|
|
|
assert!(*local_data::get(key, |k| k.map(|k| *k)).unwrap() == ~"data");
|
2013-09-17 01:34:40 -05:00
|
|
|
local_data_key!(key2: @~str)
|
2013-07-12 03:38:44 -05:00
|
|
|
local_data::set(key2, @~"data");
|
2013-09-20 01:08:47 -05:00
|
|
|
assert!(*local_data::get(key2, |k| k.map(|k| *k)).unwrap() == ~"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-06-26 18:41:00 -05:00
|
|
|
rtdebug!("trying first assert");
|
2013-04-22 19:15:31 -05:00
|
|
|
assert!(result.is_ok());
|
2013-09-27 19:02:31 -05:00
|
|
|
let result = spawntask_try(|| fail2!());
|
2013-06-26 18:41:00 -05:00
|
|
|
rtdebug!("trying second assert");
|
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-09-21 07:06:50 -05:00
|
|
|
let _ = r.next_u32();
|
2013-05-06 20:24:37 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-27 20:57:15 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn logging() {
|
|
|
|
do run_in_newsched_task() {
|
2013-09-27 19:02:31 -05:00
|
|
|
info2!("here i am. logging in a newsched task");
|
2013-04-27 20:57:15 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-17 01:12:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comm_oneshot() {
|
|
|
|
use comm::*;
|
|
|
|
|
|
|
|
do run_in_newsched_task {
|
|
|
|
let (port, chan) = oneshot();
|
2013-08-01 01:12:20 -05:00
|
|
|
chan.send(10);
|
|
|
|
assert!(port.recv() == 10);
|
2013-05-17 01:12:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2013-09-27 19:02:31 -05:00
|
|
|
spawntask_random(|| fail2!());
|
2013-06-14 01:31:19 -05:00
|
|
|
};
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
}
|
2013-06-22 03:09:06 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn heap_cycles() {
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
|
|
|
|
do run_in_newsched_task {
|
|
|
|
struct List {
|
|
|
|
next: Option<@mut List>,
|
|
|
|
}
|
|
|
|
|
|
|
|
let a = @mut List { next: None };
|
|
|
|
let b = @mut List { next: Some(a) };
|
|
|
|
|
|
|
|
a.next = Some(b);
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 16:01:59 -05:00
|
|
|
|
|
|
|
// XXX: This is a copy of test_future_result in std::task.
|
|
|
|
// It can be removed once the scheduler is turned on by default.
|
|
|
|
#[test]
|
|
|
|
fn future_result() {
|
|
|
|
do run_in_newsched_task {
|
|
|
|
use option::{Some, None};
|
|
|
|
use task::*;
|
|
|
|
|
|
|
|
let mut result = None;
|
|
|
|
let mut builder = task();
|
|
|
|
builder.future_result(|r| result = Some(r));
|
|
|
|
do builder.spawn {}
|
|
|
|
assert_eq!(result.unwrap().recv(), Success);
|
|
|
|
|
|
|
|
result = None;
|
|
|
|
let mut builder = task();
|
|
|
|
builder.future_result(|r| result = Some(r));
|
|
|
|
builder.unlinked();
|
|
|
|
do builder.spawn {
|
2013-09-27 19:02:31 -05:00
|
|
|
fail2!();
|
2013-06-23 16:01:59 -05:00
|
|
|
}
|
|
|
|
assert_eq!(result.unwrap().recv(), Failure);
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 14:26:34 -05:00
|
|
|
}
|