From 0102a01f76c855da447e25eb81191047a3ca79b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Oct 2018 22:59:54 +0300 Subject: [PATCH] Remove job handle --- Cargo.lock | 1 - crates/ra_analysis/Cargo.toml | 1 - crates/ra_analysis/src/job.rs | 53 ------------------- crates/ra_analysis/src/lib.rs | 4 -- .../ra_lsp_server/src/main_loop/handlers.rs | 21 +------- crates/ra_lsp_server/src/main_loop/mod.rs | 38 ++++++------- 6 files changed, 18 insertions(+), 100 deletions(-) delete mode 100644 crates/ra_analysis/src/job.rs diff --git a/Cargo.lock b/Cargo.lock index bb0feec2ed..f519148834 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -609,7 +609,6 @@ dependencies = [ name = "ra_analysis" version = "0.1.0" dependencies = [ - "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "fst 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "im 12.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml index dd4ec83752..d7ac69fe8d 100644 --- a/crates/ra_analysis/Cargo.toml +++ b/crates/ra_analysis/Cargo.toml @@ -7,7 +7,6 @@ authors = ["Aleksey Kladov "] [dependencies] relative-path = "0.3.7" log = "0.4.2" -crossbeam-channel = "0.2.4" parking_lot = "0.6.3" once_cell = "0.1.5" rayon = "1.0.2" diff --git a/crates/ra_analysis/src/job.rs b/crates/ra_analysis/src/job.rs deleted file mode 100644 index 2871f98390..0000000000 --- a/crates/ra_analysis/src/job.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crossbeam_channel::{bounded, Receiver, Sender}; - -pub struct JobHandle { - job_alive: Receiver, - _job_canceled: Sender, -} - -pub struct JobToken { - _job_alive: Sender, - job_canceled: Receiver, -} - -impl JobHandle { - pub fn new() -> (JobHandle, JobToken) { - let (sender_alive, receiver_alive) = bounded(0); - let (sender_canceled, receiver_canceled) = bounded(0); - let token = JobToken { - _job_alive: sender_alive, - job_canceled: receiver_canceled, - }; - let handle = JobHandle { - job_alive: receiver_alive, - _job_canceled: sender_canceled, - }; - (handle, token) - } - pub fn has_completed(&self) -> bool { - is_closed(&self.job_alive) - } - pub fn cancel(self) {} -} - -impl JobToken { - pub fn is_canceled(&self) -> bool { - is_closed(&self.job_canceled) - } -} - -// 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 {} - } - default => false, - } -} diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 7500310937..28e0a12b20 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -7,8 +7,6 @@ extern crate ra_editor; extern crate ra_syntax; extern crate rayon; extern crate relative_path; -#[macro_use] -extern crate crossbeam_channel; extern crate im; extern crate rustc_hash; extern crate salsa; @@ -16,7 +14,6 @@ extern crate salsa; mod db; mod descriptors; mod imp; -mod job; mod module_map; mod roots; mod symbol_index; @@ -31,7 +28,6 @@ use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}; pub use crate::{ descriptors::FnDescriptor, - job::{JobHandle, JobToken}, }; pub use ra_editor::{ CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 673f1bf7d8..f5dff4c80a 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -7,7 +7,7 @@ use languageserver_types::{ InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, RenameParams, WorkspaceEdit, PrepareRenameResponse }; -use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; +use ra_analysis::{FileId, FoldKind, Query, RunnableKind}; use ra_syntax::text_utils::contains_offset_nonstrict; use serde_json::to_value; @@ -22,7 +22,6 @@ use crate::{ pub fn handle_syntax_tree( world: ServerWorld, params: req::SyntaxTreeParams, - _token: JobToken, ) -> Result { let id = params.text_document.try_conv_with(&world)?; let res = world.analysis().syntax_tree(id); @@ -32,7 +31,6 @@ pub fn handle_syntax_tree( pub fn handle_extend_selection( world: ServerWorld, params: req::ExtendSelectionParams, - _token: JobToken, ) -> Result { let file_id = params.text_document.try_conv_with(&world)?; let file = world.analysis().file_syntax(file_id); @@ -50,7 +48,6 @@ pub fn handle_extend_selection( pub fn handle_find_matching_brace( world: ServerWorld, params: req::FindMatchingBraceParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let file = world.analysis().file_syntax(file_id); @@ -73,7 +70,6 @@ pub fn handle_find_matching_brace( pub fn handle_join_lines( world: ServerWorld, params: req::JoinLinesParams, - _token: JobToken, ) -> Result { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -87,7 +83,6 @@ pub fn handle_join_lines( pub fn handle_on_enter( world: ServerWorld, params: req::TextDocumentPositionParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -101,7 +96,6 @@ pub fn handle_on_enter( pub fn handle_on_type_formatting( world: ServerWorld, params: req::DocumentOnTypeFormattingParams, - _token: JobToken, ) -> Result>> { if params.ch != "=" { return Ok(None); @@ -121,7 +115,6 @@ pub fn handle_on_type_formatting( pub fn handle_document_symbol( world: ServerWorld, params: req::DocumentSymbolParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -160,7 +153,6 @@ pub fn handle_document_symbol( pub fn handle_workspace_symbol( world: ServerWorld, params: req::WorkspaceSymbolParams, - _token: JobToken, ) -> Result>> { let all_symbols = params.query.contains("#"); let libs = params.query.contains("*"); @@ -212,7 +204,6 @@ pub fn handle_workspace_symbol( pub fn handle_goto_definition( world: ServerWorld, params: req::TextDocumentPositionParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -232,7 +223,6 @@ pub fn handle_goto_definition( pub fn handle_parent_module( world: ServerWorld, params: TextDocumentIdentifier, - _token: JobToken, ) -> Result> { let file_id = params.try_conv_with(&world)?; let mut res = Vec::new(); @@ -247,7 +237,6 @@ pub fn handle_parent_module( pub fn handle_runnables( world: ServerWorld, params: req::RunnablesParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -351,7 +340,6 @@ pub fn handle_runnables( pub fn handle_decorations( world: ServerWorld, params: TextDocumentIdentifier, - _token: JobToken, ) -> Result> { let file_id = params.try_conv_with(&world)?; highlight(&world, file_id) @@ -360,7 +348,6 @@ pub fn handle_decorations( pub fn handle_completion( world: ServerWorld, params: req::CompletionParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -392,7 +379,6 @@ pub fn handle_completion( pub fn handle_folding_range( world: ServerWorld, params: FoldingRangeParams, - _token: JobToken, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -425,7 +411,6 @@ pub fn handle_folding_range( pub fn handle_signature_help( world: ServerWorld, params: req::TextDocumentPositionParams, - _token: JobToken, ) -> Result> { use languageserver_types::{ParameterInformation, SignatureInformation}; @@ -464,7 +449,6 @@ pub fn handle_signature_help( pub fn handle_prepare_rename( world: ServerWorld, params: req::TextDocumentPositionParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -486,7 +470,6 @@ pub fn handle_prepare_rename( pub fn handle_rename( world: ServerWorld, params: RenameParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -523,7 +506,6 @@ pub fn handle_rename( pub fn handle_references( world: ServerWorld, params: req::ReferenceParams, - _token: JobToken, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); @@ -539,7 +521,6 @@ pub fn handle_references( pub fn handle_code_action( world: ServerWorld, params: req::CodeActionParams, - _token: JobToken, ) -> Result> { let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index 165f2e78f0..b35ebd38b6 100644 --- a/crates/ra_lsp_server/src/main_loop/mod.rs +++ b/crates/ra_lsp_server/src/main_loop/mod.rs @@ -8,9 +8,9 @@ use gen_lsp_server::{ handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, }; use languageserver_types::NumberOrString; -use ra_analysis::{FileId, JobHandle, JobToken, LibraryData}; +use ra_analysis::{FileId, LibraryData}; use rayon::{self, ThreadPool}; -use rustc_hash::FxHashMap; +use rustc_hash::FxHashSet; use serde::{de::DeserializeOwned, Serialize}; use crate::{ @@ -47,7 +47,7 @@ pub fn main_loop( info!("server initialized, serving requests"); let mut state = ServerWorldState::new(); - let mut pending_requests = FxHashMap::default(); + let mut pending_requests = FxHashSet::default(); let mut subs = Subscriptions::new(); let main_res = main_loop_inner( internal_mode, @@ -92,7 +92,7 @@ fn main_loop_inner( fs_worker: Worker)>, ws_worker: Worker>, state: &mut ServerWorldState, - pending_requests: &mut FxHashMap, + pending_requests: &mut FxHashSet, subs: &mut Subscriptions, ) -> Result<()> { let (libdata_sender, libdata_receiver) = unbounded(); @@ -204,14 +204,13 @@ fn main_loop_inner( fn on_task( task: Task, msg_sender: &Sender, - pending_requests: &mut FxHashMap, + pending_requests: &mut FxHashSet, ) { match task { Task::Respond(response) => { - if let Some(handle) = pending_requests.remove(&response.id) { - assert!(handle.has_completed()); + if pending_requests.remove(&response.id) { + msg_sender.send(RawMessage::Response(response)) } - msg_sender.send(RawMessage::Response(response)) } Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)), } @@ -219,7 +218,7 @@ fn on_task( fn on_request( world: &mut ServerWorldState, - pending_requests: &mut FxHashMap, + pending_requests: &mut FxHashSet, pool: &ThreadPool, sender: &Sender, req: RawRequest, @@ -253,8 +252,8 @@ fn on_request( .on::(handlers::handle_references)? .finish(); match req { - Ok((id, handle)) => { - let inserted = pending_requests.insert(id, handle).is_none(); + Ok(id) => { + let inserted = pending_requests.insert(id); assert!(inserted, "duplicate request: {}", id); Ok(None) } @@ -265,7 +264,7 @@ fn on_request( fn on_notification( msg_sender: &Sender, state: &mut ServerWorldState, - pending_requests: &mut FxHashMap, + pending_requests: &mut FxHashSet, subs: &mut Subscriptions, not: RawNotification, ) -> Result<()> { @@ -277,9 +276,7 @@ fn on_notification( panic!("string id's not supported: {:?}", id); } }; - if let Some(handle) = pending_requests.remove(&id) { - handle.cancel(); - } + pending_requests.remove(&id); return Ok(()); } Err(not) => not, @@ -336,7 +333,7 @@ fn on_notification( struct PoolDispatcher<'a> { req: Option, - res: Option<(u64, JobHandle)>, + res: Option, pool: &'a ThreadPool, world: &'a ServerWorldState, sender: &'a Sender, @@ -345,7 +342,7 @@ struct PoolDispatcher<'a> { impl<'a> PoolDispatcher<'a> { fn on<'b, R>( &'b mut self, - f: fn(ServerWorld, R::Params, JobToken) -> Result, + f: fn(ServerWorld, R::Params) -> Result, ) -> Result<&'b mut Self> where R: req::Request, @@ -358,11 +355,10 @@ impl<'a> PoolDispatcher<'a> { }; match req.cast::() { Ok((id, params)) => { - let (handle, token) = JobHandle::new(); let world = self.world.snapshot(); let sender = self.sender.clone(); self.pool.spawn(move || { - let resp = match f(world, params, token) { + let resp = match f(world, params) { Ok(resp) => RawResponse::ok::(id, &resp), Err(e) => { RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()) @@ -371,14 +367,14 @@ impl<'a> PoolDispatcher<'a> { let task = Task::Respond(resp); sender.send(task); }); - self.res = Some((id, handle)); + self.res = Some(id); } Err(req) => self.req = Some(req), } Ok(self) } - fn finish(&mut self) -> ::std::result::Result<(u64, JobHandle), RawRequest> { + fn finish(&mut self) -> ::std::result::Result { match (self.res.take(), self.req.take()) { (Some(res), None) => Ok(res), (None, Some(req)) => Err(req),