The open-coroutine
is a simple, efficient and generic stackful-coroutine library.
Still under development, please do not
use this library in the production
environment !
epoll_event
structrayon
for parallel computing#[open_coroutine::main]
macro[x] hook almost all read
syscall
read syscalls
[x] hook almost all write
syscall
write syscalls
[x] hook other syscall
other syscalls
add dependency to your Cargo.toml
```toml
[dependencies]
open-coroutine = "x.y.z" ```
enable hooks ```rust //step2 enable hooks
fn main() { //...... } ```
enjoy the performance improvement brought by open-coroutine
!
simplest example
run hello example
shell
cargo run --example hello
code below ```rust use opencoroutine::co; use std::os::raw::cvoid; use std::time::Duration;
fn main() { co( |yielder, input: Option<&'static mut cvoid>| { println!("[coroutine1] launched"); input }, None, 4096, ); co( |yielder, input: Option<&'static mut cvoid>| { println!("[coroutine2] launched"); input }, None, 4096, ); std::thread::sleep(Duration::from_millis(50)); println!("scheduler finished successfully!"); } ```
preemptive example
Note: not supported for windows
run preemptive example
shell
cargo run --example preemptive
code below ```rust use opencoroutine::co; use std::os::raw::cvoid; use std::time::Duration;
fn main() { static mut FLAG: bool = true; let handle = co( |yielder, input: Option<&'static mut cvoid>| { println!("[coroutine1] launched"); unsafe { while FLAG { println!("loop"); std::thread::sleep(Duration::frommillis(10)); } } input }, Some(unsafe { std::mem::transmute(1usize) }), 4096, ); co( |yielder, input: Option<&'static mut cvoid>| { println!("[coroutine2] launched"); unsafe { FLAG = false; } input }, None, 4096, ); let result = handle.timeoutjoin(Duration::fromsecs(1)); asserteq!(result.unwrap(), 1); unsafe { assert!(!FLAG) }; println!("preemptive schedule finished successfully!"); } ```