Context utilities in Rust
toml
[dependencies]
context = "*"
or use the dev version on master
toml
[dependencies.context]
git = "https://github.com/zonyitoo/context-rs.git"
```rust
extern crate context; extern crate libc;
use std::mem; use std::boxed::FnBox;
use context::{Context, Stack};
const STACK_SIZE: usize = 2 * 1024 * 1024; // 2MB
extern "C" fn initfn(arg: usize, f: *mut libc::cvoid) -> ! {
// Transmute it back to the Box } fn main() {
// Initialize an empty context
let mut cur = Context::empty(); }
``` Use You have to drop the boxed function inside the initialize function!! The resources allocated inside the initialize function must be released before the last context switch. If you context switch inside your callback function, if you decided not to come back,
you must release all your resources allocated inside your function. This crate supports platforms in The assembly code is in AT&T-style, so currently it only supports // Call it
func();
// The `func` must be destroyed here,
// or it will cause memory leak.
}
// The argument is the context of the main function
let ctx: &Context = unsafe { mem::transmute(arg) };
// Switch back to the main function and will never comeback here
Context::load(ctx);
unreachable!("Should never comeback");
let callback: Box<FnBox()> = Box::new(move|| {
println!("Inside your function!");
});
let mut stk = Stack::new(STACK_SIZE);
let ctx = Context::new(init_fn, unsafe { mem::transmute(&cur) },
Box::into_raw(Box::new(callback)) as *mut libc::c_void, &mut stk);
println!("Before switch");
// Switch!
Context::swap(&mut cur, &ctx);
println!("Back to main function");
cargo run --example simple
to run this code snippet.Notices
*-gnu
target on Windows.