Skip to content

Commit 6baa072

Browse files
bors[bot]taiki-e
andauthored
Merge #874
874: Use get_mut instead of atomic load in Drop impls r=taiki-e a=taiki-e Similar to #811, but for deque and queue. Co-authored-by: Taiki Endo <te316e89@gmail.com>
2 parents 2d97a16 + 5866c43 commit 6baa072

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

crossbeam-deque/src/deque.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ struct Inner<T> {
116116
impl<T> Drop for Inner<T> {
117117
fn drop(&mut self) {
118118
// Load the back index, front index, and buffer.
119-
let b = self.back.load(Ordering::Relaxed);
120-
let f = self.front.load(Ordering::Relaxed);
119+
let b = *self.back.get_mut();
120+
let f = *self.front.get_mut();
121121

122122
unsafe {
123123
let buffer = self.buffer.load(Ordering::Relaxed, epoch::unprotected());
@@ -1816,9 +1816,9 @@ impl<T> Injector<T> {
18161816

18171817
impl<T> Drop for Injector<T> {
18181818
fn drop(&mut self) {
1819-
let mut head = self.head.index.load(Ordering::Relaxed);
1820-
let mut tail = self.tail.index.load(Ordering::Relaxed);
1821-
let mut block = self.head.block.load(Ordering::Relaxed);
1819+
let mut head = *self.head.index.get_mut();
1820+
let mut tail = *self.tail.index.get_mut();
1821+
let mut block = *self.head.block.get_mut();
18221822

18231823
// Erase the lower bits.
18241824
head &= !((1 << SHIFT) - 1);
@@ -1836,7 +1836,7 @@ impl<T> Drop for Injector<T> {
18361836
p.as_mut_ptr().drop_in_place();
18371837
} else {
18381838
// Deallocate the block and move to the next one.
1839-
let next = (*block).next.load(Ordering::Relaxed);
1839+
let next = *(*block).next.get_mut();
18401840
drop(Box::from_raw(block));
18411841
block = next;
18421842
}

crossbeam-queue/src/array_queue.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,24 @@ impl<T> ArrayQueue<T> {
444444
impl<T> Drop for ArrayQueue<T> {
445445
fn drop(&mut self) {
446446
// Get the index of the head.
447-
let hix = self.head.load(Ordering::Relaxed) & (self.one_lap - 1);
447+
let head = *self.head.get_mut();
448+
let tail = *self.tail.get_mut();
449+
450+
let hix = head & (self.one_lap - 1);
451+
let tix = tail & (self.one_lap - 1);
452+
453+
let len = if hix < tix {
454+
tix - hix
455+
} else if hix > tix {
456+
self.cap - hix + tix
457+
} else if tail == head {
458+
0
459+
} else {
460+
self.cap
461+
};
448462

449463
// Loop over all slots that hold a message and drop them.
450-
for i in 0..self.len() {
464+
for i in 0..len {
451465
// Compute the index of the next slot holding a message.
452466
let index = if hix + i < self.cap {
453467
hix + i

crossbeam-queue/src/seg_queue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ impl<T> SegQueue<T> {
437437

438438
impl<T> Drop for SegQueue<T> {
439439
fn drop(&mut self) {
440-
let mut head = self.head.index.load(Ordering::Relaxed);
441-
let mut tail = self.tail.index.load(Ordering::Relaxed);
442-
let mut block = self.head.block.load(Ordering::Relaxed);
440+
let mut head = *self.head.index.get_mut();
441+
let mut tail = *self.tail.index.get_mut();
442+
let mut block = *self.head.block.get_mut();
443443

444444
// Erase the lower bits.
445445
head &= !((1 << SHIFT) - 1);
@@ -457,7 +457,7 @@ impl<T> Drop for SegQueue<T> {
457457
p.as_mut_ptr().drop_in_place();
458458
} else {
459459
// Deallocate the block and move to the next one.
460-
let next = (*block).next.load(Ordering::Relaxed);
460+
let next = *(*block).next.get_mut();
461461
drop(Box::from_raw(block));
462462
block = next;
463463
}

0 commit comments

Comments
 (0)