From d1218a02e0623e9110652360d870f149f163422c Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 21 Jul 2022 23:51:44 +0900 Subject: [PATCH] Use get_mut instead of atomic load in Drop impls --- src/bounded.rs | 18 ++++++++++++++++-- src/unbounded.rs | 8 ++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/bounded.rs b/src/bounded.rs index 7a7fb71..5ceeb50 100644 --- a/src/bounded.rs +++ b/src/bounded.rs @@ -284,10 +284,24 @@ impl Bounded { impl Drop for Bounded { 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 diff --git a/src/unbounded.rs b/src/unbounded.rs index c7cba68..94b1bdf 100644 --- a/src/unbounded.rs +++ b/src/unbounded.rs @@ -371,9 +371,9 @@ impl Unbounded { impl Drop for Unbounded { 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 Drop for Unbounded { 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; }