]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/task.rs
Rewrite std::comm
[rust.git] / src / libstd / rt / task.rs
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.
4 //
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.
10
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.
15
16 use super::local_heap::LocalHeap;
17
18 use prelude::*;
19
20 use borrow;
21 use cast::transmute;
22 use cleanup;
23 use libc::{c_void, uintptr_t, c_char, size_t};
24 use local_data;
25 use option::{Option, Some, None};
26 use rt::borrowck::BorrowRecord;
27 use rt::borrowck;
28 use rt::context::Context;
29 use rt::env;
30 use io::Writer;
31 use rt::kill::Death;
32 use rt::local::Local;
33 use rt::logging::StdErrLogger;
34 use rt::sched::{Scheduler, SchedHandle};
35 use rt::stack::{StackSegment, StackPool};
36 use send_str::SendStr;
37 use unstable::finally::Finally;
38 use unstable::mutex::Mutex;
39
40 // The Task struct represents all state associated with a rust
41 // task. There are at this point two primary "subtypes" of task,
42 // however instead of using a subtype we just have a "task_type" field
43 // in the struct. This contains a pointer to another struct that holds
44 // the type-specific state.
45
46 pub struct Task {
47     heap: LocalHeap,
48     priv gc: GarbageCollector,
49     storage: LocalStorage,
50     logger: Option<StdErrLogger>,
51     unwinder: Unwinder,
52     death: Death,
53     destroyed: bool,
54     name: Option<SendStr>,
55     coroutine: Option<Coroutine>,
56     sched: Option<~Scheduler>,
57     task_type: TaskType,
58     // Dynamic borrowck debugging info
59     borrow_list: Option<~[BorrowRecord]>,
60     stdout_handle: Option<~Writer>,
61
62     // See the comments in the scheduler about why this is necessary
63     nasty_deschedule_lock: Mutex,
64 }
65
66 pub enum TaskType {
67     GreenTask(Option<SchedHome>),
68     SchedTask
69 }
70
71 /// A coroutine is nothing more than a (register context, stack) pair.
72 pub struct Coroutine {
73     /// The segment of stack on which the task is currently running or
74     /// if the task is blocked, on which the task will resume
75     /// execution.
76     ///
77     /// Servo needs this to be public in order to tell SpiderMonkey
78     /// about the stack bounds.
79     current_stack_segment: StackSegment,
80     /// Always valid if the task is alive and not running.
81     saved_context: Context
82 }
83
84 /// Some tasks have a dedicated home scheduler that they must run on.
85 pub enum SchedHome {
86     AnySched,
87     Sched(SchedHandle)
88 }
89
90 pub struct GarbageCollector;
91 pub struct LocalStorage(Option<local_data::Map>);
92
93 /// Represents the reason for the current unwinding process
94 pub enum UnwindResult {
95     /// The task is ending successfully
96     Success,
97
98     /// The Task is failing with reason `~Any`
99     Failure(~Any),
100 }
101
102 impl UnwindResult {
103     /// Returns `true` if this `UnwindResult` is a failure
104     #[inline]
105     pub fn is_failure(&self) -> bool {
106         match *self {
107             Success => false,
108             Failure(_) => true
109         }
110     }
111
112     /// Returns `true` if this `UnwindResult` is a success
113     #[inline]
114     pub fn is_success(&self) -> bool {
115         match *self {
116             Success => true,
117             Failure(_) => false
118         }
119     }
120 }
121
122 pub struct Unwinder {
123     unwinding: bool,
124     cause: Option<~Any>
125 }
126
127 impl Unwinder {
128     fn to_unwind_result(&mut self) -> UnwindResult {
129         if self.unwinding {
130             Failure(self.cause.take().unwrap())
131         } else {
132             Success
133         }
134     }
135 }
136
137 impl Task {
138
139     // A helper to build a new task using the dynamically found
140     // scheduler and task. Only works in GreenTask context.
141     pub fn build_homed_child(stack_size: Option<uint>,
142                              f: proc(),
143                              home: SchedHome)
144                              -> ~Task {
145         let mut running_task = Local::borrow(None::<Task>);
146         let mut sched = running_task.get().sched.take_unwrap();
147         let new_task = ~running_task.get()
148                                     .new_child_homed(&mut sched.stack_pool,
149                                                      stack_size,
150                                                      home,
151                                                      f);
152         running_task.get().sched = Some(sched);
153         new_task
154     }
155
156     pub fn build_child(stack_size: Option<uint>, f: proc()) -> ~Task {
157         Task::build_homed_child(stack_size, f, AnySched)
158     }
159
160     pub fn build_homed_root(stack_size: Option<uint>,
161                             f: proc(),
162                             home: SchedHome)
163                             -> ~Task {
164         let mut running_task = Local::borrow(None::<Task>);
165         let mut sched = running_task.get().sched.take_unwrap();
166         let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
167                                              stack_size,
168                                              home,
169                                              f);
170         running_task.get().sched = Some(sched);
171         new_task
172     }
173
174     pub fn build_root(stack_size: Option<uint>, f: proc()) -> ~Task {
175         Task::build_homed_root(stack_size, f, AnySched)
176     }
177
178     pub fn new_sched_task() -> Task {
179         Task {
180             heap: LocalHeap::new(),
181             gc: GarbageCollector,
182             storage: LocalStorage(None),
183             logger: None,
184             unwinder: Unwinder { unwinding: false, cause: None },
185             death: Death::new(),
186             destroyed: false,
187             coroutine: Some(Coroutine::empty()),
188             name: None,
189             sched: None,
190             task_type: SchedTask,
191             borrow_list: None,
192             stdout_handle: None,
193             nasty_deschedule_lock: unsafe { Mutex::new() },
194         }
195     }
196
197     pub fn new_root(stack_pool: &mut StackPool,
198                     stack_size: Option<uint>,
199                     start: proc()) -> Task {
200         Task::new_root_homed(stack_pool, stack_size, AnySched, start)
201     }
202
203     pub fn new_child(&mut self,
204                      stack_pool: &mut StackPool,
205                      stack_size: Option<uint>,
206                      start: proc()) -> Task {
207         self.new_child_homed(stack_pool, stack_size, AnySched, start)
208     }
209
210     pub fn new_root_homed(stack_pool: &mut StackPool,
211                           stack_size: Option<uint>,
212                           home: SchedHome,
213                           start: proc()) -> Task {
214         Task {
215             heap: LocalHeap::new(),
216             gc: GarbageCollector,
217             storage: LocalStorage(None),
218             logger: None,
219             unwinder: Unwinder { unwinding: false, cause: None },
220             death: Death::new(),
221             destroyed: false,
222             name: None,
223             coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
224             sched: None,
225             task_type: GreenTask(Some(home)),
226             borrow_list: None,
227             stdout_handle: None,
228             nasty_deschedule_lock: unsafe { Mutex::new() },
229         }
230     }
231
232     pub fn new_child_homed(&mut self,
233                            stack_pool: &mut StackPool,
234                            stack_size: Option<uint>,
235                            home: SchedHome,
236                            start: proc()) -> Task {
237         Task {
238             heap: LocalHeap::new(),
239             gc: GarbageCollector,
240             storage: LocalStorage(None),
241             logger: None,
242             unwinder: Unwinder { unwinding: false, cause: None },
243             death: Death::new(),
244             destroyed: false,
245             name: None,
246             coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
247             sched: None,
248             task_type: GreenTask(Some(home)),
249             borrow_list: None,
250             stdout_handle: None,
251             nasty_deschedule_lock: unsafe { Mutex::new() },
252         }
253     }
254
255     pub fn give_home(&mut self, new_home: SchedHome) {
256         match self.task_type {
257             GreenTask(ref mut home) => {
258                 *home = Some(new_home);
259             }
260             SchedTask => {
261                 rtabort!("type error: used SchedTask as GreenTask");
262             }
263         }
264     }
265
266     pub fn take_unwrap_home(&mut self) -> SchedHome {
267         match self.task_type {
268             GreenTask(ref mut home) => {
269                 let out = home.take_unwrap();
270                 return out;
271             }
272             SchedTask => {
273                 rtabort!("type error: used SchedTask as GreenTask");
274             }
275         }
276     }
277
278     pub fn run(&mut self, f: ||) {
279         rtdebug!("run called on task: {}", borrow::to_uint(self));
280
281         // The only try/catch block in the world. Attempt to run the task's
282         // client-specified code and catch any failures.
283         self.unwinder.try(|| {
284
285             // Run the task main function, then do some cleanup.
286             f.finally(|| {
287
288                 // First, destroy task-local storage. This may run user dtors.
289                 //
290                 // FIXME #8302: Dear diary. I'm so tired and confused.
291                 // There's some interaction in rustc between the box
292                 // annihilator and the TLS dtor by which TLS is
293                 // accessed from annihilated box dtors *after* TLS is
294                 // destroyed. Somehow setting TLS back to null, as the
295                 // old runtime did, makes this work, but I don't currently
296                 // understand how. I would expect that, if the annihilator
297                 // reinvokes TLS while TLS is uninitialized, that
298                 // TLS would be reinitialized but never destroyed,
299                 // but somehow this works. I have no idea what's going
300                 // on but this seems to make things magically work. FML.
301                 //
302                 // (added after initial comment) A possible interaction here is
303                 // that the destructors for the objects in TLS themselves invoke
304                 // TLS, or possibly some destructors for those objects being
305                 // annihilated invoke TLS. Sadly these two operations seemed to
306                 // be intertwined, and miraculously work for now...
307                 self.storage.take();
308
309                 // Destroy remaining boxes. Also may run user dtors.
310                 unsafe { cleanup::annihilate(); }
311
312                 // Finally flush and destroy any output handles which the task
313                 // owns. There are no boxes here, and no user destructors should
314                 // run after this any more.
315                 match self.stdout_handle.take() {
316                     Some(handle) => {
317                         let mut handle = handle;
318                         handle.flush();
319                     }
320                     None => {}
321                 }
322                 self.logger.take();
323             })
324         });
325
326         // Cleanup the dynamic borrowck debugging info
327         borrowck::clear_task_borrow_list();
328
329         self.death.collect_failure(self.unwinder.to_unwind_result());
330         self.destroyed = true;
331     }
332
333     // New utility functions for homes.
334
335     pub fn is_home_no_tls(&self, sched: &~Scheduler) -> bool {
336         match self.task_type {
337             GreenTask(Some(AnySched)) => { false }
338             GreenTask(Some(Sched(SchedHandle { sched_id: ref id, .. }))) => {
339                 *id == sched.sched_id()
340             }
341             GreenTask(None) => {
342                 rtabort!("task without home");
343             }
344             SchedTask => {
345                 // Awe yea
346                 rtabort!("type error: expected: GreenTask, found: SchedTask");
347             }
348         }
349     }
350
351     pub fn homed(&self) -> bool {
352         match self.task_type {
353             GreenTask(Some(AnySched)) => { false }
354             GreenTask(Some(Sched(SchedHandle { .. }))) => { true }
355             GreenTask(None) => {
356                 rtabort!("task without home");
357             }
358             SchedTask => {
359                 rtabort!("type error: expected: GreenTask, found: SchedTask");
360             }
361         }
362     }
363
364     // Grab both the scheduler and the task from TLS and check if the
365     // task is executing on an appropriate scheduler.
366     pub fn on_appropriate_sched() -> bool {
367         let mut task = Local::borrow(None::<Task>);
368         let sched_id = task.get().sched.get_ref().sched_id();
369         let sched_run_anything = task.get().sched.get_ref().run_anything;
370         match task.get().task_type {
371             GreenTask(Some(AnySched)) => {
372                 rtdebug!("anysched task in sched check ****");
373                 sched_run_anything
374             }
375             GreenTask(Some(Sched(SchedHandle { sched_id: ref id, ..}))) => {
376                 rtdebug!("homed task in sched check ****");
377                 *id == sched_id
378             }
379             GreenTask(None) => {
380                 rtabort!("task without home");
381             }
382             SchedTask => {
383                 rtabort!("type error: expected: GreenTask, found: SchedTask");
384             }
385         }
386     }
387 }
388
389 impl Drop for Task {
390     fn drop(&mut self) {
391         rtdebug!("called drop for a task: {}", borrow::to_uint(self));
392         rtassert!(self.destroyed);
393
394         unsafe { self.nasty_deschedule_lock.destroy(); }
395     }
396 }
397
398 // Coroutines represent nothing more than a context and a stack
399 // segment.
400
401 impl Coroutine {
402
403     pub fn new(stack_pool: &mut StackPool,
404                stack_size: Option<uint>,
405                start: proc())
406                -> Coroutine {
407         let stack_size = match stack_size {
408             Some(size) => size,
409             None => env::min_stack()
410         };
411         let start = Coroutine::build_start_wrapper(start);
412         let mut stack = stack_pool.take_segment(stack_size);
413         let initial_context = Context::new(start, &mut stack);
414         Coroutine {
415             current_stack_segment: stack,
416             saved_context: initial_context
417         }
418     }
419
420     pub fn empty() -> Coroutine {
421         Coroutine {
422             current_stack_segment: StackSegment::new(0),
423             saved_context: Context::empty()
424         }
425     }
426
427     fn build_start_wrapper(start: proc()) -> proc() {
428         let wrapper: proc() = proc() {
429             // First code after swap to this new context. Run our
430             // cleanup job.
431             unsafe {
432
433                 // Again - might work while safe, or it might not.
434                 {
435                     let mut sched = Local::borrow(None::<Scheduler>);
436                     sched.get().run_cleanup_job();
437                 }
438
439                 // To call the run method on a task we need a direct
440                 // reference to it. The task is in TLS, so we can
441                 // simply unsafe_borrow it to get this reference. We
442                 // need to still have the task in TLS though, so we
443                 // need to unsafe_borrow.
444                 let task: *mut Task = Local::unsafe_borrow();
445
446                 let mut start_cell = Some(start);
447                 (*task).run(|| {
448                     // N.B. Removing `start` from the start wrapper
449                     // closure by emptying a cell is critical for
450                     // correctness. The ~Task pointer, and in turn the
451                     // closure used to initialize the first call
452                     // frame, is destroyed in the scheduler context,
453                     // not task context. So any captured closures must
454                     // not contain user-definable dtors that expect to
455                     // be in task context. By moving `start` out of
456                     // the closure, all the user code goes our of
457                     // scope while the task is still running.
458                     let start = start_cell.take_unwrap();
459                     start();
460                 });
461             }
462
463             // We remove the sched from the Task in TLS right now.
464             let sched: ~Scheduler = Local::take();
465             // ... allowing us to give it away when performing a
466             // scheduling operation.
467             sched.terminate_current_task()
468         };
469         return wrapper;
470     }
471
472     /// Destroy coroutine and try to reuse stack segment.
473     pub fn recycle(self, stack_pool: &mut StackPool) {
474         match self {
475             Coroutine { current_stack_segment, .. } => {
476                 stack_pool.give_segment(current_stack_segment);
477             }
478         }
479     }
480
481 }
482
483
484 // Just a sanity check to make sure we are catching a Rust-thrown exception
485 static UNWIND_TOKEN: uintptr_t = 839147;
486
487 impl Unwinder {
488     pub fn try(&mut self, f: ||) {
489         use unstable::raw::Closure;
490
491         unsafe {
492             let closure: Closure = transmute(f);
493             let code = transmute(closure.code);
494             let env = transmute(closure.env);
495
496             let token = rust_try(try_fn, code, env);
497             assert!(token == 0 || token == UNWIND_TOKEN);
498         }
499
500         extern fn try_fn(code: *c_void, env: *c_void) {
501             unsafe {
502                 let closure: Closure = Closure {
503                     code: transmute(code),
504                     env: transmute(env),
505                 };
506                 let closure: || = transmute(closure);
507                 closure();
508             }
509         }
510
511         extern {
512             fn rust_try(f: extern "C" fn(*c_void, *c_void),
513                         code: *c_void,
514                         data: *c_void) -> uintptr_t;
515         }
516     }
517
518     pub fn begin_unwind(&mut self, cause: ~Any) -> ! {
519         self.unwinding = true;
520         self.cause = Some(cause);
521         unsafe {
522             rust_begin_unwind(UNWIND_TOKEN);
523             return transmute(());
524         }
525         extern {
526             fn rust_begin_unwind(token: uintptr_t);
527         }
528     }
529 }
530
531 /// This function is invoked from rust's current __morestack function. Segmented
532 /// stacks are currently not enabled as segmented stacks, but rather one giant
533 /// stack segment. This means that whenever we run out of stack, we want to
534 /// truly consider it to be stack overflow rather than allocating a new stack.
535 #[no_mangle]      // - this is called from C code
536 #[no_split_stack] // - it would be sad for this function to trigger __morestack
537 #[doc(hidden)]    // - Function must be `pub` to get exported, but it's
538                   //   irrelevant for documentation purposes.
539 #[cfg(not(test))] // in testing, use the original libstd's version
540 pub extern "C" fn rust_stack_exhausted() {
541     use rt::in_green_task_context;
542     use rt::task::Task;
543     use rt::local::Local;
544     use unstable::intrinsics;
545
546     unsafe {
547         // We're calling this function because the stack just ran out. We need
548         // to call some other rust functions, but if we invoke the functions
549         // right now it'll just trigger this handler being called again. In
550         // order to alleviate this, we move the stack limit to be inside of the
551         // red zone that was allocated for exactly this reason.
552         let limit = context::get_sp_limit();
553         context::record_sp_limit(limit - context::RED_ZONE / 2);
554
555         // This probably isn't the best course of action. Ideally one would want
556         // to unwind the stack here instead of just aborting the entire process.
557         // This is a tricky problem, however. There's a few things which need to
558         // be considered:
559         //
560         //  1. We're here because of a stack overflow, yet unwinding will run
561         //     destructors and hence arbitrary code. What if that code overflows
562         //     the stack? One possibility is to use the above allocation of an
563         //     extra 10k to hope that we don't hit the limit, and if we do then
564         //     abort the whole program. Not the best, but kind of hard to deal
565         //     with unless we want to switch stacks.
566         //
567         //  2. LLVM will optimize functions based on whether they can unwind or
568         //     not. It will flag functions with 'nounwind' if it believes that
569         //     the function cannot trigger unwinding, but if we do unwind on
570         //     stack overflow then it means that we could unwind in any function
571         //     anywhere. We would have to make sure that LLVM only places the
572         //     nounwind flag on functions which don't call any other functions.
573         //
574         //  3. The function that overflowed may have owned arguments. These
575         //     arguments need to have their destructors run, but we haven't even
576         //     begun executing the function yet, so unwinding will not run the
577         //     any landing pads for these functions. If this is ignored, then
578         //     the arguments will just be leaked.
579         //
580         // Exactly what to do here is a very delicate topic, and is possibly
581         // still up in the air for what exactly to do. Some relevant issues:
582         //
583         //  #3555 - out-of-stack failure leaks arguments
584         //  #3695 - should there be a stack limit?
585         //  #9855 - possible strategies which could be taken
586         //  #9854 - unwinding on windows through __morestack has never worked
587         //  #2361 - possible implementation of not using landing pads
588
589         if in_green_task_context() {
590             let mut task = Local::borrow(None::<Task>);
591             let n = task.get()
592                         .name
593                         .as_ref()
594                         .map(|n| n.as_slice())
595                         .unwrap_or("<unnamed>");
596
597             // See the message below for why this is not emitted to the
598             // task's logger. This has the additional conundrum of the
599             // logger may not be initialized just yet, meaning that an FFI
600             // call would happen to initialized it (calling out to libuv),
601             // and the FFI call needs 2MB of stack when we just ran out.
602             rterrln!("task '{}' has overflowed its stack", n);
603         } else {
604             rterrln!("stack overflow in non-task context");
605         }
606
607         intrinsics::abort();
608     }
609 }
610
611 /// This is the entry point of unwinding for things like lang items and such.
612 /// The arguments are normally generated by the compiler, and need to
613 /// have static lifetimes.
614 pub fn begin_unwind_raw(msg: *c_char, file: *c_char, line: size_t) -> ! {
615     use c_str::CString;
616     use cast::transmute;
617
618     #[inline]
619     fn static_char_ptr(p: *c_char) -> &'static str {
620         let s = unsafe { CString::new(p, false) };
621         match s.as_str() {
622             Some(s) => unsafe { transmute::<&str, &'static str>(s) },
623             None => rtabort!("message wasn't utf8?")
624         }
625     }
626
627     let msg = static_char_ptr(msg);
628     let file = static_char_ptr(file);
629
630     begin_unwind(msg, file, line as uint)
631 }
632
633 /// This is the entry point of unwinding for fail!() and assert!().
634 pub fn begin_unwind<M: Any + Send>(msg: M, file: &'static str, line: uint) -> ! {
635     use any::AnyRefExt;
636     use rt::in_green_task_context;
637     use rt::local::Local;
638     use rt::task::Task;
639     use str::Str;
640     use unstable::intrinsics;
641
642     unsafe {
643         let task: *mut Task;
644         // Note that this should be the only allocation performed in this block.
645         // Currently this means that fail!() on OOM will invoke this code path,
646         // but then again we're not really ready for failing on OOM anyway. If
647         // we do start doing this, then we should propagate this allocation to
648         // be performed in the parent of this task instead of the task that's
649         // failing.
650         let msg = ~msg as ~Any;
651
652         {
653             //let msg: &Any = msg;
654             let msg_s = match msg.as_ref::<&'static str>() {
655                 Some(s) => *s,
656                 None => match msg.as_ref::<~str>() {
657                     Some(s) => s.as_slice(),
658                     None => "~Any",
659                 }
660             };
661
662             if !in_green_task_context() {
663                 rterrln!("failed in non-task context at '{}', {}:{}",
664                          msg_s, file, line);
665                 intrinsics::abort();
666             }
667
668             task = Local::unsafe_borrow();
669             let n = (*task).name.as_ref().map(|n| n.as_slice()).unwrap_or("<unnamed>");
670
671             // XXX: this should no get forcibly printed to the console, this should
672             //      either be sent to the parent task (ideally), or get printed to
673             //      the task's logger. Right now the logger is actually a uvio
674             //      instance, which uses unkillable blocks internally for various
675             //      reasons. This will cause serious trouble if the task is failing
676             //      due to mismanagment of its own kill flag, so calling our own
677             //      logger in its current state is a bit of a problem.
678
679             rterrln!("task '{}' failed at '{}', {}:{}", n, msg_s, file, line);
680
681             if (*task).unwinder.unwinding {
682                 rtabort!("unwinding again");
683             }
684         }
685
686         (*task).unwinder.begin_unwind(msg);
687     }
688 }
689
690 #[cfg(test)]
691 mod test {
692     use super::*;
693     use rt::test::*;
694
695     #[test]
696     fn local_heap() {
697         do run_in_newsched_task() {
698             let a = @5;
699             let b = a;
700             assert!(*a == 5);
701             assert!(*b == 5);
702         }
703     }
704
705     #[test]
706     fn tls() {
707         use local_data;
708         do run_in_newsched_task() {
709             local_data_key!(key: @~str)
710             local_data::set(key, @~"data");
711             assert!(*local_data::get(key, |k| k.map(|k| *k)).unwrap() == ~"data");
712             local_data_key!(key2: @~str)
713             local_data::set(key2, @~"data");
714             assert!(*local_data::get(key2, |k| k.map(|k| *k)).unwrap() == ~"data");
715         }
716     }
717
718     #[test]
719     fn unwind() {
720         do run_in_newsched_task() {
721             let result = spawntask_try(proc()());
722             rtdebug!("trying first assert");
723             assert!(result.is_ok());
724             let result = spawntask_try(proc() fail!());
725             rtdebug!("trying second assert");
726             assert!(result.is_err());
727         }
728     }
729
730     #[test]
731     fn rng() {
732         do run_in_uv_task() {
733             use rand::{rng, Rng};
734             let mut r = rng();
735             let _ = r.next_u32();
736         }
737     }
738
739     #[test]
740     fn logging() {
741         do run_in_uv_task() {
742             info!("here i am. logging in a newsched task");
743         }
744     }
745
746     #[test]
747     fn comm_oneshot() {
748         use comm::*;
749
750         do run_in_newsched_task {
751             let (port, chan) = oneshot();
752             chan.send(10);
753             assert!(port.recv() == 10);
754         }
755     }
756
757     #[test]
758     fn comm_stream() {
759         use comm::*;
760
761         do run_in_newsched_task() {
762             let (port, chan) = stream();
763             chan.send(10);
764             assert!(port.recv() == 10);
765         }
766     }
767
768     #[test]
769     fn comm_shared_chan() {
770         use comm::*;
771
772         do run_in_newsched_task() {
773             let (port, chan) = stream();
774             let chan = SharedChan::new(chan);
775             chan.send(10);
776             assert!(port.recv() == 10);
777         }
778     }
779
780     #[test]
781     fn heap_cycles() {
782         use option::{Option, Some, None};
783
784         do run_in_newsched_task {
785             struct List {
786                 next: Option<@mut List>,
787             }
788
789             let a = @mut List { next: None };
790             let b = @mut List { next: Some(a) };
791
792             a.next = Some(b);
793         }
794     }
795
796     #[test]
797     #[should_fail]
798     fn test_begin_unwind() { begin_unwind("cause", file!(), line!()) }
799 }