Coroutine library in Rust
[dependencies.coroutine]
git = "https://github.com/rustcc/coroutine-rs.git"
or
[dependencies]
coroutine = "*"
Basic usage of Coroutine
extern crate coroutine;
use coroutine::Coroutine;
fn main() {
// Spawn a new coroutine
let coro = Coroutine::spawn(move|| {
println!("1. Hello in coroutine!");
// Yield back to it's parent
Coroutine::sched();
println!("3. We are back!!");
// Spawn a new coroutine
Coroutine::spawn(move|| {
println!("4. Begin counting ...");
for i in 0..5 {
println!("Counting {}", i);
// Yield to it's parent
Coroutine::sched();
}
println!("5. Counting finished");
}).join().ok().expect("Failed to join");
println!("6. Good bye");
});
// Resume `coro`
coro.resume().ok().expect("Failed to resume");
println!("2. We are here!");
// Resume the coroutine
coro.resume().ok().expect("Failed to resume");
println!("7. Back to main.");
}
This program will print the following to the console
1. Hello in coroutine!
2. We are here!
3. We are back!!
4. Begin counting ...
Counting 0
Counting 1
Counting 2
Counting 3
Counting 4
5. Counting finished
6. Good bye
7. Back to main.
For more detail, please run cargo doc --open
.
-
Feature
enable-clonable-handle
is enabled by default. The Coroutine is thread-safe, so the handle (coroutine::Handle
) is clonable. -
If the feature
enable-clonable-handle
is disable, the handle will become unique, which may improve context switch performance because no lock is required.
-
Basic single threaded coroutine support
-
Clonable coroutine handle
-
Unique coroutine handle (for special usage)
-
Thread-safe: can only resume a coroutine in one thread simultaneously
-
Multithreaded scheduler with work stealing feature
-
Asynchronous I/O supports
-
Currently this crate can only be built with Rust nightly because of some unstable features.
-
Basically it supports arm, i686, mips, mipsel and x86_64 platforms, but we have only tested in
-
OS X 10.10.*, x86_64, nightly
-
ArchLinux, x86_64, nightly
-
- The Rust developers (context switch ASM from libgreen)