(index<- ) ./libgreen/coroutine.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Wed Apr 9 17:27:02 2014
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 // Coroutines represent nothing more than a context and a stack
12 // segment.
13
14 use context::Context;
15 use stack::{StackPool, Stack};
16
17 /// A coroutine is nothing more than a (register context, stack) pair.
18 pub struct Coroutine {
19 /// The segment of stack on which the task is currently running or
20 /// if the task is blocked, on which the task will resume
21 /// execution.
22 ///
23 /// Servo needs this to be public in order to tell SpiderMonkey
24 /// about the stack bounds.
25 pub current_stack_segment: Stack,
26
27 /// Always valid if the task is alive and not running.
28 pub saved_context: Context
29 }
30
31 impl Coroutine {
32 pub fn empty() -> Coroutine {
33 Coroutine {
34 current_stack_segment: unsafe { Stack::dummy_stack() },
35 saved_context: Context::empty()
36 }
37 }
38
39 /// Destroy coroutine and try to reuse std::stack segment.
40 pub fn recycle(self, stack_pool: &mut StackPool) {
41 let Coroutine { current_stack_segment, .. } = self;
42 stack_pool.give_stack(current_stack_segment);
43 }
44 }