From 983de30a567f2cb4d9e28e12702e509ca713da62 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 30 Aug 2019 20:23:28 +0300 Subject: inline thread-worker --- crates/ra_lsp_server/Cargo.toml | 4 +-- crates/ra_lsp_server/src/lib.rs | 1 + crates/ra_lsp_server/src/project_model.rs | 4 +-- crates/ra_lsp_server/src/thread_worker.rs | 49 +++++++++++++++++++++++++++++++ crates/thread_worker/Cargo.toml | 10 ------- crates/thread_worker/src/lib.rs | 49 ------------------------------- 6 files changed, 52 insertions(+), 65 deletions(-) create mode 100644 crates/ra_lsp_server/src/thread_worker.rs delete mode 100644 crates/thread_worker/Cargo.toml delete mode 100644 crates/thread_worker/src/lib.rs (limited to 'crates') diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 4f834519c..eb4812633 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -15,9 +15,8 @@ log = "0.4.3" lsp-types = { version = "0.60.0", features = ["proposed"] } rustc-hash = "1.0" parking_lot = "0.9.0" - +jod-thread = "0.1.0" ra_vfs = "0.3.0" -thread_worker = { path = "../thread_worker" } ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } ra_ide_api = { path = "../ra_ide_api" } @@ -27,7 +26,6 @@ ra_prof = { path = "../ra_prof" } ra_vfs_glob = { path = "../ra_vfs_glob" } [dev-dependencies] -jod-thread = "0.1.0" tempfile = "3" test_utils = { path = "../test_utils" } diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 2c5d7c72d..69a577b3e 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -8,6 +8,7 @@ mod project_model; pub mod req; pub mod config; mod world; +mod thread_worker; pub type Result = std::result::Result>; pub use crate::{ diff --git a/crates/ra_lsp_server/src/project_model.rs b/crates/ra_lsp_server/src/project_model.rs index ad59cde64..6234563f2 100644 --- a/crates/ra_lsp_server/src/project_model.rs +++ b/crates/ra_lsp_server/src/project_model.rs @@ -1,8 +1,6 @@ use std::path::PathBuf; -use thread_worker::Worker; - -use crate::Result; +use crate::{thread_worker::Worker, Result}; pub use ra_project_model::{ CargoWorkspace, Package, ProjectWorkspace, Sysroot, Target, TargetKind, diff --git a/crates/ra_lsp_server/src/thread_worker.rs b/crates/ra_lsp_server/src/thread_worker.rs new file mode 100644 index 000000000..68e5c124d --- /dev/null +++ b/crates/ra_lsp_server/src/thread_worker.rs @@ -0,0 +1,49 @@ +//! Small utility to correctly spawn crossbeam-channel based worker threads. + +use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; + +/// A wrapper around event-processing thread with automatic shutdown semantics. +pub struct Worker { + // XXX: field order is significant here. + // + // In Rust, fields are dropped in the declaration order, and we rely on this + // here. We must close input first, so that the `thread` (who holds the + // opposite side of the channel) noticed shutdown. Then, we must join the + // thread, but we must keep out alive so that the thread does not panic. + // + // Note that a potential problem here is that we might drop some messages + // from receiver on the floor. This is ok for rust-analyzer: we have only a + // single client, so, if we are shutting down, nobody is interested in the + // unfinished work anyway! + sender: Sender, + _thread: jod_thread::JoinHandle<()>, + receiver: Receiver, +} + +impl Worker { + pub fn spawn(name: &'static str, buf: usize, f: F) -> Worker + where + F: FnOnce(Receiver, Sender) + Send + 'static, + I: Send + 'static, + O: Send + 'static, + { + // Set up worker channels in a deadlock-avoiding way. If one sets both input + // and output buffers to a fixed size, a worker might get stuck. + let (sender, input_receiver) = bounded::(buf); + let (output_sender, receiver) = unbounded::(); + let _thread = jod_thread::Builder::new() + .name(name.to_string()) + .spawn(move || f(input_receiver, output_sender)) + .expect("failed to spawn a thread"); + Worker { sender, _thread, receiver } + } +} + +impl Worker { + pub fn sender(&self) -> &Sender { + &self.sender + } + pub fn receiver(&self) -> &Receiver { + &self.receiver + } +} diff --git a/crates/thread_worker/Cargo.toml b/crates/thread_worker/Cargo.toml deleted file mode 100644 index e3babbf8d..000000000 --- a/crates/thread_worker/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -edition = "2018" -name = "thread_worker" -version = "0.1.0" -authors = ["rust-analyzer developers"] - -[dependencies] -jod-thread = "0.1.0" -crossbeam-channel = "0.3.5" -log = "0.4.3" diff --git a/crates/thread_worker/src/lib.rs b/crates/thread_worker/src/lib.rs deleted file mode 100644 index 68e5c124d..000000000 --- a/crates/thread_worker/src/lib.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Small utility to correctly spawn crossbeam-channel based worker threads. - -use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; - -/// A wrapper around event-processing thread with automatic shutdown semantics. -pub struct Worker { - // XXX: field order is significant here. - // - // In Rust, fields are dropped in the declaration order, and we rely on this - // here. We must close input first, so that the `thread` (who holds the - // opposite side of the channel) noticed shutdown. Then, we must join the - // thread, but we must keep out alive so that the thread does not panic. - // - // Note that a potential problem here is that we might drop some messages - // from receiver on the floor. This is ok for rust-analyzer: we have only a - // single client, so, if we are shutting down, nobody is interested in the - // unfinished work anyway! - sender: Sender, - _thread: jod_thread::JoinHandle<()>, - receiver: Receiver, -} - -impl Worker { - pub fn spawn(name: &'static str, buf: usize, f: F) -> Worker - where - F: FnOnce(Receiver, Sender) + Send + 'static, - I: Send + 'static, - O: Send + 'static, - { - // Set up worker channels in a deadlock-avoiding way. If one sets both input - // and output buffers to a fixed size, a worker might get stuck. - let (sender, input_receiver) = bounded::(buf); - let (output_sender, receiver) = unbounded::(); - let _thread = jod_thread::Builder::new() - .name(name.to_string()) - .spawn(move || f(input_receiver, output_sender)) - .expect("failed to spawn a thread"); - Worker { sender, _thread, receiver } - } -} - -impl Worker { - pub fn sender(&self) -> &Sender { - &self.sender - } - pub fn receiver(&self) -> &Receiver { - &self.receiver - } -} -- cgit v1.2.3