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 current.
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 try_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Duration, Duration>
pub fn try_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Duration, Duration>
Fetches the value and applies a function that can choose whether to store a new value.
Returns Ok(previous_value) when f returns Some(_), and
Err(previous_value) when it returns None. The closure can run more
than once when another thread changes the value concurrently, but it is
applied only once to the value that is stored.
set_order describes the ordering of the successful update and
fetch_order describes failed compare-and-exchange loads. They have the
same requirements as the success and failure orderings of
compare_exchange.
§Panics
Panics if the orderings are invalid for
compare_exchange.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::from_secs(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(Duration::from_secs(7)));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + Duration::from_secs(1))), Ok(Duration::from_secs(7)));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 update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Duration
pub fn update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Duration
Fetches the value, applies a function to produce a new value, stores it, and returns the previous value.
The closure can run more than once when another thread changes the value
concurrently, but it is applied only once to the value that is stored.
set_order and fetch_order have the same requirements as the success
and failure orderings of compare_exchange.
§Panics
Panics if the orderings are invalid for
compare_exchange.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::from_secs(7));
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |old| old + Duration::from_secs(1)), Duration::from_secs(7));
assert_eq!(x.load(Ordering::SeqCst), Duration::from_secs(8));Sourcepub fn fetch_min(&self, val: Duration, order: Ordering) -> Duration
pub fn fetch_min(&self, val: Duration, order: Ordering) -> Duration
Atomically stores the smaller of the current value and val, returning
the previous value.
order describes the memory ordering of the read-modify-write operation.
Using Acquire makes its store part relaxed, and
using Release makes its load part relaxed.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::from_secs(7));
assert_eq!(x.fetch_min(Duration::from_secs(5), Ordering::SeqCst), Duration::from_secs(7));
assert_eq!(x.load(Ordering::SeqCst), Duration::from_secs(5));Sourcepub fn fetch_max(&self, val: Duration, order: Ordering) -> Duration
pub fn fetch_max(&self, val: Duration, order: Ordering) -> Duration
Atomically stores the larger of the current value and val, returning
the previous value.
order describes the memory ordering of the read-modify-write operation.
Using Acquire makes its store part relaxed, and
using Release makes its load part relaxed.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::from_secs(5));
assert_eq!(x.fetch_max(Duration::from_secs(7), Ordering::SeqCst), Duration::from_secs(5));
assert_eq!(x.load(Ordering::SeqCst), Duration::from_secs(7));Sourcepub fn fetch_saturating_add(&self, val: Duration, order: Ordering) -> Duration
pub fn fetch_saturating_add(&self, val: Duration, order: Ordering) -> Duration
Adds val to the current value with saturation, returning the previous
value.
This never overflows: values above Duration::MAX remain saturated at
Duration::MAX. order describes the successful update ordering.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::new(0, 999_999_999));
assert_eq!(x.fetch_saturating_add(Duration::from_nanos(1), Ordering::SeqCst), Duration::new(0, 999_999_999));
assert_eq!(x.load(Ordering::SeqCst), Duration::from_secs(1));Sourcepub fn fetch_saturating_sub(&self, val: Duration, order: Ordering) -> Duration
pub fn fetch_saturating_sub(&self, val: Duration, order: Ordering) -> Duration
Subtracts val from the current value with saturation, returning the
previous value.
This never underflows: values below Duration::ZERO remain saturated at
Duration::ZERO. order describes the successful update ordering.
§Examples
use atomic_time::AtomicDuration;
use std::{sync::atomic::Ordering, time::Duration};
let x = AtomicDuration::new(Duration::ZERO);
assert_eq!(x.fetch_saturating_sub(Duration::from_nanos(1), Ordering::SeqCst), Duration::ZERO);
assert_eq!(x.load(Ordering::SeqCst), Duration::ZERO);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();Sourcepub const fn is_always_lock_free() -> bool
pub const fn is_always_lock_free() -> bool
Returns whether operations on values of this type are always lock-free.
A false result does not preclude lock-free operations selected through
runtime CPU feature detection; use is_lock_free
to query the current target at runtime.
§Examples
use atomic_time::AtomicDuration;
const ALWAYS_LOCK_FREE: bool = AtomicDuration::is_always_lock_free();
let _ = ALWAYS_LOCK_FREE;Trait Implementations§
Source§impl Arbitrary for AtomicDuration
Available on crate feature quickcheck only.
impl Arbitrary for AtomicDuration
quickcheck only.Source§impl Arbitrary for AtomicDuration
Available on crate feature proptest only.
impl Arbitrary for AtomicDuration
proptest only.Source§type Parameters = ()
type Parameters = ()
arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default.Source§type Strategy = BoxedStrategy<AtomicDuration>
type Strategy = BoxedStrategy<AtomicDuration>
Strategy used to generate values of type Self.Source§fn arbitrary_with(_: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy
Source§impl<'a> Arbitrary<'a> for AtomicDuration
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for AtomicDuration
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for AtomicDuration
Available on crate feature quickcheck only.
impl Clone for AtomicDuration
quickcheck only.Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates an atomic snapshot of this value with sequentially consistent ordering.
If another thread writes concurrently, the clone contains the value observed at this load’s atomic linearization point.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AtomicDuration
impl Debug for AtomicDuration
Source§impl Default for AtomicDuration
impl Default for AtomicDuration
Source§fn default() -> Self
fn default() -> Self
Creates an AtomicDuration initialized to Duration::ZERO.
Source§impl<'de> Deserialize<'de> for AtomicDuration
Available on crate feature serde only.
impl<'de> Deserialize<'de> for AtomicDuration
serde only.