rtrb/lib.rs
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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
//! A realtime-safe single-producer single-consumer (SPSC) ring buffer.
//!
//! A [`RingBuffer`] consists of two parts:
//! a [`Producer`] for writing into the ring buffer and
//! a [`Consumer`] for reading from the ring buffer.
//!
//! A fixed-capacity buffer is allocated on construction.
//! After that, no more memory is allocated (unless the type `T` does that internally).
//! Reading from and writing into the ring buffer is *lock-free* and *wait-free*.
//! All reading and writing functions return immediately.
//! Attempts to write to a full buffer return an error;
//! values inside the buffer are *not* overwritten.
//! Attempts to read from an empty buffer return an error as well.
//! Only a single thread can write into the ring buffer and a single thread
//! (typically a different one) can read from the ring buffer.
//! If the queue is empty, there is no way for the reading thread to wait
//! for new data, other than trying repeatedly until reading succeeds.
//! Similarly, if the queue is full, there is no way for the writing thread
//! to wait for newly available space to write to, other than trying repeatedly.
//!
//! # Examples
//!
//! Moving single elements into and out of a queue with
//! [`Producer::push()`] and [`Consumer::pop()`], respectively:
//!
//! ```
//! use rtrb::{RingBuffer, PushError, PopError};
//!
//! let (mut producer, mut consumer) = RingBuffer::new(2);
//!
//! assert_eq!(producer.push(10), Ok(()));
//! assert_eq!(producer.push(20), Ok(()));
//! assert_eq!(producer.push(30), Err(PushError::Full(30)));
//!
//! std::thread::spawn(move || {
//! assert_eq!(consumer.pop(), Ok(10));
//! assert_eq!(consumer.pop(), Ok(20));
//! assert_eq!(consumer.pop(), Err(PopError::Empty));
//! }).join().unwrap();
//! ```
//!
//! See the documentation of the [`chunks#examples`] module
//! for examples that write multiple items at once with
//! [`Producer::write_chunk_uninit()`] and [`Producer::write_chunk()`]
//! and read multiple items with [`Consumer::read_chunk()`].
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(rust_2018_idioms)]
#![deny(missing_docs, missing_debug_implementations)]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
extern crate alloc;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cell::Cell;
use core::fmt;
use core::marker::PhantomData;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::sync::atomic::{AtomicUsize, Ordering};
#[allow(dead_code, clippy::undocumented_unsafe_blocks)]
mod cache_padded;
use cache_padded::CachePadded;
pub mod chunks;
// This is used in the documentation.
#[allow(unused_imports)]
use chunks::WriteChunkUninit;
/// A bounded single-producer single-consumer (SPSC) queue.
///
/// Elements can be written with a [`Producer`] and read with a [`Consumer`],
/// both of which can be obtained with [`RingBuffer::new()`].
///
/// *See also the [crate-level documentation](crate).*
#[derive(Debug)]
pub struct RingBuffer<T> {
/// The head of the queue.
///
/// This integer is in range `0 .. 2 * capacity`.
head: CachePadded<AtomicUsize>,
/// The tail of the queue.
///
/// This integer is in range `0 .. 2 * capacity`.
tail: CachePadded<AtomicUsize>,
/// The buffer holding slots.
data_ptr: *mut T,
/// The queue capacity.
capacity: usize,
/// Indicates that dropping a `RingBuffer<T>` may drop elements of type `T`.
_marker: PhantomData<T>,
}
impl<T> RingBuffer<T> {
/// Creates a `RingBuffer` with the given `capacity` and returns [`Producer`] and [`Consumer`].
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (producer, consumer) = RingBuffer::<f32>::new(100);
/// ```
///
/// Specifying an explicit type with the [turbofish](https://turbo.fish/)
/// is is only necessary if it cannot be deduced by the compiler.
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (mut producer, consumer) = RingBuffer::new(100);
/// assert_eq!(producer.push(0.0f32), Ok(()));
/// ```
#[allow(clippy::new_ret_no_self)]
#[must_use]
pub fn new(capacity: usize) -> (Producer<T>, Consumer<T>) {
let buffer = Arc::new(RingBuffer {
head: CachePadded::new(AtomicUsize::new(0)),
tail: CachePadded::new(AtomicUsize::new(0)),
data_ptr: ManuallyDrop::new(Vec::with_capacity(capacity)).as_mut_ptr(),
capacity,
_marker: PhantomData,
});
let p = Producer {
buffer: buffer.clone(),
cached_head: Cell::new(0),
cached_tail: Cell::new(0),
};
let c = Consumer {
buffer,
cached_head: Cell::new(0),
cached_tail: Cell::new(0),
};
(p, c)
}
/// Returns the capacity of the queue.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (producer, consumer) = RingBuffer::<f32>::new(100);
/// assert_eq!(producer.buffer().capacity(), 100);
/// assert_eq!(consumer.buffer().capacity(), 100);
/// // Both producer and consumer of course refer to the same ring buffer:
/// assert_eq!(producer.buffer(), consumer.buffer());
/// ```
pub fn capacity(&self) -> usize {
self.capacity
}
/// Wraps a position from the range `0 .. 2 * capacity` to `0 .. capacity`.
fn collapse_position(&self, pos: usize) -> usize {
debug_assert!(pos == 0 || pos < 2 * self.capacity);
if pos < self.capacity {
pos
} else {
pos - self.capacity
}
}
/// Returns a pointer to the slot at position `pos`.
///
/// If `pos == 0 && capacity == 0`, the returned pointer must not be dereferenced!
unsafe fn slot_ptr(&self, pos: usize) -> *mut T {
debug_assert!(pos == 0 || pos < 2 * self.capacity);
let pos = self.collapse_position(pos);
// SAFETY: The caller must ensure a valid pos.
unsafe { self.data_ptr.add(pos) }
}
/// Increments a position by going `n` slots forward.
fn increment(&self, pos: usize, n: usize) -> usize {
debug_assert!(pos == 0 || pos < 2 * self.capacity);
debug_assert!(n <= self.capacity);
let threshold = 2 * self.capacity - n;
if pos < threshold {
pos + n
} else {
pos - threshold
}
}
/// Increments a position by going one slot forward.
///
/// This is more efficient than self.increment(..., 1).
fn increment1(&self, pos: usize) -> usize {
debug_assert_ne!(self.capacity, 0);
debug_assert!(pos < 2 * self.capacity);
if pos < 2 * self.capacity - 1 {
pos + 1
} else {
0
}
}
/// Returns the distance between two positions.
fn distance(&self, a: usize, b: usize) -> usize {
debug_assert!(a == 0 || a < 2 * self.capacity);
debug_assert!(b == 0 || b < 2 * self.capacity);
if a <= b {
b - a
} else {
2 * self.capacity - a + b
}
}
}
impl<T> Drop for RingBuffer<T> {
/// Drops all non-empty slots.
fn drop(&mut self) {
let mut head = self.head.load(Ordering::Relaxed);
let tail = self.tail.load(Ordering::Relaxed);
// Loop over all slots that hold a value and drop them.
while head != tail {
// SAFETY: All slots between head and tail have been initialized.
unsafe { self.slot_ptr(head).drop_in_place() };
head = self.increment1(head);
}
// Finally, deallocate the buffer, but don't run any destructors.
// SAFETY: data_ptr and capacity are still valid from the original initialization.
unsafe { Vec::from_raw_parts(self.data_ptr, 0, self.capacity) };
}
}
impl<T> PartialEq for RingBuffer<T> {
/// This method tests for `self` and `other` values to be equal, and is used by `==`.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (p1, c1) = RingBuffer::<f32>::new(1000);
/// assert_eq!(p1.buffer(), c1.buffer());
///
/// let (p2, c2) = RingBuffer::<f32>::new(1000);
/// assert_ne!(p1.buffer(), p2.buffer());
/// ```
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self, other)
}
}
impl<T> Eq for RingBuffer<T> {}
/// The producer side of a [`RingBuffer`].
///
/// Can be moved between threads,
/// but references from different threads are not allowed
/// (i.e. it is [`Send`] but not [`Sync`]).
///
/// Can only be created with [`RingBuffer::new()`]
/// (together with its counterpart, the [`Consumer`]).
///
/// Individual elements can be moved into the ring buffer with [`Producer::push()`],
/// multiple elements at once can be written with [`Producer::write_chunk()`]
/// and [`Producer::write_chunk_uninit()`].
///
/// The number of free slots currently available for writing can be obtained with
/// [`Producer::slots()`].
///
/// When the `Producer` is dropped, [`Consumer::is_abandoned()`] will return `true`.
/// This can be used as a crude way to communicate to the receiving thread
/// that no more data will be produced.
/// When the `Producer` is dropped after the [`Consumer`] has already been dropped,
/// [`RingBuffer::drop()`] will be called, freeing the allocated memory.
#[derive(Debug, PartialEq, Eq)]
pub struct Producer<T> {
/// A reference to the ring buffer.
buffer: Arc<RingBuffer<T>>,
/// A copy of `buffer.head` for quick access.
///
/// This value can be stale and sometimes needs to be resynchronized with `buffer.head`.
cached_head: Cell<usize>,
/// A copy of `buffer.tail` for quick access.
///
/// This value is always in sync with `buffer.tail`.
// NB: Caching the tail seems to have little effect on Intel CPUs, but it seems to
// improve performance on AMD CPUs, see https://github.com/mgeier/rtrb/pull/132
cached_tail: Cell<usize>,
}
// SAFETY: After moving a Producer to another thread, there is still only a single thread
// that can access the producer side of the queue.
unsafe impl<T: Send> Send for Producer<T> {}
impl<T> Producer<T> {
/// Attempts to push an element into the queue.
///
/// The element is *moved* into the ring buffer and its slot
/// is made available to be read by the [`Consumer`].
///
/// # Errors
///
/// If the queue is full, the element is returned back as an error.
///
/// # Examples
///
/// ```
/// use rtrb::{RingBuffer, PushError};
///
/// let (mut p, c) = RingBuffer::new(1);
///
/// assert_eq!(p.push(10), Ok(()));
/// assert_eq!(p.push(20), Err(PushError::Full(20)));
/// ```
pub fn push(&mut self, value: T) -> Result<(), PushError<T>> {
if let Some(tail) = self.next_tail() {
// SAFETY: tail points to an empty slot.
unsafe { self.buffer.slot_ptr(tail).write(value) };
let tail = self.buffer.increment1(tail);
self.buffer.tail.store(tail, Ordering::Release);
self.cached_tail.set(tail);
Ok(())
} else {
Err(PushError::Full(value))
}
}
/// Returns the number of slots available for writing.
///
/// Since items can be concurrently consumed on another thread, the actual number
/// of available slots may increase at any time (up to the [`RingBuffer::capacity()`]).
///
/// To check for a single available slot,
/// using [`Producer::is_full()`] is often quicker
/// (because it might not have to check an atomic variable).
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (p, c) = RingBuffer::<f32>::new(1024);
///
/// assert_eq!(p.slots(), 1024);
/// ```
pub fn slots(&self) -> usize {
let head = self.buffer.head.load(Ordering::Acquire);
self.cached_head.set(head);
self.buffer.capacity - self.buffer.distance(head, self.cached_tail.get())
}
/// Returns `true` if there are currently no slots available for writing.
///
/// A full ring buffer might cease to be full at any time
/// if the corresponding [`Consumer`] is consuming items in another thread.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (p, c) = RingBuffer::<f32>::new(1);
///
/// assert!(!p.is_full());
/// ```
///
/// Since items can be concurrently consumed on another thread, the ring buffer
/// might not be full for long:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<f32>::new(1);
/// if p.is_full() {
/// // The buffer might be full, but it might as well not be
/// // if an item was just consumed on another thread.
/// }
/// ```
///
/// However, if it's not full, another thread cannot change that:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<f32>::new(1);
/// if !p.is_full() {
/// // At least one slot is guaranteed to be available for writing.
/// }
/// ```
pub fn is_full(&self) -> bool {
self.next_tail().is_none()
}
/// Returns `true` if the corresponding [`Consumer`] has been destroyed.
///
/// Note that since Rust version 1.74.0, this is not synchronizing with the consumer thread
/// anymore, see <https://github.com/mgeier/rtrb/issues/114>.
/// In a future version of `rtrb`, the synchronizing behavior might be restored.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (mut p, c) = RingBuffer::new(7);
/// assert!(!p.is_abandoned());
/// assert_eq!(p.push(10), Ok(()));
/// drop(c);
/// // The items that are still in the ring buffer are not accessible anymore.
/// assert!(p.is_abandoned());
/// // Even though it's futile, items can still be written:
/// assert_eq!(p.push(11), Ok(()));
/// ```
///
/// Since the consumer can be concurrently dropped on another thread,
/// the producer might become abandoned at any time:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<i32>::new(1);
/// if !p.is_abandoned() {
/// // Right now, the consumer might still be alive, but it might as well not be
/// // if another thread has just dropped it.
/// }
/// ```
///
/// However, if it already is abandoned, it will stay that way:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<i32>::new(1);
/// if p.is_abandoned() {
/// // This is needed since Rust 1.74.0, see https://github.com/mgeier/rtrb/issues/114:
/// std::sync::atomic::fence(std::sync::atomic::Ordering::Acquire);
/// // The consumer does definitely not exist anymore.
/// }
/// ```
pub fn is_abandoned(&self) -> bool {
Arc::strong_count(&self.buffer) < 2
}
/// Returns a read-only reference to the ring buffer.
pub fn buffer(&self) -> &RingBuffer<T> {
&self.buffer
}
/// Get the tail position for writing the next slot, if available.
///
/// This is a strict subset of the functionality implemented in `write_chunk_uninit()`.
/// For performance, this special case is immplemented separately.
fn next_tail(&self) -> Option<usize> {
let tail = self.cached_tail.get();
// Check if the queue is *possibly* full.
if self.buffer.distance(self.cached_head.get(), tail) == self.buffer.capacity {
// Refresh the head ...
let head = self.buffer.head.load(Ordering::Acquire);
self.cached_head.set(head);
// ... and check if it's *really* full.
if self.buffer.distance(head, tail) == self.buffer.capacity {
return None;
}
}
Some(tail)
}
}
/// The consumer side of a [`RingBuffer`].
///
/// Can be moved between threads,
/// but references from different threads are not allowed
/// (i.e. it is [`Send`] but not [`Sync`]).
///
/// Can only be created with [`RingBuffer::new()`]
/// (together with its counterpart, the [`Producer`]).
///
/// Individual elements can be moved out of the ring buffer with [`Consumer::pop()`],
/// multiple elements at once can be read with [`Consumer::read_chunk()`].
///
/// The number of slots currently available for reading can be obtained with
/// [`Consumer::slots()`].
///
/// When the `Consumer` is dropped, [`Producer::is_abandoned()`] will return `true`.
/// This can be used as a crude way to communicate to the sending thread
/// that no more data will be consumed.
/// When the `Consumer` is dropped after the [`Producer`] has already been dropped,
/// [`RingBuffer::drop()`] will be called, freeing the allocated memory.
#[derive(Debug, PartialEq, Eq)]
pub struct Consumer<T> {
/// A reference to the ring buffer.
buffer: Arc<RingBuffer<T>>,
/// A copy of `buffer.head` for quick access.
///
/// This value is always in sync with `buffer.head`.
// NB: Caching the head seems to have little effect on Intel CPUs, but it seems to
// improve performance on AMD CPUs, see https://github.com/mgeier/rtrb/pull/132
cached_head: Cell<usize>,
/// A copy of `buffer.tail` for quick access.
///
/// This value can be stale and sometimes needs to be resynchronized with `buffer.tail`.
cached_tail: Cell<usize>,
}
// SAFETY: After moving a Consumer to another thread, there is still only a single thread
// that can access the consumer side of the queue.
unsafe impl<T: Send> Send for Consumer<T> {}
impl<T> Consumer<T> {
/// Attempts to pop an element from the queue.
///
/// The element is *moved* out of the ring buffer and its slot
/// is made available to be filled by the [`Producer`] again.
///
/// # Errors
///
/// If the queue is empty, an error is returned.
///
/// # Examples
///
/// ```
/// use rtrb::{PopError, RingBuffer};
///
/// let (mut p, mut c) = RingBuffer::new(1);
///
/// assert_eq!(p.push(10), Ok(()));
/// assert_eq!(c.pop(), Ok(10));
/// assert_eq!(c.pop(), Err(PopError::Empty));
/// ```
///
/// To obtain an [`Option<T>`](Option), use [`.ok()`](Result::ok) on the result.
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (mut p, mut c) = RingBuffer::new(1);
/// assert_eq!(p.push(20), Ok(()));
/// assert_eq!(c.pop().ok(), Some(20));
/// ```
pub fn pop(&mut self) -> Result<T, PopError> {
if let Some(head) = self.next_head() {
// SAFETY: head points to an initialized slot.
let value = unsafe { self.buffer.slot_ptr(head).read() };
let head = self.buffer.increment1(head);
self.buffer.head.store(head, Ordering::Release);
self.cached_head.set(head);
Ok(value)
} else {
Err(PopError::Empty)
}
}
/// Attempts to read an element from the queue without removing it.
///
/// # Errors
///
/// If the queue is empty, an error is returned.
///
/// # Examples
///
/// ```
/// use rtrb::{PeekError, RingBuffer};
///
/// let (mut p, c) = RingBuffer::new(1);
///
/// assert_eq!(c.peek(), Err(PeekError::Empty));
/// assert_eq!(p.push(10), Ok(()));
/// assert_eq!(c.peek(), Ok(&10));
/// assert_eq!(c.peek(), Ok(&10));
/// ```
pub fn peek(&self) -> Result<&T, PeekError> {
if let Some(head) = self.next_head() {
// SAFETY: head points to an initialized slot.
Ok(unsafe { &*self.buffer.slot_ptr(head) })
} else {
Err(PeekError::Empty)
}
}
/// Returns the number of slots available for reading.
///
/// Since items can be concurrently produced on another thread, the actual number
/// of available slots may increase at any time (up to the [`RingBuffer::capacity()`]).
///
/// To check for a single available slot,
/// using [`Consumer::is_empty()`] is often quicker
/// (because it might not have to check an atomic variable).
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (p, c) = RingBuffer::<f32>::new(1024);
///
/// assert_eq!(c.slots(), 0);
/// ```
pub fn slots(&self) -> usize {
let tail = self.buffer.tail.load(Ordering::Acquire);
self.cached_tail.set(tail);
self.buffer.distance(self.cached_head.get(), tail)
}
/// Returns `true` if there are currently no slots available for reading.
///
/// An empty ring buffer might cease to be empty at any time
/// if the corresponding [`Producer`] is producing items in another thread.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (p, c) = RingBuffer::<f32>::new(1);
///
/// assert!(c.is_empty());
/// ```
///
/// Since items can be concurrently produced on another thread, the ring buffer
/// might not be empty for long:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<f32>::new(1);
/// if c.is_empty() {
/// // The buffer might be empty, but it might as well not be
/// // if an item was just produced on another thread.
/// }
/// ```
///
/// However, if it's not empty, another thread cannot change that:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<f32>::new(1);
/// if !c.is_empty() {
/// // At least one slot is guaranteed to be available for reading.
/// }
/// ```
pub fn is_empty(&self) -> bool {
self.next_head().is_none()
}
/// Returns `true` if the corresponding [`Producer`] has been destroyed.
///
/// Note that since Rust version 1.74.0, this is not synchronizing with the producer thread
/// anymore, see <https://github.com/mgeier/rtrb/issues/114>.
/// In a future version of `rtrb`, the synchronizing behavior might be restored.
///
/// # Examples
///
/// ```
/// use rtrb::RingBuffer;
///
/// let (mut p, mut c) = RingBuffer::new(7);
/// assert!(!c.is_abandoned());
/// assert_eq!(p.push(10), Ok(()));
/// drop(p);
/// assert!(c.is_abandoned());
/// // The items that are left in the ring buffer can still be consumed:
/// assert_eq!(c.pop(), Ok(10));
/// ```
///
/// Since the producer can be concurrently dropped on another thread,
/// the consumer might become abandoned at any time:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<i32>::new(1);
/// if !c.is_abandoned() {
/// // Right now, the producer might still be alive, but it might as well not be
/// // if another thread has just dropped it.
/// }
/// ```
///
/// However, if it already is abandoned, it will stay that way:
///
/// ```
/// # use rtrb::RingBuffer;
/// # let (p, c) = RingBuffer::<i32>::new(1);
/// if c.is_abandoned() {
/// // This is needed since Rust 1.74.0, see https://github.com/mgeier/rtrb/issues/114:
/// std::sync::atomic::fence(std::sync::atomic::Ordering::Acquire);
/// // The producer does definitely not exist anymore.
/// }
/// ```
pub fn is_abandoned(&self) -> bool {
Arc::strong_count(&self.buffer) < 2
}
/// Returns a read-only reference to the ring buffer.
pub fn buffer(&self) -> &RingBuffer<T> {
&self.buffer
}
/// Get the head position for reading the next slot, if available.
///
/// This is a strict subset of the functionality implemented in `read_chunk()`.
/// For performance, this special case is immplemented separately.
fn next_head(&self) -> Option<usize> {
let head = self.cached_head.get();
// Check if the queue is *possibly* empty.
if head == self.cached_tail.get() {
// Refresh the tail ...
let tail = self.buffer.tail.load(Ordering::Acquire);
self.cached_tail.set(tail);
// ... and check if it's *really* empty.
if head == tail {
return None;
}
}
Some(head)
}
}
/// Extension trait used to provide a [`copy_to_uninit()`](CopyToUninit::copy_to_uninit)
/// method on built-in slices.
///
/// This can be used to safely copy data to the slices returned from
/// [`WriteChunkUninit::as_mut_slices()`].
///
/// To use this, the trait has to be brought into scope, e.g. with:
///
/// ```
/// use rtrb::CopyToUninit;
/// ```
pub trait CopyToUninit<T: Copy> {
/// Copies contents to a possibly uninitialized slice.
fn copy_to_uninit<'a>(&self, dst: &'a mut [MaybeUninit<T>]) -> &'a mut [T];
}
impl<T: Copy> CopyToUninit<T> for [T] {
/// Copies contents to a possibly uninitialized slice.
///
/// # Panics
///
/// This function will panic if the two slices have different lengths.
fn copy_to_uninit<'a>(&self, dst: &'a mut [MaybeUninit<T>]) -> &'a mut [T] {
assert_eq!(
self.len(),
dst.len(),
"source slice length does not match destination slice length"
);
let dst_ptr = dst.as_mut_ptr().cast();
// SAFETY: The lengths have been checked to be equal and
// the mutable reference makes sure that there is no overlap.
unsafe {
self.as_ptr().copy_to_nonoverlapping(dst_ptr, self.len());
core::slice::from_raw_parts_mut(dst_ptr, self.len())
}
}
}
/// Error type for [`Consumer::pop()`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PopError {
/// The queue was empty.
Empty,
}
#[cfg(feature = "std")]
impl std::error::Error for PopError {}
impl fmt::Display for PopError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PopError::Empty => "empty ring buffer".fmt(f),
}
}
}
/// Error type for [`Consumer::peek()`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PeekError {
/// The queue was empty.
Empty,
}
#[cfg(feature = "std")]
impl std::error::Error for PeekError {}
impl fmt::Display for PeekError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PeekError::Empty => "empty ring buffer".fmt(f),
}
}
}
/// Error type for [`Producer::push()`].
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum PushError<T> {
/// The queue was full.
Full(T),
}
#[cfg(feature = "std")]
impl<T> std::error::Error for PushError<T> {}
impl<T> fmt::Debug for PushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PushError::Full(_) => f.pad("Full(_)"),
}
}
}
impl<T> fmt::Display for PushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PushError::Full(_) => "full ring buffer".fmt(f),
}
}
}