Debian Codegraph @

.cargo/vendor/jobserver/src/wasm.rs

raw
1 use crate::FromEnvErrorInner;
2 use std::io;
3 use std::process::Command;
4 use std::sync::{Arc, Condvar, Mutex};
5 use std::thread::{Builder, JoinHandle};
6
7 #[derive(Debug)]
8 pub struct Client {
9 inner: Arc<Inner>,
10 }
11
12 #[derive(Debug)]
13 struct Inner {
14 count: Mutex<usize>,
15 cvar: Condvar,
16 }
17
18 #[derive(Debug)]
19 pub struct Acquired(());
20
21 impl Client {
22 pub fn new(limit: usize) -> io::Result<Client> {
23 Ok(Client {
24 inner: Arc::new(Inner {
25 count: Mutex::new(limit),
26 cvar: Condvar::new(),
27 }),
28 })
29 }
30
31 pub(crate) unsafe fn open(_s: &str, _check_pipe: bool) -> Result<Client, FromEnvErrorInner> {
32 Err(FromEnvErrorInner::Unsupported)
33 }
34
35 pub fn acquire(&self) -> io::Result<Acquired> {
36 let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner());
37 while *lock == 0 {
38 lock = self
39 .inner
40 .cvar
41 .wait(lock)
42 .unwrap_or_else(|e| e.into_inner());
43 }
44 *lock -= 1;
45 Ok(Acquired(()))
46 }
47
48 pub fn try_acquire(&self) -> io::Result<Option<Acquired>> {
49 let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner());
50 if *lock == 0 {
51 Ok(None)
52 } else {
53 *lock -= 1;
54 Ok(Some(Acquired(())))
55 }
56 }
57
58 pub fn release(&self, _data: Option<&Acquired>) -> io::Result<()> {
59 let mut lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner());
60 *lock += 1;
61 drop(lock);
62 self.inner.cvar.notify_one();
63 Ok(())
64 }
65
66 pub fn string_arg(&self) -> String {
67 panic!(
68 "On this platform there is no cross process jobserver support,
69 so Client::configure is not supported."
70 );
71 }
72
73 pub fn available(&self) -> io::Result<usize> {
74 let lock = self.inner.count.lock().unwrap_or_else(|e| e.into_inner());
75 Ok(*lock)
76 }
77
78 pub fn configure(&self, _cmd: &mut Command) {
79 unreachable!();
80 }
81 }
82
83 #[derive(Debug)]
84 pub struct Helper {
85 thread: JoinHandle<()>,
86 }
87
88 pub(crate) fn spawn_helper(
89 client: crate::Client,
90 state: Arc<super::HelperState>,
91 mut f: Box<dyn FnMut(io::Result<crate::Acquired>) + Send>,
92 ) -> io::Result<Helper> {
93 let thread = Builder::new().spawn(move || {
94 state.for_each_request(|_| f(client.acquire()));
95 })?;
96
97 Ok(Helper { thread })
98 }
99
100 impl Helper {
101 pub fn join(self) {
102 // TODO: this is not correct if the thread is blocked in
103 // `client.acquire()`.
104 drop(self.thread.join());
105 }
106 }