2013-12-12 20:01:59 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Coroutines represent nothing more than a context and a stack
|
|
|
|
// segment.
|
|
|
|
|
|
|
|
use context::Context;
|
2013-11-13 04:21:38 -06:00
|
|
|
use stack::{StackPool, Stack};
|
2013-12-12 20:01:59 -06:00
|
|
|
|
|
|
|
/// A coroutine is nothing more than a (register context, stack) pair.
|
|
|
|
pub struct Coroutine {
|
|
|
|
/// The segment of stack on which the task is currently running or
|
|
|
|
/// if the task is blocked, on which the task will resume
|
|
|
|
/// execution.
|
|
|
|
///
|
|
|
|
/// Servo needs this to be public in order to tell SpiderMonkey
|
|
|
|
/// about the stack bounds.
|
2013-11-13 04:21:38 -06:00
|
|
|
current_stack_segment: Stack,
|
2013-12-12 20:01:59 -06:00
|
|
|
|
|
|
|
/// Always valid if the task is alive and not running.
|
|
|
|
saved_context: Context
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Coroutine {
|
|
|
|
pub fn empty() -> Coroutine {
|
|
|
|
Coroutine {
|
2014-01-18 16:50:49 -06:00
|
|
|
current_stack_segment: unsafe { Stack::dummy_stack() },
|
2013-12-12 20:01:59 -06:00
|
|
|
saved_context: Context::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy coroutine and try to reuse std::stack segment.
|
|
|
|
pub fn recycle(self, stack_pool: &mut StackPool) {
|
|
|
|
let Coroutine { current_stack_segment, .. } = self;
|
2013-11-13 04:21:38 -06:00
|
|
|
stack_pool.give_stack(current_stack_segment);
|
2013-12-12 20:01:59 -06:00
|
|
|
}
|
|
|
|
}
|