Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: refactor code, move tracing atomics to TraceInner structure #5

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions utils/src/sync/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ 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::ops::Deref;
use std::time::SystemTime;

static SYS_START: Lazy<SystemTime> = Lazy::new(SystemTime::now);
Expand All @@ -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")]
Expand All @@ -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 {
Expand Down