From 8dc53c3125b4cc36268199de8cea9a62202aa12b Mon Sep 17 00:00:00 2001 From: edef Date: Wed, 15 Apr 2015 16:25:09 -0400 Subject: [PATCH] make Context::new take a stack, instead of creating one --- benches/context_new.rs | 6 ++++-- benches/swap.rs | 6 ++++-- examples/basic.rs | 6 ++++-- src/context.rs | 5 +---- src/lib.rs | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/benches/context_new.rs b/benches/context_new.rs index ec2546e..4eec654 100644 --- a/benches/context_new.rs +++ b/benches/context_new.rs @@ -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() diff --git a/benches/swap.rs b/benches/swap.rs index 18dbb1e..b6eb363 100644 --- a/benches/swap.rs +++ b/benches/swap.rs @@ -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() diff --git a/examples/basic.rs b/examples/basic.rs index af753a5..a421f13 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -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(); }); diff --git a/src/context.rs b/src/context.rs index 3fa8b7e..8ffcb30 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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) -> Context where F: FnOnce() + Send + 'static { - let mut stack = os::StackSource::get_stack(4 << 20); + pub unsafe fn new(mut stack: os::Stack, f: F) -> Context where F: FnOnce() + Send + 'static { let regs = Registers::new(&mut stack, f); Context { regs: regs, diff --git a/src/lib.rs b/src/lib.rs index eb7cefa..62eb963 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,4 +20,4 @@ pub mod context; pub mod stack; mod arch; -mod os; +pub mod os;