Skip to content

Commit 7dea8ed

Browse files
committed
Clean up
1 parent 8744c74 commit 7dea8ed

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/audio_unit/macos_helpers.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::os::raw::{c_char, c_void};
66
use std::ptr::null;
77
use std::sync::mpsc::{channel, Sender};
88
use std::sync::Mutex;
9+
use std::sync::atomic::{AtomicBool, Ordering};
910
use std::time::Duration;
1011
use std::{mem, slice, thread};
1112

@@ -556,7 +557,6 @@ impl RateListener {
556557

557558
/// Unregister this listener to stop receiving notifications
558559
pub fn unregister(&mut self) -> Result<(), Error> {
559-
// Add our sample rate change listener callback.
560560
if self.rate_listener.is_some() {
561561
let status = unsafe {
562562
AudioObjectRemovePropertyListener(
@@ -595,10 +595,10 @@ impl RateListener {
595595
}
596596
}
597597

598-
/// Use an AliveListener to get notified when a device is disconnected.
598+
/// An AliveListener is used to get notified when a device is disconnected.
599599
/// Only implemented for macOS, not iOS.
600600
pub struct AliveListener {
601-
alive: Mutex<bool>,
601+
alive: Box<AtomicBool>,
602602
device_id: AudioDeviceID,
603603
property_address: AudioObjectPropertyAddress,
604604
alive_listener: Option<
@@ -613,18 +613,16 @@ impl Drop for AliveListener {
613613
}
614614

615615
impl AliveListener {
616-
/// Create a new ErrorListener for the given AudioDeviceID.
617-
/// If a sync Sender is provided, then events will be pushed to that channel.
618-
/// If not, they will be stored in an internal queue that will need to be polled.
616+
/// Create a new AliveListener for the given AudioDeviceID.
619617
pub fn new(device_id: AudioDeviceID) -> AliveListener {
620-
// Add our error listener callback.
618+
// Add our listener callback.
621619
let property_address = AudioObjectPropertyAddress {
622620
mSelector: kAudioDevicePropertyDeviceIsAlive,
623621
mScope: kAudioObjectPropertyScopeGlobal,
624622
mElement: kAudioObjectPropertyElementMaster,
625623
};
626624
AliveListener {
627-
alive: Mutex::new(true),
625+
alive: Box::new(AtomicBool::new(true)),
628626
device_id,
629627
property_address,
630628
alive_listener: None,
@@ -655,10 +653,7 @@ impl AliveListener {
655653
&data_size as *const _ as *mut _,
656654
&alive as *const _ as *mut _,
657655
);
658-
println!("store alive {}", alive);
659-
let mut shared_alive = self_ptr.alive.lock().unwrap();
660-
*shared_alive = alive > 0;
661-
println!("stored");
656+
self_ptr.alive.store(alive > 0, Ordering::Relaxed);
662657
result
663658
}
664659

@@ -678,7 +673,6 @@ impl AliveListener {
678673

679674
/// Unregister this listener to stop receiving notifications
680675
pub fn unregister(&mut self) -> Result<(), Error> {
681-
// Add our sample rate change listener callback.
682676
if self.alive_listener.is_some() {
683677
let status = unsafe {
684678
AudioObjectRemovePropertyListener(
@@ -696,8 +690,6 @@ impl AliveListener {
696690

697691
/// Check if the device is still alive.
698692
pub fn is_alive(&self) -> bool {
699-
let alive = self.alive.lock().unwrap();
700-
println!("read alive {}", alive);
701-
*alive
693+
self.alive.load(Ordering::Relaxed)
702694
}
703695
}

0 commit comments

Comments
 (0)