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.
20 use libc::{c_void, uintptr_t};
22 use option::{Option, Some, None};
24 use rt::borrowck::BorrowRecord;
28 use rt::logging::StdErrLogger;
29 use super::local_heap::LocalHeap;
30 use rt::sched::{Scheduler, SchedHandle};
31 use rt::stack::{StackSegment, StackPool};
32 use rt::context::Context;
33 use unstable::finally::Finally;
34 use task::spawn::Taskgroup;
37 // The Task struct represents all state associated with a rust
38 // task. There are at this point two primary "subtypes" of task,
39 // however instead of using a subtype we just have a "task_type" field
40 // in the struct. This contains a pointer to another struct that holds
41 // the type-specific state.
46 storage: LocalStorage,
49 taskgroup: Option<Taskgroup>,
52 // FIXME(#6874/#7599) use StringRef to save on allocations
54 coroutine: Option<Coroutine>,
55 sched: Option<~Scheduler>,
57 // Dynamic borrowck debugging info
58 borrow_list: Option<~[BorrowRecord]>
62 GreenTask(Option<SchedHome>),
66 /// A coroutine is nothing more than a (register context, stack) pair.
67 pub struct Coroutine {
68 /// The segment of stack on which the task is currently running or
69 /// if the task is blocked, on which the task will resume
71 current_stack_segment: StackSegment,
72 /// Always valid if the task is alive and not running.
73 saved_context: Context
76 /// Some tasks have a dedicated home scheduler that they must run on.
82 pub struct GarbageCollector;
83 pub struct LocalStorage(Option<local_data::Map>);
91 // A helper to build a new task using the dynamically found
92 // scheduler and task. Only works in GreenTask context.
93 pub fn build_homed_child(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
95 let home = Cell::new(home);
96 do Local::borrow |running_task: &mut Task| {
97 let mut sched = running_task.sched.take_unwrap();
98 let new_task = ~running_task.new_child_homed(&mut sched.stack_pool,
102 running_task.sched = Some(sched);
107 pub fn build_child(stack_size: Option<uint>, f: ~fn()) -> ~Task {
108 Task::build_homed_child(stack_size, f, AnySched)
111 pub fn build_homed_root(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
112 let f = Cell::new(f);
113 let home = Cell::new(home);
114 do Local::borrow |running_task: &mut Task| {
115 let mut sched = running_task.sched.take_unwrap();
116 let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
120 running_task.sched = Some(sched);
125 pub fn build_root(stack_size: Option<uint>, f: ~fn()) -> ~Task {
126 Task::build_homed_root(stack_size, f, AnySched)
129 pub fn new_sched_task() -> Task {
131 heap: LocalHeap::new(),
132 gc: GarbageCollector,
133 storage: LocalStorage(None),
134 logger: StdErrLogger,
135 unwinder: Unwinder { unwinding: false },
139 coroutine: Some(Coroutine::empty()),
142 task_type: SchedTask,
147 pub fn new_root(stack_pool: &mut StackPool,
148 stack_size: Option<uint>,
149 start: ~fn()) -> Task {
150 Task::new_root_homed(stack_pool, stack_size, AnySched, start)
153 pub fn new_child(&mut self,
154 stack_pool: &mut StackPool,
155 stack_size: Option<uint>,
156 start: ~fn()) -> Task {
157 self.new_child_homed(stack_pool, stack_size, AnySched, start)
160 pub fn new_root_homed(stack_pool: &mut StackPool,
161 stack_size: Option<uint>,
163 start: ~fn()) -> Task {
165 heap: LocalHeap::new(),
166 gc: GarbageCollector,
167 storage: LocalStorage(None),
168 logger: StdErrLogger,
169 unwinder: Unwinder { unwinding: false },
174 coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
176 task_type: GreenTask(Some(home)),
181 pub fn new_child_homed(&mut self,
182 stack_pool: &mut StackPool,
183 stack_size: Option<uint>,
185 start: ~fn()) -> Task {
187 heap: LocalHeap::new(),
188 gc: GarbageCollector,
189 storage: LocalStorage(None),
190 logger: StdErrLogger,
191 unwinder: Unwinder { unwinding: false },
193 // FIXME(#7544) make watching optional
194 death: self.death.new_child(),
197 coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
199 task_type: GreenTask(Some(home)),
204 pub fn give_home(&mut self, new_home: SchedHome) {
205 match self.task_type {
206 GreenTask(ref mut home) => {
207 *home = Some(new_home);
210 rtabort!("type error: used SchedTask as GreenTask");
215 pub fn take_unwrap_home(&mut self) -> SchedHome {
216 match self.task_type {
217 GreenTask(ref mut home) => {
218 let out = home.take_unwrap();
222 rtabort!("type error: used SchedTask as GreenTask");
227 pub fn run(&mut self, f: &fn()) {
228 rtdebug!("run called on task: %u", borrow::to_uint(self));
230 // The only try/catch block in the world. Attempt to run the task's
231 // client-specified code and catch any failures.
232 do self.unwinder.try {
234 // Run the task main function, then do some cleanup.
236 // First, destroy task-local storage. This may run user dtors.
238 // FIXME #8302: Dear diary. I'm so tired and confused.
239 // There's some interaction in rustc between the box
240 // annihilator and the TLS dtor by which TLS is
241 // accessed from annihilated box dtors *after* TLS is
242 // destroyed. Somehow setting TLS back to null, as the
243 // old runtime did, makes this work, but I don't currently
244 // understand how. I would expect that, if the annihilator
245 // reinvokes TLS while TLS is uninitialized, that
246 // TLS would be reinitialized but never destroyed,
247 // but somehow this works. I have no idea what's going
248 // on but this seems to make things magically work. FML.
250 // (added after initial comment) A possible interaction here is
251 // that the destructors for the objects in TLS themselves invoke
252 // TLS, or possibly some destructors for those objects being
253 // annihilated invoke TLS. Sadly these two operations seemed to
254 // be intertwined, and miraculously work for now...
257 // Destroy remaining boxes. Also may run user dtors.
258 unsafe { cleanup::annihilate(); }
262 // Cleanup the dynamic borrowck debugging info
263 borrowck::clear_task_borrow_list();
265 // NB. We pass the taskgroup into death so that it can be dropped while
266 // the unkillable counter is set. This is necessary for when the
267 // taskgroup destruction code drops references on KillHandles, which
268 // might require using unkillable (to synchronize with an unwrapper).
269 self.death.collect_failure(!self.unwinder.unwinding, self.taskgroup.take());
270 self.destroyed = true;
273 // New utility functions for homes.
275 pub fn is_home_no_tls(&self, sched: &~Scheduler) -> bool {
276 match self.task_type {
277 GreenTask(Some(AnySched)) => { false }
278 GreenTask(Some(Sched(SchedHandle { sched_id: ref id, _}))) => {
279 *id == sched.sched_id()
282 rtabort!("task without home");
286 rtabort!("type error: expected: GreenTask, found: SchedTask");
291 pub fn homed(&self) -> bool {
292 match self.task_type {
293 GreenTask(Some(AnySched)) => { false }
294 GreenTask(Some(Sched(SchedHandle { _ }))) => { true }
296 rtabort!("task without home");
299 rtabort!("type error: expected: GreenTask, found: SchedTask");
304 // Grab both the scheduler and the task from TLS and check if the
305 // task is executing on an appropriate scheduler.
306 pub fn on_appropriate_sched() -> bool {
307 do Local::borrow |task: &mut Task| {
308 let sched_id = task.sched.get_ref().sched_id();
309 let sched_run_anything = task.sched.get_ref().run_anything;
310 match task.task_type {
311 GreenTask(Some(AnySched)) => {
312 rtdebug!("anysched task in sched check ****");
315 GreenTask(Some(Sched(SchedHandle { sched_id: ref id, _ }))) => {
316 rtdebug!("homed task in sched check ****");
320 rtabort!("task without home");
323 rtabort!("type error: expected: GreenTask, found: SchedTask");
332 rtdebug!("called drop for a task: %u", borrow::to_uint(self));
333 rtassert!(self.destroyed)
337 // Coroutines represent nothing more than a context and a stack
342 pub fn new(stack_pool: &mut StackPool, stack_size: Option<uint>, start: ~fn()) -> Coroutine {
343 let stack_size = match stack_size {
345 None => env::min_stack()
347 let start = Coroutine::build_start_wrapper(start);
348 let mut stack = stack_pool.take_segment(stack_size);
349 let initial_context = Context::new(start, &mut stack);
351 current_stack_segment: stack,
352 saved_context: initial_context
356 pub fn empty() -> Coroutine {
358 current_stack_segment: StackSegment::new(0),
359 saved_context: Context::empty()
363 fn build_start_wrapper(start: ~fn()) -> ~fn() {
364 let start_cell = Cell::new(start);
365 let wrapper: ~fn() = || {
366 // First code after swap to this new context. Run our
370 // Again - might work while safe, or it might not.
371 do Local::borrow |sched: &mut Scheduler| {
372 sched.run_cleanup_job();
375 // To call the run method on a task we need a direct
376 // reference to it. The task is in TLS, so we can
377 // simply unsafe_borrow it to get this reference. We
378 // need to still have the task in TLS though, so we
379 // need to unsafe_borrow.
380 let task: *mut Task = Local::unsafe_borrow();
383 // N.B. Removing `start` from the start wrapper
384 // closure by emptying a cell is critical for
385 // correctness. The ~Task pointer, and in turn the
386 // closure used to initialize the first call
387 // frame, is destroyed in the scheduler context,
388 // not task context. So any captured closures must
389 // not contain user-definable dtors that expect to
390 // be in task context. By moving `start` out of
391 // the closure, all the user code goes our of
392 // scope while the task is still running.
393 let start = start_cell.take();
398 // We remove the sched from the Task in TLS right now.
399 let sched: ~Scheduler = Local::take();
400 // ... allowing us to give it away when performing a
401 // scheduling operation.
402 sched.terminate_current_task()
407 /// Destroy coroutine and try to reuse stack segment.
408 pub fn recycle(self, stack_pool: &mut StackPool) {
410 Coroutine { current_stack_segment, _ } => {
411 stack_pool.give_segment(current_stack_segment);
419 // Just a sanity check to make sure we are catching a Rust-thrown exception
420 static UNWIND_TOKEN: uintptr_t = 839147;
423 pub fn try(&mut self, f: &fn()) {
424 use unstable::raw::Closure;
427 let closure: Closure = transmute(f);
428 let code = transmute(closure.code);
429 let env = transmute(closure.env);
431 let token = rust_try(try_fn, code, env);
432 assert!(token == 0 || token == UNWIND_TOKEN);
435 extern fn try_fn(code: *c_void, env: *c_void) {
437 let closure: Closure = Closure {
438 code: transmute(code),
441 let closure: &fn() = transmute(closure);
449 fn rust_try(f: extern "C" fn(*c_void, *c_void),
451 data: *c_void) -> uintptr_t;
457 data: *c_void) -> uintptr_t;
461 pub fn begin_unwind(&mut self) -> ! {
462 #[fixed_stack_segment]; #[inline(never)];
464 self.unwinding = true;
466 rust_begin_unwind(UNWIND_TOKEN);
467 return transmute(());
470 fn rust_begin_unwind(token: uintptr_t);
481 do run_in_newsched_task() {
492 do run_in_newsched_task() {
493 static key: local_data::Key<@~str> = &local_data::Key;
494 local_data::set(key, @~"data");
495 assert!(*local_data::get(key, |k| k.map_move(|k| *k)).unwrap() == ~"data");
496 static key2: local_data::Key<@~str> = &local_data::Key;
497 local_data::set(key2, @~"data");
498 assert!(*local_data::get(key2, |k| k.map_move(|k| *k)).unwrap() == ~"data");
504 do run_in_newsched_task() {
505 let result = spawntask_try(||());
506 rtdebug!("trying first assert");
507 assert!(result.is_ok());
508 let result = spawntask_try(|| fail!());
509 rtdebug!("trying second assert");
510 assert!(result.is_err());
516 do run_in_newsched_task() {
517 use rand::{rng, Rng};
525 do run_in_newsched_task() {
526 info!("here i am. logging in a newsched task");
534 do run_in_newsched_task {
535 let (port, chan) = oneshot();
537 assert!(port.recv() == 10);
545 do run_in_newsched_task() {
546 let (port, chan) = stream();
548 assert!(port.recv() == 10);
553 fn comm_shared_chan() {
556 do run_in_newsched_task() {
557 let (port, chan) = stream();
558 let chan = SharedChan::new(chan);
560 assert!(port.recv() == 10);
565 fn linked_failure() {
566 do run_in_newsched_task() {
567 let res = do spawntask_try {
568 spawntask_random(|| fail!());
570 assert!(res.is_err());
576 use option::{Option, Some, None};
578 do run_in_newsched_task {
580 next: Option<@mut List>,
583 let a = @mut List { next: None };
584 let b = @mut List { next: Some(a) };
590 // XXX: This is a copy of test_future_result in std::task.
591 // It can be removed once the scheduler is turned on by default.
594 do run_in_newsched_task {
595 use option::{Some, None};
598 let mut result = None;
599 let mut builder = task();
600 builder.future_result(|r| result = Some(r));
602 assert_eq!(result.unwrap().recv(), Success);
605 let mut builder = task();
606 builder.future_result(|r| result = Some(r));
611 assert_eq!(result.unwrap().recv(), Failure);