Skip to content

Commit

Permalink
Use get_mut instead of atomic load in Drop impls
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jul 21, 2022
1 parent 2d97a16 commit c29ed9c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
10 changes: 5 additions & 5 deletions crossbeam-deque/src/deque.rs
Original file line number Diff line number Diff line change
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 Down
18 changes: 16 additions & 2 deletions crossbeam-queue/src/array_queue.rs
Original file line number Diff line number Diff line change
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 tail = *self.tail.get_mut();
let head = *self.head.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
Original file line number Diff line number Diff line change
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 c29ed9c

Please sign in to comment.