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>
impl<T> VersionedParker<T>
Sourcepub fn new(data: T) -> Self
pub fn new(data: T) -> Self
Creates a new VersionedParker
, with the initial version being 0
, and
the shared data being data
.
Sourcepub fn lock(&self) -> VersionedGuard<'_, T>
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.
Sourcepub fn notify_one(&self)
pub fn notify_one(&self)
Increases the version and notifies one blocked thread.
Sourcepub fn notify_one_mutate(&self, mutate: fn(&mut T))
pub fn notify_one_mutate(&self, mutate: fn(&mut T))
Increases the version, mutates the shared data and notifies one blocked thread.
Sourcepub fn try_notify_one(&self, expected_version: usize) -> bool
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.
Sourcepub fn try_notify_one_mutate(
&self,
expected_version: usize,
mutate: fn(&mut T),
) -> bool
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.
Sourcepub fn notify_all(&self)
pub fn notify_all(&self)
Increases the version and notifies all blocked threads.
Sourcepub fn notify_all_mutate(&self, mutate: fn(&mut T))
pub fn notify_all_mutate(&self, mutate: fn(&mut T))
Increases the version, modifies the shared data and notifies all blocked threads.
Sourcepub fn try_notify_all(&self, expected_version: usize) -> bool
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.
Trait Implementations§
Source§impl<T: Clone> Clone for VersionedParker<T>
impl<T: Clone> Clone for VersionedParker<T>
Source§fn clone(&self) -> VersionedParker<T>
fn clone(&self) -> VersionedParker<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more