Function sparking_lot_core::unpark_some
source · pub fn unpark_some(addr: *const (), count: usize)
Expand description
Wakes at most count
threads parked
on addr
.
Should be called after making the expected
of
the corresponding parks
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 sparking_lot_core::{park, unpark_some};
static tasks: YourTaskQueue = YourTaskQueue::new();
fn add_tasks<T: Iterator<Item = Task>>(new_tasks: T) {
let mut count = 0;
for t in new_tasks {
tasks.push_task(t);
count += 1;
}
unpark_some(&tasks as *const _ as *const _, count);
}
fn get_task() -> Task {
let mut task = None;
loop {
// some other unblocked thread might
// have taken our task, so we loop
task = tasks.pop_task();
if let Some(task) = task {
return task;
}
unsafe {
/* SAFETY:
* - no calls to sparking_lot_core funtions in closure
* - the task queue **has to be** be private
*/
park(&tasks as *const _ as *const _, || {
task = tasks.pop_task();
task.is_none()
});
}
}
}