VersionedParker

Struct VersionedParker 

Source
pub struct VersionedParker<T> { /* private fields */ }
Expand description

A thread parking and locking primitive that provide version numbers.

Like an std::sync::Condvar, VersionedParker provides a wait method and several notify methods. The wait method blocks the current thread, while the notify methods unblocks waiting threads. Each time notify is called, the parker version is increased. When a blocked thread wakes up, it can check the internal counter and learn how many times it has been notified. The version can be obtained by calling method VersionedParker::version().

VersionedParker holds a piece of data that can be modified during notify and wait operations. The data is versioned also versioned by the same parker version.

use more_sync::VersionedParker;

let versioned_parker = VersionedParker::new(0);
let mut guard = versioned_parker.lock();

let parker_clone = versioned_parker.clone();
std::thread::spawn(move || {
    parker_clone.notify_one_mutate(|i| *i = 16);
    assert_eq!(parker_clone.version(), 1);
    // Version is 1, try_notify_all() should fail.
    assert!(!parker_clone.try_notify_all(0));
});

guard.wait();
assert_eq!(guard.notified_count(), 1);
assert_eq!(*guard, 16);

Implementations§

Source§

impl<T> VersionedParker<T>

Source

pub fn new(data: T) -> Self

Creates a new VersionedParker, with the initial version being 0, and the shared data being data.

Source

pub fn lock(&self) -> VersionedGuard<'_, T>

Locks the shared data and the version.

A thread can then call VersionedGuard::wait() to wait for version changes.

Source

pub fn notify_one(&self)

Increases the version and notifies one blocked thread.

Source

pub fn notify_one_mutate(&self, mutate: fn(&mut T))

Increases the version, mutates the shared data and notifies one blocked thread.

Source

pub fn try_notify_one(&self, expected_version: usize) -> bool

Increases the version and notifies one blocked thread, if the current version is expected_version.

Returns true if the version matches.

Source

pub fn try_notify_one_mutate( &self, expected_version: usize, mutate: fn(&mut T), ) -> bool

Increases the version, modifies the shared data and notifies one blocked thread, if the current version is expected_version.

Returns true if the version matches.

Source

pub fn notify_all(&self)

Increases the version and notifies all blocked threads.

Source

pub fn notify_all_mutate(&self, mutate: fn(&mut T))

Increases the version, modifies the shared data and notifies all blocked threads.

Source

pub fn try_notify_all(&self, expected_version: usize) -> bool

Increases the version and notifies all blocked threads, if the current version is expected_version.

Returns true if the version matches.

Source

pub fn try_notify_all_mutate( &self, expected_version: usize, mutate: fn(&mut T), ) -> bool

Increases the version, modifies the shared data and notifies all blocked threads, if the current version is expected_version.

Returns true if the version matches.

Source

pub fn version(&self) -> usize

Returns the current version.

Trait Implementations§

Source§

impl<T: Clone> Clone for VersionedParker<T>

Source§

fn clone(&self) -> VersionedParker<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for VersionedParker<T>

Source§

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

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

impl<T: Default> Default for VersionedParker<T>

Source§

fn default() -> VersionedParker<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for VersionedParker<T>

§

impl<T> RefUnwindSafe for VersionedParker<T>

§

impl<T> Send for VersionedParker<T>
where T: Send,

§

impl<T> Sync for VersionedParker<T>
where T: Send,

§

impl<T> Unpin for VersionedParker<T>

§

impl<T> UnwindSafe for VersionedParker<T>

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.