diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/ringbuf/src/lib.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ringbuf/src/lib.rs')
-rw-r--r-- | third_party/rust/ringbuf/src/lib.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/third_party/rust/ringbuf/src/lib.rs b/third_party/rust/ringbuf/src/lib.rs new file mode 100644 index 0000000000..e37ca686f2 --- /dev/null +++ b/third_party/rust/ringbuf/src/lib.rs @@ -0,0 +1,67 @@ +//! Lock-free single-producer single-consumer (SPSC) FIFO ring buffer with direct access to inner data. +//! +//! # Overview +//! +//! `RingBuffer` is the initial structure representing ring buffer itself. +//! Ring buffer can be splitted into pair of `Producer` and `Consumer`. +//! +//! `Producer` and `Consumer` are used to append/remove elements to/from the ring buffer accordingly. They can be safely sent between threads. +//! Operations with `Producer` and `Consumer` are lock-free - they succeed or fail immediately without blocking or waiting. +//! +//! Elements can be effectively appended/removed one by one or many at once. +//! Also data could be loaded/stored directly into/from [`Read`]/[`Write`] instances. +//! And finally, there are `unsafe` methods allowing thread-safe direct access in place to the inner memory being appended/removed. +//! +//! [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +//! [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +//! +//! When building with nightly toolchain it is possible to run benchmarks via `cargo bench --features benchmark`. +//! +//! # Examples +//! +//! ## Simple example +//! +//! ```rust +//! # extern crate ringbuf; +//! use ringbuf::RingBuffer; +//! # fn main() { +//! let rb = RingBuffer::<i32>::new(2); +//! let (mut prod, mut cons) = rb.split(); +//! +//! prod.push(0).unwrap(); +//! prod.push(1).unwrap(); +//! assert_eq!(prod.push(2), Err(2)); +//! +//! assert_eq!(cons.pop().unwrap(), 0); +//! +//! prod.push(2).unwrap(); +//! +//! assert_eq!(cons.pop().unwrap(), 1); +//! assert_eq!(cons.pop().unwrap(), 2); +//! assert_eq!(cons.pop(), None); +//! # } +//! ``` + +#![no_std] +#![cfg_attr(feature = "benchmark", feature(test))] + +extern crate alloc; +#[cfg(feature = "std")] +extern crate std; + +#[cfg(feature = "benchmark")] +extern crate test; + +#[cfg(feature = "benchmark")] +mod benchmark; + +#[cfg(test)] +mod tests; + +mod consumer; +mod producer; +mod ring_buffer; + +pub use consumer::*; +pub use producer::*; +pub use ring_buffer::*; |