Skip to content

Implement Stealer::will_steal_from#1103

Open
james7132 wants to merge 5 commits intocrossbeam-rs:masterfrom
james7132:is-same-as
Open

Implement Stealer::will_steal_from#1103
james7132 wants to merge 5 commits intocrossbeam-rs:masterfrom
james7132:is-same-as

Conversation

@james7132
Copy link
Copy Markdown

Currently it's not possible to tell if a Worker and a Stealer point to the same underlying queue. This make it difficult to tell which stealer to remove from a list and you have a local Worker on hand, or skip stealing from when given a global list of stealers. The implementation just forwards to Arc::ptr_eq on the inner buffer. Added a doctest as well.

Copy link
Copy Markdown
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I'm positive about adding such a method.

Comment thread crossbeam-deque/src/deque.rs Outdated
Comment thread crossbeam-deque/src/deque.rs Outdated
/// assert!(!w_1.is_same_as(&s_2));
/// assert!(!w_2.is_same_as(&s_1));
/// ```
pub fn is_same_as(&self, stealer: &Stealer<T>) -> bool {
Copy link
Copy Markdown
Member

@taiki-e taiki-e Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel this method name is easy to understand, but I'm not sure what the good name would be.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if Worker::ptr_eq is better. I can't seem to come up with a better name here either.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Worker::donates_to?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went ahead and renamed the function as @notgull requested.

@taiki-e taiki-e added crossbeam-deque S-waiting-on-bikeshed Status: Awaiting a decision on trivial things. labels Dec 7, 2024
@james7132 james7132 requested review from notgull and taiki-e July 1, 2025 07:18
@taiki-e
Copy link
Copy Markdown
Member

taiki-e commented Feb 21, 2026

As for naming: While looking at several unrelated APIs (Waker::will_wake/parking::Unparker::will_unpark), I started thinking that adding a will_steal (or will_steal_from) method to Stealer instead might be a good idea.

stealer.will_steal(&worker)
// or
stealer.will_steal_from(&worker)

@james7132 james7132 changed the title Implement Worker::is_same_as Implement Stealer::will_steal_from Mar 17, 2026
@james7132
Copy link
Copy Markdown
Author

@taiki-e, went ahead with the suggested rename.

Comment on lines +641 to +646
/// assert!(!s_1.will_steal_fromq(&w_1));
/// assert!(!s_2.will_steal_fromq(&w_2));
/// assert!(s_1.will_steal_fromq(&w_2));
/// assert!(s_2.will_steal_fromq(&w_1));
/// ```
pub fn will_steal_fromq(&self, worker: &Worker<T>) -> bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// assert!(!s_1.will_steal_fromq(&w_1));
/// assert!(!s_2.will_steal_fromq(&w_2));
/// assert!(s_1.will_steal_fromq(&w_2));
/// assert!(s_2.will_steal_fromq(&w_1));
/// ```
pub fn will_steal_fromq(&self, worker: &Worker<T>) -> bool {
/// assert!(!s_1.will_steal_from(&w_1));
/// assert!(!s_2.will_steal_from(&w_2));
/// assert!(s_1.will_steal_from(&w_2));
/// assert!(s_2.will_steal_from(&w_1));
/// ```
pub fn will_steal_from(&self, worker: &Worker<T>) -> bool {


/// Checks if the stealer will steal from the provided worker.
///
/// If both are pointing to the same underlying queue, this will return false.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that the reverse?

  • steal steals from the underlying queue.
  • steal_batch steals from the underlying queue and pushes to other worker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crossbeam-deque S-waiting-on-bikeshed Status: Awaiting a decision on trivial things.

Development

Successfully merging this pull request may close these issues.

3 participants