Skip to main content

AtomicOptionInstant

Struct AtomicOptionInstant 

Source
pub struct AtomicOptionInstant(/* private fields */);
Available on crate feature std only.
Expand description

Atomic version of Option<Instant>.

Values are encoded relative to a process-local baseline pairing std::time::SystemTime::now() with std::time::Instant::now() (see crate::utils::encode_instant_to_duration and crate::utils::decode_instant_from_duration). Within the same process, values in the platform’s representable Instant range round-trip exactly. Encodings are not portable across processes or restarts: there they only approximate wall-clock time and must not be used as persistent deadlines. System clock adjustments can affect that cross-process interpretation, while behavior across system sleep follows the platform’s Instant semantics.

With the serde feature, this type retains its Option<Duration> proxy wire format. For persistent wall-clock values, use crate::AtomicOptionSystemTime.

Implementations§

Source§

impl AtomicOptionInstant

Source

pub const fn none() -> Self

Equivalent to atomic version Option::<Instant>::None.

§Examples
use atomic_time::AtomicOptionInstant;

let none = AtomicOptionInstant::none();
assert_eq!(none.load(std::sync::atomic::Ordering::SeqCst), None);
Source

pub fn now() -> Self

Returns the instant corresponding to “now”.

§Examples
use atomic_time::AtomicOptionInstant;

let now = AtomicOptionInstant::now();
Source

pub fn new(instant: Option<Instant>) -> Self

Creates a new AtomicOptionInstant with the given Instant value.

Source

pub fn load(&self, order: Ordering) -> Option<Instant>

Loads a value from the atomic instant.

Source

pub fn store(&self, instant: Option<Instant>, order: Ordering)

Stores a value into the atomic instant.

Source

pub fn swap(&self, instant: Option<Instant>, order: Ordering) -> Option<Instant>

Stores a value into the atomic instant, returning the previous value.

Source

pub fn compare_exchange( &self, current: Option<Instant>, new: Option<Instant>, success: Ordering, failure: Ordering, ) -> Result<Option<Instant>, Option<Instant>>

Stores a value into the atomic instant if the current value is the same as the current value.

Source

pub fn compare_exchange_weak( &self, current: Option<Instant>, new: Option<Instant>, success: Ordering, failure: Ordering, ) -> Result<Option<Instant>, Option<Instant>>

Stores a value into the atomic instant if the current value is the same as the current value.

Source

pub fn try_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Option<Instant>, Option<Instant>>

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 outer Option is the update decision; returning Some(None) stores 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.

§Examples
use atomic_time::AtomicOptionInstant;
use std::{sync::atomic::Ordering, time::{Duration, Instant}};

let start = Instant::now();
let x = AtomicOptionInstant::new(Some(start));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(Some(start)));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old.map(|instant| instant + Duration::from_secs(1)))), Ok(Some(start)));
Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<Option<Instant>, Option<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::AtomicOptionInstant;
use std::{time::{Duration, Instant}, sync::atomic::Ordering};

let now = Instant::now();
let x = AtomicOptionInstant::none();
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(None));
x.store(Some(now), Ordering::SeqCst);
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x.map(|val| val + Duration::from_secs(1)))), Ok(Some(now)));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x.map(|val| val + Duration::from_secs(1)))), Ok(Some(now + Duration::from_secs(1))));
assert_eq!(x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x.map(|val| val + Duration::from_secs(1)))), Ok(Some(now + Duration::from_secs(2))));
assert_eq!(x.load(Ordering::SeqCst), Some(now + Duration::from_secs(3)));
Source

pub fn update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Option<Instant>

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.

§Examples
use atomic_time::AtomicOptionInstant;
use std::{sync::atomic::Ordering, time::{Duration, Instant}};

let start = Instant::now();
let x = AtomicOptionInstant::new(Some(start));
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |old| old.map(|instant| instant + Duration::from_secs(1))), Some(start));
assert_eq!(x.load(Ordering::SeqCst), Some(start + Duration::from_secs(1)));
Source

pub fn fetch_min( &self, val: Option<Instant>, order: Ordering, ) -> Option<Instant>

Atomically stores the smaller of the current value and val, returning the previous value.

None is ordered before every Some value. Some values are ordered by the process-local baseline encoding. This ordering is meaningful only for values using the same process-local baseline; it does not assign ordering semantics across processes or process restarts. 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::AtomicOptionInstant;
use std::{sync::atomic::Ordering, time::{Duration, Instant}};

let middle = Instant::now();
let low = middle - Duration::from_secs(1);
let high = middle + Duration::from_secs(1);
let x = AtomicOptionInstant::new(Some(middle));
assert_eq!(x.fetch_min(Some(high), Ordering::SeqCst), Some(middle));
assert_eq!(x.fetch_min(Some(low), Ordering::SeqCst), Some(middle));
assert_eq!(x.load(Ordering::SeqCst), Some(low));
Source

pub fn fetch_max( &self, val: Option<Instant>, order: Ordering, ) -> Option<Instant>

Atomically stores the larger of the current value and val, returning the previous value.

None is ordered before every Some value. Some values are ordered by the process-local baseline encoding. This ordering is meaningful only for values using the same process-local baseline; it does not assign ordering semantics across processes or process restarts. 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::AtomicOptionInstant;
use std::{sync::atomic::Ordering, time::{Duration, Instant}};

let high = Instant::now() + Duration::from_secs(1);
let x = AtomicOptionInstant::none();
assert_eq!(x.fetch_max(None, Ordering::SeqCst), None);
assert_eq!(x.fetch_max(Some(high), Ordering::SeqCst), None);
assert_eq!(x.load(Ordering::SeqCst), Some(high));
Source

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::AtomicOptionInstant;

let is_lock_free = AtomicOptionInstant::is_lock_free();
Source

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::AtomicOptionInstant;

const ALWAYS_LOCK_FREE: bool = AtomicOptionInstant::is_always_lock_free();
let _ = ALWAYS_LOCK_FREE;
Source

pub fn into_inner(self) -> Option<Instant>

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 Debug for AtomicOptionInstant

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AtomicOptionInstant

Source§

fn default() -> Self

Equivalent to Option::<Instant>::None.

Source§

impl<'de> Deserialize<'de> for AtomicOptionInstant

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Option<Instant>> for AtomicOptionInstant

Source§

fn from(instant: Option<Instant>) -> Self

Converts to this type from the input type.
Source§

impl Serialize for AtomicOptionInstant

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V