diff --git a/Cargo.lock b/Cargo.lock index 7c56b8f98f6..e0c9f458577 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,14 +178,6 @@ dependencies = [ "url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "cmake" version = "0.1.31" @@ -242,18 +234,6 @@ name = "crossbeam" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "crossbeam-channel" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "crossbeam-epoch 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam-utils 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", - "smallvec 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "crossbeam-deque" version = "0.2.0" @@ -277,19 +257,6 @@ dependencies = [ "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "crossbeam-epoch" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam-utils 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "crossbeam-utils" version = "0.2.2" @@ -298,11 +265,6 @@ dependencies = [ "cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "crossbeam-utils" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "crypto-hash" version = "0.3.1" @@ -964,23 +926,6 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "rand" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", - "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "rand_core" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "rayon" version = "1.0.1" @@ -1070,7 +1015,6 @@ dependencies = [ "cargo 0.30.0 (git+https://github.com/rust-lang/cargo?rev=af9e40c26b4ea2ebd6f31ee86ee61d5ac1c74eb0)", "cargo_metadata 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "clippy_lints 0.0.212 (git+https://github.com/rust-lang-nursery/rust-clippy?rev=f27aaacb9bf1d0d3492f20f92346bb1aaf45e3d8)", - "crossbeam-channel 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1732,7 +1676,6 @@ dependencies = [ "checksum cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efe5c877e17a9c717a0bf3613b2709f723202c4e4675cc8f12926ded29bcb17e" "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" "checksum clippy_lints 0.0.212 (git+https://github.com/rust-lang-nursery/rust-clippy?rev=f27aaacb9bf1d0d3492f20f92346bb1aaf45e3d8)" = "" -"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cmake 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "95470235c31c726d72bf2e1f421adc1e65b9d561bf5529612cbe1a72da1467b3" "checksum commoncrypto 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d056a8586ba25a1e4d61cb090900e495952c7886786fc55f909ab2f819b69007" "checksum commoncrypto-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1fed34f46747aa73dfaa578069fd8279d2818ade2b55f38f22a9401c7f4083e2" @@ -1740,12 +1683,9 @@ dependencies = [ "checksum core-foundation-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b2a53cce0ddcf7e7e1f998738d757d5a3bf08bf799a180e50ebe50d298f52f5a" "checksum crates-io 0.18.0 (git+https://github.com/rust-lang/cargo?rev=af9e40c26b4ea2ebd6f31ee86ee61d5ac1c74eb0)" = "" "checksum crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24ce9782d4d5c53674646a6a4c1863a21a8fc0cb649b3c94dfc16e45071dea19" -"checksum crossbeam-channel 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "258f3c07af0255827670241eacc8b0af7dbfc363df537ad062c6c515ca4a32ee" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" -"checksum crossbeam-epoch 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "48deb8586d997ab13e98fb7e057b232149f9440321c73845b2f4cee483da29bc" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" -"checksum crossbeam-utils 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ea52fab26a99d96cdff39d0ca75c9716125937f5dba2ab83923aaaf5928f684a" "checksum crypto-hash 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "09de9ee0fc255ace04c7fa0763c9395a945c37c8292bb554f8d48361d1dcf1b4" "checksum curl 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "aaf20bbe084f285f215eef2165feed70d6b75ba29cad24469badb853a4a287d0" "checksum curl-sys 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71c63a540a9ee4e15e56c3ed9b11a2f121239b9f6d7b7fe30f616e048148df9a" @@ -1824,8 +1764,6 @@ dependencies = [ "checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035" "checksum racer 2.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "e713729f45f12df5c5e182d39506766f76c09133fb661d3622e0ddf8078911c2" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" -"checksum rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "12397506224b2f93e6664ffc4f664b29be8208e5157d3d90b44f09b5fae470ea" -"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" "checksum rayon 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80e811e76f1dbf68abf87a759083d34600017fc4e10b6bd5ad84a700f9dba4b1" "checksum rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d24ad214285a7729b174ed6d3bcfcb80177807f959d95fafd5bfc5c4f201ac8" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" diff --git a/Cargo.toml b/Cargo.toml index bc93eb1ff10..ca258a4367c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,6 @@ url = "1.1.0" walkdir = "2.1" regex = "1" ordslice = "0.3" -crossbeam-channel = "0.2.1" [dev-dependencies] json = "0.11" diff --git a/src/concurrency.rs b/src/concurrency.rs index 5197972312a..fd9b76dda21 100644 --- a/src/concurrency.rs +++ b/src/concurrency.rs @@ -1,6 +1,7 @@ -use std::{thread}; - -use crossbeam_channel::{bounded, Receiver, Sender}; +use std::{ + thread, + sync::{Arc, Mutex, Condvar}, +}; /// `ConcurrentJob` is a handle for some long-running computation /// off the main thread. It can be used, indirectly, to wait for @@ -18,12 +19,17 @@ use crossbeam_channel::{bounded, Receiver, Sender}; /// a `JobToken` signals that the corresponding job has finished. #[must_use] pub struct ConcurrentJob { - chan: Receiver, + is_done: Arc, } pub struct JobToken { - #[allow(unused)] // for drop - chan: Sender, + is_done: Arc +} + +impl Drop for JobToken { + fn drop(&mut self) { + self.is_done.set() + } } pub struct Jobs { @@ -42,17 +48,8 @@ impl Jobs { /// Blocks the current thread until all pending jobs are finished. pub fn wait_for_all(&mut self) { - while !self.jobs.is_empty() { - let done: usize = { - let chans = self.jobs.iter().map(|j| &j.chan); - select! { - recv(chans, msg, from) => { - assert!(msg.is_none()); - self.jobs.iter().position(|j| &j.chan == from).unwrap() - } - } - }; - drop(self.jobs.swap_remove(done)); + for job in self.jobs.drain(..) { + job.wait(); } } @@ -63,14 +60,18 @@ impl Jobs { impl ConcurrentJob { pub fn new() -> (ConcurrentJob, JobToken) { - let (tx, rx) = bounded(0); - let job = ConcurrentJob { chan: rx }; - let token = JobToken { chan: tx }; + let is_done = Arc::new(AtomicFlag::new()); + let job = ConcurrentJob { is_done: is_done.clone() }; + let token = JobToken { is_done }; (job, token) } + fn wait(&self) { + self.is_done.wait() + } + fn is_completed(&self) -> bool { - is_closed(&self.chan) + self.is_done.is_set() } } @@ -83,18 +84,32 @@ impl Drop for ConcurrentJob { } } -// We don't actually send messages through the channels, -// and instead just check if the channel is closed, -// so we use uninhabited enum as a message type -enum Never {} - -/// Nonblocking -fn is_closed(chan: &Receiver) -> bool { - select! { - recv(chan, msg) => match msg { - None => true, - Some(never) => match never {} +struct AtomicFlag { + flag: Mutex, + cvar: Condvar, +} + +impl AtomicFlag { + fn new() -> AtomicFlag { + AtomicFlag { + flag: Mutex::new(false), + cvar: Condvar::new(), + } + } + + fn is_set(&self) -> bool { + *self.flag.lock().unwrap() + } + + fn set(&self) { + *self.flag.lock().unwrap() = true; + self.cvar.notify_all(); + } + + fn wait(&self) { + let mut is_set = self.flag.lock().unwrap(); + while !*is_set { + is_set = self.cvar.wait(is_set).unwrap(); } - default => false, } } diff --git a/src/main.rs b/src/main.rs index f8b3d09a32d..5495500220e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,8 +43,6 @@ extern crate log; extern crate serde_derive; #[macro_use] extern crate serde_json; -#[macro_use] -extern crate crossbeam_channel; use std::env; use std::sync::Arc;