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