Skip to content

Commit ba8e1a1

Browse files
authored
Merge pull request #1119 from cuviper/fetch_update
Use `fetch_update` instead of `compare_exchange_weak`
2 parents 6cb8246 + a4da641 commit ba8e1a1

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

src/iter/find_first_last/mod.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,17 @@ impl<'p, P: 'p + Fn(&T) -> bool, T> Folder<T> for FindFolder<'p, T, P> {
181181
};
182182

183183
if !found_best_in_range && (self.find_op)(&item) {
184-
// Continuously try to set best_found until we succeed or we
185-
// discover a better match was already found.
186-
let mut current = self.best_found.load(Ordering::Relaxed);
187-
loop {
188-
if better_position(current, self.boundary, self.match_position) {
189-
break;
190-
}
191-
match self.best_found.compare_exchange_weak(
192-
current,
193-
self.boundary,
194-
Ordering::Relaxed,
195-
Ordering::Relaxed,
196-
) {
197-
Ok(_) => {
198-
self.item = Some(item);
199-
break;
200-
}
201-
Err(v) => current = v,
202-
}
184+
// Update the best found index if ours is better.
185+
let update =
186+
self.best_found
187+
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
188+
better_position(self.boundary, current, self.match_position)
189+
.then_some(self.boundary)
190+
});
191+
192+
// Save this item if our index was better or equal.
193+
if update.is_ok() || update == Err(self.boundary) {
194+
self.item = Some(item);
203195
}
204196
}
205197
self

src/iter/par_bridge.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,11 @@ impl<Iter: Iterator + Send> UnindexedProducer for &IterParallelProducer<'_, Iter
109109
type Item = Iter::Item;
110110

111111
fn split(self) -> (Self, Option<Self>) {
112-
let mut count = self.split_count.load(Ordering::SeqCst);
113-
114-
loop {
115-
// Check if the iterator is exhausted
116-
if let Some(new_count) = count.checked_sub(1) {
117-
match self.split_count.compare_exchange_weak(
118-
count,
119-
new_count,
120-
Ordering::SeqCst,
121-
Ordering::SeqCst,
122-
) {
123-
Ok(_) => return (self, Some(self)),
124-
Err(last_count) => count = last_count,
125-
}
126-
} else {
127-
return (self, None);
128-
}
129-
}
112+
// Check if the iterator is exhausted
113+
let update = self
114+
.split_count
115+
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |c| c.checked_sub(1));
116+
(self, update.is_ok().then_some(self))
130117
}
131118

132119
fn fold_with<F>(self, mut folder: F) -> F

0 commit comments

Comments
 (0)