diff --git a/.travis.yml b/.travis.yml index eab5eaee8..130fe22e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,9 @@ matrix: - rust: 1.26.0 name: "crossbeam-epoch on 1.26.0" script: ./ci/crossbeam-epoch.sh + - rust: 1.26.0 + name: "crossbeam-queue on 1.26.0" + script: ./ci/crossbeam-queue.sh - rust: 1.28.0 name: "crossbeam-skiplist on 1.28.0" script: ./ci/crossbeam-skiplist.sh @@ -56,6 +59,9 @@ matrix: - llvm-3.8-dev - clang-3.8 - clang-3.8-dev + - rust: nightly + name: "crossbeam-queue on nightly" + script: ./ci/crossbeam-queue.sh - rust: nightly name: "crossbeam-skiplist on nightly" script: ./ci/crossbeam-skiplist.sh diff --git a/Cargo.toml b/Cargo.toml index 0b77b0414..7a909aa92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,10 @@ path = "./crossbeam-deque" version = "0.7.0" path = "./crossbeam-epoch" +[dependencies.crossbeam-queue] +version = "0.0.0" +path = "./crossbeam-queue" + [dependencies.crossbeam-utils] version = "0.6.3" path = "./crossbeam-utils" @@ -53,14 +57,7 @@ members = [ "crossbeam-channel/benchmarks", "crossbeam-deque", "crossbeam-epoch", + "crossbeam-queue", "crossbeam-skiplist", "crossbeam-utils", ] - -# [patch.crates-io] -# crossbeam = { path = "." } -# crossbeam-channel = { path = "./crossbeam-channel" } -# crossbeam-deque = { path = "./crossbeam-deque" } -# crossbeam-epoch = { path = "./crossbeam-epoch" } -# crossbeam-skiplist = { path = "./crossbeam-skiplist" } -# crossbeam-utils = { path = "./crossbeam-utils" } diff --git a/ci/crossbeam-queue.sh b/ci/crossbeam-queue.sh new file mode 100755 index 000000000..d11faeccf --- /dev/null +++ b/ci/crossbeam-queue.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +cd "$(dirname "$0")"/../crossbeam-queue +set -ex + +export RUSTFLAGS="-D warnings" + +cargo check --bins --examples --tests +cargo test diff --git a/crossbeam-channel/benchmarks/segqueue.rs b/crossbeam-channel/benchmarks/segqueue.rs index 6b35306ee..492e7b3d1 100644 --- a/crossbeam-channel/benchmarks/segqueue.rs +++ b/crossbeam-channel/benchmarks/segqueue.rs @@ -16,7 +16,7 @@ fn seq() { } for _ in 0..MESSAGES { - q.try_pop().unwrap(); + q.pop().unwrap(); } } @@ -32,7 +32,7 @@ fn spsc() { for _ in 0..MESSAGES { loop { - if q.try_pop().is_none() { + if q.pop().is_err() { thread::yield_now(); } else { break; @@ -56,7 +56,7 @@ fn mpsc() { for _ in 0..MESSAGES { loop { - if q.try_pop().is_none() { + if q.pop().is_err() { thread::yield_now(); } else { break; @@ -82,7 +82,7 @@ fn mpmc() { scope.spawn(|_| { for _ in 0..MESSAGES / THREADS { loop { - if q.try_pop().is_none() { + if q.pop().is_err() { thread::yield_now(); } else { break; diff --git a/crossbeam-channel/src/channel.rs b/crossbeam-channel/src/channel.rs index 7acada044..e71b22bcc 100644 --- a/crossbeam-channel/src/channel.rs +++ b/crossbeam-channel/src/channel.rs @@ -205,7 +205,7 @@ pub fn after(duration: Duration) -> Receiver { /// s.send(1).unwrap(); /// }); /// -/// // This duration can be a `Some` or a `None`. +/// // Suppose this duration can be a `Some` or a `None`. /// let duration = Some(Duration::from_millis(100)); /// /// // Create a channel that times out after the specified duration. diff --git a/crossbeam-queue/CHANGELOG.md b/crossbeam-queue/CHANGELOG.md new file mode 100644 index 000000000..e69de29bb diff --git a/crossbeam-queue/Cargo.toml b/crossbeam-queue/Cargo.toml new file mode 100644 index 000000000..794050dfd --- /dev/null +++ b/crossbeam-queue/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "crossbeam-queue" +# When publishing a new version: +# - Update CHANGELOG.md +# - Update README.md +# - Create "crossbeam-queue-X.Y.Z" git tag +version = "0.0.0" +authors = ["The Crossbeam Project Developers"] +license = "MIT/Apache-2.0" +readme = "README.md" +repository = "https://github.com/crossbeam-rs/crossbeam" +homepage = "https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-utils" +documentation = "https://docs.rs/crossbeam-queue" +description = "Concurrent queues" +keywords = ["queue", "mpmc", "lock-free", "producer", "consumer"] +categories = ["concurrency", "data-structures"] + +[dependencies.crossbeam-utils] +version = "0.6" +path = "../crossbeam-utils" + +[dev-dependencies] +rand = "0.6" diff --git a/crossbeam-queue/LICENSE-APACHE b/crossbeam-queue/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/crossbeam-queue/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/crossbeam-queue/LICENSE-MIT b/crossbeam-queue/LICENSE-MIT new file mode 100644 index 000000000..31aa79387 --- /dev/null +++ b/crossbeam-queue/LICENSE-MIT @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/crossbeam-queue/LICENSE-THIRD-PARTY b/crossbeam-queue/LICENSE-THIRD-PARTY new file mode 100644 index 000000000..8914949a8 --- /dev/null +++ b/crossbeam-queue/LICENSE-THIRD-PARTY @@ -0,0 +1,31 @@ +=============================================================================== + +Bounded MPMC queue +http://www.1024cores.net/home/code-license + +Copyright (c) 2010-2011 Dmitry Vyukov. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of Dmitry Vyukov. diff --git a/crossbeam-queue/README.md b/crossbeam-queue/README.md new file mode 100644 index 000000000..0a8c1427b --- /dev/null +++ b/crossbeam-queue/README.md @@ -0,0 +1,67 @@ +# Crossbeam Queue + +[![Build Status](https://travis-ci.org/crossbeam-rs/crossbeam.svg?branch=master)]( +https://travis-ci.org/crossbeam-rs/crossbeam) +[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)]( +https://github.com/crossbeam-rs/crossbeam-queue/tree/master/src) +[![Cargo](https://img.shields.io/crates/v/crossbeam-queue.svg)]( +https://crates.io/crates/crossbeam-queue) +[![Documentation](https://docs.rs/crossbeam-queue/badge.svg)]( +https://docs.rs/crossbeam-queue) +[![Rust 1.26+](https://img.shields.io/badge/rust-1.26+-lightgray.svg)]( +https://www.rust-lang.org) + +This crate provides concurrent queues that can be shared among threads: + +* [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction. +* [`SegQueue`], an unbounded MPMC queue that allocates segments on demand. Segments are small + buffers that can hold a handful of elements. + +[`ArrayQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.ArrayQueue.html +[`SegQueue`]: https://docs.rs/crossbeam-queue/*/crossbeam_queue/struct.SegQueue.html + +## Usage + +Add this to your `Cargo.toml`: + +```toml +[dependencies] +crossbeam-queue = "0.1" +``` + +Next, add this to your crate: + +```rust +extern crate crossbeam_queue; +``` + +## Compatibility + +The minimum supported Rust version is 1.26. + +## License + +Licensed under either of + + * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +#### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. + +#### Third party software + +This product includes copies and modifications of software developed by third parties: + +* [src/array_queue.rs](src/array_queue.rs) is based on + [Bounded MPMC queue](http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue) + by Dmitry Vyukov, licensed under the Simplified BSD License and the Apache License, Version 2.0. + +See the source code files for more details. + +Copies of third party licenses can be found in [LICENSE-THIRD-PARTY](LICENSE-THIRD-PARTY). diff --git a/src/queue/array_queue.rs b/crossbeam-queue/src/array_queue.rs similarity index 77% rename from src/queue/array_queue.rs rename to crossbeam-queue/src/array_queue.rs index f012b0fcc..7ce939c89 100644 --- a/src/queue/array_queue.rs +++ b/crossbeam-queue/src/array_queue.rs @@ -1,10 +1,7 @@ -//! Bounded multi-producer multi-consumer queue. -//! //! The implementation is based on Dmitry Vyukov's bounded MPMC queue. //! //! Source: //! - http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue -//! - https://docs.google.com/document/d/1yIAYmbvL3JxOKOjuCyon7JhW4cSv1wy5hC0ApeGMV9s/pub //! //! Copyright & License: //! - Copyright (c) 2010-2011 Dmitry Vyukov @@ -18,8 +15,9 @@ use std::mem; use std::ptr; use std::sync::atomic::{self, AtomicUsize, Ordering}; -use queue::{PopError, PushError}; -use utils::{Backoff, CachePadded}; +use crossbeam_utils::{Backoff, CachePadded}; + +use err::{PopError, PushError}; /// A slot in a queue. struct Slot { @@ -34,13 +32,33 @@ struct Slot { } /// A bounded multi-producer multi-consumer queue. +/// +/// This queue allocates a fixed-capacity buffer on construction, which is used to store pushed +/// elements. The queue cannot hold more elements that the buffer allows. Attempting to push an +/// element into a full queue will fail. Having a buffer allocated upfront makes this queue a bit +/// faster than [`SegQueue`]. +/// +/// [`SegQueue`]: struct.SegQueue.html +/// +/// # Examples +/// +/// ``` +/// use crossbeam_queue::{ArrayQueue, PushError}; +/// +/// let q = ArrayQueue::new(2); +/// +/// assert_eq!(q.push('a'), Ok(())); +/// assert_eq!(q.push('b'), Ok(())); +/// assert_eq!(q.push('c'), Err(PushError('c'))); +/// assert_eq!(q.pop(), Ok('a')); +/// ``` pub struct ArrayQueue { /// The head of the queue. /// /// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a /// single `usize`. The lower bits represent the index, while the upper bits represent the lap. /// - /// Values are popped from the head of the queue. + /// Elements are popped from the head of the queue. head: CachePadded, /// The tail of the queue. @@ -48,7 +66,7 @@ pub struct ArrayQueue { /// This value is a "stamp" consisting of an index into the buffer and a lap, but packed into a /// single `usize`. The lower bits represent the index, while the upper bits represent the lap. /// - /// Values are pushed into the tail of the queue. + /// Elements are pushed into the tail of the queue. tail: CachePadded, /// The buffer holding slots. @@ -60,7 +78,7 @@ pub struct ArrayQueue { /// A stamp with the value of `{ lap: 1, index: 0 }`. one_lap: usize, - /// Indicates that dropping an `ArrayQueue` may drop values of type `T`. + /// Indicates that dropping an `ArrayQueue` may drop elements of type `T`. _marker: PhantomData, } @@ -68,13 +86,21 @@ unsafe impl Sync for ArrayQueue {} unsafe impl Send for ArrayQueue {} impl ArrayQueue { - /// Creates a new queue of capacity `cap`. + /// Creates a new bounded queue with the given capacity. /// /// # Panics /// /// Panics if the capacity is zero. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::ArrayQueue; + /// + /// let q = ArrayQueue::::new(100); + /// ``` pub fn new(cap: usize) -> ArrayQueue { - assert!(cap > 0, "capacity must be positive"); + assert!(cap > 0, "capacity must be non-zero"); // Head is initialized to `{ lap: 0, index: 0 }`. // Tail is initialized to `{ lap: 0, index: 0 }`. @@ -111,7 +137,20 @@ impl ArrayQueue { } } - /// Attempts to push `value` into the queue. + /// Attempts to push an element into the queue. + /// + /// If the queue is full, the element is returned back as an error. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PushError}; + /// + /// let q = ArrayQueue::new(1); + /// + /// assert_eq!(q.push(10), Ok(())); + /// assert_eq!(q.push(20), Err(PushError(20))); + /// ``` pub fn push(&self, value: T) -> Result<(), PushError> { let backoff = Backoff::new(); let mut tail = self.tail.load(Ordering::Relaxed); @@ -173,7 +212,21 @@ impl ArrayQueue { } } - /// Attempts to pop a value from the queue. + /// Attempts to pop an element from the queue. + /// + /// If the queue is empty, an error is returned. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PopError}; + /// + /// let q = ArrayQueue::new(1); + /// assert_eq!(q.push(10), Ok(())); + /// + /// assert_eq!(q.pop(), Ok(10)); + /// assert_eq!(q.pop(), Err(PopError)); + /// ``` pub fn pop(&self) -> Result { let backoff = Backoff::new(); let mut head = self.head.load(Ordering::Relaxed); @@ -235,11 +288,33 @@ impl ArrayQueue { } /// Returns the capacity of the queue. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PopError}; + /// + /// let q = ArrayQueue::::new(100); + /// + /// assert_eq!(q.capacity(), 100); + /// ``` pub fn capacity(&self) -> usize { self.cap } /// Returns `true` if the queue is empty. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PopError}; + /// + /// let q = ArrayQueue::new(100); + /// + /// assert!(q.is_empty()); + /// q.push(1).unwrap(); + /// assert!(!q.is_empty()); + /// ``` pub fn is_empty(&self) -> bool { let head = self.head.load(Ordering::SeqCst); let tail = self.tail.load(Ordering::SeqCst); @@ -253,6 +328,18 @@ impl ArrayQueue { } /// Returns `true` if the queue is full. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PopError}; + /// + /// let q = ArrayQueue::new(1); + /// + /// assert!(!q.is_full()); + /// q.push(1).unwrap(); + /// assert!(q.is_full()); + /// ``` pub fn is_full(&self) -> bool { let tail = self.tail.load(Ordering::SeqCst); let head = self.head.load(Ordering::SeqCst); @@ -264,7 +351,22 @@ impl ArrayQueue { head.wrapping_add(self.one_lap) == tail } - /// Returns the current number of values in the queue. + /// Returns the number of elements in the queue. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{ArrayQueue, PopError}; + /// + /// let q = ArrayQueue::new(100); + /// assert_eq!(q.len(), 0); + /// + /// q.push(10).unwrap(); + /// assert_eq!(q.len(), 1); + /// + /// q.push(20).unwrap(); + /// assert_eq!(q.len(), 2); + /// ``` pub fn len(&self) -> usize { loop { // Load the tail, then load the head. diff --git a/src/queue/mod.rs b/crossbeam-queue/src/err.rs similarity index 90% rename from src/queue/mod.rs rename to crossbeam-queue/src/err.rs index dd9465b48..fcc84bb28 100644 --- a/src/queue/mod.rs +++ b/crossbeam-queue/src/err.rs @@ -1,12 +1,6 @@ use std::error; use std::fmt; -mod array_queue; -mod seg_queue; - -pub use self::array_queue::ArrayQueue; -pub use self::seg_queue::SegQueue; - /// Error which occurs when popping from an empty queue. #[derive(Clone, Copy, Eq, PartialEq)] pub struct PopError; diff --git a/crossbeam-queue/src/lib.rs b/crossbeam-queue/src/lib.rs new file mode 100644 index 000000000..8e5e82b37 --- /dev/null +++ b/crossbeam-queue/src/lib.rs @@ -0,0 +1,23 @@ +//! Concurrent queues. +//! +//! This crate provides concurrent queues that can be shared among threads: +//! +//! * [`ArrayQueue`], a bounded MPMC queue that allocates a fixed-capacity buffer on construction. +//! * [`SegQueue`], an unbounded MPMC queue that allocates segments on demand. Segments are small +//! buffers that can hold a handful of elements. +//! +//! [`ArrayQueue`]: struct.ArrayQueue.html +//! [`SegQueue`]: struct.SegQueue.html + +#![warn(missing_docs)] +#![warn(missing_debug_implementations)] + +extern crate crossbeam_utils; + +mod array_queue; +mod err; +mod seg_queue; + +pub use self::array_queue::ArrayQueue; +pub use self::seg_queue::SegQueue; +pub use self::err::{PopError, PushError}; diff --git a/src/queue/seg_queue.rs b/crossbeam-queue/src/seg_queue.rs similarity index 85% rename from src/queue/seg_queue.rs rename to crossbeam-queue/src/seg_queue.rs index ce5faa829..d9783edaf 100644 --- a/src/queue/seg_queue.rs +++ b/crossbeam-queue/src/seg_queue.rs @@ -5,7 +5,9 @@ use std::mem::{self, ManuallyDrop}; use std::ptr; use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering}; -use utils::{Backoff, CachePadded}; +use crossbeam_utils::{Backoff, CachePadded}; + +use err::PopError; // Bits indicating the state of a slot: // * If a value has been written into the slot, `WRITE` is set. @@ -102,9 +104,29 @@ struct Position { block: AtomicPtr>, } -/// Unbounded queue that allocates segments of values. +/// An unbounded multi-producer multi-consumer queue. +/// +/// This queue is implemented as a linked list of segments, where each segment is a small buffer +/// that can hold a handful of elements. There is no limit to how many elements can be in the queue +/// at a time. However, since segments need to be dynamically allocated as elements get pushed, +/// this queue is somewhat slower than [`ArrayQueue`]. +/// +/// [`ArrayQueue`]: struct.ArrayQueue.html +/// +/// # Examples +/// +/// ``` +/// use crossbeam_queue::{PopError, SegQueue}; +/// +/// let q = SegQueue::new(); +/// +/// q.push('a'); +/// q.push('b'); /// -/// This queue can be shared among threads and used with any number of producers or consumers. +/// assert_eq!(q.pop(), Ok('a')); +/// assert_eq!(q.pop(), Ok('b')); +/// assert_eq!(q.pop(), Err(PopError)); +/// ``` pub struct SegQueue { /// The head of the queue. head: CachePadded>, @@ -121,6 +143,14 @@ unsafe impl Sync for SegQueue {} impl SegQueue { /// Creates a new unbounded queue. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::SegQueue; + /// + /// let q = SegQueue::::new(); + /// ``` pub fn new() -> SegQueue { SegQueue { head: CachePadded::new(Position { @@ -135,7 +165,18 @@ impl SegQueue { } } - /// Pushes a value into the queue. + /// Pushes an element into the queue. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::SegQueue; + /// + /// let q = SegQueue::new(); + /// + /// q.push(10); + /// q.push(20); + /// ``` pub fn push(&self, value: T) { let backoff = Backoff::new(); let mut tail = self.tail.index.load(Ordering::Acquire); @@ -213,8 +254,22 @@ impl SegQueue { } } - /// Pops a value from the queue. - pub fn try_pop(&self) -> Option { + /// Pops an element from the queue. + /// + /// If the queue is empty, an error is returned. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{PopError, SegQueue}; + /// + /// let q = SegQueue::new(); + /// + /// q.push(10); + /// assert_eq!(q.pop(), Ok(10)); + /// assert_eq!(q.pop(), Err(PopError)); + /// ``` + pub fn pop(&self) -> Result { let backoff = Backoff::new(); let mut head = self.head.index.load(Ordering::Acquire); let mut block = self.head.block.load(Ordering::Acquire); @@ -239,7 +294,7 @@ impl SegQueue { // If the tail equals the head, that means the queue is empty. if head >> SHIFT == tail >> SHIFT { - return None; + return Err(PopError); } // If head and tail are not in the same block, set `HAS_NEXT` in head. @@ -293,7 +348,7 @@ impl SegQueue { Block::destroy(block, offset + 1); } - return Some(value); + return Ok(value); } Err(h) => { head = h; @@ -305,13 +360,40 @@ impl SegQueue { } /// Returns `true` if the queue is empty. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::SegQueue; + /// + /// let q = SegQueue::new(); + /// + /// assert!(q.is_empty()); + /// q.push(1); + /// assert!(!q.is_empty()); + /// ``` pub fn is_empty(&self) -> bool { let head = self.head.index.load(Ordering::SeqCst); let tail = self.tail.index.load(Ordering::SeqCst); head >> SHIFT == tail >> SHIFT } - /// Returns the current number of values in the queue. + /// Returns the number of elements in the queue. + /// + /// # Examples + /// + /// ``` + /// use crossbeam_queue::{SegQueue, PopError}; + /// + /// let q = SegQueue::new(); + /// assert_eq!(q.len(), 0); + /// + /// q.push(10); + /// assert_eq!(q.len(), 1); + /// + /// q.push(20); + /// assert_eq!(q.len(), 2); + /// ``` pub fn len(&self) -> usize { loop { // Load the tail index, then load the head index. diff --git a/tests/array_queue.rs b/crossbeam-queue/tests/array_queue.rs similarity index 95% rename from tests/array_queue.rs rename to crossbeam-queue/tests/array_queue.rs index 6e006c02b..6f09d3c1b 100644 --- a/tests/array_queue.rs +++ b/crossbeam-queue/tests/array_queue.rs @@ -1,10 +1,11 @@ -extern crate crossbeam; +extern crate crossbeam_queue; +extern crate crossbeam_utils; extern crate rand; use std::sync::atomic::{AtomicUsize, Ordering}; -use crossbeam::queue::ArrayQueue; -use crossbeam::scope; +use crossbeam_queue::ArrayQueue; +use crossbeam_utils::thread::scope; use rand::{thread_rng, Rng}; #[test] @@ -27,6 +28,12 @@ fn capacity() { } } +#[test] +#[should_panic(expected = "capacity must be non-zero")] +fn zero_capacity() { + let _ = ArrayQueue::::new(0); +} + #[test] fn len_empty_full() { let q = ArrayQueue::new(2); diff --git a/crossbeam-queue/tests/seg_queue.rs b/crossbeam-queue/tests/seg_queue.rs new file mode 100644 index 000000000..871f542c3 --- /dev/null +++ b/crossbeam-queue/tests/seg_queue.rs @@ -0,0 +1,164 @@ +extern crate crossbeam_queue; +extern crate crossbeam_utils; +extern crate rand; + +use std::sync::atomic::{AtomicUsize, Ordering}; + +use crossbeam_queue::SegQueue; +use crossbeam_utils::thread::scope; +use rand::{thread_rng, Rng}; + +#[test] +fn smoke() { + let q = SegQueue::new(); + q.push(7); + assert_eq!(q.pop(), Ok(7)); + + q.push(8); + assert_eq!(q.pop(), Ok(8)); + assert!(q.pop().is_err()); +} + +#[test] +fn len_empty_full() { + let q = SegQueue::new(); + + assert_eq!(q.len(), 0); + assert_eq!(q.is_empty(), true); + + q.push(()); + + assert_eq!(q.len(), 1); + assert_eq!(q.is_empty(), false); + + q.pop().unwrap(); + + assert_eq!(q.len(), 0); + assert_eq!(q.is_empty(), true); +} + +#[test] +fn len() { + let q = SegQueue::new(); + + assert_eq!(q.len(), 0); + + for i in 0..50 { + q.push(i); + assert_eq!(q.len(), i + 1); + } + + for i in 0..50 { + q.pop().unwrap(); + assert_eq!(q.len(), 50 - i - 1); + } + + assert_eq!(q.len(), 0); +} + +#[test] +fn spsc() { + const COUNT: usize = 100_000; + + let q = SegQueue::new(); + + scope(|scope| { + scope.spawn(|_| { + for i in 0..COUNT { + loop { + if let Ok(x) = q.pop() { + assert_eq!(x, i); + break; + } + } + } + assert!(q.pop().is_err()); + }); + scope.spawn(|_| { + for i in 0..COUNT { + q.push(i); + } + }); + }).unwrap(); +} + +#[test] +fn mpmc() { + const COUNT: usize = 25_000; + const THREADS: usize = 4; + + let q = SegQueue::::new(); + let v = (0..COUNT).map(|_| AtomicUsize::new(0)).collect::>(); + + scope(|scope| { + for _ in 0..THREADS { + scope.spawn(|_| { + for _ in 0..COUNT { + let n = loop { + if let Ok(x) = q.pop() { + break x; + } + }; + v[n].fetch_add(1, Ordering::SeqCst); + } + }); + } + for _ in 0..THREADS { + scope.spawn(|_| { + for i in 0..COUNT { + q.push(i); + } + }); + } + }).unwrap(); + + for c in v { + assert_eq!(c.load(Ordering::SeqCst), THREADS); + } +} + +#[test] +fn drops() { + static DROPS: AtomicUsize = AtomicUsize::new(0); + + #[derive(Debug, PartialEq)] + struct DropCounter; + + impl Drop for DropCounter { + fn drop(&mut self) { + DROPS.fetch_add(1, Ordering::SeqCst); + } + } + + let mut rng = thread_rng(); + + for _ in 0..100 { + let steps = rng.gen_range(0, 10_000); + let additional = rng.gen_range(0, 1000); + + DROPS.store(0, Ordering::SeqCst); + let q = SegQueue::new(); + + scope(|scope| { + scope.spawn(|_| { + for _ in 0..steps { + while q.pop().is_err() {} + } + }); + + scope.spawn(|_| { + for _ in 0..steps { + q.push(DropCounter); + } + }); + }).unwrap(); + + for _ in 0..additional { + q.push(DropCounter); + } + + assert_eq!(DROPS.load(Ordering::SeqCst), steps); + drop(q); + assert_eq!(DROPS.load(Ordering::SeqCst), steps + additional); + } +} diff --git a/src/lib.rs b/src/lib.rs index 343455ae0..9f4f54905 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,8 +99,11 @@ cfg_if! { #[doc(hidden)] pub use _channel::*; - /// Concurrent queues. - pub mod queue; + mod _queue { + pub extern crate crossbeam_queue; + } + #[doc(inline)] + pub use _queue::crossbeam_queue as queue; pub use crossbeam_utils::sync; pub use crossbeam_utils::thread; diff --git a/tests/seg_queue.rs b/tests/seg_queue.rs deleted file mode 100644 index c6030c9ee..000000000 --- a/tests/seg_queue.rs +++ /dev/null @@ -1,140 +0,0 @@ -extern crate crossbeam; - -use crossbeam::queue::SegQueue; -use crossbeam::scope; - -const CONC_COUNT: i64 = 1000000; - -#[test] -fn push_pop_1() { - let q: SegQueue = SegQueue::new(); - q.push(37); - assert_eq!(q.try_pop(), Some(37)); -} - -#[test] -fn push_pop_2() { - let q: SegQueue = SegQueue::new(); - q.push(37); - q.push(48); - assert_eq!(q.try_pop(), Some(37)); - assert_eq!(q.try_pop(), Some(48)); -} - -#[test] -fn push_pop_empty_check() { - let q: SegQueue = SegQueue::new(); - assert_eq!(q.is_empty(), true); - q.push(42); - assert_eq!(q.is_empty(), false); - assert_eq!(q.try_pop(), Some(42)); - assert_eq!(q.is_empty(), true); -} - -#[test] -fn push_pop_many_seq() { - let q: SegQueue = SegQueue::new(); - for i in 0..200 { - q.push(i) - } - for i in 0..200 { - assert_eq!(q.try_pop(), Some(i)); - } -} - -#[test] -fn push_pop_many_spsc() { - let q: SegQueue = SegQueue::new(); - - scope(|scope| { - scope.spawn(|_| { - let mut next = 0; - - while next < CONC_COUNT { - if let Some(elem) = q.try_pop() { - assert_eq!(elem, next); - next += 1; - } - } - }); - - for i in 0..CONC_COUNT { - q.push(i) - } - }).unwrap(); -} - -#[test] -fn push_pop_many_spmc() { - fn recv(_t: i32, q: &SegQueue) { - let mut cur = -1; - for _i in 0..CONC_COUNT { - if let Some(elem) = q.try_pop() { - assert!(elem > cur); - cur = elem; - - if cur == CONC_COUNT - 1 { - break; - } - } - } - } - - let q: SegQueue = SegQueue::new(); - scope(|scope| { - for i in 0..3 { - let q = &q; - scope.spawn(move |_| recv(i, q)); - } - - scope.spawn(|_| { - for i in 0..CONC_COUNT { - q.push(i); - } - }); - }).unwrap(); -} - -#[test] -fn push_pop_many_mpmc() { - enum LR { - Left(i64), - Right(i64), - } - - let q: SegQueue = SegQueue::new(); - - scope(|scope| { - for _t in 0..2 { - scope.spawn(|_| { - for i in CONC_COUNT - 1..CONC_COUNT { - q.push(LR::Left(i)) - } - }); - scope.spawn(|_| { - for i in CONC_COUNT - 1..CONC_COUNT { - q.push(LR::Right(i)) - } - }); - scope.spawn(|_| { - let mut vl = vec![]; - let mut vr = vec![]; - for _i in 0..CONC_COUNT { - match q.try_pop() { - Some(LR::Left(x)) => vl.push(x), - Some(LR::Right(x)) => vr.push(x), - _ => {} - } - } - - let mut vl2 = vl.clone(); - let mut vr2 = vr.clone(); - vl2.sort(); - vr2.sort(); - - assert_eq!(vl, vl2); - assert_eq!(vr, vr2); - }); - } - }).unwrap(); -} diff --git a/tests/subcrates.rs b/tests/subcrates.rs index 837583922..545129af6 100644 --- a/tests/subcrates.rs +++ b/tests/subcrates.rs @@ -25,6 +25,13 @@ fn epoch() { crossbeam::epoch::pin(); } +#[test] +fn queue() { + let a = crossbeam::queue::ArrayQueue::new(10); + let _ = a.push(1); + let _ = a.pop(); +} + #[test] fn utils() { crossbeam::utils::CachePadded::new(7);