open-coroutine

What is open-coroutine ?

The open-coroutine is a simple, efficient and generic stackful-coroutine library.

Status

Still under development, please do not use this library in the production environment !

Features

0.2.0

0.1.0

How to use this library ?

step1

add dependency to your Cargo.toml ```toml [dependencies]

check https://crates.io/crates/open-coroutine

open-coroutine = "x.y.z" ```

step2

enable hooks ```rust //step2 enable hooks

[open_coroutine::main]

fn main() { //...... } ```

step3

enjoy the performance improvement brought by open-coroutine !

examples

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;

[open_coroutine::main]

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;

[open_coroutine::main]

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!"); } ```