crossbeam icon indicating copy to clipboard operation
crossbeam copied to clipboard

Having a version of `Unpark::unpark()` that returns a status

Open re-xyr opened this issue 1 month ago • 2 comments

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 Unparker that tracks sleep states, complicating the code.

re-xyr avatar Nov 28 '25 20:11 re-xyr

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.

re-xyr avatar Nov 29 '25 10:11 re-xyr

Sorry, I misunderstood the problem.

taiki-e avatar Nov 29 '25 10:11 taiki-e