2019-09-03 09:16:29 +00:00
|
|
|
//! Helpers for quirks of the current tokio runtime.
|
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
use std::cell::RefCell;
|
2019-09-03 09:16:29 +00:00
|
|
|
use std::future::Future;
|
2020-01-21 09:24:52 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::task::{Context, Poll, RawWaker, Waker};
|
|
|
|
use std::thread::{self, Thread};
|
2019-09-03 09:16:29 +00:00
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use tokio::runtime::{self, Runtime};
|
|
|
|
|
|
|
|
thread_local! {
|
2020-02-17 08:48:22 +00:00
|
|
|
static BLOCKING: RefCell<bool> = RefCell::new(false);
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_in_tokio() -> bool {
|
2020-02-13 07:56:37 +00:00
|
|
|
tokio::runtime::Handle::try_current()
|
|
|
|
.is_ok()
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 08:48:22 +00:00
|
|
|
fn is_blocking() -> bool {
|
|
|
|
BLOCKING.with(|v| *v.borrow())
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 08:48:22 +00:00
|
|
|
struct BlockingGuard(bool);
|
2020-01-20 11:52:22 +00:00
|
|
|
|
2020-02-17 08:48:22 +00:00
|
|
|
impl BlockingGuard {
|
|
|
|
fn set() -> Self {
|
|
|
|
Self(BLOCKING.with(|v| {
|
2020-01-20 11:52:22 +00:00
|
|
|
let old = *v.borrow();
|
|
|
|
*v.borrow_mut() = true;
|
|
|
|
old
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 08:48:22 +00:00
|
|
|
impl Drop for BlockingGuard {
|
2020-01-20 11:52:22 +00:00
|
|
|
fn drop(&mut self) {
|
2020-02-17 08:48:22 +00:00
|
|
|
BLOCKING.with(|v| {
|
2020-01-20 11:52:22 +00:00
|
|
|
*v.borrow_mut() = self.0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref RUNTIME: Runtime = {
|
|
|
|
runtime::Builder::new()
|
|
|
|
.threaded_scheduler()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.expect("failed to spawn tokio runtime")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get or create the current main tokio runtime.
|
|
|
|
///
|
|
|
|
/// This makes sure that tokio's worker threads are marked for us so that we know whether we
|
|
|
|
/// can/need to use `block_in_place` in our `block_on` helper.
|
|
|
|
pub fn get_runtime() -> &'static Runtime {
|
|
|
|
&RUNTIME
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Block on a synchronous piece of code.
|
|
|
|
pub fn block_in_place<R>(fut: impl FnOnce() -> R) -> R {
|
2020-02-17 08:48:22 +00:00
|
|
|
// don't double-exit the context (tokio doesn't like that)
|
|
|
|
// also, if we're not actually in a tokio-worker we must not use block_in_place() either
|
|
|
|
if is_blocking() || !is_in_tokio() {
|
2020-01-20 11:52:22 +00:00
|
|
|
fut()
|
2020-02-17 08:48:22 +00:00
|
|
|
} else {
|
|
|
|
// we are in an actual tokio worker thread, block it:
|
|
|
|
tokio::task::block_in_place(move || {
|
|
|
|
let _guard = BlockingGuard::set();
|
|
|
|
fut()
|
|
|
|
})
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Block on a future in this thread.
|
2020-01-20 13:09:24 +00:00
|
|
|
pub fn block_on<F: Future>(fut: F) -> F::Output {
|
2020-02-17 08:48:22 +00:00
|
|
|
// don't double-exit the context (tokio doesn't like that)
|
|
|
|
if is_blocking() {
|
2020-01-21 09:24:52 +00:00
|
|
|
block_on_local_future(fut)
|
2020-02-17 08:48:22 +00:00
|
|
|
} else if is_in_tokio() {
|
|
|
|
// inside a tokio worker we need to tell tokio that we're about to really block:
|
|
|
|
tokio::task::block_in_place(move || {
|
|
|
|
let _guard = BlockingGuard::set();
|
|
|
|
block_on_local_future(fut)
|
|
|
|
})
|
2020-01-20 11:52:22 +00:00
|
|
|
} else {
|
|
|
|
// not a worker thread, not associated with a runtime, make sure we have a runtime (spawn
|
2020-02-17 08:48:22 +00:00
|
|
|
// it on demand if necessary), then enter it
|
|
|
|
let _guard = BlockingGuard::set();
|
|
|
|
get_runtime().enter(move || block_on_local_future(fut))
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-03 09:16:29 +00:00
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
/*
|
|
|
|
fn block_on_impl<F>(mut fut: F) -> F::Output
|
|
|
|
where
|
|
|
|
F: Future + Send,
|
|
|
|
F::Output: Send + 'static,
|
|
|
|
{
|
|
|
|
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
|
|
let fut_ptr = &mut fut as *mut F as usize; // hack to not require F to be 'static
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let fut: F = unsafe { std::ptr::read(fut_ptr as *mut F) };
|
|
|
|
tx
|
|
|
|
.send(fut.await)
|
|
|
|
.map_err(drop)
|
|
|
|
.expect("failed to send block_on result to channel")
|
|
|
|
});
|
|
|
|
|
|
|
|
futures::executor::block_on(async move {
|
|
|
|
rx.await.expect("failed to receive block_on result from channel")
|
2019-09-03 09:16:29 +00:00
|
|
|
})
|
2020-01-20 11:52:22 +00:00
|
|
|
std::mem::forget(fut);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// This used to be our tokio main entry point. Now this just calls out to `block_on` for
|
|
|
|
/// compatibility, which will perform all the necessary tasks on-demand anyway.
|
2020-01-20 13:09:24 +00:00
|
|
|
pub fn main<F: Future>(fut: F) -> F::Output {
|
2020-01-20 11:52:22 +00:00
|
|
|
block_on(fut)
|
2019-09-03 09:16:29 +00:00
|
|
|
}
|
2020-01-21 09:24:52 +00:00
|
|
|
|
|
|
|
fn block_on_local_future<F: Future>(mut fut: F) -> F::Output {
|
|
|
|
use std::pin::Pin;
|
|
|
|
let mut fut = unsafe { Pin::new_unchecked(&mut fut) };
|
|
|
|
|
|
|
|
let waker = Arc::new(thread::current());
|
|
|
|
let waker = thread_waker_clone(Arc::into_raw(waker) as *const ());
|
|
|
|
let waker = unsafe { Waker::from_raw(waker) };
|
|
|
|
let mut context = Context::from_waker(&waker);
|
|
|
|
loop {
|
|
|
|
match fut.as_mut().poll(&mut context) {
|
|
|
|
Poll::Ready(out) => return out,
|
2020-01-21 09:37:16 +00:00
|
|
|
Poll::Pending => thread::park(),
|
2020-01-21 09:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const THREAD_WAKER_VTABLE: std::task::RawWakerVTable = std::task::RawWakerVTable::new(
|
|
|
|
thread_waker_clone,
|
|
|
|
thread_waker_wake,
|
|
|
|
thread_waker_wake_by_ref,
|
|
|
|
thread_waker_drop,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn thread_waker_clone(this: *const ()) -> RawWaker {
|
|
|
|
let this = unsafe { Arc::from_raw(this as *const Thread) };
|
|
|
|
let cloned = Arc::clone(&this);
|
|
|
|
let _ = Arc::into_raw(this);
|
|
|
|
|
|
|
|
RawWaker::new(Arc::into_raw(cloned) as *const (), &THREAD_WAKER_VTABLE)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thread_waker_wake(this: *const ()) {
|
|
|
|
let this = unsafe { Arc::from_raw(this as *const Thread) };
|
|
|
|
this.unpark();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thread_waker_wake_by_ref(this: *const ()) {
|
|
|
|
let this = unsafe { Arc::from_raw(this as *const Thread) };
|
|
|
|
this.unpark();
|
|
|
|
let _ = Arc::into_raw(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thread_waker_drop(this: *const ()) {
|
|
|
|
let this = unsafe { Arc::from_raw(this as *const Thread) };
|
|
|
|
drop(this);
|
|
|
|
}
|