aboutsummaryrefslogtreecommitdiff
path: root/crates/libanalysis/src/job.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libanalysis/src/job.rs')
-rw-r--r--crates/libanalysis/src/job.rs49
1 files changed, 49 insertions, 0 deletions
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 @@
1use crossbeam_channel::{bounded, Receiver, Sender};
2
3pub struct JobHandle {
4 job_alive: Receiver<Never>,
5 _job_canceled: Sender<Never>,
6}
7
8pub struct JobToken {
9 _job_alive: Sender<Never>,
10 job_canceled: Receiver<Never>,
11}
12
13impl JobHandle {
14 pub fn new() -> (JobHandle, JobToken) {
15 let (sender_alive, receiver_alive) = bounded(0);
16 let (sender_canceled, receiver_canceled) = bounded(0);
17 let token = JobToken { _job_alive: sender_alive, job_canceled: receiver_canceled };
18 let handle = JobHandle { job_alive: receiver_alive, _job_canceled: sender_canceled };
19 (handle, token)
20 }
21 pub fn is_alive(&self) -> bool {
22 !is_closed(&self.job_alive)
23 }
24 pub fn cancel(self) {
25 }
26}
27
28impl JobToken {
29 pub fn is_canceled(&self) -> bool {
30 is_closed(&self.job_canceled)
31 }
32}
33
34
35// We don't actually send messages through the channels,
36// and instead just check if the channel is closed,
37// so we use uninhabited enum as a message type
38enum Never {}
39
40/// Nonblocking
41fn is_closed(chan: &Receiver<Never>) -> bool {
42 select! {
43 recv(chan, msg) => match msg {
44 None => true,
45 Some(never) => match never {}
46 }
47 default => false,
48 }
49}