Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add len() to Stealer #708

Merged
merged 1 commit into from May 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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