+use lazy_static::lazy_static;
+use tokio::runtime::{self, Runtime};
+
+thread_local! {
+ static HAS_RUNTIME: RefCell<bool> = RefCell::new(false);
+ static IN_TOKIO: RefCell<bool> = RefCell::new(false);
+}
+
+fn is_in_tokio() -> bool {
+ IN_TOKIO.with(|v| *v.borrow())
+}
+
+fn has_runtime() -> bool {
+ HAS_RUNTIME.with(|v| *v.borrow())
+}
+
+struct RuntimeGuard(bool);
+
+impl RuntimeGuard {
+ fn enter() -> Self {
+ Self(HAS_RUNTIME.with(|v| {
+ let old = *v.borrow();
+ *v.borrow_mut() = true;
+ old
+ }))
+ }
+}
+
+impl Drop for RuntimeGuard {
+ fn drop(&mut self) {
+ HAS_RUNTIME.with(|v| {
+ *v.borrow_mut() = self.0;
+ });
+ }
+}
+
+lazy_static! {
+ static ref RUNTIME: Runtime = {
+ runtime::Builder::new()
+ .threaded_scheduler()
+ .enable_all()
+ .on_thread_start(|| IN_TOKIO.with(|v| *v.borrow_mut() = true))
+ .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
+}
+
+/// Associate the current newly spawned thread with the main tokio runtime.
+pub fn enter_runtime<R>(f: impl FnOnce() -> R) -> R {
+ let _guard = RuntimeGuard::enter();
+ get_runtime().enter(f)
+}
+
+/// Block on a synchronous piece of code.
+pub fn block_in_place<R>(fut: impl FnOnce() -> R) -> R {
+ if is_in_tokio() {
+ // we are in an actual tokio worker thread, block it:
+ tokio::task::block_in_place(fut)
+ } else {
+ // we're not inside a tokio worker, so just run the code:
+ fut()
+ }
+}
+
+/// Block on a future in this thread.
+pub fn block_on<R, F>(fut: F) -> R