-
Notifications
You must be signed in to change notification settings - Fork 483
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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> { | ||
|
@@ -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 { | ||
StealHalf::Data(data) | ||
} else { | ||
while data.len() > 0 { | ||
mem::forget(data.pop()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, nowsteal_half
andpop
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? :)
There was a problem hiding this comment.
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 beyondt + half
before asteal_half()
is successfully stealing fromt
tot + 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!