From 124458f4a6c519809b8b0455a5294000e7f68d9f Mon Sep 17 00:00:00 2001 From: james7132 Date: Fri, 12 Apr 2024 23:52:48 -0700 Subject: [PATCH 1/7] Make unbounded a const function --- src/lib.rs | 2 +- src/unbounded.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8b495ce..8e5c832 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,7 +140,7 @@ impl ConcurrentQueue { /// /// let q = ConcurrentQueue::::unbounded(); /// ``` - pub fn unbounded() -> ConcurrentQueue { + pub const fn unbounded() -> ConcurrentQueue { ConcurrentQueue(Inner::Unbounded(Unbounded::new())) } diff --git a/src/unbounded.rs b/src/unbounded.rs index a84f856..90f6667 100644 --- a/src/unbounded.rs +++ b/src/unbounded.rs @@ -149,7 +149,7 @@ pub struct Unbounded { impl Unbounded { /// Creates a new unbounded queue. - pub fn new() -> Unbounded { + pub const fn new() -> Unbounded { Unbounded { head: CachePadded::new(Position { block: AtomicPtr::new(ptr::null_mut()), From 45a9e02da39917f2bf9a7cbb9d710720a7c34dca Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 13 Apr 2024 00:00:36 -0700 Subject: [PATCH 2/7] Fix loom tests --- src/lib.rs | 7 +++++++ src/unbounded.rs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8e5c832..6724d5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,6 +140,13 @@ impl ConcurrentQueue { /// /// let q = ConcurrentQueue::::unbounded(); /// ``` + #[cfg(not(loom))] + pub const fn unbounded() -> ConcurrentQueue { + ConcurrentQueue(Inner::Unbounded(Unbounded::new())) + } + + // Loom's primitives are not const constructible. + #[cfg(loom)] pub const fn unbounded() -> ConcurrentQueue { ConcurrentQueue(Inner::Unbounded(Unbounded::new())) } diff --git a/src/unbounded.rs b/src/unbounded.rs index 90f6667..19b2a00 100644 --- a/src/unbounded.rs +++ b/src/unbounded.rs @@ -149,6 +149,7 @@ pub struct Unbounded { impl Unbounded { /// Creates a new unbounded queue. + #[cfg(not(loom))] pub const fn new() -> Unbounded { Unbounded { head: CachePadded::new(Position { @@ -162,6 +163,21 @@ impl Unbounded { } } + // Loom's AtomicPtrs are not const constructible. + #[cfg(loom)] + pub fn new() -> Unbounded { + Unbounded { + head: CachePadded::new(Position { + block: AtomicPtr::new(ptr::null_mut()), + index: AtomicUsize::new(0), + }), + tail: CachePadded::new(Position { + block: AtomicPtr::new(ptr::null_mut()), + index: AtomicUsize::new(0), + }), + } + } + /// Pushes an item into the queue. pub fn push(&self, value: T) -> Result<(), PushError> { let mut tail = self.tail.index.load(Ordering::Acquire); From c766b46d0c4aa078bc5bca1c9ba1050c8706af34 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 13 Apr 2024 00:05:27 -0700 Subject: [PATCH 3/7] Whoops --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6724d5e..c48df32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,7 @@ impl ConcurrentQueue { // Loom's primitives are not const constructible. #[cfg(loom)] - pub const fn unbounded() -> ConcurrentQueue { + pub fn unbounded() -> ConcurrentQueue { ConcurrentQueue(Inner::Unbounded(Unbounded::new())) } From d599dd6ee39049b009a30c78eca9a5272cddb56a Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 13 Apr 2024 00:07:22 -0700 Subject: [PATCH 4/7] Allow missing docs on the loom variants --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index c48df32..a108c8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,6 +147,7 @@ impl ConcurrentQueue { // Loom's primitives are not const constructible. #[cfg(loom)] + #[allow(missing_docs)] pub fn unbounded() -> ConcurrentQueue { ConcurrentQueue(Inner::Unbounded(Unbounded::new())) } From e8c834260e5c86e5f8d59e7dd7b3169730253fe5 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 13 Apr 2024 04:05:11 -0700 Subject: [PATCH 5/7] Use const_fn! macro --- src/lib.rs | 46 +++++++++++++++++++++++++++++++++------------- src/unbounded.rs | 44 ++++++++++++++++---------------------------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a108c8e..4bfc922 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,24 @@ mod unbounded; mod sync; +/// Make the given function const if the given condition is true. +macro_rules! const_fn { + ( + const_if: #[cfg($($cfg:tt)+)]; + $(#[$($attr:tt)*])* + $vis:vis const fn $($rest:tt)* + ) => { + #[cfg($($cfg)+)] + $(#[$($attr)*])* + $vis const fn $($rest)* + #[cfg(not($($cfg)+))] + $(#[$($attr)*])* + $vis fn $($rest)* + }; +} + +pub(crate) use const_fn; + /// A concurrent queue. /// /// # Examples @@ -131,19 +149,21 @@ impl ConcurrentQueue { } } - /// Creates a new unbounded queue. - /// - /// # Examples - /// - /// ``` - /// use concurrent_queue::ConcurrentQueue; - /// - /// let q = ConcurrentQueue::::unbounded(); - /// ``` - #[cfg(not(loom))] - pub const fn unbounded() -> ConcurrentQueue { - ConcurrentQueue(Inner::Unbounded(Unbounded::new())) - } + const_fn!( + const_if: #[cfg(loom)]; + /// Creates a new unbounded queue. + /// + /// # Examples + /// + /// ``` + /// use concurrent_queue::ConcurrentQueue; + /// + /// let q = ConcurrentQueue::::unbounded(); + /// ``` + pub const fn unbounded() -> ConcurrentQueue { + ConcurrentQueue(Inner::Unbounded(Unbounded::new())) + } + ); // Loom's primitives are not const constructible. #[cfg(loom)] diff --git a/src/unbounded.rs b/src/unbounded.rs index 19b2a00..8e1c40d 100644 --- a/src/unbounded.rs +++ b/src/unbounded.rs @@ -4,6 +4,7 @@ use core::ptr; use crossbeam_utils::CachePadded; +use crate::const_fn; use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; use crate::sync::cell::UnsafeCell; #[allow(unused_imports)] @@ -148,35 +149,22 @@ pub struct Unbounded { } impl Unbounded { - /// Creates a new unbounded queue. - #[cfg(not(loom))] - pub const fn new() -> Unbounded { - Unbounded { - head: CachePadded::new(Position { - block: AtomicPtr::new(ptr::null_mut()), - index: AtomicUsize::new(0), - }), - tail: CachePadded::new(Position { - block: AtomicPtr::new(ptr::null_mut()), - index: AtomicUsize::new(0), - }), - } - } - - // Loom's AtomicPtrs are not const constructible. - #[cfg(loom)] - pub fn new() -> Unbounded { - Unbounded { - head: CachePadded::new(Position { - block: AtomicPtr::new(ptr::null_mut()), - index: AtomicUsize::new(0), - }), - tail: CachePadded::new(Position { - block: AtomicPtr::new(ptr::null_mut()), - index: AtomicUsize::new(0), - }), + const_fn!( + const_if: #[cfg(not(loom))]; + /// Creates a new unbounded queue. + pub const fn new() -> Unbounded { + Unbounded { + head: CachePadded::new(Position { + block: AtomicPtr::new(ptr::null_mut()), + index: AtomicUsize::new(0), + }), + tail: CachePadded::new(Position { + block: AtomicPtr::new(ptr::null_mut()), + index: AtomicUsize::new(0), + }), + } } - } + ); /// Pushes an item into the queue. pub fn push(&self, value: T) -> Result<(), PushError> { From 579b3c5f37f1090fab8a8ebf539e3d6d86ea5fc1 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 13 Apr 2024 04:06:19 -0700 Subject: [PATCH 6/7] Remove extra definition --- src/lib.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4bfc922..bc31cd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,13 +165,6 @@ impl ConcurrentQueue { } ); - // Loom's primitives are not const constructible. - #[cfg(loom)] - #[allow(missing_docs)] - pub fn unbounded() -> ConcurrentQueue { - ConcurrentQueue(Inner::Unbounded(Unbounded::new())) - } - /// Attempts to push an item into the queue. /// /// If the queue is full or closed, the item is returned back as an error. From 64d0c729738d6d5ffd297eee49181094871785ba Mon Sep 17 00:00:00 2001 From: James Liu Date: Sat, 13 Apr 2024 04:26:42 -0700 Subject: [PATCH 7/7] Whoops Co-authored-by: Taiki Endo --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index bc31cd6..a4d26b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,7 +150,7 @@ impl ConcurrentQueue { } const_fn!( - const_if: #[cfg(loom)]; + const_if: #[cfg(not(loom))]; /// Creates a new unbounded queue. /// /// # Examples