mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
move thread worker to a separate crate
This commit is contained in:
parent
4a1ab869b7
commit
193992fd14
9 changed files with 62 additions and 46 deletions
|
@ -5,7 +5,6 @@ mod path_map;
|
|||
mod project_model;
|
||||
pub mod req;
|
||||
mod server_world;
|
||||
pub mod thread_watcher;
|
||||
mod vfs;
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
|
||||
|
|
|
@ -10,6 +10,7 @@ use gen_lsp_server::{
|
|||
use languageserver_types::NumberOrString;
|
||||
use ra_analysis::{Canceled, FileId, LibraryData};
|
||||
use rayon;
|
||||
use thread_worker::Worker;
|
||||
use threadpool::ThreadPool;
|
||||
use rustc_hash::FxHashSet;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
@ -21,7 +22,6 @@ use crate::{
|
|||
project_model::{workspace_loader, CargoWorkspace},
|
||||
req,
|
||||
server_world::{ServerWorld, ServerWorldState},
|
||||
thread_watcher::Worker,
|
||||
vfs::{self, FileEvent},
|
||||
Result,
|
||||
};
|
||||
|
@ -92,8 +92,8 @@ pub fn main_loop(
|
|||
let ws_res = ws_watcher.stop();
|
||||
|
||||
main_res?;
|
||||
fs_res?;
|
||||
ws_res?;
|
||||
fs_res.map_err(|_| format_err!("fs watcher died"))?;
|
||||
ws_res.map_err(|_| format_err!("ws watcher died"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,11 +4,9 @@ use cargo_metadata::{metadata_run, CargoOpt};
|
|||
use ra_syntax::SmolStr;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use failure::{format_err, bail};
|
||||
use thread_worker::{WorkerHandle, Worker};
|
||||
|
||||
use crate::{
|
||||
Result,
|
||||
thread_watcher::{ThreadWatcher, Worker},
|
||||
};
|
||||
use crate::Result;
|
||||
|
||||
/// `CargoWorksapce` represents the logical structure of, well, a Cargo
|
||||
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
||||
|
@ -199,8 +197,8 @@ impl TargetKind {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn workspace_loader() -> (Worker<PathBuf, Result<CargoWorkspace>>, ThreadWatcher) {
|
||||
Worker::<PathBuf, Result<CargoWorkspace>>::spawn(
|
||||
pub fn workspace_loader() -> (Worker<PathBuf, Result<CargoWorkspace>>, WorkerHandle) {
|
||||
thread_worker::spawn::<PathBuf, Result<CargoWorkspace>, _>(
|
||||
"workspace loader",
|
||||
1,
|
||||
|input_receiver, output_sender| {
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
use std::thread;
|
||||
|
||||
use crossbeam_channel::{bounded, unbounded, Receiver, Sender};
|
||||
use drop_bomb::DropBomb;
|
||||
use failure::format_err;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub struct Worker<I, O> {
|
||||
pub inp: Sender<I>,
|
||||
pub out: Receiver<O>,
|
||||
}
|
||||
|
||||
impl<I, O> Worker<I, O> {
|
||||
pub fn spawn<F>(name: &'static str, buf: usize, f: F) -> (Self, ThreadWatcher)
|
||||
where
|
||||
F: FnOnce(Receiver<I>, Sender<O>) + Send + 'static,
|
||||
I: Send + 'static,
|
||||
O: Send + 'static,
|
||||
{
|
||||
let (worker, inp_r, out_s) = worker_chan(buf);
|
||||
let watcher = ThreadWatcher::spawn(name, move || f(inp_r, out_s));
|
||||
(worker, watcher)
|
||||
}
|
||||
|
||||
pub fn stop(self) -> Receiver<O> {
|
||||
self.out
|
||||
}
|
||||
|
||||
pub fn send(&self, item: I) {
|
||||
self.inp.send(item)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThreadWatcher {
|
||||
name: &'static str,
|
||||
thread: thread::JoinHandle<()>,
|
||||
bomb: DropBomb,
|
||||
}
|
||||
|
||||
impl ThreadWatcher {
|
||||
fn spawn(name: &'static str, f: impl FnOnce() + Send + 'static) -> ThreadWatcher {
|
||||
let thread = thread::spawn(f);
|
||||
ThreadWatcher {
|
||||
name,
|
||||
thread,
|
||||
bomb: DropBomb::new(format!("ThreadWatcher {} was not stopped", name)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(mut self) -> Result<()> {
|
||||
log::info!("waiting for {} to finish ...", self.name);
|
||||
let name = self.name;
|
||||
self.bomb.defuse();
|
||||
let res = self
|
||||
.thread
|
||||
.join()
|
||||
.map_err(|_| format_err!("ThreadWatcher {} died", name));
|
||||
match &res {
|
||||
Ok(()) => log::info!("... {} terminated with ok", name),
|
||||
Err(_) => log::error!("... {} terminated with err", name),
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets up worker channels in a deadlock-avoind way.
|
||||
/// If one sets both input and output buffers to a fixed size,
|
||||
/// a worker might get stuck.
|
||||
fn worker_chan<I, O>(buf: usize) -> (Worker<I, O>, Receiver<I>, Sender<O>) {
|
||||
let (input_sender, input_receiver) = bounded::<I>(buf);
|
||||
let (output_sender, output_receiver) = unbounded::<O>();
|
||||
(
|
||||
Worker {
|
||||
inp: input_sender,
|
||||
out: output_receiver,
|
||||
},
|
||||
input_receiver,
|
||||
output_sender,
|
||||
)
|
||||
}
|
|
@ -4,8 +4,7 @@ use std::{
|
|||
};
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::thread_watcher::{ThreadWatcher, Worker};
|
||||
use thread_worker::{WorkerHandle, Worker};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FileEvent {
|
||||
|
@ -18,8 +17,8 @@ pub enum FileEventKind {
|
|||
Add(String),
|
||||
}
|
||||
|
||||
pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatcher) {
|
||||
Worker::<PathBuf, (PathBuf, Vec<FileEvent>)>::spawn(
|
||||
pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
|
||||
thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
|
||||
"roots loader",
|
||||
128,
|
||||
|input_receiver, output_sender| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue