pub struct AtomicInstant(/* private fields */);std only.Expand description
Atomic version of Instant.
Implementations§
Source§impl AtomicInstant
impl AtomicInstant
Sourcepub fn now() -> Self
pub fn now() -> Self
Returns the system time corresponding to “now”.
§Examples
use atomic_time::AtomicInstant;
let now = AtomicInstant::now();Sourcepub fn new(instant: Instant) -> Self
pub fn new(instant: Instant) -> Self
Creates a new AtomicInstant with the given SystemTime value.
Sourcepub fn swap(&self, instant: Instant, order: Ordering) -> Instant
pub fn swap(&self, instant: Instant, order: Ordering) -> Instant
Stores a value into the atomic instant, returning the previous value.
Sourcepub fn compare_exchange(
&self,
current: Instant,
new: Instant,
success: Ordering,
failure: Ordering,
) -> Result<Instant, Instant>
pub fn compare_exchange( &self, current: Instant, new: Instant, success: Ordering, failure: Ordering, ) -> Result<Instant, Instant>
Stores a value into the atomic instant if the current value is the same as the current
value.
Sourcepub fn compare_exchange_weak(
&self,
current: Instant,
new: Instant,
success: Ordering,
failure: Ordering,
) -> Result<Instant, Instant>
pub fn compare_exchange_weak( &self, current: Instant, new: Instant, success: Ordering, failure: Ordering, ) -> Result<Instant, Instant>
Stores a value into the atomic instant if the current value is the same as the current
value.
Sourcepub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<Instant, Instant>
pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Instant, Instant>
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::AtomicInstant;
use std::{time::{Duration, Instant}, sync::atomic::Ordering};
let now = Instant::now();
let x = AtomicInstant::new(now);
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(now));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + Duration::from_secs(1))), Ok(now));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + Duration::from_secs(1))), Ok(now + Duration::from_secs(1)));
assert_eq!(x.load(Ordering::SeqCst), now + Duration::from_secs(2));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::AtomicInstant;
let is_lock_free = AtomicInstant::is_lock_free();Trait Implementations§
Source§impl<'de> Deserialize<'de> for AtomicInstant
Available on crate feature serde only.
impl<'de> Deserialize<'de> for AtomicInstant
serde only.