Skip to content

Commit 21fa84e

Browse files
committed
update to atomics
1 parent 293120b commit 21fa84e

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

opentelemetry-sdk/src/trace/provider.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,6 @@ mod tests {
385385
use std::env;
386386
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
387387
use std::sync::Arc;
388-
use std::sync::Mutex;
389388

390389
// fields below is wrapped with Arc so we can assert it
391390
#[derive(Default, Debug)]
@@ -645,12 +644,12 @@ mod tests {
645644

646645
#[derive(Debug)]
647646
struct CountingShutdownProcessor {
648-
shutdown_count: Arc<Mutex<i32>>,
649-
flush_called: Arc<Mutex<bool>>,
647+
shutdown_count: Arc<AtomicU32>,
648+
flush_called: Arc<AtomicBool>,
650649
}
651650

652651
impl CountingShutdownProcessor {
653-
fn new(shutdown_count: Arc<Mutex<i32>>, flush_called: Arc<Mutex<bool>>) -> Self {
652+
fn new(shutdown_count: Arc<AtomicU32>, flush_called: Arc<AtomicBool>) -> Self {
654653
CountingShutdownProcessor {
655654
shutdown_count,
656655
flush_called,
@@ -668,27 +667,26 @@ mod tests {
668667
}
669668

670669
fn force_flush(&self) -> TraceResult<()> {
671-
*self.flush_called.lock().unwrap() = true;
670+
self.flush_called.store(true, Ordering::SeqCst);
672671
Ok(())
673672
}
674673

675674
fn shutdown(&self) -> TraceResult<()> {
676-
let mut count = self.shutdown_count.lock().unwrap();
677-
*count += 1;
675+
self.shutdown_count.fetch_add(1, Ordering::SeqCst);
678676
Ok(())
679677
}
680678
}
681679

682680
#[test]
683681
fn drop_test_with_multiple_providers() {
684-
let shutdown_called = Arc::new(Mutex::new(0));
685-
let flush_called = Arc::new(Mutex::new(false));
682+
let shutdown_count = Arc::new(AtomicU32::new(0));
683+
let flush_called = Arc::new(AtomicBool::new(false));
686684

687685
{
688686
// Create a shared TracerProviderInner and use it across multiple providers
689687
let shared_inner = Arc::new(TracerProviderInner {
690688
processors: vec![Box::new(CountingShutdownProcessor::new(
691-
shutdown_called.clone(),
689+
shutdown_count.clone(),
692690
flush_called.clone(),
693691
))],
694692
config: Config::default(),
@@ -714,23 +712,23 @@ mod tests {
714712
}
715713
// At this point, both `tracer_provider1` and `tracer_provider2` are dropped,
716714
// but `shared_inner` still holds a reference, so `TracerProviderInner` is NOT dropped yet.
717-
assert_eq!(*shutdown_called.lock().unwrap(), 0);
715+
assert_eq!(shutdown_count.load(Ordering::SeqCst), 0);
718716
}
719717
// Verify shutdown was called during the drop of the shared TracerProviderInner
720-
assert_eq!(*shutdown_called.lock().unwrap(), 1);
718+
assert_eq!(shutdown_count.load(Ordering::SeqCst), 1);
721719
// Verify flush was not called during drop
722-
assert!(!*flush_called.lock().unwrap());
720+
assert!(!flush_called.load(Ordering::SeqCst));
723721
}
724722

725723
#[test]
726724
fn drop_after_shutdown_test_with_multiple_providers() {
727-
let shutdown_called = Arc::new(Mutex::new(0)); // Count the number of times shutdown is called
728-
let flush_called = Arc::new(Mutex::new(false));
725+
let shutdown_count = Arc::new(AtomicU32::new(0));
726+
let flush_called = Arc::new(AtomicBool::new(false));
729727

730728
// Create a shared TracerProviderInner and use it across multiple providers
731729
let shared_inner = Arc::new(TracerProviderInner {
732730
processors: vec![Box::new(CountingShutdownProcessor::new(
733-
shutdown_called.clone(),
731+
shutdown_count.clone(),
734732
flush_called.clone(),
735733
))],
736734
config: Config::default(),
@@ -751,7 +749,7 @@ mod tests {
751749
assert!(shutdown_result.is_ok());
752750

753751
// Verify that shutdown was called exactly once
754-
assert_eq!(*shutdown_called.lock().unwrap(), 1);
752+
assert_eq!(shutdown_count.load(Ordering::SeqCst), 1);
755753

756754
// TracerProvider2 should observe the shutdown state but not trigger another shutdown
757755
let shutdown_result2 = tracer_provider2.shutdown();
@@ -761,6 +759,6 @@ mod tests {
761759
}
762760

763761
// Verify that shutdown was only called once, even after drop
764-
assert_eq!(*shutdown_called.lock().unwrap(), 1);
762+
assert_eq!(shutdown_count.load(Ordering::SeqCst), 1);
765763
}
766764
}

0 commit comments

Comments
 (0)