Add example of suspended coroutines

This commit is contained in:
Yota Toyama
2019-05-02 06:50:31 +00:00
parent 50097eb073
commit 3f8b5e7aa7
3 changed files with 185 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
extern crate bdwgc_alloc;
extern crate coroutine;
use bdwgc_alloc::Allocator;
use coroutine::asymmetric::Coroutine;
use std::alloc::Layout;
#[global_allocator]
static GLOBAL_ALLOCATOR: Allocator = Allocator;
fn main() {
unsafe { Allocator::initialize() }
Allocator::disable_gc();
let mut handle = Coroutine::spawn(move |me, _| {
let bottom: u8 = 0;
unsafe { Allocator::set_stack_bottom(&bottom) }
Allocator::enable_gc();
me.yield_with(42);
unsafe { Allocator::set_stack_bottom(&bottom) }
Allocator::enable_gc();
loop {
unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
}
});
Allocator::disable_gc();
assert_eq!(handle.resume(0).unwrap(), 42);
handle.resume(0).unwrap();
}