Skip to content

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

@re-xyr

Description

@re-xyr

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions