├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── README.tpl ├── perf.png └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | dist: trusty 3 | sudo: false 4 | rust: 5 | - nightly 6 | - beta 7 | - stable 8 | cache: cargo 9 | matrix: 10 | allow_failures: 11 | - rust: nightly 12 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hurdles" 3 | version = "1.0.1" 4 | 5 | description = "Counter-based thread barrier" 6 | readme = "README.md" 7 | 8 | authors = ["Jon Gjengset "] 9 | 10 | documentation = "https://docs.rs/hurdles" 11 | homepage = "https://github.com/jonhoo/hurdles" 12 | repository = "https://github.com/jonhoo/hurdles.git" 13 | 14 | keywords = ["synchronization", "barrier", "multithreading"] 15 | categories = ["concurrency", "memory-management"] 16 | 17 | license = "MIT/Apache-2.0" 18 | 19 | [badges] 20 | travis-ci = { repository = "jonhoo/hurdles" } 21 | maintenance = { status = "passively-maintained" } 22 | 23 | [dependencies] 24 | parking_lot_core = "0.4" 25 | 26 | [features] 27 | nightly = [] 28 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jon Gjengset 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hurdles 2 | 3 | [![Crates.io](https://img.shields.io/crates/v/hurdles.svg)](https://crates.io/crates/hurdles) 4 | [![Documentation](https://docs.rs/hurdles/badge.svg)](https://docs.rs/hurdles/) 5 | [![Build Status](https://travis-ci.org/jonhoo/arccstr.svg?branch=master)](https://travis-ci.org/jonhoo/arccstr) 6 | 7 | A scalable barrier (like [`std::sync::Barrier`]) that enables multiple threads to synchronize 8 | the beginning of some computation. 9 | 10 | This crate provides a similar interface as [`std::sync::Barrier`], but behaves much better in 11 | the face of many concurrently waiting threads, and incurs a lower per-thread latency penalty 12 | (see benchmarks below). The interface does differ from the standard library barrier however: 13 | 14 | - `Barrier` in this crate is `Clone`, and should *not* be wrapped in a `sync::Arc`. 15 | - `Barrier::wait` in this crate takes a `&mut self` receiver as each thread must keep some 16 | local state. 17 | 18 | Furthermore, when a thread blocks on `Barrier::wait`, the thread will (currently) *never* be 19 | suspended, and instead spin on the barrier. For the first few spins, it will also not call 20 | `sched_yield` to avoid the cost of thread sleep/wakeup. If threads are expected to reach the 21 | barrier at nearly the same time, or barrier latency is critical, this is probably what you 22 | want. However, if barriers are staggered and far between, then you may want to use 23 | [`std::sync::Barrier`] instead, as it is better about handling long waits. 24 | 25 | ## Examples 26 | 27 | ```rust 28 | use hurdles::Barrier; 29 | use std::thread; 30 | 31 | let mut handles = Vec::with_capacity(10); 32 | let mut barrier = Barrier::new(10); 33 | for _ in 0..10 { 34 | let mut c = barrier.clone(); 35 | // The same messages will be printed together. 36 | // You will NOT see any interleaving. 37 | handles.push(thread::spawn(move|| { 38 | println!("before wait"); 39 | c.wait(); 40 | println!("after wait"); 41 | })); 42 | } 43 | // Wait for other threads to finish. 44 | for handle in handles { 45 | handle.join().unwrap(); 46 | } 47 | ``` 48 | 49 | ## Implementation 50 | 51 | At the time of writing, the implementation of `std::sync::Barrier` internally uses a `Mutex`, 52 | which causes contention with many waiting threads, and incurs an undue performance overhead for 53 | each call to `wait`. 54 | 55 | This crate instead implements a counter-based linear barrier as described in "3.1 Centralized 56 | Barriers" in Mellor-Crummey and Scott’s paper [Algorithms for scalable synchronization on 57 | shared-memory multiprocessors][1] from 1991. For a higher-level explanation, see Lars-Dominik 58 | Braun's [Introduction to barrier algorithms][2]. 59 | 60 | ## Numbers 61 | 62 | Modern laptop with 2-core (4HT) Intel Core i7-5600U @ 2.60GHz: 63 | 64 | ```text 65 | test tests::ours_2 ... bench: 190 ns/iter (+/- 24) 66 | test tests::std_2 ... bench: 2,054 ns/iter (+/- 822) 67 | test tests::ours_4 ... bench: 236 ns/iter (+/- 2) 68 | test tests::std_4 ... bench: 11,913 ns/iter (+/- 60) 69 | ``` 70 | 71 | Dell server with 2x 10-core (20HT) Intel Xeon E5-2660 v3 @ 2.60GHz across two NUMA nodes: 72 | 73 | ```text 74 | test tests::ours_4 ... bench: 689 ns/iter (+/- 9) 75 | test tests::std_4 ... bench: 4,762 ns/iter (+/- 151) 76 | test tests::ours_8 ... bench: 1,380 ns/iter (+/- 13) 77 | test tests::std_8 ... bench: 17,545 ns/iter (+/- 288) 78 | test tests::ours_16 ... bench: 2,970 ns/iter (+/- 33) 79 | test tests::std_16 ... bench: 38,215 ns/iter (+/- 469) 80 | test tests::ours_32 ... bench: 3,838 ns/iter (+/- 129) 81 | test tests::std_32 ... bench: 94,266 ns/iter (+/- 12,243) 82 | ``` 83 | 84 | [1]: https://dl.acm.org/citation.cfm?doid=103727.103729 85 | [2]: https://6xq.net/barrier-intro/ 86 | [`std::sync::Barrier`]: https://doc.rust-lang.org/std/sync/struct.Barrier.html 87 | 88 | Or, in plot form: 89 | 90 | ![Barrier time as the number of threads grow](perf.png) 91 | -------------------------------------------------------------------------------- /README.tpl: -------------------------------------------------------------------------------- 1 | # {{crate}} 2 | 3 | [![Crates.io](https://img.shields.io/crates/v/hurdles.svg)](https://crates.io/crates/hurdles) 4 | [![Documentation](https://docs.rs/hurdles/badge.svg)](https://docs.rs/hurdles/) 5 | [![Build Status](https://travis-ci.org/jonhoo/arccstr.svg?branch=master)](https://travis-ci.org/jonhoo/arccstr) 6 | 7 | {{readme}} 8 | 9 | Or, in plot form: 10 | 11 | ![Barrier time as the number of threads grow](perf.png) 12 | -------------------------------------------------------------------------------- /perf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonhoo/hurdles/d225d56968c20d49e8247c2086f2917fca22de84/perf.png -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A scalable barrier (like [`std::sync::Barrier`]) that enables multiple threads to synchronize 2 | //! the beginning of some computation. 3 | //! 4 | //! This crate provides a similar interface as [`std::sync::Barrier`], but behaves much better in 5 | //! the face of many concurrently waiting threads, and incurs a lower per-thread latency penalty 6 | //! (see benchmarks below). The interface does differ from the standard library barrier however: 7 | //! 8 | //! - `Barrier` in this crate is `Clone`, and should *not* be wrapped in a `sync::Arc`. 9 | //! - `Barrier::wait` in this crate takes a `&mut self` receiver as each thread must keep some 10 | //! local state. 11 | //! 12 | //! Furthermore, when a thread blocks on `Barrier::wait`, the thread will (currently) *never* be 13 | //! suspended, and instead spin on the barrier. For the first few spins, it will also not call 14 | //! `sched_yield` to avoid the cost of thread sleep/wakeup. If threads are expected to reach the 15 | //! barrier at nearly the same time, or barrier latency is critical, this is probably what you 16 | //! want. However, if barriers are staggered and far between, then you may want to use 17 | //! [`std::sync::Barrier`] instead, as it is better about handling long waits. 18 | //! 19 | //! # Examples 20 | //! 21 | //! ``` 22 | //! use hurdles::Barrier; 23 | //! use std::thread; 24 | //! 25 | //! let mut handles = Vec::with_capacity(10); 26 | //! let mut barrier = Barrier::new(10); 27 | //! for _ in 0..10 { 28 | //! let mut c = barrier.clone(); 29 | //! // The same messages will be printed together. 30 | //! // You will NOT see any interleaving. 31 | //! handles.push(thread::spawn(move|| { 32 | //! println!("before wait"); 33 | //! c.wait(); 34 | //! println!("after wait"); 35 | //! })); 36 | //! } 37 | //! // Wait for other threads to finish. 38 | //! for handle in handles { 39 | //! handle.join().unwrap(); 40 | //! } 41 | //! ``` 42 | //! 43 | //! # Implementation 44 | //! 45 | //! At the time of writing, the implementation of `std::sync::Barrier` internally uses a `Mutex`, 46 | //! which causes contention with many waiting threads, and incurs an undue performance overhead for 47 | //! each call to `wait`. 48 | //! 49 | //! This crate instead implements a counter-based linear barrier as described in "3.1 Centralized 50 | //! Barriers" in Mellor-Crummey and Scott’s paper [Algorithms for scalable synchronization on 51 | //! shared-memory multiprocessors][1] from 1991. For a higher-level explanation, see Lars-Dominik 52 | //! Braun's [Introduction to barrier algorithms][2]. 53 | //! 54 | //! # Numbers 55 | //! 56 | //! Modern laptop with 2-core (4HT) Intel Core i7-5600U @ 2.60GHz: 57 | //! 58 | //! ```text 59 | //! test tests::ours_2 ... bench: 190 ns/iter (+/- 24) 60 | //! test tests::std_2 ... bench: 2,054 ns/iter (+/- 822) 61 | //! test tests::ours_4 ... bench: 236 ns/iter (+/- 2) 62 | //! test tests::std_4 ... bench: 11,913 ns/iter (+/- 60) 63 | //! ``` 64 | //! 65 | //! Dell server with 2x 10-core (20HT) Intel Xeon E5-2660 v3 @ 2.60GHz across two NUMA nodes: 66 | //! 67 | //! ```text 68 | //! test tests::ours_4 ... bench: 689 ns/iter (+/- 9) 69 | //! test tests::std_4 ... bench: 4,762 ns/iter (+/- 151) 70 | //! test tests::ours_8 ... bench: 1,380 ns/iter (+/- 13) 71 | //! test tests::std_8 ... bench: 17,545 ns/iter (+/- 288) 72 | //! test tests::ours_16 ... bench: 2,970 ns/iter (+/- 33) 73 | //! test tests::std_16 ... bench: 38,215 ns/iter (+/- 469) 74 | //! test tests::ours_32 ... bench: 3,838 ns/iter (+/- 129) 75 | //! test tests::std_32 ... bench: 94,266 ns/iter (+/- 12,243) 76 | //! ``` 77 | //! 78 | //! [1]: https://dl.acm.org/citation.cfm?doid=103727.103729 79 | //! [2]: https://6xq.net/barrier-intro/ 80 | //! [`std::sync::Barrier`]: https://doc.rust-lang.org/std/sync/struct.Barrier.html 81 | #![deny(missing_docs)] 82 | #![cfg_attr(feature = "nightly", feature(test))] 83 | 84 | #[cfg(feature = "nightly")] 85 | extern crate test; 86 | 87 | extern crate parking_lot_core; 88 | 89 | use std::sync::{atomic, Arc}; 90 | 91 | struct BarrierInner { 92 | gsense: atomic::AtomicBool, 93 | count: atomic::AtomicUsize, 94 | max: usize, 95 | } 96 | 97 | /// A barrier which enables multiple threads to synchronize the beginning of some computation. 98 | pub struct Barrier { 99 | inner: Arc, 100 | lsense: bool, 101 | used: bool, 102 | } 103 | 104 | /// A `BarrierWaitResult` is returned by [`wait`] when all threads in the [`Barrier`] 105 | /// have rendezvoused. 106 | /// 107 | /// # Examples 108 | /// 109 | /// ``` 110 | /// use hurdles::Barrier; 111 | /// 112 | /// let mut barrier = Barrier::new(1); 113 | /// let barrier_wait_result = barrier.wait(); 114 | /// ``` 115 | /// 116 | /// [`wait`]: struct.Barrier.html#method.wait 117 | /// [`Barrier`]: struct.Barrier.html 118 | pub struct BarrierWaitResult(bool); 119 | 120 | impl Barrier { 121 | /// Creates a new barrier that can block a given number of threads. 122 | /// 123 | /// A barrier will block `n-1` threads which call [`wait`] and then wake up all threads at once 124 | /// when the `n`th thread calls [`wait`]. 125 | /// 126 | /// # Examples 127 | /// 128 | /// ``` 129 | /// use hurdles::Barrier; 130 | /// let mut barrier = Barrier::new(10); 131 | /// ``` 132 | /// 133 | /// [`wait`]: struct.Barrier.html#method.wait 134 | pub fn new(n: usize) -> Self { 135 | Barrier { 136 | used: false, 137 | lsense: true, 138 | inner: Arc::new(BarrierInner { 139 | gsense: atomic::AtomicBool::new(true), 140 | count: atomic::AtomicUsize::new(n), 141 | max: n, 142 | }), 143 | } 144 | } 145 | 146 | /// Blocks the current thread until all threads have rendezvoused here. 147 | /// 148 | /// Barriers are re-usable after all threads have rendezvoused once, and can be used 149 | /// continuously. 150 | /// 151 | /// A single (arbitrary) thread will receive a [`BarrierWaitResult`] that returns `true` from 152 | /// [`is_leader`] when returning from this function, and all other threads will receive a 153 | /// result that will return `false` from [`is_leader`]. 154 | /// 155 | /// # Examples 156 | /// 157 | /// ``` 158 | /// use hurdles::Barrier; 159 | /// use std::thread; 160 | /// 161 | /// let mut handles = Vec::with_capacity(10); 162 | /// let mut barrier = Barrier::new(10); 163 | /// for _ in 0..10 { 164 | /// let mut c = barrier.clone(); 165 | /// // The same messages will be printed together. 166 | /// // You will NOT see any interleaving. 167 | /// handles.push(thread::spawn(move|| { 168 | /// println!("before wait"); 169 | /// c.wait(); 170 | /// println!("after wait"); 171 | /// })); 172 | /// } 173 | /// // Wait for other threads to finish. 174 | /// for handle in handles { 175 | /// handle.join().unwrap(); 176 | /// } 177 | /// ``` 178 | /// 179 | /// [`BarrierWaitResult`]: struct.BarrierWaitResult.html 180 | /// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader 181 | pub fn wait(&mut self) -> BarrierWaitResult { 182 | self.used = true; 183 | self.lsense = !self.lsense; 184 | if self.inner.count.fetch_sub(1, atomic::Ordering::SeqCst) == 1 { 185 | // we're the last to reach the barrier -- release all 186 | self.inner 187 | .count 188 | .store(self.inner.max, atomic::Ordering::SeqCst); 189 | self.inner 190 | .gsense 191 | .store(self.lsense, atomic::Ordering::SeqCst); 192 | BarrierWaitResult(true) 193 | } else { 194 | // wait for everyone to reach the barrier 195 | let mut wait = parking_lot_core::SpinWait::new(); 196 | while self.inner.gsense.load(atomic::Ordering::SeqCst) != self.lsense { 197 | // XXX: in theory we could go even further and park the thread eventually 198 | wait.spin(); 199 | } 200 | BarrierWaitResult(false) 201 | } 202 | } 203 | } 204 | 205 | impl Clone for Barrier { 206 | fn clone(&self) -> Self { 207 | assert!(!self.used); 208 | Barrier { 209 | used: false, 210 | lsense: self.lsense, 211 | inner: self.inner.clone(), 212 | } 213 | } 214 | } 215 | 216 | impl BarrierWaitResult { 217 | /// Returns whether this thread from [`wait`] is the "leader thread". 218 | /// 219 | /// Only one thread will have `true` returned from their result, all other 220 | /// threads will have `false` returned. 221 | /// 222 | /// [`wait`]: struct.Barrier.html#method.wait 223 | /// 224 | /// # Examples 225 | /// 226 | /// ``` 227 | /// use hurdles::Barrier; 228 | /// 229 | /// let mut barrier = Barrier::new(1); 230 | /// let barrier_wait_result = barrier.wait(); 231 | /// assert_eq!(barrier_wait_result.is_leader(), true); 232 | /// ``` 233 | pub fn is_leader(&self) -> bool { 234 | self.0 235 | } 236 | } 237 | 238 | #[cfg(test)] 239 | mod tests { 240 | use super::Barrier; 241 | use std::sync::mpsc::{channel, TryRecvError}; 242 | use std::thread; 243 | 244 | #[cfg(feature = "nightly")] 245 | use test::Bencher; 246 | 247 | #[cfg(feature = "nightly")] 248 | const BENCH_THREADS: usize = 4; 249 | 250 | #[cfg(feature = "nightly")] 251 | #[cfg_attr(feature = "nightly", bench)] 252 | fn ours(b: &mut Bencher) { 253 | let mut barrier = Barrier::new(BENCH_THREADS); 254 | for _ in 0..(BENCH_THREADS - 1) { 255 | let mut barrier = barrier.clone(); 256 | thread::spawn(move || loop { 257 | barrier.wait(); 258 | }); 259 | } 260 | b.iter(move || { barrier.wait(); }) 261 | } 262 | 263 | #[cfg(feature = "nightly")] 264 | #[cfg_attr(feature = "nightly", bench)] 265 | fn std(b: &mut Bencher) { 266 | use std::sync::{self, Arc}; 267 | let barrier = Arc::new(sync::Barrier::new(BENCH_THREADS)); 268 | for _ in 0..(BENCH_THREADS - 1) { 269 | let barrier = barrier.clone(); 270 | thread::spawn(move || loop { 271 | barrier.wait(); 272 | }); 273 | } 274 | b.iter(move || { barrier.wait(); }) 275 | } 276 | 277 | #[test] 278 | fn test_barrier() { 279 | const N: usize = 10; 280 | 281 | let mut barrier = Barrier::new(N); 282 | let (tx, rx) = channel(); 283 | 284 | for _ in 0..N - 1 { 285 | let mut c = barrier.clone(); 286 | let tx = tx.clone(); 287 | thread::spawn(move || { tx.send(c.wait().is_leader()).unwrap(); }); 288 | } 289 | 290 | // At this point, all spawned threads should be blocked, 291 | // so we shouldn't get anything from the port 292 | assert!(match rx.try_recv() { 293 | Err(TryRecvError::Empty) => true, 294 | _ => false, 295 | }); 296 | 297 | let mut leader_found = barrier.wait().is_leader(); 298 | 299 | // Now, the barrier is cleared and we should get data. 300 | for _ in 0..N - 1 { 301 | if rx.recv().unwrap() { 302 | assert!(!leader_found); 303 | leader_found = true; 304 | } 305 | } 306 | assert!(leader_found); 307 | } 308 | } 309 | --------------------------------------------------------------------------------