@@ -6,6 +6,7 @@ use std::os::raw::{c_char, c_void};
6
6
use std:: ptr:: null;
7
7
use std:: sync:: mpsc:: { channel, Sender } ;
8
8
use std:: sync:: Mutex ;
9
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
9
10
use std:: time:: Duration ;
10
11
use std:: { mem, slice, thread} ;
11
12
@@ -556,7 +557,6 @@ impl RateListener {
556
557
557
558
/// Unregister this listener to stop receiving notifications
558
559
pub fn unregister ( & mut self ) -> Result < ( ) , Error > {
559
- // Add our sample rate change listener callback.
560
560
if self . rate_listener . is_some ( ) {
561
561
let status = unsafe {
562
562
AudioObjectRemovePropertyListener (
@@ -595,10 +595,10 @@ impl RateListener {
595
595
}
596
596
}
597
597
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.
599
599
/// Only implemented for macOS, not iOS.
600
600
pub struct AliveListener {
601
- alive : Mutex < bool > ,
601
+ alive : Box < AtomicBool > ,
602
602
device_id : AudioDeviceID ,
603
603
property_address : AudioObjectPropertyAddress ,
604
604
alive_listener : Option <
@@ -613,18 +613,16 @@ impl Drop for AliveListener {
613
613
}
614
614
615
615
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.
619
617
pub fn new ( device_id : AudioDeviceID ) -> AliveListener {
620
- // Add our error listener callback.
618
+ // Add our listener callback.
621
619
let property_address = AudioObjectPropertyAddress {
622
620
mSelector : kAudioDevicePropertyDeviceIsAlive,
623
621
mScope : kAudioObjectPropertyScopeGlobal,
624
622
mElement : kAudioObjectPropertyElementMaster,
625
623
} ;
626
624
AliveListener {
627
- alive : Mutex :: new ( true ) ,
625
+ alive : Box :: new ( AtomicBool :: new ( true ) ) ,
628
626
device_id,
629
627
property_address,
630
628
alive_listener : None ,
@@ -655,10 +653,7 @@ impl AliveListener {
655
653
& data_size as * const _ as * mut _ ,
656
654
& alive as * const _ as * mut _ ,
657
655
) ;
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 ) ;
662
657
result
663
658
}
664
659
@@ -678,7 +673,6 @@ impl AliveListener {
678
673
679
674
/// Unregister this listener to stop receiving notifications
680
675
pub fn unregister ( & mut self ) -> Result < ( ) , Error > {
681
- // Add our sample rate change listener callback.
682
676
if self . alive_listener . is_some ( ) {
683
677
let status = unsafe {
684
678
AudioObjectRemovePropertyListener (
@@ -696,8 +690,6 @@ impl AliveListener {
696
690
697
691
/// Check if the device is still alive.
698
692
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 )
702
694
}
703
695
}
0 commit comments