Use get_mut instead of atomic load in Drop impls

This commit is contained in:
Taiki Endo 2022-07-21 23:51:44 +09:00
parent d61005fc79
commit d1218a02e0
2 changed files with 20 additions and 6 deletions

View File

@ -284,10 +284,24 @@ impl<T> Bounded<T> {
impl<T> Drop for Bounded<T> {
fn drop(&mut self) {
// Get the index of the head.
let hix = self.head.load(Ordering::Relaxed) & (self.mark_bit - 1);
let head = *self.head.get_mut();
let tail = *self.tail.get_mut();
let hix = head & (self.mark_bit - 1);
let tix = tail & (self.mark_bit - 1);
let len = if hix < tix {
tix - hix
} else if hix > tix {
self.buffer.len() - hix + tix
} else if (tail & !self.mark_bit) == head {
0
} else {
self.buffer.len()
};
// Loop over all slots that hold a value and drop them.
for i in 0..self.len() {
for i in 0..len {
// Compute the index of the next slot holding a value.
let index = if hix + i < self.buffer.len() {
hix + i

View File

@ -371,9 +371,9 @@ impl<T> Unbounded<T> {
impl<T> Drop for Unbounded<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);
@ -391,7 +391,7 @@ impl<T> Drop for Unbounded<T> {
drop(value);
} 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;
}