pub struct AtomicDuration(/* private fields */);Expand description
Atomic version of Duration
Implementations§
Source§impl AtomicDuration
impl AtomicDuration
Sourcepub const fn new(duration: Duration) -> Self
pub const fn new(duration: Duration) -> Self
Creates a new AtomicDuration with the given value.
Sourcepub fn swap(&self, val: Duration, ordering: Ordering) -> Duration
pub fn swap(&self, val: Duration, ordering: Ordering) -> Duration
Stores a value into the AtomicDuration, returning the old value.
swap takes an Ordering argument which describes the memory ordering
of this operation.
Sourcepub fn compare_exchange_weak(
&self,
current: Duration,
new: Duration,
success: Ordering,
failure: Ordering,
) -> Result<Duration, Duration>
pub fn compare_exchange_weak( &self, current: Duration, new: Duration, success: Ordering, failure: Ordering, ) -> Result<Duration, Duration>
Stores a value into the AtomicDuration if the current value is the same as the
current value.
Unlike compare_exchange, this function is allowed to spuriously fail
even when the comparison succeeds, which can result in more efficient
code on some platforms. The return value is a result indicating whether
the new value was written and containing the previous value.
compare_exchange takes two Ordering arguments to describe the memory
ordering of this operation. The first describes the required ordering if
the operation succeeds while the second describes the required ordering
when the operation fails. The failure ordering can’t be Release or
AcqRel and must be equivalent or weaker than the success ordering.
success ordering.
Sourcepub fn compare_exchange(
&self,
current: Duration,
new: Duration,
success: Ordering,
failure: Ordering,
) -> Result<Duration, Duration>
pub fn compare_exchange( &self, current: Duration, new: Duration, success: Ordering, failure: Ordering, ) -> Result<Duration, Duration>
Stores a value into the AtomicDuration if the current value is the same as the
current value.
The return value is a result indicating whether the new value was
written and containing the previous value. On success this value is
guaranteed to be equal to new.
compare_exchange takes two Ordering arguments to describe the memory
ordering of this operation. The first describes the required ordering if
the operation succeeds while the second describes the required ordering
when the operation fails. The failure ordering can’t be Release or
AcqRel and must be equivalent or weaker than the success ordering.
Sourcepub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Duration, Duration>
pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Duration, Duration>
Fetches the value, and applies a function to it that returns an optional
new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else
Err(previous_value).
Note: This may call the function multiple times if the value has been changed from other threads in
the meantime, as long as the function returns Some(_), but the function will have been applied
only once to the stored value.
fetch_update takes two Ordering arguments to describe the memory ordering of this operation.
The first describes the required ordering for when the operation finally succeeds while the second
describes the required ordering for loads. These correspond to the success and failure orderings of
compare_exchange respectively.
Using Acquire as success ordering makes the store part
of this operation Relaxed, and using Release makes the final successful load
Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed
and must be equivalent to or weaker than the success ordering.
§Examples
use atomic_time::AtomicDuration;
use std::{time::Duration, sync::atomic::Ordering};
let x = AtomicDuration::new(Duration::from_secs(7));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(Duration::from_secs(7)));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + Duration::from_secs(1))), Ok(Duration::from_secs(7)));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + Duration::from_secs(1))), Ok(Duration::from_secs(8)));
assert_eq!(x.load(Ordering::SeqCst), Duration::from_secs(9));Sourcepub fn into_inner(self) -> Duration
pub fn into_inner(self) -> Duration
Consumes the atomic and returns the contained value.
This is safe because passing self by value guarantees that no other threads are
concurrently accessing the atomic data.
Sourcepub fn is_lock_free() -> bool
pub fn is_lock_free() -> bool
Returns true if operations on values of this type are lock-free.
If the compiler or the platform doesn’t support the necessary
atomic instructions, global locks for every potentially
concurrent atomic operation will be used.
§Examples
use atomic_time::AtomicDuration;
let is_lock_free = AtomicDuration::is_lock_free();Trait Implementations§
Source§impl Debug for AtomicDuration
impl Debug for AtomicDuration
Source§impl<'de> Deserialize<'de> for AtomicDuration
Available on crate feature serde only.
impl<'de> Deserialize<'de> for AtomicDuration
serde only.