Skip to content

Commit

Permalink
📝 fix unsound issue in may_queue (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Sep 1, 2024
1 parent 8a8768c commit 5095ab1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions may_queue/src/mpsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl<T> BlockNode<T> {
/// write index with data
#[inline]
fn set(&self, id: usize, v: T) {
debug_assert!(id < BLOCK_SIZE);
unsafe {
let data = self.data.get_unchecked(id);
data.value.get().write(MaybeUninit::new(v));
Expand All @@ -79,6 +80,7 @@ impl<T> BlockNode<T> {

#[inline]
fn try_get(&self, id: usize) -> Option<T> {
debug_assert!(id < BLOCK_SIZE);
let data = unsafe { self.data.get_unchecked(id) };
if data.ready.load(Ordering::Acquire) != 0 {
Some(unsafe { data.value.get().read().assume_init() })
Expand All @@ -89,6 +91,7 @@ impl<T> BlockNode<T> {

#[inline]
fn get(&self, id: usize) -> T {
debug_assert!(id < BLOCK_SIZE);
let data = unsafe { self.data.get_unchecked(id) };
while data.ready.load(Ordering::Acquire) == 0 {
std::hint::spin_loop();
Expand Down
1 change: 1 addition & 0 deletions may_queue/src/spmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl<T> BlockNode<T> {
/// this would make the underlying data dropped when it get out of scope
#[inline]
fn get(&self, id: usize) -> T {
debug_assert!(id < BLOCK_SIZE);
unsafe {
let data = self.data.get_unchecked(id);
data.value.get().read().assume_init()
Expand Down
1 change: 1 addition & 0 deletions may_queue/src/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl<T> BlockNode<T> {
/// this would make the underlying data dropped when it get out of scope
#[inline]
fn get(&self, id: usize) -> T {
debug_assert!(id < BLOCK_SIZE);
unsafe {
let data = self.data.get_unchecked(id);
data.value.get().read().assume_init()
Expand Down

0 comments on commit 5095ab1

Please sign in to comment.