Skip to content

Commit

Permalink
Merge #874
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and taiki-e committed Jul 21, 2022
2 parents 2d97a16 + 5866c43 commit 6baa072
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
12 changes: 6 additions & 6 deletions crossbeam-deque/src/deque.rs
Expand Up @@ -116,8 +116,8 @@ struct Inner<T> {
impl<T> Drop for Inner<T> {
fn drop(&mut self) {
// Load the back index, front index, and buffer.
let b = self.back.load(Ordering::Relaxed);
let f = self.front.load(Ordering::Relaxed);
let b = *self.back.get_mut();
let f = *self.front.get_mut();

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

impl<T> Drop for Injector<T> {
fn drop(&mut self) {
let mut head = self.head.index.load(Ordering::Relaxed);
let mut tail = self.tail.index.load(Ordering::Relaxed);
let mut block = self.head.block.load(Ordering::Relaxed);
let mut head = *self.head.index.get_mut();
let mut tail = *self.tail.index.get_mut();
let mut block = *self.head.block.get_mut();

// Erase the lower bits.
head &= !((1 << SHIFT) - 1);
Expand All @@ -1836,7 +1836,7 @@ impl<T> Drop for Injector<T> {
p.as_mut_ptr().drop_in_place();
} else {
// Deallocate the block and move to the next one.
let next = (*block).next.load(Ordering::Relaxed);
let next = *(*block).next.get_mut();
drop(Box::from_raw(block));
block = next;
}
Expand Down
18 changes: 16 additions & 2 deletions crossbeam-queue/src/array_queue.rs
Expand Up @@ -444,10 +444,24 @@ impl<T> ArrayQueue<T> {
impl<T> Drop for ArrayQueue<T> {
fn drop(&mut self) {
// Get the index of the head.
let hix = self.head.load(Ordering::Relaxed) & (self.one_lap - 1);
let head = *self.head.get_mut();
let tail = *self.tail.get_mut();

let hix = head & (self.one_lap - 1);
let tix = tail & (self.one_lap - 1);

let len = if hix < tix {
tix - hix
} else if hix > tix {
self.cap - hix + tix
} else if tail == head {
0
} else {
self.cap
};

// Loop over all slots that hold a message and drop them.
for i in 0..self.len() {
for i in 0..len {
// Compute the index of the next slot holding a message.
let index = if hix + i < self.cap {
hix + i
Expand Down
8 changes: 4 additions & 4 deletions crossbeam-queue/src/seg_queue.rs
Expand Up @@ -437,9 +437,9 @@ impl<T> SegQueue<T> {

impl<T> Drop for SegQueue<T> {
fn drop(&mut self) {
let mut head = self.head.index.load(Ordering::Relaxed);
let mut tail = self.tail.index.load(Ordering::Relaxed);
let mut block = self.head.block.load(Ordering::Relaxed);
let mut head = *self.head.index.get_mut();
let mut tail = *self.tail.index.get_mut();
let mut block = *self.head.block.get_mut();

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

0 comments on commit 6baa072

Please sign in to comment.