]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/task.rs
auto merge of #10977 : brson/rust/androidtest, r=brson
[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 io::Writer;
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;
30 use rt::context::Context;
31 use rt::env;
32 use rt::kill::Death;
33 use rt::local::Local;
34 use rt::logging::StdErrLogger;
35 use rt::sched::{Scheduler, SchedHandle};
36 use rt::stack::{StackSegment, StackPool};
37 use send_str::SendStr;
38 use task::TaskResult;
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 pub struct Unwinder {
96     unwinding: bool,
97     cause: Option<~Any>
98 }
99
100 impl Unwinder {
101     fn result(&mut self) -> TaskResult {
102         if self.unwinding {
103             Err(self.cause.take().unwrap())
104         } else {
105             Ok(())
106         }
107     }
108 }
109
110 impl Task {
111
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>,
115                              f: proc(),
116                              home: SchedHome)
117                              -> ~Task {
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,
122                                                      stack_size,
123                                                      home,
124                                                      f);
125         running_task.get().sched = Some(sched);
126         new_task
127     }
128
129     pub fn build_child(stack_size: Option<uint>, f: proc()) -> ~Task {
130         Task::build_homed_child(stack_size, f, AnySched)
131     }
132
133     pub fn build_homed_root(stack_size: Option<uint>,
134                             f: proc(),
135                             home: SchedHome)
136                             -> ~Task {
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,
140                                              stack_size,
141                                              home,
142                                              f);
143         running_task.get().sched = Some(sched);
144         new_task
145     }
146
147     pub fn build_root(stack_size: Option<uint>, f: proc()) -> ~Task {
148         Task::build_homed_root(stack_size, f, AnySched)
149     }
150
151     pub fn new_sched_task() -> Task {
152         Task {
153             heap: LocalHeap::new(),
154             gc: GarbageCollector,
155             storage: LocalStorage(None),
156             logger: None,
157             unwinder: Unwinder { unwinding: false, cause: None },
158             death: Death::new(),
159             destroyed: false,
160             coroutine: Some(Coroutine::empty()),
161             name: None,
162             sched: None,
163             task_type: SchedTask,
164             borrow_list: None,
165             stdout_handle: None,
166             nasty_deschedule_lock: unsafe { Mutex::new() },
167         }
168     }
169
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)
174     }
175
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)
181     }
182
183     pub fn new_root_homed(stack_pool: &mut StackPool,
184                           stack_size: Option<uint>,
185                           home: SchedHome,
186                           start: proc()) -> Task {
187         Task {
188             heap: LocalHeap::new(),
189             gc: GarbageCollector,
190             storage: LocalStorage(None),
191             logger: None,
192             unwinder: Unwinder { unwinding: false, cause: None },
193             death: Death::new(),
194             destroyed: false,
195             name: None,
196             coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
197             sched: None,
198             task_type: GreenTask(Some(home)),
199             borrow_list: None,
200             stdout_handle: None,
201             nasty_deschedule_lock: unsafe { Mutex::new() },
202         }
203     }
204
205     pub fn new_child_homed(&mut self,
206                            stack_pool: &mut StackPool,
207                            stack_size: Option<uint>,
208                            home: SchedHome,
209                            start: proc()) -> Task {
210         Task {
211             heap: LocalHeap::new(),
212             gc: GarbageCollector,
213             storage: LocalStorage(None),
214             logger: None,
215             unwinder: Unwinder { unwinding: false, cause: None },
216             death: Death::new(),
217             destroyed: false,
218             name: None,
219             coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
220             sched: None,
221             task_type: GreenTask(Some(home)),
222             borrow_list: None,
223             stdout_handle: None,
224             nasty_deschedule_lock: unsafe { Mutex::new() },
225         }
226     }
227
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);
232             }
233             SchedTask => {
234                 rtabort!("type error: used SchedTask as GreenTask");
235             }
236         }
237     }
238
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();
243                 return out;
244             }
245             SchedTask => {
246                 rtabort!("type error: used SchedTask as GreenTask");
247             }
248         }
249     }
250
251     pub fn run(&mut self, f: ||) {
252         rtdebug!("run called on task: {}", borrow::to_uint(self));
253
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(|| {
257
258             // Run the task main function, then do some cleanup.
259             f.finally(|| {
260
261                 // First, destroy task-local storage. This may run user dtors.
262                 //
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.
274                 //
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...
280                 self.storage.take();
281
282                 // Destroy remaining boxes. Also may run user dtors.
283                 unsafe { cleanup::annihilate(); }
284
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() {
289                     Some(handle) => {
290                         let mut handle = handle;
291                         handle.flush();
292                     }
293                     None => {}
294                 }
295                 self.logger.take();
296             })
297         });
298
299         // Cleanup the dynamic borrowck debugging info
300         borrowck::clear_task_borrow_list();
301
302         self.death.collect_failure(self.unwinder.result());
303         self.destroyed = true;
304     }
305
306     // New utility functions for homes.
307
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()
313             }
314             GreenTask(None) => {
315                 rtabort!("task without home");
316             }
317             SchedTask => {
318                 // Awe yea
319                 rtabort!("type error: expected: GreenTask, found: SchedTask");
320             }
321         }
322     }
323
324     pub fn homed(&self) -> bool {
325         match self.task_type {
326             GreenTask(Some(AnySched)) => { false }
327             GreenTask(Some(Sched(SchedHandle { .. }))) => { true }
328             GreenTask(None) => {
329                 rtabort!("task without home");
330             }
331             SchedTask => {
332                 rtabort!("type error: expected: GreenTask, found: SchedTask");
333             }
334         }
335     }
336
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 ****");
346                 sched_run_anything
347             }
348             GreenTask(Some(Sched(SchedHandle { sched_id: ref id, ..}))) => {
349                 rtdebug!("homed task in sched check ****");
350                 *id == sched_id
351             }
352             GreenTask(None) => {
353                 rtabort!("task without home");
354             }
355             SchedTask => {
356                 rtabort!("type error: expected: GreenTask, found: SchedTask");
357             }
358         }
359     }
360 }
361
362 impl Drop for Task {
363     fn drop(&mut self) {
364         rtdebug!("called drop for a task: {}", borrow::to_uint(self));
365         rtassert!(self.destroyed);
366
367         unsafe { self.nasty_deschedule_lock.destroy(); }
368     }
369 }
370
371 // Coroutines represent nothing more than a context and a stack
372 // segment.
373
374 impl Coroutine {
375
376     pub fn new(stack_pool: &mut StackPool,
377                stack_size: Option<uint>,
378                start: proc())
379                -> Coroutine {
380         let stack_size = match stack_size {
381             Some(size) => size,
382             None => env::min_stack()
383         };
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);
387         Coroutine {
388             current_stack_segment: stack,
389             saved_context: initial_context
390         }
391     }
392
393     pub fn empty() -> Coroutine {
394         Coroutine {
395             current_stack_segment: StackSegment::new(0),
396             saved_context: Context::empty()
397         }
398     }
399
400     fn build_start_wrapper(start: proc()) -> proc() {
401         let wrapper: proc() = proc() {
402             // First code after swap to this new context. Run our
403             // cleanup job.
404             unsafe {
405
406                 // Again - might work while safe, or it might not.
407                 {
408                     let mut sched = Local::borrow(None::<Scheduler>);
409                     sched.get().run_cleanup_job();
410                 }
411
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();
418
419                 let mut start_cell = Some(start);
420                 (*task).run(|| {
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();
432                     start();
433                 });
434             }
435
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()
441         };
442         return wrapper;
443     }
444
445     /// Destroy coroutine and try to reuse stack segment.
446     pub fn recycle(self, stack_pool: &mut StackPool) {
447         match self {
448             Coroutine { current_stack_segment, .. } => {
449                 stack_pool.give_segment(current_stack_segment);
450             }
451         }
452     }
453
454 }
455
456
457 // Just a sanity check to make sure we are catching a Rust-thrown exception
458 static UNWIND_TOKEN: uintptr_t = 839147;
459
460 impl Unwinder {
461     pub fn try(&mut self, f: ||) {
462         use unstable::raw::Closure;
463
464         unsafe {
465             let closure: Closure = transmute(f);
466             let code = transmute(closure.code);
467             let env = transmute(closure.env);
468
469             let token = rust_try(try_fn, code, env);
470             assert!(token == 0 || token == UNWIND_TOKEN);
471         }
472
473         extern fn try_fn(code: *c_void, env: *c_void) {
474             unsafe {
475                 let closure: Closure = Closure {
476                     code: transmute(code),
477                     env: transmute(env),
478                 };
479                 let closure: || = transmute(closure);
480                 closure();
481             }
482         }
483
484         extern {
485             fn rust_try(f: extern "C" fn(*c_void, *c_void),
486                         code: *c_void,
487                         data: *c_void) -> uintptr_t;
488         }
489     }
490
491     pub fn begin_unwind(&mut self, cause: ~Any) -> ! {
492         self.unwinding = true;
493         self.cause = Some(cause);
494         unsafe {
495             rust_begin_unwind(UNWIND_TOKEN);
496             return transmute(());
497         }
498         extern {
499             fn rust_begin_unwind(token: uintptr_t);
500         }
501     }
502 }
503
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;
515     use rt::task::Task;
516     use rt::local::Local;
517     use unstable::intrinsics;
518
519     unsafe {
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);
527
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
531         // be considered:
532         //
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.
539         //
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.
546         //
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.
552         //
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:
555         //
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
561
562         if in_green_task_context() {
563             let mut task = Local::borrow(None::<Task>);
564             let n = task.get()
565                         .name
566                         .as_ref()
567                         .map(|n| n.as_slice())
568                         .unwrap_or("<unnamed>");
569
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);
576         } else {
577             rterrln!("stack overflow in non-task context");
578         }
579
580         intrinsics::abort();
581     }
582 }
583
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) -> ! {
588     use c_str::CString;
589     use cast::transmute;
590
591     #[inline]
592     fn static_char_ptr(p: *c_char) -> &'static str {
593         let s = unsafe { CString::new(p, false) };
594         match s.as_str() {
595             Some(s) => unsafe { transmute::<&str, &'static str>(s) },
596             None => rtabort!("message wasn't utf8?")
597         }
598     }
599
600     let msg = static_char_ptr(msg);
601     let file = static_char_ptr(file);
602
603     begin_unwind(msg, file, line as uint)
604 }
605
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) -> ! {
608     use any::AnyRefExt;
609     use rt::in_green_task_context;
610     use rt::local::Local;
611     use rt::task::Task;
612     use str::Str;
613     use unstable::intrinsics;
614
615     unsafe {
616         let task: *mut Task;
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
622         // failing.
623         let msg = ~msg as ~Any;
624
625         {
626             //let msg: &Any = msg;
627             let msg_s = match msg.as_ref::<&'static str>() {
628                 Some(s) => *s,
629                 None => match msg.as_ref::<~str>() {
630                     Some(s) => s.as_slice(),
631                     None => "~Any",
632                 }
633             };
634
635             if !in_green_task_context() {
636                 rterrln!("failed in non-task context at '{}', {}:{}",
637                          msg_s, file, line);
638                 intrinsics::abort();
639             }
640
641             task = Local::unsafe_borrow();
642             let n = (*task).name.as_ref().map(|n| n.as_slice()).unwrap_or("<unnamed>");
643
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.
651
652             rterrln!("task '{}' failed at '{}', {}:{}", n, msg_s, file, line);
653
654             if (*task).unwinder.unwinding {
655                 rtabort!("unwinding again");
656             }
657         }
658
659         (*task).unwinder.begin_unwind(msg);
660     }
661 }
662
663 #[cfg(test)]
664 mod test {
665     use super::*;
666     use rt::test::*;
667     use prelude::*;
668
669     #[test]
670     fn local_heap() {
671         do run_in_newsched_task() {
672             let a = @5;
673             let b = a;
674             assert!(*a == 5);
675             assert!(*b == 5);
676         }
677     }
678
679     #[test]
680     fn tls() {
681         use local_data;
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");
689         }
690     }
691
692     #[test]
693     fn unwind() {
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());
701         }
702     }
703
704     #[test]
705     fn rng() {
706         do run_in_uv_task() {
707             use rand::{rng, Rng};
708             let mut r = rng();
709             let _ = r.next_u32();
710         }
711     }
712
713     #[test]
714     fn logging() {
715         do run_in_uv_task() {
716             info!("here i am. logging in a newsched task");
717         }
718     }
719
720     #[test]
721     fn comm_stream() {
722         do run_in_newsched_task() {
723             let (port, chan) = Chan::new();
724             chan.send(10);
725             assert!(port.recv() == 10);
726         }
727     }
728
729     #[test]
730     fn comm_shared_chan() {
731         do run_in_newsched_task() {
732             let (port, chan) = SharedChan::new();
733             chan.send(10);
734             assert!(port.recv() == 10);
735         }
736     }
737
738     #[test]
739     fn heap_cycles() {
740         use option::{Option, Some, None};
741
742         do run_in_newsched_task {
743             struct List {
744                 next: Option<@mut List>,
745             }
746
747             let a = @mut List { next: None };
748             let b = @mut List { next: Some(a) };
749
750             a.next = Some(b);
751         }
752     }
753
754     #[test]
755     #[should_fail]
756     fn test_begin_unwind() { begin_unwind("cause", file!(), line!()) }
757 }