├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── benches └── basic.rs ├── examples ├── channel.rs └── mpmc.rs ├── src ├── channel.rs ├── lib.rs ├── stream.rs └── util.rs └── tests └── basic.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hyperbridge" 3 | version = "0.2.5" 4 | edition = "2018" 5 | authors = ["Anton Kundenko "] 6 | description = "Fast multi-producer multi-consumer channel with async support" 7 | repository = "https://github.com/singaraiona/hyperbridge" 8 | license = "Apache-2.0/MIT" 9 | 10 | [dependencies] 11 | futures = { version = "^0.3.15", optional = true } 12 | 13 | [dev-dependencies] 14 | crossbeam = "0.8.1" 15 | criterion = "0.3.5" 16 | 17 | [features] 18 | with-futures = ["futures"] 19 | 20 | [[bench]] 21 | name = "basic" 22 | harness = false -------------------------------------------------------------------------------- /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. -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hyperbridge 2 | 3 | Fast multi-producer, multi-consumer unbounded channel with async support. Inspired by [crossbeam unbounded channel](https://github.com/crossbeam-rs/crossbeam). 4 | 5 | [![Cargo](https://img.shields.io/crates/v/hyperbridge.svg)]( 6 | https://crates.io/crates/hyperbridge) 7 | [![Documentation](https://docs.rs/hyperbridge/badge.svg)]( 8 | https://docs.rs/hyperbridge) 9 | [![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)]( 10 | https://github.com/zesterer/hyperbridge) 11 | 12 | ## Examples 13 | 14 | ### `Hyperbridge::channel`: mpsc 15 | 16 | ```rust 17 | use hyperbridge::channel; 18 | use std::thread; 19 | 20 | let (sender, receiver) = hyperbridge::channel::new(); 21 | let mut counter = 0; 22 | let threads = 10; 23 | let values = 10000; 24 | 25 | let mut handles = vec![]; 26 | 27 | for i in 0..threads { 28 | let ch = sender.clone(); 29 | let jh = thread::spawn(move || { 30 | for _ in 0..values { 31 | ch.send(i).unwrap(); 32 | } 33 | }); 34 | handles.push(jh); 35 | } 36 | 37 | let mut iters = threads * values; 38 | 39 | while iters > 0 { 40 | match receiver.try_recv() { 41 | Ok(Some(v)) => { 42 | counter += v as usize; 43 | iters -= 1; 44 | } 45 | _ => {} 46 | } 47 | } 48 | 49 | let total = (0..threads).map(|i| i * values).sum(); 50 | 51 | for jh in handles.drain(..) { 52 | let _ = jh.join(); 53 | } 54 | ``` 55 | 56 | ### `Hyperbridge::channel`: mpmc 57 | 58 | ```rust 59 | use hyperbridge::channel; 60 | use std::thread; 61 | 62 | const VALUES: usize = 10000; 63 | const THREADS: usize = 16; 64 | 65 | let (sender, receiver) = channel::new(); 66 | let counter = Arc::new(AtomicUsize::new(0)); 67 | 68 | let mut handles = vec![]; 69 | 70 | for i in 0..THREADS { 71 | let ch = sender.clone(); 72 | let jh = thread::spawn(move || { 73 | for _ in 0..VALUES { 74 | ch.send(i).unwrap(); 75 | } 76 | }); 77 | handles.push(jh); 78 | } 79 | 80 | for _ in 0..THREADS { 81 | let ch = receiver.clone(); 82 | let local_counter = counter.clone(); 83 | let jh = thread::spawn(move || { 84 | let mut iters = VALUES; 85 | while iters > 0 { 86 | if let Ok(Some(v)) = ch.try_recv() { 87 | local_counter.fetch_add(v as usize, Relaxed); 88 | iters -= 1; 89 | } 90 | } 91 | }); 92 | handles.push(jh); 93 | } 94 | 95 | for jh in handles.drain(..) { 96 | let _ = jh.join(); 97 | } 98 | 99 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 100 | ``` 101 | 102 | ## Dependencies 103 | 104 | ```toml 105 | # Cargo.toml 106 | [dependencies] 107 | hyperbridge = "0.1.0" 108 | ``` 109 | 110 | ## Feature with-futures 111 | 112 | Turns on support for futures Sink, Stream: 113 | 114 | ```toml 115 | # Cargo.toml 116 | [dependencies] 117 | hyperbridge = { version = "0.1.0", features = ["with-futures"] } 118 | ``` 119 | 120 | ## License 121 | 122 | MIT/Apache-2.0 123 | -------------------------------------------------------------------------------- /benches/basic.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate criterion; 3 | 4 | use criterion::{Bencher, Criterion}; 5 | use hyperbridge::channel; 6 | use std::sync::atomic::AtomicUsize; 7 | use std::sync::atomic::Ordering::Relaxed; 8 | use std::sync::Arc; 9 | use std::thread; 10 | 11 | const VALUES: usize = 10000; 12 | const THREADS: usize = 16; 13 | 14 | fn test_hyperbridge_mpsc(b: &mut Bencher) { 15 | b.iter(|| { 16 | let (sender, receiver) = channel::new(); 17 | let mut counter = 0; 18 | 19 | let mut handles = vec![]; 20 | 21 | for i in 0..THREADS { 22 | let ch = sender.clone(); 23 | let jh = thread::spawn(move || { 24 | for _ in 0..VALUES { 25 | ch.send(i).unwrap(); 26 | } 27 | }); 28 | handles.push(jh); 29 | } 30 | 31 | let mut iters = THREADS * VALUES; 32 | 33 | while iters > 0 { 34 | match receiver.try_recv() { 35 | Ok(Some(v)) => { 36 | counter += v as usize; 37 | iters -= 1; 38 | } 39 | _ => {} 40 | } 41 | } 42 | 43 | let total: usize = (0..THREADS).map(|i| i * VALUES).sum(); 44 | 45 | for jh in handles.drain(..) { 46 | let _ = jh.join(); 47 | } 48 | 49 | assert_eq!(counter, total); 50 | }); 51 | } 52 | 53 | fn test_crossbeam_mpsc(b: &mut Bencher) { 54 | b.iter(|| { 55 | let (sender, receiver) = crossbeam::channel::unbounded(); 56 | let mut counter = 0; 57 | 58 | let mut handles = vec![]; 59 | 60 | for i in 0..THREADS { 61 | let ch = sender.clone(); 62 | let jh = thread::spawn(move || { 63 | for _ in 0..VALUES { 64 | ch.send(i).unwrap(); 65 | } 66 | }); 67 | handles.push(jh); 68 | } 69 | 70 | let mut iters = THREADS * VALUES; 71 | 72 | while iters > 0 { 73 | match receiver.try_recv() { 74 | Ok(v) => { 75 | counter += v as usize; 76 | iters -= 1; 77 | } 78 | _ => {} 79 | } 80 | } 81 | 82 | let total: usize = (0..THREADS).map(|i| i * VALUES).sum(); 83 | 84 | for jh in handles.drain(..) { 85 | let _ = jh.join(); 86 | } 87 | 88 | assert_eq!(counter, total); 89 | }); 90 | } 91 | 92 | fn test_hyperbridge_mpmc(b: &mut Bencher) { 93 | b.iter(|| { 94 | let (sender, receiver) = channel::new(); 95 | let counter = Arc::new(AtomicUsize::new(0)); 96 | 97 | let mut handles = vec![]; 98 | 99 | for i in 0..THREADS { 100 | let ch = sender.clone(); 101 | let jh = thread::spawn(move || { 102 | for _ in 0..VALUES { 103 | ch.send(i).unwrap(); 104 | } 105 | }); 106 | handles.push(jh); 107 | } 108 | 109 | for _ in 0..THREADS { 110 | let ch = receiver.clone(); 111 | let local_counter = counter.clone(); 112 | let jh = thread::spawn(move || { 113 | let mut iters = VALUES; 114 | while iters > 0 { 115 | if let Ok(Some(v)) = ch.try_recv() { 116 | local_counter.fetch_add(v as usize, Relaxed); 117 | iters -= 1; 118 | } 119 | } 120 | }); 121 | handles.push(jh); 122 | } 123 | 124 | for jh in handles.drain(..) { 125 | let _ = jh.join(); 126 | } 127 | 128 | let total: usize = (0..THREADS).map(|i| i * VALUES).sum(); 129 | 130 | assert_eq!(counter.load(Relaxed), total); 131 | }); 132 | } 133 | 134 | fn test_crossbeam_mpmc(b: &mut Bencher) { 135 | b.iter(|| { 136 | let (sender, receiver) = crossbeam::channel::unbounded(); 137 | let counter = Arc::new(AtomicUsize::new(0)); 138 | 139 | let mut handles = vec![]; 140 | 141 | for i in 0..THREADS { 142 | let ch = sender.clone(); 143 | let jh = thread::spawn(move || { 144 | for _ in 0..VALUES { 145 | ch.send(i).unwrap(); 146 | } 147 | }); 148 | handles.push(jh); 149 | } 150 | 151 | for _ in 0..THREADS { 152 | let ch = receiver.clone(); 153 | let local_counter = counter.clone(); 154 | let jh = thread::spawn(move || { 155 | let mut iters = VALUES; 156 | while iters > 0 { 157 | if let Ok(v) = ch.try_recv() { 158 | local_counter.fetch_add(v as usize, Relaxed); 159 | iters -= 1; 160 | } 161 | } 162 | }); 163 | handles.push(jh); 164 | } 165 | 166 | for jh in handles.drain(..) { 167 | let _ = jh.join(); 168 | } 169 | 170 | let total: usize = (0..THREADS).map(|i| i * VALUES).sum(); 171 | 172 | assert_eq!(counter.load(Relaxed), total); 173 | }); 174 | } 175 | 176 | fn hyperbridge_channel(b: &mut Criterion) { 177 | b.bench_function("hyperbridge-mpsc", |b| test_hyperbridge_mpsc(b)); 178 | b.bench_function("hyperbridge-mpmc", |b| test_hyperbridge_mpmc(b)); 179 | } 180 | 181 | fn crossbeam_channel(b: &mut Criterion) { 182 | b.bench_function("crossbeam-mpsc", |b| test_crossbeam_mpsc(b)); 183 | b.bench_function("crossbeam-mpmc", |b| test_crossbeam_mpmc(b)); 184 | } 185 | 186 | criterion_group!(compare, hyperbridge_channel, crossbeam_channel); 187 | criterion_main!(compare); 188 | -------------------------------------------------------------------------------- /examples/channel.rs: -------------------------------------------------------------------------------- 1 | extern crate hyperbridge; 2 | use std::thread; 3 | 4 | fn main() { 5 | let (sender, receiver) = hyperbridge::channel::new(); 6 | let threads = 10; 7 | let values = 10000; 8 | 9 | let mut handles = vec![]; 10 | 11 | for i in 0..threads { 12 | let ch = sender.clone(); 13 | let jh = thread::spawn(move || { 14 | for _ in 0..values { 15 | ch.send(i).unwrap(); 16 | } 17 | }); 18 | handles.push(jh); 19 | } 20 | 21 | let mut iters = threads * values; 22 | 23 | while iters > 0 { 24 | match receiver.try_recv() { 25 | Ok(Some(_v)) => { 26 | iters -= 1; 27 | } 28 | _ => {} 29 | } 30 | } 31 | 32 | let total: usize = (0..threads).map(|i| i * values).sum(); 33 | 34 | for jh in handles.drain(..) { 35 | let _ = jh.join(); 36 | } 37 | 38 | println!("Send and received: {} items", total); 39 | } 40 | -------------------------------------------------------------------------------- /examples/mpmc.rs: -------------------------------------------------------------------------------- 1 | extern crate hyperbridge; 2 | use std::sync::atomic::AtomicUsize; 3 | use std::sync::atomic::Ordering::*; 4 | use std::sync::Arc; 5 | use std::thread; 6 | 7 | fn main() { 8 | let (sender, receiver) = hyperbridge::channel::new(); 9 | let counter = Arc::new(AtomicUsize::new(0)); 10 | let threads = 128; 11 | let values = 10000; 12 | 13 | let mut handles = vec![]; 14 | 15 | for _ in 0..threads { 16 | let ch = receiver.clone(); 17 | let local_counter = counter.clone(); 18 | let jh = thread::spawn(move || { 19 | let mut iters = values; 20 | while iters > 0 { 21 | if let Ok(Some(v)) = ch.try_recv() { 22 | local_counter.fetch_add(v as usize, Relaxed); 23 | iters -= 1; 24 | } 25 | } 26 | }); 27 | handles.push(jh); 28 | } 29 | 30 | for i in 0..threads { 31 | let ch = sender.clone(); 32 | let jh = thread::spawn(move || { 33 | for _ in 0..values { 34 | ch.send(i).unwrap(); 35 | } 36 | }); 37 | handles.push(jh); 38 | } 39 | 40 | for jh in handles.drain(..) { 41 | let _ = jh.join(); 42 | } 43 | 44 | let total: usize = (0..threads).map(|i| i * values).sum(); 45 | 46 | println!("Send and received: {} items", total); 47 | } 48 | -------------------------------------------------------------------------------- /src/channel.rs: -------------------------------------------------------------------------------- 1 | use crate::util::{Backoff, CachePadded}; 2 | use core::cell::UnsafeCell; 3 | use core::mem::MaybeUninit; 4 | use core::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, Release, SeqCst}; 5 | use core::sync::atomic::{fence, AtomicPtr, AtomicUsize}; 6 | use std::sync::Arc; 7 | use std::{fmt, io}; 8 | 9 | // Slot states 10 | const WRITE: usize = 1; 11 | const READ: usize = 1 << 1; 12 | const DESTROY: usize = 1 << 2; 13 | // Read/Write round 14 | const ROUND: usize = 64; 15 | // Block capacity 16 | const BLOCK_SIZE: usize = ROUND - 1; 17 | // In a tail means that channel has been closed 18 | const CLOSED_FLAG: usize = 1 << 63; 19 | // In a head means head and tail are in a different blocks 20 | const CROSSED_FLAG: usize = CLOSED_FLAG; 21 | 22 | /// A place for storing a message 23 | struct Slot { 24 | state: AtomicUsize, 25 | message: UnsafeCell>, 26 | } 27 | 28 | impl fmt::Debug for Slot { 29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 30 | f.debug_struct("Slot") 31 | .field("state", &self.state.load(Acquire)) 32 | .finish() 33 | } 34 | } 35 | 36 | /// A block in a linked list. 37 | struct Block { 38 | next: AtomicPtr>, 39 | slots: [Slot; BLOCK_SIZE], 40 | } 41 | 42 | impl Block { 43 | /// Creates new empty block 44 | fn new() -> *mut Self { 45 | let block = unsafe { MaybeUninit::zeroed().assume_init() }; 46 | Box::into_raw(Box::new(block)) 47 | } 48 | 49 | /// Blocks current thread waiting the next block is set 50 | fn wait_next(&self) -> *mut Block { 51 | let backoff = Backoff::new(); 52 | loop { 53 | let next = self.next.load(Acquire); 54 | if !next.is_null() { 55 | return next; 56 | } 57 | backoff.spin(); 58 | } 59 | } 60 | 61 | /// Stores new one allocated block inside `next` 62 | fn set_next(&self, next: *mut Block) { 63 | let prev = self.next.swap(next, Release); 64 | debug_assert!(prev.is_null()); 65 | } 66 | 67 | /// Returns block inside `next` 68 | fn get_next(&self) -> Option<*mut Block> { 69 | let next = self.next.load(Acquire); 70 | if next.is_null() { 71 | return None; 72 | } 73 | Some(next) 74 | } 75 | 76 | /// Drop block if there are no readers using this block remains or leave dropping to a next thread 77 | fn destroy(this: *mut Block, start: usize) { 78 | // we can skip marking the last block with DESTROY because it has started destroy process 79 | for i in start..BLOCK_SIZE - 1 { 80 | let slot = unsafe { (*this).slots.get_unchecked(i) }; 81 | 82 | // set DESTROY bit if someone is still reading from this slot. 83 | if slot.state.load(Acquire) & READ == 0 84 | && slot.state.fetch_or(DESTROY, AcqRel) & READ == 0 85 | { 86 | // if someone is still using the slot, it will continue destruction of the block. 87 | return; 88 | } 89 | } 90 | 91 | // noone is using the block, now it is safe to destroy it. 92 | unsafe { drop(Box::from_raw(this)) }; 93 | } 94 | } 95 | 96 | /// A pointer to a block/slot 97 | #[derive(Debug)] 98 | struct Cursor { 99 | index: AtomicUsize, 100 | block: AtomicPtr>, 101 | } 102 | 103 | impl Cursor { 104 | fn from(block: *mut Block) -> Self { 105 | Cursor { 106 | index: AtomicUsize::new(0), 107 | block: AtomicPtr::new(block), 108 | } 109 | } 110 | 111 | #[inline] 112 | fn slot<'a>(block: *mut Block, index: usize) -> &'a Slot { 113 | debug_assert!(index < BLOCK_SIZE); 114 | unsafe { (*block).slots.get_unchecked(index) } 115 | } 116 | } 117 | 118 | /// Unbounded channel 119 | #[derive(Debug)] 120 | struct Channel { 121 | tail: CachePadded>, 122 | head: CachePadded>, 123 | } 124 | 125 | impl Drop for Channel { 126 | fn drop(&mut self) { 127 | // read all unread items to drop correctly 128 | while let Ok(Some(_)) = self.try_recv() {} 129 | 130 | let block = self.head.block.load(Acquire); 131 | 132 | // noone is using the block, now it is safe to destroy it. 133 | unsafe { drop(Box::from_raw(block)) }; 134 | } 135 | } 136 | 137 | impl Channel { 138 | /// Creates new unbounded channel 139 | fn new() -> Channel { 140 | let block = Block::::new(); 141 | Channel { 142 | tail: CachePadded::new(Cursor::from(block)), 143 | head: CachePadded::new(Cursor::from(block)), 144 | } 145 | } 146 | 147 | /// Try to send a message to a channel. 148 | /// Can not fail but when channel is closed 149 | #[inline] 150 | fn send(&self, msg: T) -> io::Result<()> { 151 | let backoff = Backoff::new(); 152 | let mut tail = self.tail.index.load(Acquire); 153 | let mut block = self.tail.block.load(Acquire); 154 | 155 | loop { 156 | let index = tail & BLOCK_SIZE; 157 | 158 | // channel is closed 159 | if tail & CLOSED_FLAG == CLOSED_FLAG { 160 | return Err(io::Error::new( 161 | io::ErrorKind::BrokenPipe, 162 | "channel is closed", 163 | )); 164 | } 165 | 166 | // wait next block 167 | if index == BLOCK_SIZE { 168 | backoff.snooze(); 169 | tail = self.tail.index.load(Acquire); 170 | block = self.tail.block.load(Acquire); 171 | continue; 172 | } 173 | 174 | // try to move tail forward 175 | match self 176 | .tail 177 | .index 178 | .compare_exchange_weak(tail, tail + 1, SeqCst, Relaxed) 179 | { 180 | Ok(_) => { 181 | let slot = Cursor::slot(block, index); 182 | 183 | // End of block, need to setup new one 184 | if index + 1 == BLOCK_SIZE { 185 | let next = Block::new(); 186 | self.tail.block.store(next, Release); 187 | self.tail.index.fetch_add(1, Release); 188 | unsafe { (*block).set_next(next) }; 189 | } 190 | 191 | unsafe { slot.message.get().write(MaybeUninit::new(msg)) }; 192 | slot.state.fetch_or(WRITE, Release); 193 | return Ok(()); 194 | } 195 | Err(t) => { 196 | tail = t; 197 | block = self.tail.block.load(Acquire); 198 | backoff.spin(); 199 | } 200 | } 201 | } 202 | } 203 | 204 | /// Try to receive message from a channel. 205 | /// Can fail or return uncompleted. 206 | #[inline] 207 | fn try_recv(&self) -> io::Result> { 208 | let backoff = Backoff::new(); 209 | let mut head = self.head.index.load(Acquire); 210 | let mut block = self.head.block.load(Acquire); 211 | 212 | loop { 213 | let index = head & BLOCK_SIZE; 214 | 215 | // wait next block 216 | if index == BLOCK_SIZE { 217 | backoff.snooze(); 218 | head = self.head.index.load(Acquire); 219 | block = self.head.block.load(Acquire); 220 | continue; 221 | } 222 | 223 | let mut new_head = head + 1; 224 | 225 | // head and tail are in the same block 226 | if head & CROSSED_FLAG == 0 { 227 | fence(SeqCst); 228 | let tail = self.tail.index.load(Relaxed); 229 | 230 | // Nothing to read 231 | if head == tail & !CLOSED_FLAG { 232 | // channel is closed 233 | if tail & CLOSED_FLAG != 0 { 234 | return Err(io::Error::new( 235 | io::ErrorKind::BrokenPipe, 236 | "channel is closed", 237 | )); 238 | } 239 | 240 | return Ok(None); 241 | } 242 | 243 | // mark head that it is in a different blocks with tail 244 | if head / ROUND != (tail & !CLOSED_FLAG) / ROUND { 245 | new_head |= CROSSED_FLAG; 246 | } 247 | } 248 | 249 | // try to move head forward 250 | match self 251 | .head 252 | .index 253 | .compare_exchange_weak(head, new_head, SeqCst, Relaxed) 254 | { 255 | Ok(_) => unsafe { 256 | // last slot in a block 257 | if index + 1 == BLOCK_SIZE { 258 | let next_block = (*block).wait_next(); 259 | if (*next_block).get_next().is_some() { 260 | new_head |= CROSSED_FLAG; 261 | } else { 262 | new_head &= !CROSSED_FLAG; 263 | } 264 | self.head.block.store(next_block, Release); 265 | self.head.index.store(new_head + 1, Release); 266 | } 267 | 268 | let slot = Cursor::slot(block, index); 269 | 270 | // wait until write operation completes 271 | while slot.state.load(Acquire) & WRITE == 0 { 272 | backoff.spin(); 273 | } 274 | 275 | let msg = slot.message.get().read().assume_init(); 276 | 277 | // this is the last block, so start destroying it 278 | if index + 1 == BLOCK_SIZE { 279 | Block::destroy(block, 0); 280 | } 281 | // someone started block destroy 282 | else if slot.state.fetch_or(READ, AcqRel) & DESTROY != 0 { 283 | Block::destroy(block, index + 1); 284 | } 285 | 286 | return Ok(Some(msg)); 287 | }, 288 | 289 | Err(h) => { 290 | head = h; 291 | block = self.head.block.load(Acquire); 292 | backoff.spin(); 293 | } 294 | } 295 | } 296 | } 297 | 298 | /// Check if there are no mesages in a channel 299 | #[inline] 300 | fn is_empty(&self) -> bool { 301 | let head = self.head.index.load(SeqCst); 302 | let tail = self.tail.index.load(SeqCst); 303 | head & !CROSSED_FLAG == tail & !CLOSED_FLAG 304 | } 305 | 306 | // Closes a channel 307 | #[inline] 308 | fn close(&self) { 309 | self.tail.index.fetch_or(CLOSED_FLAG, AcqRel); 310 | } 311 | } 312 | 313 | // Helpers struct holds together receivers and senders rc's 314 | // for handling drops of each side of channel (all senders or all receivers) 315 | // and calling close on entire channel for that 316 | pub(crate) struct Counters { 317 | pub(crate) receivers: AtomicUsize, 318 | pub(crate) senders: AtomicUsize, 319 | } 320 | 321 | /// Tx handle to a channel. Can be cloned 322 | pub struct Sender { 323 | chan: Arc>, 324 | pub(crate) cnts: Arc, 325 | } 326 | 327 | impl fmt::Debug for Sender { 328 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 329 | write!(f, "{:?}", self.chan) 330 | } 331 | } 332 | 333 | impl Sender { 334 | #[inline] 335 | pub fn send(&self, item: T) -> io::Result<()> { 336 | self.chan.send(item) 337 | } 338 | 339 | #[inline] 340 | pub fn is_empty(&self) -> bool { 341 | self.chan.is_empty() 342 | } 343 | 344 | pub fn close(&self) { 345 | self.chan.close() 346 | } 347 | 348 | pub fn receiver(&self) -> Receiver { 349 | self.cnts.receivers.fetch_add(1, SeqCst); 350 | Receiver { 351 | chan: Arc::clone(&self.chan), 352 | cnts: self.cnts.clone(), 353 | } 354 | } 355 | } 356 | 357 | impl Clone for Sender { 358 | fn clone(&self) -> Self { 359 | self.cnts.senders.fetch_add(1, SeqCst); 360 | Sender { 361 | chan: Arc::clone(&self.chan), 362 | cnts: self.cnts.clone(), 363 | } 364 | } 365 | } 366 | 367 | impl Drop for Sender { 368 | fn drop(&mut self) { 369 | let senders = self.cnts.senders.fetch_sub(1, SeqCst); 370 | // if it was the last sender - close channel 371 | if senders == 1 { 372 | self.chan.close(); 373 | } 374 | } 375 | } 376 | 377 | unsafe impl Send for Sender {} 378 | 379 | /// Rx handle to a channel. Can be cloned 380 | pub struct Receiver { 381 | chan: Arc>, 382 | pub(crate) cnts: Arc, 383 | } 384 | 385 | impl Receiver { 386 | #[inline] 387 | pub fn try_recv(&self) -> io::Result> { 388 | self.chan.try_recv() 389 | } 390 | 391 | pub fn sender(&self) -> Sender { 392 | self.cnts.senders.fetch_add(1, SeqCst); 393 | Sender { 394 | chan: Arc::clone(&self.chan), 395 | cnts: self.cnts.clone(), 396 | } 397 | } 398 | } 399 | 400 | impl Clone for Receiver { 401 | fn clone(&self) -> Self { 402 | self.cnts.receivers.fetch_add(1, SeqCst); 403 | Receiver { 404 | chan: Arc::clone(&self.chan), 405 | cnts: self.cnts.clone(), 406 | } 407 | } 408 | } 409 | 410 | impl Drop for Receiver { 411 | fn drop(&mut self) { 412 | let receivers = self.cnts.receivers.fetch_sub(1, SeqCst); 413 | // if it was the last receiver - close channel 414 | if receivers == 1 { 415 | self.chan.close(); 416 | } 417 | } 418 | } 419 | 420 | unsafe impl Send for Receiver {} 421 | 422 | /// Creates a new channel and splits it ro a Tx, Rx pair 423 | pub fn new() -> (Sender, Receiver) { 424 | let chan = Arc::new(Channel::new()); 425 | let cnts = Arc::new(Counters { 426 | receivers: AtomicUsize::new(1), 427 | senders: AtomicUsize::new(1), 428 | }); 429 | let tx = Sender { 430 | chan: chan.clone(), 431 | cnts: cnts.clone(), 432 | }; 433 | let rx = Receiver { 434 | chan: chan.clone(), 435 | cnts: cnts, 436 | }; 437 | (tx, rx) 438 | } 439 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! # Hyperbridge 2 | //! 3 | //! Fast multi-producer, multi-consumer unbounded channel with async support. 4 | //! 5 | //! ## Example 6 | //! 7 | //! ``` 8 | //! let (tx, rx) = hyperbridge::channel::new(); 9 | //! 10 | //! tx.send(42).unwrap(); 11 | //! let v = loop { 12 | //! match rx.try_recv() { 13 | //! Ok(None) => {}, // not ready 14 | //! Ok(Some(val)) => break val, 15 | //! Err(e) => panic!("channel read error: {:?}", e), 16 | //! } 17 | //! }; 18 | //! assert_eq!(v, 42); 19 | //! ``` 20 | 21 | #[cfg(feature = "with-futures")] 22 | extern crate futures; 23 | 24 | // mpmc channel 25 | pub mod channel; 26 | // Sink, Stream implementations for channel 27 | #[cfg(feature = "with-futures")] 28 | pub mod stream; 29 | // auxiliary tools 30 | pub mod util; 31 | -------------------------------------------------------------------------------- /src/stream.rs: -------------------------------------------------------------------------------- 1 | use crate::channel; 2 | use core::task::{Context, Poll}; 3 | use futures::sink::Sink; 4 | use futures::stream::Stream; 5 | use futures::task::AtomicWaker; 6 | use std::io; 7 | use std::pin::Pin; 8 | use std::sync::atomic::Ordering; 9 | use std::sync::Arc; 10 | 11 | pub struct Sender { 12 | tx: channel::Sender, 13 | rx_waker: Arc, 14 | } 15 | 16 | impl Clone for Sender { 17 | fn clone(&self) -> Self { 18 | Sender { 19 | tx: self.tx.clone(), 20 | rx_waker: self.rx_waker.clone(), 21 | } 22 | } 23 | } 24 | 25 | impl Sink for Sender 26 | where 27 | T: Unpin + 'static, 28 | { 29 | type Error = io::Error; 30 | 31 | fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { 32 | Poll::Ready(Ok(())) 33 | } 34 | 35 | fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> { 36 | self.tx.send(item) 37 | } 38 | 39 | fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { 40 | self.rx_waker.wake(); 41 | Poll::Ready(Ok(())) 42 | } 43 | 44 | fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { 45 | self.tx.close(); 46 | self.rx_waker.wake(); 47 | Poll::Ready(Ok(())) 48 | } 49 | } 50 | 51 | impl Drop for Sender { 52 | fn drop(&mut self) { 53 | // check if it was the last sender 54 | let senders = self.tx.cnts.senders.load(Ordering::SeqCst); 55 | // if so - close entire channel and wake up receiver 56 | if senders == 1 { 57 | self.tx.close(); 58 | self.rx_waker.wake(); 59 | } 60 | } 61 | } 62 | 63 | pub struct Receiver { 64 | rx: channel::Receiver, 65 | waker: Arc, 66 | } 67 | 68 | impl Receiver { 69 | #[inline] 70 | fn next_message(&self) -> Poll>> { 71 | match self.rx.try_recv() { 72 | Ok(Some(item)) => Poll::Ready(Some(Ok(item))), 73 | Ok(None) => Poll::Pending, 74 | Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Poll::Ready(None), 75 | Err(e) => Poll::Ready(Some(Err(e))), 76 | } 77 | } 78 | } 79 | 80 | impl Stream for Receiver { 81 | type Item = Result; 82 | 83 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { 84 | match self.next_message() { 85 | Poll::Pending => { 86 | self.waker.register(cx.waker()); 87 | self.next_message() 88 | } 89 | res => res, 90 | } 91 | } 92 | } 93 | 94 | pub fn new() -> (Sender, Receiver) { 95 | let (tx, rx) = channel::new(); 96 | let rx_waker = Arc::new(AtomicWaker::new()); 97 | let tx = Sender { 98 | tx: tx, 99 | rx_waker: rx_waker.clone(), 100 | }; 101 | let rx = Receiver { 102 | rx: rx, 103 | waker: rx_waker, 104 | }; 105 | (tx, rx) 106 | } 107 | -------------------------------------------------------------------------------- /src/util.rs: -------------------------------------------------------------------------------- 1 | use core::ops::{Deref, DerefMut}; 2 | use std::cell::Cell; 3 | 4 | const SPIN_LIMIT: u32 = 8; 5 | 6 | #[repr(transparent)] 7 | #[derive(Debug)] 8 | pub struct Backoff { 9 | rounds: Cell, 10 | } 11 | 12 | impl Backoff { 13 | #[inline] 14 | pub fn new() -> Self { 15 | Backoff { 16 | rounds: Cell::new(0), 17 | } 18 | } 19 | 20 | #[inline] 21 | pub fn reset(&self) { 22 | self.rounds.set(0) 23 | } 24 | 25 | #[inline] 26 | pub fn rounds(&self) -> u32 { 27 | self.rounds.get() 28 | } 29 | 30 | #[inline] 31 | pub fn spin_once(&self) { 32 | std::hint::spin_loop(); 33 | } 34 | 35 | #[inline] 36 | pub fn spin(&self) { 37 | for _ in 0..1 << self.rounds.get().min(SPIN_LIMIT) { 38 | std::hint::spin_loop(); 39 | } 40 | 41 | if self.rounds.get() <= SPIN_LIMIT { 42 | self.rounds.set(self.rounds.get() + 1); 43 | } 44 | } 45 | 46 | #[inline] 47 | pub fn snooze(&self) { 48 | if self.rounds.get() <= SPIN_LIMIT { 49 | self.spin(); 50 | } else { 51 | std::thread::yield_now(); 52 | } 53 | } 54 | } 55 | 56 | #[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))] 57 | #[cfg_attr( 58 | not(any(target_arch = "x86_64", target_arch = "aarch64")), 59 | repr(align(64)) 60 | )] 61 | #[derive(Debug)] 62 | pub struct CachePadded(T); 63 | 64 | impl CachePadded { 65 | pub const fn new(t: T) -> CachePadded { 66 | CachePadded(t) 67 | } 68 | pub fn into_inner(self) -> T { 69 | self.0 70 | } 71 | } 72 | 73 | impl Deref for CachePadded { 74 | type Target = T; 75 | fn deref(&self) -> &T { 76 | &self.0 77 | } 78 | } 79 | 80 | impl DerefMut for CachePadded { 81 | fn deref_mut(&mut self) -> &mut T { 82 | &mut self.0 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /tests/basic.rs: -------------------------------------------------------------------------------- 1 | #![feature(test)] 2 | extern crate crossbeam; 3 | extern crate test; 4 | 5 | use core::sync::atomic::AtomicUsize; 6 | use core::sync::atomic::Ordering::Relaxed; 7 | use hyperbridge::channel; 8 | use std::sync::Arc; 9 | use std::thread; 10 | 11 | const VALUES: usize = 10000; 12 | const THREADS: usize = 16; 13 | 14 | #[test] 15 | fn hyperbridge_close() { 16 | let (sender, receiver) = channel::new(); 17 | let mut counter = 0; 18 | 19 | let mut handles = vec![]; 20 | 21 | for i in 0..THREADS { 22 | let ch = sender.clone(); 23 | let jh = thread::spawn(move || { 24 | for _ in 0..VALUES { 25 | ch.send(i).unwrap(); 26 | } 27 | }); 28 | handles.push(jh); 29 | } 30 | 31 | for jh in handles.drain(..) { 32 | let _ = jh.join(); 33 | } 34 | 35 | sender.close(); 36 | 37 | let mut iters = THREADS * VALUES; 38 | 39 | while iters > 0 { 40 | match receiver.try_recv() { 41 | Ok(Some(v)) => { 42 | counter += v as usize; 43 | iters -= 1; 44 | } 45 | _ => {} 46 | } 47 | } 48 | 49 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 50 | 51 | assert_eq!(counter, total); 52 | } 53 | 54 | #[test] 55 | fn hyperbridge_mpsc() { 56 | let (sender, receiver) = channel::new(); 57 | let mut counter = 0; 58 | 59 | let mut handles = vec![]; 60 | 61 | for i in 0..THREADS { 62 | let ch = sender.clone(); 63 | let jh = thread::spawn(move || { 64 | for _ in 0..VALUES { 65 | ch.send(i).unwrap(); 66 | } 67 | }); 68 | handles.push(jh); 69 | } 70 | 71 | let mut iters = THREADS * VALUES; 72 | 73 | while iters > 0 { 74 | match receiver.try_recv() { 75 | Ok(Some(v)) => { 76 | counter += v as usize; 77 | iters -= 1; 78 | } 79 | _ => {} 80 | } 81 | } 82 | 83 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 84 | 85 | for jh in handles.drain(..) { 86 | let _ = jh.join(); 87 | } 88 | 89 | assert_eq!(counter, total); 90 | } 91 | 92 | #[test] 93 | fn hyperbridge_mpmc() { 94 | let (sender, receiver) = channel::new(); 95 | let counter = Arc::new(AtomicUsize::new(0)); 96 | 97 | let mut handles = vec![]; 98 | 99 | for i in 0..THREADS { 100 | let ch = sender.clone(); 101 | let jh = thread::spawn(move || { 102 | for _ in 0..VALUES { 103 | ch.send(i).unwrap(); 104 | } 105 | }); 106 | handles.push(jh); 107 | } 108 | 109 | for _ in 0..THREADS { 110 | let ch = receiver.clone(); 111 | let local_counter = counter.clone(); 112 | let jh = thread::spawn(move || { 113 | let mut iters = VALUES; 114 | while iters > 0 { 115 | if let Ok(Some(v)) = ch.try_recv() { 116 | local_counter.fetch_add(v as usize, Relaxed); 117 | iters -= 1; 118 | } 119 | } 120 | }); 121 | handles.push(jh); 122 | } 123 | 124 | for jh in handles.drain(..) { 125 | let _ = jh.join(); 126 | } 127 | 128 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 129 | 130 | assert_eq!(counter.load(Relaxed), total); 131 | } 132 | 133 | #[test] 134 | fn hyperbridge_drop_senders() { 135 | let (sender, receiver) = channel::new(); 136 | let mut counter = 0; 137 | 138 | let mut handles = vec![]; 139 | 140 | for i in 0..THREADS { 141 | let ch = sender.clone(); 142 | let jh = thread::spawn(move || { 143 | for _ in 0..VALUES { 144 | ch.send(i).unwrap(); 145 | } 146 | }); 147 | handles.push(jh); 148 | } 149 | 150 | for jh in handles.drain(..) { 151 | let _ = jh.join(); 152 | } 153 | 154 | let mut iters = THREADS * VALUES; 155 | 156 | while iters > 0 { 157 | match receiver.try_recv() { 158 | Ok(Some(v)) => { 159 | counter += v as usize; 160 | iters -= 1; 161 | } 162 | _ => {} 163 | } 164 | } 165 | 166 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 167 | 168 | assert_eq!(counter, total); 169 | 170 | // should call close on drop last sender 171 | drop(sender); 172 | 173 | match receiver.try_recv() { 174 | Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::BrokenPipe), 175 | _ => panic!("expected closed channel"), 176 | } 177 | } 178 | 179 | #[test] 180 | fn hyperbridge_drop_receiver() { 181 | let (sender, receiver) = channel::new(); 182 | let mut counter = 0; 183 | 184 | let mut handles = vec![]; 185 | 186 | for i in 0..THREADS { 187 | let ch = sender.clone(); 188 | let jh = thread::spawn(move || { 189 | for _ in 0..VALUES { 190 | ch.send(i).unwrap(); 191 | } 192 | }); 193 | handles.push(jh); 194 | } 195 | 196 | for jh in handles.drain(..) { 197 | let _ = jh.join(); 198 | } 199 | 200 | let mut iters = THREADS * VALUES; 201 | 202 | while iters > 0 { 203 | match receiver.try_recv() { 204 | Ok(Some(v)) => { 205 | counter += v as usize; 206 | iters -= 1; 207 | } 208 | _ => {} 209 | } 210 | } 211 | 212 | let total = (0..THREADS).map(|i| i * VALUES).sum(); 213 | 214 | assert_eq!(counter, total); 215 | 216 | // should call close on drop last receiver 217 | drop(receiver); 218 | 219 | match sender.send(0) { 220 | Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::BrokenPipe), 221 | _ => panic!("expected closed channel"), 222 | } 223 | } 224 | --------------------------------------------------------------------------------