summaryrefslogtreecommitdiff
path: root/src/tokio_util.rs
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-09-18 11:53:16 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-25 17:02:49 -0400
commit4fd2b19f640d19e57511eb142b63e16c879ef6fd (patch)
treed45db774ca5d51ecaac5491aec68db0cbfdcdf27 /src/tokio_util.rs
parent7c128df4a041f3b2c04725a0f5f3320db684d067 (diff)
Make Deno multithreaded.
By using the tokio default runtime. This patch makes all of the ops thread safe. Adds libdeno to JS globals to make for easier testing. Preliminary work for #733.
Diffstat (limited to 'src/tokio_util.rs')
-rw-r--r--src/tokio_util.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/tokio_util.rs b/src/tokio_util.rs
new file mode 100644
index 000000000..de81620ef
--- /dev/null
+++ b/src/tokio_util.rs
@@ -0,0 +1,30 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+
+use futures;
+use futures::Future;
+use tokio;
+use tokio_executor;
+
+pub fn block_on<F, R, E>(future: F) -> Result<R, E>
+where
+ F: Send + 'static + Future<Item = R, Error = E>,
+ R: Send + 'static,
+ E: Send + 'static,
+{
+ let (tx, rx) = futures::sync::oneshot::channel();
+ tokio::spawn(future.then(move |r| tx.send(r).map_err(|_| unreachable!())));
+ rx.wait().unwrap()
+}
+
+// Set the default executor so we can use tokio::spawn(). It's difficult to
+// pass around mut references to the runtime, so using with_default is
+// preferable. Ideally Tokio would provide this function.
+pub fn init<F>(f: F)
+where
+ F: FnOnce(),
+{
+ let rt = tokio::runtime::Runtime::new().unwrap();
+ let mut executor = rt.executor();
+ let mut enter = tokio_executor::enter().expect("Multiple executors at once");
+ tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
+}