1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Language-level runtime services that should reasonably expected
12 //! to be available 'everywhere'. Local heaps, GC, unwinding,
13 //! local storage, and logging. Even a 'freestanding' Rust would likely want
14 //! to implement this.
16 use super::local_heap::LocalHeap;
24 use libc::{c_void, uintptr_t, c_char, size_t};
26 use option::{Option, Some, None};
27 use rt::borrowck::BorrowRecord;
30 use rt::context::Context;
34 use rt::logging::StdErrLogger;
35 use rt::sched::{Scheduler, SchedHandle};
36 use rt::stack::{StackSegment, StackPool};
37 use send_str::SendStr;
39 use unstable::finally::Finally;
40 use unstable::mutex::Mutex;
42 // The Task struct represents all state associated with a rust
43 // task. There are at this point two primary "subtypes" of task,
44 // however instead of using a subtype we just have a "task_type" field
45 // in the struct. This contains a pointer to another struct that holds
46 // the type-specific state.
50 priv gc: GarbageCollector,
51 storage: LocalStorage,
52 logger: Option<StdErrLogger>,
56 name: Option<SendStr>,
57 coroutine: Option<Coroutine>,
58 sched: Option<~Scheduler>,
60 // Dynamic borrowck debugging info
61 borrow_list: Option<~[BorrowRecord]>,
62 stdout_handle: Option<~Writer>,
64 // See the comments in the scheduler about why this is necessary
65 nasty_deschedule_lock: Mutex,
69 GreenTask(Option<SchedHome>),
73 /// A coroutine is nothing more than a (register context, stack) pair.
74 pub struct Coroutine {
75 /// The segment of stack on which the task is currently running or
76 /// if the task is blocked, on which the task will resume
79 /// Servo needs this to be public in order to tell SpiderMonkey
80 /// about the stack bounds.
81 current_stack_segment: StackSegment,
82 /// Always valid if the task is alive and not running.
83 saved_context: Context
86 /// Some tasks have a dedicated home scheduler that they must run on.
92 pub struct GarbageCollector;
93 pub struct LocalStorage(Option<local_data::Map>);
101 fn result(&mut self) -> TaskResult {
103 Err(self.cause.take().unwrap())
112 // A helper to build a new task using the dynamically found
113 // scheduler and task. Only works in GreenTask context.
114 pub fn build_homed_child(stack_size: Option<uint>,
118 let mut running_task = Local::borrow(None::<Task>);
119 let mut sched = running_task.get().sched.take_unwrap();
120 let new_task = ~running_task.get()
121 .new_child_homed(&mut sched.stack_pool,
125 running_task.get().sched = Some(sched);
129 pub fn build_child(stack_size: Option<uint>, f: proc()) -> ~Task {
130 Task::build_homed_child(stack_size, f, AnySched)
133 pub fn build_homed_root(stack_size: Option<uint>,
137 let mut running_task = Local::borrow(None::<Task>);
138 let mut sched = running_task.get().sched.take_unwrap();
139 let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
143 running_task.get().sched = Some(sched);
147 pub fn build_root(stack_size: Option<uint>, f: proc()) -> ~Task {
148 Task::build_homed_root(stack_size, f, AnySched)
151 pub fn new_sched_task() -> Task {
153 heap: LocalHeap::new(),
154 gc: GarbageCollector,
155 storage: LocalStorage(None),
157 unwinder: Unwinder { unwinding: false, cause: None },
160 coroutine: Some(Coroutine::empty()),
163 task_type: SchedTask,
166 nasty_deschedule_lock: unsafe { Mutex::new() },
170 pub fn new_root(stack_pool: &mut StackPool,
171 stack_size: Option<uint>,
172 start: proc()) -> Task {
173 Task::new_root_homed(stack_pool, stack_size, AnySched, start)
176 pub fn new_child(&mut self,
177 stack_pool: &mut StackPool,
178 stack_size: Option<uint>,
179 start: proc()) -> Task {
180 self.new_child_homed(stack_pool, stack_size, AnySched, start)
183 pub fn new_root_homed(stack_pool: &mut StackPool,
184 stack_size: Option<uint>,
186 start: proc()) -> Task {
188 heap: LocalHeap::new(),
189 gc: GarbageCollector,
190 storage: LocalStorage(None),
192 unwinder: Unwinder { unwinding: false, cause: None },
196 coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
198 task_type: GreenTask(Some(home)),
201 nasty_deschedule_lock: unsafe { Mutex::new() },
205 pub fn new_child_homed(&mut self,
206 stack_pool: &mut StackPool,
207 stack_size: Option<uint>,
209 start: proc()) -> Task {
211 heap: LocalHeap::new(),
212 gc: GarbageCollector,
213 storage: LocalStorage(None),
215 unwinder: Unwinder { unwinding: false, cause: None },
219 coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
221 task_type: GreenTask(Some(home)),
224 nasty_deschedule_lock: unsafe { Mutex::new() },
228 pub fn give_home(&mut self, new_home: SchedHome) {
229 match self.task_type {
230 GreenTask(ref mut home) => {
231 *home = Some(new_home);
234 rtabort!("type error: used SchedTask as GreenTask");
239 pub fn take_unwrap_home(&mut self) -> SchedHome {
240 match self.task_type {
241 GreenTask(ref mut home) => {
242 let out = home.take_unwrap();
246 rtabort!("type error: used SchedTask as GreenTask");
251 pub fn run(&mut self, f: ||) {
252 rtdebug!("run called on task: {}", borrow::to_uint(self));
254 // The only try/catch block in the world. Attempt to run the task's
255 // client-specified code and catch any failures.
256 self.unwinder.try(|| {
258 // Run the task main function, then do some cleanup.
261 // First, destroy task-local storage. This may run user dtors.
263 // FIXME #8302: Dear diary. I'm so tired and confused.
264 // There's some interaction in rustc between the box
265 // annihilator and the TLS dtor by which TLS is
266 // accessed from annihilated box dtors *after* TLS is
267 // destroyed. Somehow setting TLS back to null, as the
268 // old runtime did, makes this work, but I don't currently
269 // understand how. I would expect that, if the annihilator
270 // reinvokes TLS while TLS is uninitialized, that
271 // TLS would be reinitialized but never destroyed,
272 // but somehow this works. I have no idea what's going
273 // on but this seems to make things magically work. FML.
275 // (added after initial comment) A possible interaction here is
276 // that the destructors for the objects in TLS themselves invoke
277 // TLS, or possibly some destructors for those objects being
278 // annihilated invoke TLS. Sadly these two operations seemed to
279 // be intertwined, and miraculously work for now...
282 // Destroy remaining boxes. Also may run user dtors.
283 unsafe { cleanup::annihilate(); }
285 // Finally flush and destroy any output handles which the task
286 // owns. There are no boxes here, and no user destructors should
287 // run after this any more.
288 match self.stdout_handle.take() {
290 let mut handle = handle;
299 // Cleanup the dynamic borrowck debugging info
300 borrowck::clear_task_borrow_list();
302 self.death.collect_failure(self.unwinder.result());
303 self.destroyed = true;
306 // New utility functions for homes.
308 pub fn is_home_no_tls(&self, sched: &~Scheduler) -> bool {
309 match self.task_type {
310 GreenTask(Some(AnySched)) => { false }
311 GreenTask(Some(Sched(SchedHandle { sched_id: ref id, .. }))) => {
312 *id == sched.sched_id()
315 rtabort!("task without home");
319 rtabort!("type error: expected: GreenTask, found: SchedTask");
324 pub fn homed(&self) -> bool {
325 match self.task_type {
326 GreenTask(Some(AnySched)) => { false }
327 GreenTask(Some(Sched(SchedHandle { .. }))) => { true }
329 rtabort!("task without home");
332 rtabort!("type error: expected: GreenTask, found: SchedTask");
337 // Grab both the scheduler and the task from TLS and check if the
338 // task is executing on an appropriate scheduler.
339 pub fn on_appropriate_sched() -> bool {
340 let mut task = Local::borrow(None::<Task>);
341 let sched_id = task.get().sched.get_ref().sched_id();
342 let sched_run_anything = task.get().sched.get_ref().run_anything;
343 match task.get().task_type {
344 GreenTask(Some(AnySched)) => {
345 rtdebug!("anysched task in sched check ****");
348 GreenTask(Some(Sched(SchedHandle { sched_id: ref id, ..}))) => {
349 rtdebug!("homed task in sched check ****");
353 rtabort!("task without home");
356 rtabort!("type error: expected: GreenTask, found: SchedTask");
364 rtdebug!("called drop for a task: {}", borrow::to_uint(self));
365 rtassert!(self.destroyed);
367 unsafe { self.nasty_deschedule_lock.destroy(); }
371 // Coroutines represent nothing more than a context and a stack
376 pub fn new(stack_pool: &mut StackPool,
377 stack_size: Option<uint>,
380 let stack_size = match stack_size {
382 None => env::min_stack()
384 let start = Coroutine::build_start_wrapper(start);
385 let mut stack = stack_pool.take_segment(stack_size);
386 let initial_context = Context::new(start, &mut stack);
388 current_stack_segment: stack,
389 saved_context: initial_context
393 pub fn empty() -> Coroutine {
395 current_stack_segment: StackSegment::new(0),
396 saved_context: Context::empty()
400 fn build_start_wrapper(start: proc()) -> proc() {
401 let wrapper: proc() = proc() {
402 // First code after swap to this new context. Run our
406 // Again - might work while safe, or it might not.
408 let mut sched = Local::borrow(None::<Scheduler>);
409 sched.get().run_cleanup_job();
412 // To call the run method on a task we need a direct
413 // reference to it. The task is in TLS, so we can
414 // simply unsafe_borrow it to get this reference. We
415 // need to still have the task in TLS though, so we
416 // need to unsafe_borrow.
417 let task: *mut Task = Local::unsafe_borrow();
419 let mut start_cell = Some(start);
421 // N.B. Removing `start` from the start wrapper
422 // closure by emptying a cell is critical for
423 // correctness. The ~Task pointer, and in turn the
424 // closure used to initialize the first call
425 // frame, is destroyed in the scheduler context,
426 // not task context. So any captured closures must
427 // not contain user-definable dtors that expect to
428 // be in task context. By moving `start` out of
429 // the closure, all the user code goes our of
430 // scope while the task is still running.
431 let start = start_cell.take_unwrap();
436 // We remove the sched from the Task in TLS right now.
437 let sched: ~Scheduler = Local::take();
438 // ... allowing us to give it away when performing a
439 // scheduling operation.
440 sched.terminate_current_task()
445 /// Destroy coroutine and try to reuse stack segment.
446 pub fn recycle(self, stack_pool: &mut StackPool) {
448 Coroutine { current_stack_segment, .. } => {
449 stack_pool.give_segment(current_stack_segment);
457 // Just a sanity check to make sure we are catching a Rust-thrown exception
458 static UNWIND_TOKEN: uintptr_t = 839147;
461 pub fn try(&mut self, f: ||) {
462 use unstable::raw::Closure;
465 let closure: Closure = transmute(f);
466 let code = transmute(closure.code);
467 let env = transmute(closure.env);
469 let token = rust_try(try_fn, code, env);
470 assert!(token == 0 || token == UNWIND_TOKEN);
473 extern fn try_fn(code: *c_void, env: *c_void) {
475 let closure: Closure = Closure {
476 code: transmute(code),
479 let closure: || = transmute(closure);
485 fn rust_try(f: extern "C" fn(*c_void, *c_void),
487 data: *c_void) -> uintptr_t;
491 pub fn begin_unwind(&mut self, cause: ~Any) -> ! {
492 self.unwinding = true;
493 self.cause = Some(cause);
495 rust_begin_unwind(UNWIND_TOKEN);
496 return transmute(());
499 fn rust_begin_unwind(token: uintptr_t);
504 /// This function is invoked from rust's current __morestack function. Segmented
505 /// stacks are currently not enabled as segmented stacks, but rather one giant
506 /// stack segment. This means that whenever we run out of stack, we want to
507 /// truly consider it to be stack overflow rather than allocating a new stack.
508 #[no_mangle] // - this is called from C code
509 #[no_split_stack] // - it would be sad for this function to trigger __morestack
510 #[doc(hidden)] // - Function must be `pub` to get exported, but it's
511 // irrelevant for documentation purposes.
512 #[cfg(not(test))] // in testing, use the original libstd's version
513 pub extern "C" fn rust_stack_exhausted() {
514 use rt::in_green_task_context;
516 use rt::local::Local;
517 use unstable::intrinsics;
520 // We're calling this function because the stack just ran out. We need
521 // to call some other rust functions, but if we invoke the functions
522 // right now it'll just trigger this handler being called again. In
523 // order to alleviate this, we move the stack limit to be inside of the
524 // red zone that was allocated for exactly this reason.
525 let limit = context::get_sp_limit();
526 context::record_sp_limit(limit - context::RED_ZONE / 2);
528 // This probably isn't the best course of action. Ideally one would want
529 // to unwind the stack here instead of just aborting the entire process.
530 // This is a tricky problem, however. There's a few things which need to
533 // 1. We're here because of a stack overflow, yet unwinding will run
534 // destructors and hence arbitrary code. What if that code overflows
535 // the stack? One possibility is to use the above allocation of an
536 // extra 10k to hope that we don't hit the limit, and if we do then
537 // abort the whole program. Not the best, but kind of hard to deal
538 // with unless we want to switch stacks.
540 // 2. LLVM will optimize functions based on whether they can unwind or
541 // not. It will flag functions with 'nounwind' if it believes that
542 // the function cannot trigger unwinding, but if we do unwind on
543 // stack overflow then it means that we could unwind in any function
544 // anywhere. We would have to make sure that LLVM only places the
545 // nounwind flag on functions which don't call any other functions.
547 // 3. The function that overflowed may have owned arguments. These
548 // arguments need to have their destructors run, but we haven't even
549 // begun executing the function yet, so unwinding will not run the
550 // any landing pads for these functions. If this is ignored, then
551 // the arguments will just be leaked.
553 // Exactly what to do here is a very delicate topic, and is possibly
554 // still up in the air for what exactly to do. Some relevant issues:
556 // #3555 - out-of-stack failure leaks arguments
557 // #3695 - should there be a stack limit?
558 // #9855 - possible strategies which could be taken
559 // #9854 - unwinding on windows through __morestack has never worked
560 // #2361 - possible implementation of not using landing pads
562 if in_green_task_context() {
563 let mut task = Local::borrow(None::<Task>);
567 .map(|n| n.as_slice())
568 .unwrap_or("<unnamed>");
570 // See the message below for why this is not emitted to the
571 // task's logger. This has the additional conundrum of the
572 // logger may not be initialized just yet, meaning that an FFI
573 // call would happen to initialized it (calling out to libuv),
574 // and the FFI call needs 2MB of stack when we just ran out.
575 rterrln!("task '{}' has overflowed its stack", n);
577 rterrln!("stack overflow in non-task context");
584 /// This is the entry point of unwinding for things like lang items and such.
585 /// The arguments are normally generated by the compiler, and need to
586 /// have static lifetimes.
587 pub fn begin_unwind_raw(msg: *c_char, file: *c_char, line: size_t) -> ! {
592 fn static_char_ptr(p: *c_char) -> &'static str {
593 let s = unsafe { CString::new(p, false) };
595 Some(s) => unsafe { transmute::<&str, &'static str>(s) },
596 None => rtabort!("message wasn't utf8?")
600 let msg = static_char_ptr(msg);
601 let file = static_char_ptr(file);
603 begin_unwind(msg, file, line as uint)
606 /// This is the entry point of unwinding for fail!() and assert!().
607 pub fn begin_unwind<M: Any + Send>(msg: M, file: &'static str, line: uint) -> ! {
609 use rt::in_green_task_context;
610 use rt::local::Local;
613 use unstable::intrinsics;
617 // Note that this should be the only allocation performed in this block.
618 // Currently this means that fail!() on OOM will invoke this code path,
619 // but then again we're not really ready for failing on OOM anyway. If
620 // we do start doing this, then we should propagate this allocation to
621 // be performed in the parent of this task instead of the task that's
623 let msg = ~msg as ~Any;
626 //let msg: &Any = msg;
627 let msg_s = match msg.as_ref::<&'static str>() {
629 None => match msg.as_ref::<~str>() {
630 Some(s) => s.as_slice(),
635 if !in_green_task_context() {
636 rterrln!("failed in non-task context at '{}', {}:{}",
641 task = Local::unsafe_borrow();
642 let n = (*task).name.as_ref().map(|n| n.as_slice()).unwrap_or("<unnamed>");
644 // XXX: this should no get forcibly printed to the console, this should
645 // either be sent to the parent task (ideally), or get printed to
646 // the task's logger. Right now the logger is actually a uvio
647 // instance, which uses unkillable blocks internally for various
648 // reasons. This will cause serious trouble if the task is failing
649 // due to mismanagment of its own kill flag, so calling our own
650 // logger in its current state is a bit of a problem.
652 rterrln!("task '{}' failed at '{}', {}:{}", n, msg_s, file, line);
654 if (*task).unwinder.unwinding {
655 rtabort!("unwinding again");
659 (*task).unwinder.begin_unwind(msg);
671 do run_in_newsched_task() {
682 do run_in_newsched_task() {
683 local_data_key!(key: @~str)
684 local_data::set(key, @~"data");
685 assert!(*local_data::get(key, |k| k.map(|k| *k)).unwrap() == ~"data");
686 local_data_key!(key2: @~str)
687 local_data::set(key2, @~"data");
688 assert!(*local_data::get(key2, |k| k.map(|k| *k)).unwrap() == ~"data");
694 do run_in_newsched_task() {
695 let result = spawntask_try(proc()());
696 rtdebug!("trying first assert");
697 assert!(result.is_ok());
698 let result = spawntask_try(proc() fail!());
699 rtdebug!("trying second assert");
700 assert!(result.is_err());
706 do run_in_uv_task() {
707 use rand::{rng, Rng};
709 let _ = r.next_u32();
715 do run_in_uv_task() {
716 info!("here i am. logging in a newsched task");
722 do run_in_newsched_task() {
723 let (port, chan) = Chan::new();
725 assert!(port.recv() == 10);
730 fn comm_shared_chan() {
731 do run_in_newsched_task() {
732 let (port, chan) = SharedChan::new();
734 assert!(port.recv() == 10);
740 use option::{Option, Some, None};
742 do run_in_newsched_task {
744 next: Option<@mut List>,
747 let a = @mut List { next: None };
748 let b = @mut List { next: Some(a) };
756 fn test_begin_unwind() { begin_unwind("cause", file!(), line!()) }