#[macro_export]
macro_rules! go {
($func:expr) => {{
unsafe { $crate::coroutine::spawn($func) }
}};
($builder:expr, $func:expr) => {{
use $crate::coroutine::Spawn;
unsafe { $builder.spawn($func) }
}};
($cqueue:expr, $token:expr, $func:expr) => {{
unsafe { $cqueue.add($token, $func) }
}};
}
#[macro_export]
macro_rules! go_with {
($stack_size:expr, $func:expr) => {{
fn _go_check<F, T>(stack_size: usize, f: F) -> F
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
f
}
let f = _go_check($stack_size, $func);
let builder = $crate::coroutine::Builder::new().stack_size($stack_size);
unsafe { builder.spawn(f) }
}};
($name: expr, $stack_size:expr, $func:expr) => {{
fn _go_check<F, T>(name: &str, stack_size: usize, f: F) -> F
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
f
}
let f = _go_check($name, $stack_size, $func);
let builder = $crate::coroutine::Builder::new()
.name($name.to_owned())
.stack_size($stack_size);
unsafe { builder.spawn(f) }
}};
}
#[macro_export]
macro_rules! cqueue_add {
($cqueue:ident, $token:expr, $name:pat = $top:expr => $bottom:expr) => {{
$crate::go!($cqueue, $token, |es| loop {
let $name = $top;
es.send(es.get_token());
$bottom
})
}};
}
#[macro_export]
macro_rules! cqueue_add_oneshot {
($cqueue:ident, $token:expr, $name:pat = $top:expr => $bottom:expr) => {{
$crate::go!($cqueue, $token, |es| {
if let $name = $top{
$bottom
}
es.send(es.get_token());
})
}};
}
#[macro_export]
macro_rules! select {
(
$($name:pat = $top:expr => $bottom:expr), +$(,)?
) => ($crate::select_token!($($name = $top => $bottom), +););
}
#[macro_export]
macro_rules! select_token {
(
$($name:pat = $top:expr => $bottom:expr), +$(,)?
) => ({
$crate::cqueue::scope(|cqueue| {
let mut _token = 0;
$(
$crate::cqueue_add_oneshot!(cqueue, _token, $name = $top => $bottom);
_token += 1;
)+
match cqueue.poll(None) {
Ok(ev) => return ev.token,
_ => unreachable!("select error"),
}
})
});
}
#[macro_export]
macro_rules! join {
(
$($body:expr),+
) => ({
use $crate::coroutine;
coroutine::scope(|s| {
$(
$crate::go!(s, || $body);
)+
})
})
}
#[macro_export]
macro_rules! coroutine_local {
(static $NAME:ident : $t:ty = $e:expr) => {
static $NAME: $crate::LocalKey<$t> = {
fn __init() -> $t {
$e
}
fn __key() -> ::std::any::TypeId {
struct __A;
::std::any::TypeId::of::<__A>()
}
$crate::LocalKey {
__init: __init,
__key: __key,
}
};
};
}