1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
use crate::{AllocError, InnerAtomicFlag, FALSE, TRUE};
use core::fmt::Debug;
use core::{
alloc::Layout,
iter::FusedIterator,
ptr::NonNull,
sync::atomic::{AtomicPtr, Ordering},
};
#[cfg(feature = "alloc_api")]
use {alloc::alloc::Global, core::alloc::*};
macro_rules! impl_all {
(impl $(@$tr:path =>)? $target:ident {
$($t:tt)*
}) => {
cfg_if::cfg_if! {
if #[cfg(feature = "alloc_api")] {
impl<T, A: Allocator> $($tr for)? $target <T, A> {
$($t)*
}
} else {
impl<T> $($tr for)? $target <T> {
$($t)*
}
}
}
};
}
struct PrevCell<T> {
init: InnerAtomicFlag,
prev: AtomicPtr<FillQueueNode<T>>,
}
impl<T> PrevCell<T> {
#[inline]
pub const fn new() -> Self {
return Self {
init: InnerAtomicFlag::new(FALSE),
prev: AtomicPtr::new(core::ptr::null_mut()),
};
}
#[inline]
pub fn set(&self, prev: *mut FillQueueNode<T>) {
cfg_if::cfg_if! {
if #[cfg(debug_assertions)] {
assert!(self.prev.swap(prev, Ordering::AcqRel).is_null());
self.init.store(TRUE, Ordering::Release);
} else {
self.prev.store(prev, Ordering::Release);
self.init.store(TRUE, Ordering::Release);
}
}
}
#[inline]
pub fn set_mut(&mut self, prev: *mut FillQueueNode<T>) {
let this_prev = self.prev.get_mut();
debug_assert!(this_prev.is_null());
*this_prev = prev;
*self.init.get_mut() = TRUE;
}
pub fn get(&self) -> *mut FillQueueNode<T> {
while self.init.load(Ordering::Acquire) == FALSE {
core::hint::spin_loop()
}
return self.prev.swap(core::ptr::null_mut(), Ordering::Acquire);
}
}
struct FillQueueNode<T> {
prev: PrevCell<T>,
v: T,
}
/// An atomic queue intended for use cases where taking the full contents of the queue is needed.
///
/// The queue is, basically, an atomic singly-linked list, where nodes are first allocated and then the list's tail
/// is atomically updated.
///
/// When the queue is "chopped", the list's tail is swaped to null, and it's previous tail is used as the base of the [`ChopIter`]
///
/// # Performance
/// The performance of pushing elements is expected to be similar to pushing elements to a [`SegQueue`](crossbeam::queue::SegQueue) or `Mutex<Vec<_>>`,
/// but "chopping" elements is expected to be arround 2 times faster than with a `Mutex<Vec<_>>`, and 3 times faster than a [`SegQueue`](crossbeam::queue::SegQueue)
///
/// > You can see the benchmark results [here](https://docs.google.com/spreadsheets/d/1wcyD3TlCQMCPFHOfeko5ytn-R7aM8T7lyKVir6vf_Wo/edit?usp=sharing)
///
/// # Use `FillQueue` when:
/// - You want a queue that's updateable by shared reference
/// - You want to retreive all elements of the queue at once
/// - There is no specifically desired order for the elements to be retreived on, or that order is LIFO (Last In First Out)
///
/// # Don't use `FillQueue` when:
/// - You don't need a queue updateable by shared reference
/// - You want to retreive the elements of the queue one by one (see [`SegQueue`](crossbeam::queue::SegQueue))
/// - You require the elements in a specific order that isn't LIFO
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct FillQueue<T, #[cfg(feature = "alloc_api")] A: Allocator = Global> {
head: AtomicPtr<FillQueueNode<T>>,
#[cfg(feature = "alloc_api")]
alloc: A,
}
impl<T> FillQueue<T> {
/// Creates a new [`FillQueue`] with the global allocator.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
/// ```
#[inline]
pub const fn new() -> Self {
Self {
head: AtomicPtr::new(core::ptr::null_mut()),
#[cfg(feature = "alloc_api")]
alloc: Global,
}
}
}
#[docfg::docfg(feature = "alloc_api")]
impl<T, A: Allocator> FillQueue<T, A> {
/// Creates a new [`FillQueue`] with the given allocator.
/// # Example
/// ```rust
/// #![feature(allocator_api)]
///
/// use utils_atomics::prelude::*;
/// use std::alloc::Global;
///
/// let queue = FillQueue::<i32>::new_in(Global);
/// ```
#[inline]
pub const fn new_in(alloc: A) -> Self {
Self {
head: AtomicPtr::new(core::ptr::null_mut()),
alloc,
}
}
/// Returns a reference to this queue's allocator.
/// # Example
/// ```rust
/// #![feature(allocator_api)]
///
/// use utils_atomics::prelude::*;
/// use std::alloc::Global;
///
/// let queue = FillQueue::<i32>::new();
/// let alloc : &Global = queue.allocator();
/// ```
#[inline]
pub fn allocator(&self) -> &A {
&self.alloc
}
}
impl_all! {
impl FillQueue {
/// Returns `true` if the que is currently empty, `false` otherwise.
/// # Safety
/// Whilst this method is not unsafe, it's result should be considered immediately stale.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
/// assert!(queue.is_empty());
/// ```
#[inline]
pub fn is_empty (&self) -> bool {
self.head.load(Ordering::Relaxed).is_null()
}
/// Uses atomic operations to push an element to the queue.
/// # Panics
/// This method panics if `alloc` fails to allocate the memory needed for the node.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
/// queue.push(1);
/// assert_eq!(queue.chop().next(), Some(1));
/// ```
#[inline]
pub fn push (&self, v: T) {
self.try_push(v).unwrap()
}
/// Uses non-atomic operations to push an element to the queue.
/// # Panics
/// This method panics if `alloc` fails to allocate the memory needed for the node.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let mut queue = FillQueue::<i32>::new();
/// queue.push_mut(1);
/// assert_eq!(queue.chop_mut().next(), Some(1));
/// ```
#[inline]
pub fn push_mut (&mut self, v: T) {
self.try_push_mut(v).unwrap()
}
/// Uses atomic operations to push an element to the queue.
///
/// # Errors
///
/// This method returns an error if `alloc` fails to allocate the memory needed for the node.
///
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
/// assert!(queue.try_push(1).is_ok());
/// assert_eq!(queue.chop().next(), Some(1));
/// ```
pub fn try_push (&self, v: T) -> Result<(), AllocError> {
let node = FillQueueNode {
prev: PrevCell::new(),
v
};
let layout = Layout::new::<FillQueueNode<T>>();
#[cfg(feature = "alloc_api")]
let ptr = self.alloc.allocate(layout)?.cast::<FillQueueNode<T>>();
#[cfg(not(feature = "alloc_api"))]
let ptr = match unsafe { NonNull::new(alloc::alloc::alloc(layout)) } {
Some(x) => x.cast::<FillQueueNode<T>>(),
None => return Err(AllocError)
};
unsafe {
ptr.as_ptr().write(node)
}
let prev = self.head.swap(ptr.as_ptr(), Ordering::AcqRel);
unsafe {
let rf = &*ptr.as_ptr();
rf.prev.set(prev);
}
Ok(())
}
/// Uses non-atomic operations to push an element to the queue.
///
/// # Safety
///
/// This method is safe because the mutable reference guarantees we are the only thread that can access this queue.
///
/// # Errors
///
/// This method returns an error if `alloc` fails to allocate the memory needed for the node.
///
/// # Example
///
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let mut queue = FillQueue::<i32>::new();
/// assert!(queue.try_push_mut(1).is_ok());
/// assert_eq!(queue.chop_mut().next(), Some(1));
/// ```
pub fn try_push_mut (&mut self, v: T) -> Result<(), AllocError> {
let node = FillQueueNode {
prev: PrevCell::new(),
v
};
let layout = Layout::new::<FillQueueNode<T>>();
#[cfg(feature = "alloc_api")]
let mut ptr = self.alloc.allocate(layout)?.cast::<FillQueueNode<T>>();
#[cfg(not(feature = "alloc_api"))]
let mut ptr = match unsafe { NonNull::new(alloc::alloc::alloc(layout)) } {
Some(x) => x.cast::<FillQueueNode<T>>(),
None => return Err(AllocError)
};
unsafe {
ptr.as_ptr().write(node);
let prev = core::ptr::replace(self.head.get_mut(), ptr.as_ptr());
ptr.as_mut().prev.set_mut(prev);
Ok(())
}
}
}
}
#[cfg(feature = "alloc_api")]
impl<T, A: Allocator> FillQueue<T, A> {
/// Returns a LIFO (Last In First Out) iterator over a chopped chunk of a [`FillQueue`].
/// The elements that find themselves inside the chopped region of the queue will be accessed through non-atomic operations.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
///
/// queue.push(1);
/// queue.push(2);
/// queue.push(3);
///
/// let mut iter = queue.chop();
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), None)
/// ```
#[inline]
pub fn chop(&self) -> ChopIter<T, A>
where
A: Clone,
{
let ptr = self.head.swap(core::ptr::null_mut(), Ordering::AcqRel);
ChopIter {
ptr: NonNull::new(ptr),
alloc: self.alloc.clone(),
}
}
/// Returns a LIFO (Last In First Out) iterator over a chopped chunk of a [`FillQueue`]. The chopping is done with non-atomic operations.
/// # Safety
/// This method is safe because the mutable reference guarantees we are the only thread that can access this queue.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let mut queue = FillQueue::<i32>::new();
///
/// queue.push_mut(1);
/// queue.push_mut(2);
/// queue.push_mut(3);
///
/// let mut iter = queue.chop_mut();
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), None)
/// ```
#[inline]
pub fn chop_mut(&mut self) -> ChopIter<T, A>
where
A: Clone,
{
let ptr = unsafe { core::ptr::replace(self.head.get_mut(), core::ptr::null_mut()) };
ChopIter {
ptr: NonNull::new(ptr),
alloc: self.alloc.clone(),
}
}
}
#[cfg(not(feature = "alloc_api"))]
impl<T> FillQueue<T> {
/// Returns a LIFO (Last In First Out) iterator over a chopped chunk of a [`FillQueue`].
/// The elements that find themselves inside the chopped region of the queue will be accessed through non-atomic operations.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let queue = FillQueue::<i32>::new();
///
/// queue.push(1);
/// queue.push(2);
/// queue.push(3);
///
/// let mut iter = queue.chop();
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), None)
/// ```
#[inline]
pub fn chop(&self) -> ChopIter<T> {
let ptr = self.head.swap(core::ptr::null_mut(), Ordering::AcqRel);
ChopIter {
ptr: NonNull::new(ptr),
}
}
/// Returns a LIFO (Last In First Out) iterator over a chopped chunk of a [`FillQueue`]. The chopping is done with non-atomic operations.
/// # Safety
/// This method is safe because the mutable reference guarantees we are the only thread that can access this queue.
/// # Example
/// ```rust
/// use utils_atomics::prelude::*;
///
/// let mut queue = FillQueue::<i32>::new();
///
/// queue.push_mut(1);
/// queue.push_mut(2);
/// queue.push_mut(3);
///
/// let mut iter = queue.chop_mut();
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(1));
/// assert_eq!(iter.next(), None)
/// ```
#[inline]
pub fn chop_mut(&mut self) -> ChopIter<T> {
let ptr = unsafe { core::ptr::replace(self.head.get_mut(), core::ptr::null_mut()) };
ChopIter {
ptr: NonNull::new(ptr),
}
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "alloc_api")] {
unsafe impl<T: Send, A: Send + Allocator> Send for FillQueue<T, A> {}
unsafe impl<T: Sync, A: Sync + Allocator> Sync for FillQueue<T, A> {}
unsafe impl<T: Send, A: Send + Allocator> Send for ChopIter<T, A> {}
unsafe impl<T: Sync, A: Sync + Allocator> Sync for ChopIter<T, A> {}
} else {
unsafe impl<T: Send> Send for FillQueue<T> {}
unsafe impl<T: Sync> Sync for FillQueue<T> {}
unsafe impl<T: Send> Send for ChopIter<T> {}
unsafe impl<T: Sync> Sync for ChopIter<T> {}
}
}
/// Iterator of [`FillQueue::chop`] and [`FillQueue::chop_mut`]
pub struct ChopIter<T, #[cfg(feature = "alloc_api")] A: Allocator = Global> {
ptr: Option<NonNull<FillQueueNode<T>>>,
#[cfg(feature = "alloc_api")]
alloc: A,
}
impl_all! {
impl @Iterator => ChopIter {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(ptr) = self.ptr {
unsafe {
let node = &*ptr.as_ptr();
let value = core::ptr::read(&node.v);
self.ptr = NonNull::new(node.prev.get());
#[cfg(feature = "alloc_api")]
self.alloc.deallocate(ptr.cast(), Layout::new::<FillQueueNode<T>>());
#[cfg(not(feature = "alloc_api"))]
alloc::alloc::dealloc(ptr.as_ptr().cast(), Layout::new::<FillQueueNode<T>>());
return Some(value)
}
}
None
}
}
}
impl_all! {
impl @Drop => ChopIter {
#[inline]
fn drop(&mut self) {
self.for_each(core::mem::drop)
}
}
}
impl_all! {
impl @FusedIterator => ChopIter {}
}
#[cfg(feature = "alloc_api")]
impl<T, A: Debug + Allocator> Debug for FillQueue<T, A> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
f.debug_struct("FillQueue")
.field("alloc", &self.alloc)
.finish_non_exhaustive()
}
}
#[cfg(not(feature = "alloc_api"))]
impl<T> Debug for FillQueue<T> {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_struct("FillQueue").finish_non_exhaustive()
}
}
// Thanks ChatGPT!
#[cfg(test)]
mod tests {
use super::FillQueue;
#[test]
fn test_basic_functionality() {
let mut fill_queue = FillQueue::new();
assert!(fill_queue.is_empty());
fill_queue.push(1);
fill_queue.push(2);
fill_queue.push(3);
assert!(!fill_queue.is_empty());
let mut chop_iter = fill_queue.chop_mut();
assert_eq!(chop_iter.next(), Some(3));
assert_eq!(chop_iter.next(), Some(2));
assert_eq!(chop_iter.next(), Some(1));
assert_eq!(chop_iter.next(), None);
fill_queue.push_mut(1);
fill_queue.push_mut(2);
fill_queue.push_mut(3);
let mut chop_iter = fill_queue.chop();
assert_eq!(chop_iter.next(), Some(3));
assert_eq!(chop_iter.next(), Some(2));
assert_eq!(chop_iter.next(), Some(1));
assert_eq!(chop_iter.next(), None);
assert!(fill_queue.is_empty());
}
#[cfg(feature = "std")]
#[test]
fn test_concurrent_fill_queue() {
use core::sync::atomic::{AtomicUsize, Ordering};
let fill_queue = FillQueue::new();
let mut count = AtomicUsize::new(0);
std::thread::scope(|s| {
for _ in 0..10 {
s.spawn(|| {
for i in 1..=10 {
fill_queue.push(i);
}
count.fetch_add(fill_queue.chop().count(), Ordering::Relaxed);
});
}
});
assert_eq!(*count.get_mut(), 100);
}
}