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.
19 use libc::{c_void, uintptr_t};
22 use option::{Option, Some, None};
25 use rt::logging::StdErrLogger;
26 use super::local_heap::LocalHeap;
27 use rt::sched::{Scheduler, SchedHandle};
28 use rt::stack::{StackSegment, StackPool};
29 use rt::context::Context;
30 use unstable::finally::Finally;
31 use task::spawn::Taskgroup;
34 // The Task struct represents all state associated with a rust
35 // task. There are at this point two primary "subtypes" of task,
36 // however instead of using a subtype we just have a "task_type" field
37 // in the struct. This contains a pointer to another struct that holds
38 // the type-specific state.
43 storage: LocalStorage,
46 taskgroup: Option<Taskgroup>,
49 // FIXME(#6874/#7599) use StringRef to save on allocations
51 coroutine: Option<Coroutine>,
52 sched: Option<~Scheduler>,
57 GreenTask(Option<~SchedHome>),
61 /// A coroutine is nothing more than a (register context, stack) pair.
62 pub struct Coroutine {
63 /// The segment of stack on which the task is currently running or
64 /// if the task is blocked, on which the task will resume
66 priv current_stack_segment: StackSegment,
67 /// Always valid if the task is alive and not running.
68 saved_context: Context
71 /// Some tasks have a deciated home scheduler that they must run on.
77 pub struct GarbageCollector;
78 pub struct LocalStorage(*c_void, Option<extern "Rust" fn(*c_void)>);
86 // A helper to build a new task using the dynamically found
87 // scheduler and task. Only works in GreenTask context.
88 pub fn build_homed_child(f: ~fn(), home: SchedHome) -> ~Task {
90 let home = Cell::new(home);
91 do Local::borrow::<Task, ~Task> |running_task| {
92 let mut sched = running_task.sched.take_unwrap();
93 let new_task = ~running_task.new_child_homed(&mut sched.stack_pool,
96 running_task.sched = Some(sched);
101 pub fn build_child(f: ~fn()) -> ~Task {
102 Task::build_homed_child(f, AnySched)
105 pub fn build_homed_root(f: ~fn(), home: SchedHome) -> ~Task {
106 let f = Cell::new(f);
107 let home = Cell::new(home);
108 do Local::borrow::<Task, ~Task> |running_task| {
109 let mut sched = running_task.sched.take_unwrap();
110 let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
113 running_task.sched = Some(sched);
118 pub fn build_root(f: ~fn()) -> ~Task {
119 Task::build_homed_root(f, AnySched)
122 pub fn new_sched_task() -> Task {
124 heap: LocalHeap::new(),
125 gc: GarbageCollector,
126 storage: LocalStorage(ptr::null(), None),
127 logger: StdErrLogger,
128 unwinder: Unwinder { unwinding: false },
132 coroutine: Some(Coroutine::empty()),
139 pub fn new_root(stack_pool: &mut StackPool,
140 start: ~fn()) -> Task {
141 Task::new_root_homed(stack_pool, AnySched, start)
144 pub fn new_child(&mut self,
145 stack_pool: &mut StackPool,
146 start: ~fn()) -> Task {
147 self.new_child_homed(stack_pool, AnySched, start)
150 pub fn new_root_homed(stack_pool: &mut StackPool,
152 start: ~fn()) -> Task {
154 heap: LocalHeap::new(),
155 gc: GarbageCollector,
156 storage: LocalStorage(ptr::null(), None),
157 logger: StdErrLogger,
158 unwinder: Unwinder { unwinding: false },
163 coroutine: Some(Coroutine::new(stack_pool, start)),
165 task_type: GreenTask(Some(~home))
169 pub fn new_child_homed(&mut self,
170 stack_pool: &mut StackPool,
172 start: ~fn()) -> Task {
174 heap: LocalHeap::new(),
175 gc: GarbageCollector,
176 storage: LocalStorage(ptr::null(), None),
177 logger: StdErrLogger,
178 unwinder: Unwinder { unwinding: false },
180 // FIXME(#7544) make watching optional
181 death: self.death.new_child(),
184 coroutine: Some(Coroutine::new(stack_pool, start)),
186 task_type: GreenTask(Some(~home))
190 pub fn give_home(&mut self, new_home: SchedHome) {
191 match self.task_type {
192 GreenTask(ref mut home) => {
193 *home = Some(~new_home);
196 rtabort!("type error: used SchedTask as GreenTask");
201 pub fn take_unwrap_home(&mut self) -> SchedHome {
202 match self.task_type {
203 GreenTask(ref mut home) => {
204 let out = home.take_unwrap();
208 rtabort!("type error: used SchedTask as GreenTask");
213 pub fn run(&mut self, f: &fn()) {
214 rtdebug!("run called on task: %u", borrow::to_uint(self));
216 // The only try/catch block in the world. Attempt to run the task's
217 // client-specified code and catch any failures.
218 do self.unwinder.try {
220 // Run the task main function, then do some cleanup.
223 // Destroy task-local storage. This may run user dtors.
225 LocalStorage(ptr, Some(ref dtor)) => {
231 // FIXME #8302: Dear diary. I'm so tired and confused.
232 // There's some interaction in rustc between the box
233 // annihilator and the TLS dtor by which TLS is
234 // accessed from annihilated box dtors *after* TLS is
235 // destroyed. Somehow setting TLS back to null, as the
236 // old runtime did, makes this work, but I don't currently
237 // understand how. I would expect that, if the annihilator
238 // reinvokes TLS while TLS is uninitialized, that
239 // TLS would be reinitialized but never destroyed,
240 // but somehow this works. I have no idea what's going
241 // on but this seems to make things magically work. FML.
242 self.storage = LocalStorage(ptr::null(), None);
244 // Destroy remaining boxes. Also may run user dtors.
245 unsafe { cleanup::annihilate(); }
249 // FIXME(#7544): We pass the taskgroup into death so that it can be
250 // dropped while the unkillable counter is set. This should not be
251 // necessary except for an extraneous clone() in task/spawn.rs that
252 // causes a killhandle to get dropped, which mustn't receive a kill
253 // signal since we're outside of the unwinder's try() scope.
254 // { let _ = self.taskgroup.take(); }
255 self.death.collect_failure(!self.unwinder.unwinding, self.taskgroup.take());
256 self.destroyed = true;
259 // New utility functions for homes.
261 pub fn is_home_no_tls(&self, sched: &~Scheduler) -> bool {
262 match self.task_type {
263 GreenTask(Some(~AnySched)) => { false }
264 GreenTask(Some(~Sched(SchedHandle { sched_id: ref id, _}))) => {
265 *id == sched.sched_id()
268 rtabort!("task without home");
272 rtabort!("type error: expected: GreenTask, found: SchedTask");
277 pub fn homed(&self) -> bool {
278 match self.task_type {
279 GreenTask(Some(~AnySched)) => { false }
280 GreenTask(Some(~Sched(SchedHandle { _ }))) => { true }
282 rtabort!("task without home");
285 rtabort!("type error: expected: GreenTask, found: SchedTask");
290 // Grab both the scheduler and the task from TLS and check if the
291 // task is executing on an appropriate scheduler.
292 pub fn on_appropriate_sched() -> bool {
293 do Local::borrow::<Task,bool> |task| {
294 let sched_id = task.sched.get_ref().sched_id();
295 let sched_run_anything = task.sched.get_ref().run_anything;
296 match task.task_type {
297 GreenTask(Some(~AnySched)) => {
298 rtdebug!("anysched task in sched check ****");
301 GreenTask(Some(~Sched(SchedHandle { sched_id: ref id, _ }))) => {
302 rtdebug!("homed task in sched check ****");
306 rtabort!("task without home");
309 rtabort!("type error: expected: GreenTask, found: SchedTask");
318 rtdebug!("called drop for a task: %u", borrow::to_uint(self));
319 rtassert!(self.destroyed)
323 // Coroutines represent nothing more than a context and a stack
328 pub fn new(stack_pool: &mut StackPool, start: ~fn()) -> Coroutine {
329 static MIN_STACK_SIZE: uint = 2000000; // XXX: Too much stack
331 let start = Coroutine::build_start_wrapper(start);
332 let mut stack = stack_pool.take_segment(MIN_STACK_SIZE);
333 let initial_context = Context::new(start, &mut stack);
335 current_stack_segment: stack,
336 saved_context: initial_context
340 pub fn empty() -> Coroutine {
342 current_stack_segment: StackSegment::new(0),
343 saved_context: Context::empty()
347 fn build_start_wrapper(start: ~fn()) -> ~fn() {
348 let start_cell = Cell::new(start);
349 let wrapper: ~fn() = || {
350 // First code after swap to this new context. Run our
354 // Again - might work while safe, or it might not.
355 do Local::borrow::<Scheduler,()> |sched| {
356 (sched).run_cleanup_job();
359 // To call the run method on a task we need a direct
360 // reference to it. The task is in TLS, so we can
361 // simply unsafe_borrow it to get this reference. We
362 // need to still have the task in TLS though, so we
363 // need to unsafe_borrow.
364 let task = Local::unsafe_borrow::<Task>();
367 // N.B. Removing `start` from the start wrapper
368 // closure by emptying a cell is critical for
369 // correctness. The ~Task pointer, and in turn the
370 // closure used to initialize the first call
371 // frame, is destroyed in the scheduler context,
372 // not task context. So any captured closures must
373 // not contain user-definable dtors that expect to
374 // be in task context. By moving `start` out of
375 // the closure, all the user code goes our of
376 // scope while the task is still running.
377 let start = start_cell.take();
382 // We remove the sched from the Task in TLS right now.
383 let sched = Local::take::<Scheduler>();
384 // ... allowing us to give it away when performing a
385 // scheduling operation.
386 sched.terminate_current_task()
391 /// Destroy coroutine and try to reuse stack segment.
392 pub fn recycle(self, stack_pool: &mut StackPool) {
394 Coroutine { current_stack_segment, _ } => {
395 stack_pool.give_segment(current_stack_segment);
403 // Just a sanity check to make sure we are catching a Rust-thrown exception
404 static UNWIND_TOKEN: uintptr_t = 839147;
407 pub fn try(&mut self, f: &fn()) {
408 use unstable::raw::Closure;
411 let closure: Closure = transmute(f);
412 let code = transmute(closure.code);
413 let env = transmute(closure.env);
415 let token = rust_try(try_fn, code, env);
416 assert!(token == 0 || token == UNWIND_TOKEN);
419 extern fn try_fn(code: *c_void, env: *c_void) {
421 let closure: Closure = Closure {
422 code: transmute(code),
425 let closure: &fn() = transmute(closure);
432 fn rust_try(f: *u8, code: *c_void, data: *c_void) -> uintptr_t;
436 pub fn begin_unwind(&mut self) -> ! {
437 self.unwinding = true;
439 rust_begin_unwind(UNWIND_TOKEN);
440 return transmute(());
443 fn rust_begin_unwind(token: uintptr_t);
454 do run_in_newsched_task() {
465 do run_in_newsched_task() {
466 static key: local_data::Key<@~str> = &local_data::Key;
467 local_data::set(key, @~"data");
468 assert!(*local_data::get(key, |k| k.map_move(|k| *k)).unwrap() == ~"data");
469 static key2: local_data::Key<@~str> = &local_data::Key;
470 local_data::set(key2, @~"data");
471 assert!(*local_data::get(key2, |k| k.map_move(|k| *k)).unwrap() == ~"data");
477 do run_in_newsched_task() {
478 let result = spawntask_try(||());
479 rtdebug!("trying first assert");
480 assert!(result.is_ok());
481 let result = spawntask_try(|| fail!());
482 rtdebug!("trying second assert");
483 assert!(result.is_err());
489 do run_in_newsched_task() {
490 use rand::{rng, Rng};
498 do run_in_newsched_task() {
499 info!("here i am. logging in a newsched task");
507 do run_in_newsched_task {
508 let (port, chan) = oneshot();
510 assert!(recv_one(port) == 10);
518 do run_in_newsched_task() {
519 let (port, chan) = stream();
521 assert!(port.recv() == 10);
526 fn comm_shared_chan() {
529 do run_in_newsched_task() {
530 let (port, chan) = stream();
531 let chan = SharedChan::new(chan);
533 assert!(port.recv() == 10);
538 fn linked_failure() {
539 do run_in_newsched_task() {
540 let res = do spawntask_try {
541 spawntask_random(|| fail!());
543 assert!(res.is_err());
549 use option::{Option, Some, None};
551 do run_in_newsched_task {
553 next: Option<@mut List>,
556 let a = @mut List { next: None };
557 let b = @mut List { next: Some(a) };
563 // XXX: This is a copy of test_future_result in std::task.
564 // It can be removed once the scheduler is turned on by default.
567 do run_in_newsched_task {
568 use option::{Some, None};
571 let mut result = None;
572 let mut builder = task();
573 builder.future_result(|r| result = Some(r));
575 assert_eq!(result.unwrap().recv(), Success);
578 let mut builder = task();
579 builder.future_result(|r| result = Some(r));
584 assert_eq!(result.unwrap().recv(), Failure);