Remove job handle

This commit is contained in:
Aleksey Kladov 2018-10-20 22:59:54 +03:00
parent 8bb4380448
commit 0102a01f76
6 changed files with 18 additions and 100 deletions

1
Cargo.lock generated
View File

@ -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)",

View File

@ -7,7 +7,6 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[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"

View File

@ -1,53 +0,0 @@
use crossbeam_channel::{bounded, Receiver, Sender};
pub struct JobHandle {
job_alive: Receiver<Never>,
_job_canceled: Sender<Never>,
}
pub struct JobToken {
_job_alive: Sender<Never>,
job_canceled: Receiver<Never>,
}
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<Never>) -> bool {
select! {
recv(chan, msg) => match msg {
None => true,
Some(never) => match never {}
}
default => false,
}
}

View File

@ -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,

View File

@ -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<String> {
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<req::ExtendSelectionResult> {
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<Vec<Position>> {
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<req::SourceChange> {
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<Option<req::SourceChange>> {
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<Option<Vec<TextEdit>>> {
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<Option<req::DocumentSymbolResponse>> {
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<Option<Vec<SymbolInformation>>> {
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<Option<req::GotoDefinitionResponse>> {
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<Vec<Location>> {
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<Vec<req::Runnable>> {
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<Vec<Decoration>> {
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<Option<req::CompletionResponse>> {
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<Option<Vec<FoldingRange>>> {
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<Option<req::SignatureHelp>> {
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<Option<PrepareRenameResponse>> {
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<Option<WorkspaceEdit>> {
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<Option<Vec<Location>>> {
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<Option<CodeActionResponse>> {
let file_id = params.text_document.try_conv_with(&world)?;
let line_index = world.analysis().file_line_index(file_id);

View File

@ -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<PathBuf, (PathBuf, Vec<FileEvent>)>,
ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
state: &mut ServerWorldState,
pending_requests: &mut FxHashMap<u64, JobHandle>,
pending_requests: &mut FxHashSet<u64>,
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<RawMessage>,
pending_requests: &mut FxHashMap<u64, JobHandle>,
pending_requests: &mut FxHashSet<u64>,
) {
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<u64, JobHandle>,
pending_requests: &mut FxHashSet<u64>,
pool: &ThreadPool,
sender: &Sender<Task>,
req: RawRequest,
@ -253,8 +252,8 @@ fn on_request(
.on::<req::References>(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<RawMessage>,
state: &mut ServerWorldState,
pending_requests: &mut FxHashMap<u64, JobHandle>,
pending_requests: &mut FxHashSet<u64>,
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<RawRequest>,
res: Option<(u64, JobHandle)>,
res: Option<u64>,
pool: &'a ThreadPool,
world: &'a ServerWorldState,
sender: &'a Sender<Task>,
@ -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<R::Result>,
f: fn(ServerWorld, R::Params) -> Result<R::Result>,
) -> Result<&'b mut Self>
where
R: req::Request,
@ -358,11 +355,10 @@ impl<'a> PoolDispatcher<'a> {
};
match req.cast::<R>() {
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::<R>(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<u64, RawRequest> {
match (self.res.take(), self.req.take()) {
(Some(res), None) => Ok(res),
(None, Some(req)) => Err(req),