From efa6a952b4cb9c9f139822fbf9781a610bba3813 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Aug 2018 10:27:38 +0300 Subject: cancelation tokens --- crates/libanalysis/src/job.rs | 49 +++++++++++++++++++++++++++++++++++++++++++ crates/libanalysis/src/lib.rs | 4 ++++ 2 files changed, 53 insertions(+) create mode 100644 crates/libanalysis/src/job.rs (limited to 'crates/libanalysis/src') diff --git a/crates/libanalysis/src/job.rs b/crates/libanalysis/src/job.rs new file mode 100644 index 000000000..4d393b915 --- /dev/null +++ b/crates/libanalysis/src/job.rs @@ -0,0 +1,49 @@ +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 is_alive(&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/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs index c25d31f4b..20ddb69d6 100644 --- a/crates/libanalysis/src/lib.rs +++ b/crates/libanalysis/src/lib.rs @@ -7,10 +7,13 @@ extern crate libeditor; extern crate fst; extern crate rayon; extern crate relative_path; +#[macro_use] +extern crate crossbeam_channel; mod symbol_index; mod module_map; mod imp; +mod job; use std::sync::Arc; @@ -22,6 +25,7 @@ pub use libeditor::{ StructureNode, LineIndex, FileSymbol, Runnable, RunnableKind, HighlightedRange, CompletionItem, }; +pub use job::{JobToken, JobHandle}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct FileId(pub u32); -- cgit v1.2.3