From fbc1f058ad8e27219c3f3c30fa4b999142c60a5c Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Mon, 5 Aug 2019 23:15:36 +0200 Subject: [PATCH] pulseaudio: Clean up locking a bit --- src/sound.rs | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/sound.rs b/src/sound.rs index 4461f41..1d6da96 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -399,39 +399,37 @@ impl PulseAudioSoundDevice { pub struct PulseAudio { device: Arc>, - dirty: bool, + dirty: Arc>, } impl PulseAudio { - pub fn new(listener: Sender) -> Result>, ::std::io::Error> { + pub fn new(listener: Sender) -> Result { let (tx, rx) = channel(); - let device = PulseAudioSoundDevice::new(tx)?; - let d = device.clone(); - let pa = Arc::new(Mutex::new(PulseAudio { - device: device, - dirty: true, - })); - let p = pa.clone(); + let pa = PulseAudio { + device: PulseAudioSoundDevice::new(tx)?, + dirty: Arc::new(Mutex::new(true)), + }; + let device = pa.device.clone(); + let dirty = pa.dirty.clone(); std::thread::spawn(move || loop { rx.recv().unwrap(); - d.lock().unwrap().get_info().unwrap(); - p.lock().unwrap().dirty = true; + device.lock().unwrap().get_info().unwrap(); + *(dirty.lock().unwrap()) = true; listener.send(true).unwrap(); }); Ok(pa) } } -impl ModuleImpl for Arc> { +impl ModuleImpl for PulseAudio { fn draw( &self, buf: &mut Buffer, bg: &Color, _time: &DateTime, ) -> Result, ::std::io::Error> { - let s = self.lock().unwrap(); - let muted = s.device.lock().unwrap().muted; - let mut vol = s.device.lock().unwrap().volume(); + let muted = self.device.lock().unwrap().muted; + let mut vol = self.device.lock().unwrap().volume(); buf.memset(bg); let c = if muted { Color::new(1.0, 1.0, 0.0, 1.0) @@ -459,9 +457,9 @@ impl ModuleImpl for Arc> { } fn update(&mut self, _time: &DateTime, force: bool) -> Result { - let mut s = self.lock().unwrap(); - if s.dirty || force { - s.dirty = false; + let mut d = self.dirty.lock().unwrap(); + if *d || force { + *d = false; Ok(true) } else { Ok(false) @@ -475,9 +473,7 @@ impl ModuleImpl for Arc> { x: _x, y, } => { - self.lock() - .unwrap() - .device + self.device .lock() .unwrap() .set_volume(y as f32 / 800.0) @@ -485,9 +481,7 @@ impl ModuleImpl for Arc> { } Input::Click { pos: _pos, button } => match button { 273 => { - self.lock() - .unwrap() - .device + self.device .lock() .unwrap() .toggle()