From 78ec6737ea5d13ac236fbcd39b3c2be5a97d1c96 Mon Sep 17 00:00:00 2001 From: max143672 Date: Thu, 22 Aug 2024 12:35:47 +0400 Subject: [PATCH 1/2] style: refactor code, move tracing atomics to TraceInner structure --- utils/src/sync/semaphore.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/utils/src/sync/semaphore.rs b/utils/src/sync/semaphore.rs index ad53ceda0..f243ff9fc 100644 --- a/utils/src/sync/semaphore.rs +++ b/utils/src/sync/semaphore.rs @@ -11,6 +11,8 @@ mod trace { use once_cell::sync::Lazy; use std::time::SystemTime; + use std::ops::Deref; + use crate::sync::semaphore::Semaphore; static SYS_START: Lazy = Lazy::new(SystemTime::now); @@ -18,6 +20,22 @@ mod trace { pub(super) fn sys_now() -> u64 { SystemTime::now().duration_since(*SYS_START).unwrap_or_default().as_micros() as u64 } + + impl Deref for Semaphore { + type Target = TraceInner; + fn deref(&self) -> &Self::Target { + &self.trace_inner + } + } + + #[derive(Debug, Default)] + pub struct TraceInner { + pub(super) readers_start: AtomicU64, + pub(super) readers_end: AtomicU64, + pub(super) readers_time: AtomicU64, + pub(super) log_time: AtomicU64, + pub(super) log_value: AtomicU64, + } } #[cfg(feature = "semaphore-trace")] @@ -39,31 +57,19 @@ pub(crate) struct Semaphore { counter: AtomicUsize, signal: Event, #[cfg(feature = "semaphore-trace")] - readers_start: AtomicU64, - #[cfg(feature = "semaphore-trace")] - readers_end: AtomicU64, - #[cfg(feature = "semaphore-trace")] - readers_time: AtomicU64, - #[cfg(feature = "semaphore-trace")] - log_time: AtomicU64, - #[cfg(feature = "semaphore-trace")] - log_value: AtomicU64, + trace_inner: TraceInner, } impl Semaphore { pub const MAX_PERMITS: usize = usize::MAX; - pub const fn new(available_permits: usize) -> Semaphore { + pub fn new(available_permits: usize) -> Semaphore { cfg_if::cfg_if! { if #[cfg(feature = "semaphore-trace")] { Semaphore { counter: AtomicUsize::new(available_permits), signal: Event::new(), - readers_start: AtomicU64::new(0), - readers_end: AtomicU64::new(0), - readers_time: AtomicU64::new(0), - log_time: AtomicU64::new(0), - log_value: AtomicU64::new(0), + trace_inner: Default::default(), } } else { Semaphore { From 048cde0e3edb4c4f1cf8207fbdafb72d1eb9becc Mon Sep 17 00:00:00 2001 From: max143672 Date: Thu, 22 Aug 2024 12:37:09 +0400 Subject: [PATCH 2/2] style: fmt --- utils/src/sync/semaphore.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/src/sync/semaphore.rs b/utils/src/sync/semaphore.rs index f243ff9fc..aee76e917 100644 --- a/utils/src/sync/semaphore.rs +++ b/utils/src/sync/semaphore.rs @@ -9,10 +9,10 @@ mod trace { pub(super) use log::debug; pub(super) use std::sync::atomic::AtomicU64; + use crate::sync::semaphore::Semaphore; use once_cell::sync::Lazy; - use std::time::SystemTime; use std::ops::Deref; - use crate::sync::semaphore::Semaphore; + use std::time::SystemTime; static SYS_START: Lazy = Lazy::new(SystemTime::now);