pub struct AtomicSystemTime(/* private fields */);std only.Expand description
An atomic version of std::time::SystemTime.
Only SystemTime values at or after SystemTime::UNIX_EPOCH are supported.
Implementations§
Source§impl AtomicSystemTime
impl AtomicSystemTime
Sourcepub fn now() -> Self
pub fn now() -> Self
Returns the system time corresponding to “now”.
§Examples
use atomic_time::AtomicSystemTime;
let sys_time = AtomicSystemTime::now();Sourcepub fn new(system_time: SystemTime) -> Self
pub fn new(system_time: SystemTime) -> Self
Creates a new AtomicSystemTime with the given SystemTime value.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
Sourcepub fn load(&self, order: Ordering) -> SystemTime
pub fn load(&self, order: Ordering) -> SystemTime
Loads a value from the atomic system time.
Sourcepub fn store(&self, system_time: SystemTime, order: Ordering)
pub fn store(&self, system_time: SystemTime, order: Ordering)
Stores a value into the atomic system time.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
Sourcepub fn swap(&self, system_time: SystemTime, order: Ordering) -> SystemTime
pub fn swap(&self, system_time: SystemTime, order: Ordering) -> SystemTime
Stores a value into the atomic system time, returning the previous value.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
Sourcepub fn compare_exchange(
&self,
current: SystemTime,
new: SystemTime,
success: Ordering,
failure: Ordering,
) -> Result<SystemTime, SystemTime>
pub fn compare_exchange( &self, current: SystemTime, new: SystemTime, success: Ordering, failure: Ordering, ) -> Result<SystemTime, SystemTime>
Stores a value into the atomic system time if the current value is the same as the current
value.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
Sourcepub fn compare_exchange_weak(
&self,
current: SystemTime,
new: SystemTime,
success: Ordering,
failure: Ordering,
) -> Result<SystemTime, SystemTime>
pub fn compare_exchange_weak( &self, current: SystemTime, new: SystemTime, success: Ordering, failure: Ordering, ) -> Result<SystemTime, SystemTime>
Stores a value into the atomic system time if the current value is the same as the current
value.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
Sourcepub fn try_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<SystemTime, SystemTime>
pub fn try_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<SystemTime, SystemTime>
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, or if the function returns
a SystemTime value earlier than UNIX_EPOCH.
§Examples
use atomic_time::AtomicSystemTime;
use std::{sync::atomic::Ordering, time::{Duration, SystemTime}};
let start = SystemTime::UNIX_EPOCH + Duration::from_secs(7);
let x = AtomicSystemTime::new(start);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(start));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + Duration::from_secs(1))), Ok(start));Sourcepub fn fetch_update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<SystemTime, SystemTime>
pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<SystemTime, SystemTime>
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.
§Panics
If the given SystemTime value is earlier than the UNIX_EPOCH.
§Examples
use atomic_time::AtomicSystemTime;
use std::{time::{Duration, SystemTime}, sync::atomic::Ordering};
let now = SystemTime::now();
let x = AtomicSystemTime::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 update<F>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> SystemTime
pub fn update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> SystemTime
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, or if the function returns
a SystemTime value earlier than UNIX_EPOCH.
§Examples
use atomic_time::AtomicSystemTime;
use std::{sync::atomic::Ordering, time::{Duration, SystemTime}};
let start = SystemTime::UNIX_EPOCH + Duration::from_secs(7);
let x = AtomicSystemTime::new(start);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |old| old + Duration::from_secs(1)), start);
assert_eq!(x.load(Ordering::SeqCst), start + Duration::from_secs(1));Sourcepub fn fetch_min(&self, val: SystemTime, order: Ordering) -> SystemTime
pub fn fetch_min(&self, val: SystemTime, order: Ordering) -> SystemTime
Atomically stores the smaller of the current value and val, returning
the previous value.
Values are ordered by their duration from UNIX_EPOCH.
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.
§Panics
Panics if val is earlier than UNIX_EPOCH.
§Examples
use atomic_time::AtomicSystemTime;
use std::{sync::atomic::Ordering, time::{Duration, SystemTime}};
let start = SystemTime::UNIX_EPOCH + Duration::from_secs(5);
let x = AtomicSystemTime::new(start);
assert_eq!(x.fetch_min(start + Duration::from_secs(2), Ordering::SeqCst), start);
assert_eq!(x.fetch_min(start - Duration::from_secs(1), Ordering::SeqCst), start);
assert_eq!(x.load(Ordering::SeqCst), start - Duration::from_secs(1));Sourcepub fn fetch_max(&self, val: SystemTime, order: Ordering) -> SystemTime
pub fn fetch_max(&self, val: SystemTime, order: Ordering) -> SystemTime
Atomically stores the larger of the current value and val, returning
the previous value.
Values are ordered by their duration from UNIX_EPOCH.
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.
§Panics
Panics if val is earlier than UNIX_EPOCH.
§Examples
use atomic_time::AtomicSystemTime;
use std::{sync::atomic::Ordering, time::{Duration, SystemTime}};
let start = SystemTime::UNIX_EPOCH + Duration::from_secs(5);
let x = AtomicSystemTime::new(start);
assert_eq!(x.fetch_max(start - Duration::from_secs(2), Ordering::SeqCst), start);
assert_eq!(x.fetch_max(start + Duration::from_secs(1), Ordering::SeqCst), start);
assert_eq!(x.load(Ordering::SeqCst), start + Duration::from_secs(1));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::AtomicSystemTime;
let is_lock_free = AtomicSystemTime::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::AtomicSystemTime;
const ALWAYS_LOCK_FREE: bool = AtomicSystemTime::is_always_lock_free();
let _ = ALWAYS_LOCK_FREE;Sourcepub fn into_inner(self) -> SystemTime
pub fn into_inner(self) -> SystemTime
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.
Trait Implementations§
Source§impl Arbitrary for AtomicSystemTime
Available on crate feature quickcheck only.
impl Arbitrary for AtomicSystemTime
quickcheck only.Source§impl Arbitrary for AtomicSystemTime
Available on crate feature proptest only.
impl Arbitrary for AtomicSystemTime
proptest only.Source§type Parameters = ()
type Parameters = ()
arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default.Source§type Strategy = BoxedStrategy<AtomicSystemTime>
type Strategy = BoxedStrategy<AtomicSystemTime>
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 AtomicSystemTime
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for AtomicSystemTime
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 AtomicSystemTime
Available on crate feature quickcheck only.
impl Clone for AtomicSystemTime
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 AtomicSystemTime
impl Debug for AtomicSystemTime
Source§impl<'de> Deserialize<'de> for AtomicSystemTime
Available on crate feature serde only.
impl<'de> Deserialize<'de> for AtomicSystemTime
serde only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl From<SystemTime> for AtomicSystemTime
impl From<SystemTime> for AtomicSystemTime
Source§fn from(system_time: SystemTime) -> Self
fn from(system_time: SystemTime) -> Self
§Panics
Panics if the given SystemTime value is earlier than UNIX_EPOCH.