2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-18 14:53:16 -04:00
|
|
|
use futures;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
|
|
|
use std::future::Future;
|
2018-09-18 14:53:16 -04:00
|
|
|
use tokio;
|
2019-04-23 16:27:44 -04:00
|
|
|
use tokio::runtime;
|
|
|
|
|
2019-09-11 20:10:14 -04:00
|
|
|
pub fn create_threadpool_runtime(
|
|
|
|
) -> Result<tokio::runtime::Runtime, tokio::io::Error> {
|
2019-04-23 16:27:44 -04:00
|
|
|
runtime::Builder::new()
|
2019-05-28 09:32:43 -04:00
|
|
|
.panic_handler(|err| std::panic::resume_unwind(err))
|
2019-04-23 16:27:44 -04:00
|
|
|
.build()
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
pub fn run<F>(future: F)
|
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
F: Future<Output = Result<(), ()>> + Send + 'static,
|
2019-03-14 19:17:52 -04:00
|
|
|
{
|
|
|
|
// tokio::runtime::current_thread::run(future)
|
2019-09-11 20:10:14 -04:00
|
|
|
let rt = create_threadpool_runtime().expect("Unable to create Tokio runtime");
|
2019-11-16 19:17:47 -05:00
|
|
|
rt.block_on_all(future.boxed().compat()).unwrap();
|
2019-04-14 16:07:24 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:02:20 -04:00
|
|
|
pub fn run_on_current_thread<F>(future: F)
|
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
F: Future<Output = Result<(), ()>> + Send + 'static,
|
2019-07-31 11:02:20 -04:00
|
|
|
{
|
2019-11-16 19:17:47 -05:00
|
|
|
tokio::runtime::current_thread::run(future.boxed().compat());
|
2019-07-31 11:02:20 -04:00
|
|
|
}
|