From e92831f53b6f3da119fa17b676e0b0ca8bc3bcee Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Sun, 25 Oct 2020 19:37:11 +0100 Subject: [PATCH] Rename crate, add error module, add Cargo.toml --- .gitignore | 2 ++ Cargo.toml | 20 +++++++++++++++++ src/error.rs | 11 +++++++++ src/{spsc.rs => lib.rs} | 50 ++++++++++++++++++----------------------- tests/spsc.rs | 15 +++++-------- 5 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/error.rs rename src/{spsc.rs => lib.rs} (88%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..80faede --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +/target/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8973cb1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "rtrb" +version = "0.0.0" +authors = [ + "Stjepan Glavina ", + "Matthias Geier ", +] +repository = "https://github.com/mgeier/rtrb" +description = "A realtime-safe single-producer single-consumer ring buffer" +categories = ["concurrency", "data-structures"] +keywords = ["lock-free", "wait-free", "spsc", "queue"] +license = "MIT OR Apache-2.0" +edition = "2018" + +[dependencies] +cache-padded = "1.1" + +[dev-dependencies] +crossbeam-utils = "0.8" +rand = "0.7.3" diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..0ee24d8 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,11 @@ +// TODO: Display impls + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum PopError { + Empty, +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum PushError { + Full(T), +} diff --git a/src/spsc.rs b/src/lib.rs similarity index 88% rename from src/spsc.rs rename to src/lib.rs index f915fbe..31cb532 100644 --- a/src/spsc.rs +++ b/src/lib.rs @@ -3,9 +3,7 @@ //! # Examples //! //! ``` -//! use crossbeam_queue::spsc; -//! -//! let (p, c) = spsc::new(2); +//! let (p, c) = rtrb::new(2); //! //! assert!(p.push(1).is_ok()); //! assert!(p.push(2).is_ok()); @@ -23,9 +21,11 @@ use std::mem; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; -use crossbeam_utils::CachePadded; +use cache_padded::CachePadded; + +mod error; -use err::{PopError, PushError}; +pub use error::{PopError, PushError}; /// The inner representation of a single-producer single-consumer queue. struct Inner { @@ -118,9 +118,7 @@ impl Drop for Inner { /// # Examples /// /// ``` -/// use crossbeam_queue::spsc; -/// -/// let (p, c) = spsc::new::(100); +/// let (p, c) = rtrb::new::(100); /// ``` pub fn new(cap: usize) -> (Producer, Consumer) { assert!(cap > 0, "capacity must be non-zero"); @@ -161,12 +159,12 @@ pub fn new(cap: usize) -> (Producer, Consumer) { /// # Examples /// /// ``` -/// use crossbeam_queue::{spsc, PushError}; +/// use rtrb::PushError; /// -/// let (p, c) = spsc::new::(1); +/// let (p, c) = rtrb::new::(1); /// /// assert_eq!(p.push(10), Ok(())); -/// assert_eq!(p.push(20), Err(PushError(20))); +/// assert_eq!(p.push(20), Err(PushError::Full(20))); /// ``` pub struct Producer { /// The inner representation of the queue. @@ -193,12 +191,12 @@ impl Producer { /// # Examples /// /// ``` - /// use crossbeam_queue::{spsc, PushError}; + /// use rtrb::PushError; /// - /// let (p, c) = spsc::new(1); + /// let (p, c) = rtrb::new(1); /// /// assert_eq!(p.push(10), Ok(())); - /// assert_eq!(p.push(20), Err(PushError(20))); + /// assert_eq!(p.push(20), Err(PushError::Full(20))); /// ``` pub fn push(&self, value: T) -> Result<(), PushError> { let mut head = self.head.get(); @@ -212,7 +210,7 @@ impl Producer { // Is the queue *really* full? if self.inner.distance(head, tail) == self.inner.cap { - return Err(PushError(value)); + return Err(PushError::Full(value)); } } @@ -234,9 +232,7 @@ impl Producer { /// # Examples /// /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new::(100); + /// let (p, c) = rtrb::new::(100); /// /// assert_eq!(p.capacity(), 100); /// ``` @@ -256,13 +252,13 @@ impl fmt::Debug for Producer { /// # Examples /// /// ``` -/// use crossbeam_queue::{spsc, PopError}; +/// use rtrb::PopError; /// -/// let (p, c) = spsc::new(1); +/// let (p, c) = rtrb::new(1); /// assert_eq!(p.push(10), Ok(())); /// /// assert_eq!(c.pop(), Ok(10)); -/// assert_eq!(c.pop(), Err(PopError)); +/// assert_eq!(c.pop(), Err(PopError::Empty)); /// ``` pub struct Consumer { /// The inner representation of the queue. @@ -289,13 +285,13 @@ impl Consumer { /// # Examples /// /// ``` - /// use crossbeam_queue::{spsc, PopError}; + /// use rtrb::PopError; /// - /// let (p, c) = spsc::new(1); + /// let (p, c) = rtrb::new(1); /// assert_eq!(p.push(10), Ok(())); /// /// assert_eq!(c.pop(), Ok(10)); - /// assert_eq!(c.pop(), Err(PopError)); + /// assert_eq!(c.pop(), Err(PopError::Empty)); /// ``` pub fn pop(&self) -> Result { let mut head = self.head.get(); @@ -309,7 +305,7 @@ impl Consumer { // Is the queue *really* empty? if head == tail { - return Err(PopError); + return Err(PopError::Empty); } } @@ -329,9 +325,7 @@ impl Consumer { /// # Examples /// /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new::(100); + /// let (p, c) = rtrb::new::(100); /// /// assert_eq!(c.capacity(), 100); /// ``` diff --git a/tests/spsc.rs b/tests/spsc.rs index 727f51e..de283e2 100644 --- a/tests/spsc.rs +++ b/tests/spsc.rs @@ -1,16 +1,11 @@ -extern crate crossbeam_queue; -extern crate crossbeam_utils; -extern crate rand; - use std::sync::atomic::{AtomicUsize, Ordering}; -use crossbeam_queue::spsc; use crossbeam_utils::thread::scope; use rand::{thread_rng, Rng}; #[test] fn smoke() { - let (p, c) = spsc::new(1); + let (p, c) = rtrb::new(1); p.push(7).unwrap(); assert_eq!(c.pop(), Ok(7)); @@ -23,7 +18,7 @@ fn smoke() { #[test] fn capacity() { for i in 1..10 { - let (p, c) = spsc::new::(i); + let (p, c) = rtrb::new::(i); assert_eq!(p.capacity(), i); assert_eq!(c.capacity(), i); } @@ -32,14 +27,14 @@ fn capacity() { #[test] #[should_panic(expected = "capacity must be non-zero")] fn zero_capacity() { - let _ = spsc::new::(0); + let _ = rtrb::new::(0); } #[test] fn parallel() { const COUNT: usize = 100_000; - let (p, c) = spsc::new(3); + let (p, c) = rtrb::new(3); scope(|s| { s.spawn(move |_| { @@ -85,7 +80,7 @@ fn drops() { let additional = rng.gen_range(0, 50); DROPS.store(0, Ordering::SeqCst); - let (p, c) = spsc::new(50); + let (p, c) = rtrb::new(50); let p = scope(|s| { s.spawn(move |_| {