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

Steal-half in Chase-Lev #148

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/sync/chase_lev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ pub enum Steal<T> {
Data(T),
}

/// When stealing half of the data, this is an enumeration of the possible
/// outcomes.
#[derive(PartialEq, Eq, Debug)]
pub enum StealHalf<T> {
/// The deque was empty at the time of stealing.
Empty,
/// The stealer lost the race for stealing data, and a retry may return more
/// data.
Abort,
/// The stealer has successfully stolen some data.
Data(Vec<T>),
}

// An internal buffer used by the chase-lev deque. This structure is actually
// implemented as a circular buffer, and is used as the intermediate storage of
// the data in the deque.
Expand Down Expand Up @@ -137,6 +150,12 @@ impl<T> Stealer<T> {
pub fn steal(&self) -> Steal<T> {
self.deque.steal()
}

/// Steals half of the work off the end of the queue (opposite of the
/// worker's end)
pub fn steal_half(&self) -> StealHalf<T> {
self.deque.steal_half()
}
}

impl<T> Clone for Stealer<T> {
Expand Down Expand Up @@ -254,6 +273,40 @@ impl<T> Deque<T> {
}
}

fn steal_half(&self) -> StealHalf<T> {
let guard = epoch::pin();

let t = self.top.load(Acquire);
fence(SeqCst);
let b = self.bottom.load(Acquire);

let size = b - t;
if size <= 0 {
return StealHalf::Empty;
}

let half = (size + 1) / 2;

unsafe {
let a = self.array.load(Acquire, &guard).unwrap();

let mut data = Vec::with_capacity(half as usize);
for i in t..t + half {
data.push(a.get(i));
}

if self.top.compare_and_swap(t, t + half, SeqCst) == t {
Copy link

@ghost ghost Jul 24, 2017

Choose a reason for hiding this comment

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

What happens if just before this CAS the worker thread pops most of the elements, so that t + half overshoots the bottom?

There is an if-statement in fn pop where we deal with a special racy case when popping the last element. With the presence of the steal-half operation, now steal_half and pop can race in more complex ways.

The racy case in fn pop is currently handled by incrementing top rather than decrementing bottom. When racing with steal-half we can't perform the same trick.

Perhaps this is the difficulty with implementing steal-half we've been missing all along? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stjepang is right -- my bad. This implementation of steal_half() is wrong, and I cannot imagine how to fix it.

The reason I thought it was correct is I only considered races among stealers. As @stjepang said, b can be decremented beyond t + half before a steal_half() is successfully stealing from t to t + half. That breaks deque's invariant.

Thank you pointing out that. I think I should be more humble in front of concurrency... 🤕 I am closing this PR.

@fitzgen this PR is finally resolved, but negatively. I will work more on designing a proper steal_half() in deque!

StealHalf::Data(data)
} else {
while data.len() > 0 {
mem::forget(data.pop());
}
Copy link

Choose a reason for hiding this comment

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

You can rewrite this loop in a different, perhaps more idiomatic way:

while let Some(x) = data.pop() {
    mem::forget(x);
}

StealHalf::Abort
}
}

}

// potentially shrink the array. This can be called only from the worker.
unsafe fn maybe_shrink(&self, b: isize, t: isize, guard: &epoch::Guard) {
let a = self.array.load(SeqCst, guard).unwrap();
Expand Down