Expand description
Lightweigh green threads & coroutines in stable Rust.
§Getting started
Add greenie to your Cargo.toml:
[dependencies]
greenie = "*"Create greenie main function in src/main.rs:
ⓘ
use greenie::*;
#[greenie_main]
fn main() {
}
// Or you can invoke `create_main`:
fn main() {
create_main(|| {
});
}
§Example
Ping-pong program
ⓘ
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();
}Re-exports§
pub use generator::generator_yield;pub use scheduler::spawn_greenie;pub use scheduler::yield_thread;pub use fiber::Fiber;pub use generator::*;
Modules§
- algorithm
- common
- ctx
- fiber
- generator
- ptr
- Just type that wraps
*mut Tfor easy access toTcontents (unsafe!) - scheduler
Macros§
Functions§
- create_
main - Specify entry point for program that will use greenie.
- thread_
sleep - Puts the current thread to sleep for at least the specified amount of time.
Attribute Macros§
- greenify
- Inserts
thread_yieldin function so programmer don’t need to insert it by hand - greeny_
main