Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Remove jobs from LsService
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jul 16, 2018
1 parent 9b5dc85 commit 0eb2238
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,14 @@ impl InitActionContext {
self.build(&self.current_project, priority, out);
}

fn add_job(&self, job: ConcurrentJob) {
pub fn add_job(&self, job: ConcurrentJob) {
self.jobs.lock().unwrap().add(job);
}

pub fn wait_for_background_jobs(&self) {
self.jobs.lock().unwrap().wait_for_all();
}

/// Block until any builds and analysis tasks are complete.
fn block_on_build(&self) {
self.build_queue.block_on_build();
Expand Down
26 changes: 13 additions & 13 deletions src/server/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ macro_rules! define_dispatch_request_enum {
#[allow(large_enum_variant)] // seems ok for a short lived macro-enum
pub enum DispatchRequest {
$(
$request_type(Request<$request_type>, InitActionContext),
$request_type(Request<$request_type>),
)*
}

$(
impl From<(Request<$request_type>, InitActionContext)> for DispatchRequest {
fn from((req, ctx): (Request<$request_type>, InitActionContext)) -> Self {
DispatchRequest::$request_type(req, ctx)
impl From<Request<$request_type>> for DispatchRequest {
fn from(req: Request<$request_type>) -> Self {
DispatchRequest::$request_type(req)
}
}
)*

impl DispatchRequest {
fn handle<O: Output>(self, out: &O) {
fn handle<O: Output>(self, ctx: InitActionContext, out: &O) {
match self {
$(
DispatchRequest::$request_type(req, ctx) => {
DispatchRequest::$request_type(req) => {
let Request { id, params, received, .. } = req;
let timeout = $request_type::timeout();

Expand Down Expand Up @@ -112,19 +112,19 @@ define_dispatch_request_enum!(
/// Requests dispatched this way are automatically timed out & avoid
/// processing if have already timed out before starting.
pub struct Dispatcher {
sender: mpsc::Sender<(DispatchRequest, JobToken)>,
sender: mpsc::Sender<(DispatchRequest, InitActionContext, JobToken)>,
}

impl Dispatcher {
/// Creates a new `Dispatcher` starting a new thread and channel
pub fn new<O: Output>(out: O) -> Self {
let (sender, receiver) = mpsc::channel::<(DispatchRequest, JobToken)>();
let (sender, receiver) = mpsc::channel::<(DispatchRequest, InitActionContext, JobToken)>();

thread::Builder::new()
.name("dispatch-worker".into())
.spawn(move || {
while let Ok((request, token)) = receiver.recv() {
request.handle(&out);
while let Ok((request, ctx, token)) = receiver.recv() {
request.handle(ctx, &out);
drop(token);
}
})
Expand All @@ -134,12 +134,12 @@ impl Dispatcher {
}

/// Sends a request to the dispatch-worker thread, does not block
pub fn dispatch<R: Into<DispatchRequest>>(&mut self, request: R) -> ConcurrentJob {
pub fn dispatch<R: Into<DispatchRequest>>(&mut self, request: R, ctx: InitActionContext) {
let (job, token) = ConcurrentJob::new();
if let Err(err) = self.sender.send((request.into(), token)) {
ctx.add_job(job);
if let Err(err) = self.sender.send((request.into(), ctx, token)) {
debug!("Failed to dispatch request: {:?}", err);
}
job
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ pub struct LsService<O: Output> {
output: O,
ctx: ActionContext,
dispatcher: Dispatcher,
jobs: Jobs,
}

impl<O: Output> LsService<O> {
Expand All @@ -151,7 +150,6 @@ impl<O: Output> LsService<O> {
output,
ctx: ActionContext::new(analysis, vfs, config),
dispatcher,
jobs: Jobs::new(),
}
}

Expand Down Expand Up @@ -223,8 +221,7 @@ impl<O: Output> LsService<O> {
<$request as LSPRequest>::METHOD => {
let request: Request<$request> = msg.parse_as_request()?;
if let Ok(ctx) = self.ctx.inited() {
let job = self.dispatcher.dispatch((request, ctx));
self.jobs.add(job);
self.dispatcher.dispatch(request, ctx);
}
else {
warn!(
Expand Down Expand Up @@ -341,7 +338,12 @@ impl<O: Output> LsService<O> {
}

pub fn wait_for_background_jobs(&mut self) {
self.jobs.wait_for_all()
match &self.ctx {
ActionContext::Init(ctx) => {
ctx.wait_for_background_jobs()
}
ActionContext::Uninit(_) => {}
}
}
}

Expand Down

0 comments on commit 0eb2238

Please sign in to comment.