#yield #points #generator #green #insert #thread #greenie

macro greenie-proc

Insert yield points into your code

2 unstable releases

0.2.0 Jan 10, 2020
0.1.0 Jan 7, 2020

#23 in #green

23 downloads per month
Used in greenie

MIT license

5KB
91 lines

greenie

Simple green threads in Rust programming language.

Features

  • Generators in stable Rust!
  • Synchronization primitives: Mutex,Condvar others will be implemented later ( see TODO ).
  • Fast.
  • Semi-automatic scheduling using greenify macro that inserts yield points in your functions.

TODO

  • Preemptive scheduling.
  • Implement RwLock.
  • Futures.

Example

Condvar and Mutex example:

use greenie::channel::*;

use greenie::{greeny_main, Fiber};
#[greeny_main]
fn main() {
    let chan_1 = Channel::<&'static str>::new(2);
    let chan_2 = Channel::<&'static str>::new(2);

    let fping = Fiber::new_capture(
        |chan_1, chan_2| {
            chan_1.send("ping");
            println!("{}", chan_2.recv().unwrap());
            chan_1.send("ping");
            println!("{}", chan_2.recv().unwrap());
            chan_1.send("ping");
            println!("{}", chan_2.recv().unwrap());
        },
        (chan_1.clone(), chan_2.clone()),
    );
    let fpong = Fiber::new_capture(
        |chan_1, chan_2| {
            chan_2.send("pong");
            println!("{}", chan_1.recv().unwrap());
            chan_2.send("pong");
            println!("{}", chan_1.recv().unwrap());
            chan_2.send("pong");
            println!("{}", chan_1.recv().unwrap());
        },
        (chan_1.clone(), chan_2.clone()),
    );

    fpong.start().unwrap();
    fping.start().unwrap();
}

For more examples read documentation or examples/

Dependencies

~1.5MB
~33K SLoC