Skip to content

Commit

Permalink
Replacing threadpool with futures::executor for runtime-polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
svartalf committed Aug 9, 2019
1 parent 5bd2ff7 commit f1670b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .ci/lints-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ stages:
- script: |
rustup component add clippy --toolchain nightly
cargo +nightly clippy --all-targets --all-features -- -D warnings
cargo +nightly clippy --all-targets --no-default-features--features runtime-polyfill -- -D warnings
cargo +nightly clippy --all-targets --no-default-features--features runtime-tokio -- -D warnings
displayName: Run clippy
continueOnError: true
3 changes: 1 addition & 2 deletions heim-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ heim-common = { version = "0.0.6", path = "../heim-common" }
cfg-if = "0.1.9"

# `runtime-polyfill` feature dependencies
threadpool = { version = "1.7.1", optional = true }
lazy_static = { version = "1.3.0", optional = true }
futures-preview = { version = "0.3.0-alpha.17", optional = true }

Expand All @@ -28,5 +27,5 @@ version-sync = "0.8"

[features]
default = ["runtime-polyfill"]
runtime-polyfill = ["threadpool", "lazy_static", "futures-preview"]
runtime-polyfill = ["lazy_static", "futures-preview"]
runtime-tokio = ["tokio", "tokio-fs"]
26 changes: 19 additions & 7 deletions heim-runtime/src/shims/sync/pool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fmt::Debug;
use std::sync::Mutex;

use futures::executor::ThreadPool;
use futures::channel::oneshot;
use futures::future::Future;
use threadpool::ThreadPool;
use futures::future::{self, Future};
use futures::task::Spawn;

use heim_common::prelude::*;

Expand All @@ -16,20 +17,31 @@ pub struct FuturePool(Mutex<ThreadPool>);

impl FuturePool {
pub fn new() -> FuturePool {
FuturePool(Mutex::new(ThreadPool::default()))
let inner = ThreadPool::builder()
.name_prefix("heim-")
.create()
.expect("Misconfigured thread pool");

FuturePool(Mutex::new(inner))
}

pub fn spawn<F, T>(&self, f: F) -> impl Future<Output = T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + Debug + 'static,
{
let pool = self.0.lock().expect("Thread pool mutex is poisoned");

let (tx, rx) = oneshot::channel();
pool.execute(move || {
let _ = tx.send(f());
});
let fut: future::FutureObj<()> = future::lazy(|_| {
let _ = tx.send(f());
})
.boxed()
.into();

{
let mut pool = self.0.lock().expect("Futures pool mutex is poisoned");
let _ = pool.spawn_obj(fut);
}

rx.map(|res| res.expect("Runtime future was canceled"))
}
Expand Down

0 comments on commit f1670b5

Please sign in to comment.