Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move fetch_and after the prev_value check in ConcurrentQueue::single::force_push #71

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,27 @@ impl<T> Single<T> {
}

if prev == state {
// Swap out the value.
// SAFETY: We have locked the state.
let prev_value = unsafe {
self.slot
.with_mut(move |slot| ptr::replace(slot, MaybeUninit::new(value)))
};

// We can unlock the slot now.
self.state.fetch_and(!LOCKED, Ordering::Release);

// If the value was pushed, initialize it and return it.
// If the value was pushed, swap out the value.
let prev_value = if prev & PUSHED == 0 {
// SAFETY: write is safe because we have locked the state.
self.slot.with_mut(|slot| unsafe {
slot.write(MaybeUninit::new(value));
});
None
} else {
Some(unsafe { prev_value.assume_init() })
// SAFETY: replace is safe because we have locked the state, and
// assume_init is safe because we have checked that the value was pushed.
let prev_value = unsafe {
self.slot.with_mut(move |slot| {
ptr::replace(slot, MaybeUninit::new(value)).assume_init()
})
};
Some(prev_value)
};

// We can unlock the slot now.
self.state.fetch_and(!LOCKED, Ordering::Release);

// Return the old value.
return Ok(prev_value);
}
Expand Down
Loading