Function sparking_lot_core::unpark_one
source · pub fn unpark_one(addr: *const ())
Expand description
Wakes one thread parked
on addr
.
Should be called after making the expected
of
the corresponding park
return false.
Notes
- The memory pointed to by
addr
isn’t written to, it isn’t read and no references to it are formed. - If no thread is waiting on
addr
, no thread is woken, but it still requires locking, so it’s not recommended to call it without reason. - This function ensures that if it is called after an
effect, that would cause the
expected
of a call topark
with the sameaddr
,park
will either be woken, or it will not have gone to sleep and will return.
Example
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use sparking_lot_core::{park, unpark_one};
struct BadMutex(AtomicBool);
impl BadMutex {
fn new() -> Self {
Self(AtomicBool::new(false))
}
fn lock(&self) {
loop {
if self.0.compare_exchange(false, true, Acquire, Relaxed).is_ok() {
return
}
/* SAFETY:
* - no calls to sparking_lot_core funtions in closure
* - owned address
*/
unsafe {
park(self as *const _ as *const _, || self.0.load(Acquire));
}
}
}
fn unlock(&self) {
self.0.store(false, Release);
unpark_one(self as *const _ as *const _);
}
}