2015-04-16 20:06:57 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
2015-04-16 18:08:44 +08:00
|
|
|
// Copyright (c) 2015, edef <edef@edef.eu>
|
|
|
|
// See the LICENSE file included in this distribution.
|
2015-04-16 15:53:49 +08:00
|
|
|
use core::marker::PhantomData;
|
2015-08-25 12:42:26 +08:00
|
|
|
|
|
|
|
use void::Void;
|
|
|
|
|
2015-04-16 02:22:28 +08:00
|
|
|
use arch::Registers;
|
2015-04-16 04:26:51 +08:00
|
|
|
use stack;
|
2015-04-16 14:30:10 +08:00
|
|
|
use debug::StackId;
|
2014-12-24 13:12:39 +08:00
|
|
|
|
2015-04-16 22:12:26 +08:00
|
|
|
/// Context is the heart of libfringe.
|
|
|
|
/// A context represents a saved thread of execution, along with a stack.
|
|
|
|
/// It can be swapped into and out of with the swap method,
|
|
|
|
/// and once you're done with it, you can get the stack back through unwrap.
|
|
|
|
///
|
|
|
|
/// Every operation is unsafe, because libfringe can't make any guarantees
|
|
|
|
/// about the state of the context.
|
2015-04-16 19:10:34 +08:00
|
|
|
#[derive(Debug)]
|
2015-04-16 15:53:49 +08:00
|
|
|
pub struct Context<'a, Stack: stack::Stack> {
|
2014-12-23 11:24:40 +08:00
|
|
|
regs: Registers,
|
2015-04-16 14:30:10 +08:00
|
|
|
_stack_id: StackId,
|
2015-04-16 15:53:49 +08:00
|
|
|
stack: Stack,
|
|
|
|
_ref: PhantomData<&'a ()>
|
2014-12-23 11:24:40 +08:00
|
|
|
}
|
|
|
|
|
2015-04-16 19:06:30 +08:00
|
|
|
unsafe impl<'a, Stack> Send for Context<'a, Stack>
|
|
|
|
where Stack: stack::Stack + Send {}
|
|
|
|
|
2015-04-16 15:53:49 +08:00
|
|
|
impl<'a, Stack> Context<'a, Stack> where Stack: stack::Stack {
|
2015-04-16 22:12:26 +08:00
|
|
|
/// Create a new Context. When it is swapped into,
|
|
|
|
/// it will call the passed closure.
|
2015-01-14 15:31:17 +08:00
|
|
|
#[inline]
|
2015-04-16 15:53:49 +08:00
|
|
|
pub unsafe fn new<F>(mut stack: Stack, f: F) -> Context<'a, Stack>
|
2015-08-25 12:42:26 +08:00
|
|
|
where F: FnOnce() -> Void + Send + 'a {
|
2015-04-16 14:30:10 +08:00
|
|
|
let stack_id = StackId::register(&mut stack);
|
2015-04-16 02:22:28 +08:00
|
|
|
let regs = Registers::new(&mut stack, f);
|
2014-12-23 11:24:40 +08:00
|
|
|
Context {
|
2015-01-14 15:31:17 +08:00
|
|
|
regs: regs,
|
2015-04-16 14:30:10 +08:00
|
|
|
_stack_id: stack_id,
|
2015-04-16 15:53:49 +08:00
|
|
|
stack: stack,
|
|
|
|
_ref: PhantomData
|
2014-12-23 11:24:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:12:26 +08:00
|
|
|
/// Switch to the context, saving the current thread of execution there.
|
2014-12-23 11:24:40 +08:00
|
|
|
#[inline(always)]
|
2015-01-14 15:31:17 +08:00
|
|
|
pub unsafe fn swap(&mut self) {
|
2015-04-16 02:22:28 +08:00
|
|
|
self.regs.swap()
|
2014-12-23 11:24:40 +08:00
|
|
|
}
|
2015-04-16 09:54:56 +08:00
|
|
|
|
2015-04-16 22:12:26 +08:00
|
|
|
/// Unwrap the context, returning the stack it contained.
|
2015-04-16 18:43:09 +08:00
|
|
|
#[inline]
|
2015-04-16 19:49:13 +08:00
|
|
|
pub unsafe fn unwrap(self) -> Stack {
|
2015-04-16 09:54:56 +08:00
|
|
|
self.stack
|
|
|
|
}
|
2014-12-23 11:24:40 +08:00
|
|
|
}
|