Skip to content

Commit

Permalink
Merge #708
Browse files Browse the repository at this point in the history
708: add len() to Stealer r=taiki-e a=dinfuehr

Stealer only has the method `is_empty()` at the moment, but it would also be useful to check the size of the deque to e.g. steal from the longest deque.

Closes #572

Co-authored-by: Dominik Inführ <dominik.infuehr@gmail.com>
  • Loading branch information
bors[bot] and dinfuehr committed May 31, 2021
2 parents 6d4cdd4 + cad3537 commit 1f3104e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crossbeam-deque/src/deque.rs
Expand Up @@ -588,6 +588,27 @@ impl<T> Stealer<T> {
b.wrapping_sub(f) <= 0
}

/// Returns the number of tasks in the deque.
///
/// ```
/// use crossbeam_deque::Worker;
///
/// let w = Worker::new_lifo();
/// let s = w.stealer();
///
/// assert_eq!(s.len(), 0);
/// w.push(1);
/// assert_eq!(s.len(), 1);
/// w.push(2);
/// assert_eq!(s.len(), 2);
/// ```
pub fn len(&self) -> usize {
let f = self.inner.front.load(Ordering::Acquire);
atomic::fence(Ordering::SeqCst);
let b = self.inner.back.load(Ordering::Acquire);
b.wrapping_sub(f).max(0) as usize
}

/// Steals a task from the queue.
///
/// # Examples
Expand Down

0 comments on commit 1f3104e

Please sign in to comment.