1
0
Fork 0

make Context::new take a stack, instead of creating one

This commit is contained in:
edef 2015-04-15 16:25:09 -04:00
parent 27318bc271
commit 8dc53c3125
5 changed files with 14 additions and 11 deletions

View File

@ -1,14 +1,16 @@
#![feature(test)]
extern crate test;
extern crate lwkt;
use lwkt::Context;
use lwkt::{Context, StackSource};
static mut ctx_slot: *mut Context = 0 as *mut Context;
#[bench]
fn context_new(b: &mut test::Bencher) {
b.iter(|| unsafe {
let mut ctx = Context::new(move || {
let stack = lwkt::os::StackSource::get_stack(4 << 20);
let mut ctx = Context::new(stack, move || {
let ctx_ptr = ctx_slot;
loop {
(*ctx_ptr).swap()

View File

@ -1,14 +1,16 @@
#![feature(test)]
extern crate test;
extern crate lwkt;
use lwkt::Context;
use lwkt::{Context, StackSource};
static mut ctx_slot: *mut Context = 0 as *mut Context;
#[bench]
fn swap(b: &mut test::Bencher) {
unsafe {
let mut ctx = Context::new(move || {
let stack = lwkt::os::StackSource::get_stack(4 << 20);
let mut ctx = Context::new(stack, move || {
let ctx_ptr = ctx_slot;
loop {
(*ctx_ptr).swap()

View File

@ -1,13 +1,15 @@
#![feature(thread_local)]
extern crate lwkt;
use lwkt::Context;
use lwkt::{Context, StackSource};
#[thread_local]
static mut ctx_slot: *mut Context = 0 as *mut Context;
fn main() {
unsafe {
let mut ctx = Context::new(move || {
let stack = lwkt::os::StackSource::get_stack(4 << 20);
let mut ctx = Context::new(stack, move || {
println!("it's alive!");
(*ctx_slot).swap();
});

View File

@ -1,7 +1,5 @@
use core::prelude::*;
use stack::StackSource;
use arch::Registers;
use stack::Stack;
use os;
pub struct Context {
@ -11,8 +9,7 @@ pub struct Context {
impl Context {
#[inline]
pub unsafe fn new<F>(f: F) -> Context where F: FnOnce() + Send + 'static {
let mut stack = os::StackSource::get_stack(4 << 20);
pub unsafe fn new<F>(mut stack: os::Stack, f: F) -> Context where F: FnOnce() + Send + 'static {
let regs = Registers::new(&mut stack, f);
Context {
regs: regs,

View File

@ -20,4 +20,4 @@ pub mod context;
pub mod stack;
mod arch;
mod os;
pub mod os;