Add coroutine support

This commit is contained in:
Yota Toyama
2019-04-29 15:46:01 +00:00
parent 56d2f54060
commit a63a24d4d2
8 changed files with 234 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ extern crate libc;
mod error;
use libc::{c_int, c_void, size_t};
use libc::{c_char, c_int, c_void, size_t};
use std::alloc::{GlobalAlloc, Layout};
const GC_SUCCESS: c_int = 0;
@@ -16,17 +16,28 @@ struct GcStackBase {
#[link(name = "gc", kind = "static")]
extern "C" {
fn GC_allow_register_threads() -> c_void;
fn GC_disable() -> c_void;
fn GC_enable() -> c_void;
fn GC_free(ptr: *mut c_void);
fn GC_get_stack_base(stack_base: *mut GcStackBase) -> c_int;
fn GC_init() -> c_void;
fn GC_malloc(size: size_t) -> *mut c_void;
fn GC_register_my_thread(stack_base: *const GcStackBase) -> c_int;
fn GC_set_stack_bottom(thread: *const c_void, stack_bottom: *const c_char);
fn GC_unregister_my_thread();
}
pub struct Allocator;
impl Allocator {
pub fn disable_gc() {
unsafe { GC_disable() };
}
pub fn enable_gc() {
unsafe { GC_enable() };
}
pub unsafe fn initialize() {
GC_init();
GC_allow_register_threads();
@@ -47,6 +58,13 @@ impl Allocator {
Ok(())
}
pub unsafe fn set_stack_bottom(bottom: *const u8) {
GC_set_stack_bottom(
std::mem::transmute(nix::sys::pthread::pthread_self()),
std::mem::transmute(bottom),
);
}
pub unsafe fn unregister_current_thread() {
GC_unregister_my_thread()
}