Having a version of `Unpark::unpark()` that returns a status
Hi crossbeam-rs maintainers,
It is sometimes useful to know the status of an unpark operation, for example in a rayon-style thread pool where we need to heuristically wake a thread for incoming tasks:
// We have a new task, so we call this function to wake a thread to do the task if appropriate.
fn wake_one(&self) {
let (idle_count, asleep_count) = self.counter.get();
// If there are no awake idle threads to do the task, but there are asleep ones, wake one up.
if idle_count == 0 && asleep_count > 0 {
for unparker in self.unparkers {
let old_state = unparker.unpark_returning(); // Imaginary API
if old_state == ParkState::Parked { break; }
}
}
}
Having an Unparker::unpark_returning() -> ParkState here would be helpful since it avoids the alternatives:
- Having to call
unpark()on all threads, introducing more wakeups and swaps than necessary. - Have some other atomic variable external to
Unparkerthat tracks sleep states, complicating the code.
I see this issue is marked as a duplicate of #601. However, that issue seems to concern something different, i.e. returning the reason for an unpark in the return value of Parker::park_timeout(). This issue proposes a variant of Unparker::unpark() that returns the old value of the park state, which is something different.
Sorry, I misunderstood the problem.