open-coroutine 0.2.0

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

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

  • use correct epoll_event struct

  • use rayon for parallel computing

  • support #[open_coroutine::main] macro

  • hook almost all read syscall

    • recv
    • readv
    • pread
    • preadv
    • recvfrom
    • recvmsg
  • hook almost all write syscall

    • send
    • write
    • writev
    • sendto
    • sendmsg
    • pwrite
    • pwritev
  • hook other syscall

    • sleep
    • usleep
    • nanosleep
    • connect
    • listen
    • accept
    • shutdown
    • poll
    • select

0.1.0

  • basic suspend/resume supported
  • use jemalloc as memory pool
  • higher level coroutine abstraction supported
  • preemptive scheduling supported
  • work stealing supported
  • sleep system call hooks supported

How to use this library ?

step1

add dependency to your Cargo.toml

[dependencies]
# check https://crates.io/crates/open-coroutine
open-coroutine = "x.y.z"

step2

enable hooks

//step2 enable hooks
#[open_coroutine::main]
fn main() {
    //......
}

step3

enjoy the performance improvement brought by open-coroutine !

examples

run hello example

cargo run --example hello

code below

use open_coroutine::co;
use std::os::raw::c_void;
use std::time::Duration;

#[open_coroutine::main]
fn main() {
    co(
        |_yielder, input: Option<&'static mut c_void>| {
            println!("[coroutine1] launched");
            input
        },
        None,
        4096,
    );
    co(
        |_yielder, input: Option<&'static mut c_void>| {
            println!("[coroutine2] launched");
            input
        },
        None,
        4096,
    );
    std::thread::sleep(Duration::from_millis(50));
    println!("scheduler finished successfully!");
}

Note: not supported for windows

run preemptive example

cargo run --example preemptive

code below

use open_coroutine::co;
use std::os::raw::c_void;
use std::time::Duration;

#[open_coroutine::main]
fn main() {
    static mut FLAG: bool = true;
    let handle = co(
        |_yielder, input: Option<&'static mut c_void>| {
            println!("[coroutine1] launched");
            unsafe {
                while FLAG {
                    println!("loop");
                    std::thread::sleep(Duration::from_millis(10));
                }
            }
            input
        },
        Some(unsafe { std::mem::transmute(1usize) }),
        4096,
    );
    co(
        |_yielder, input: Option<&'static mut c_void>| {
            println!("[coroutine2] launched");
            unsafe {
                FLAG = false;
            }
            input
        },
        None,
        4096,
    );
    let result = handle.timeout_join(Duration::from_secs(1));
    assert_eq!(result.unwrap(), 1);
    unsafe { assert!(!FLAG) };
    println!("preemptive schedule finished successfully!");
}