Skip to content

Commit fdc8fe6

Browse files
authored
Merge pull request #242 from async-rs/barrier-unstable
mark sync::Barrier as unstable
2 parents 0f0b354 + 0b39306 commit fdc8fe6

File tree

4 files changed

+64
-55
lines changed

4 files changed

+64
-55
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ features = ["docs"]
2121
rustdoc-args = ["--cfg", "feature=\"docs\""]
2222

2323
[features]
24-
docs = []
25-
unstable = []
24+
docs = ["broadcaster"]
25+
unstable = ["broadcaster"]
2626

2727
[dependencies]
2828
async-macros = "1.0.0"
@@ -42,7 +42,7 @@ num_cpus = "1.10.1"
4242
pin-utils = "0.1.0-alpha.4"
4343
slab = "0.4.2"
4444
kv-log-macro = "1.0.4"
45-
broadcaster = "0.2.4"
45+
broadcaster = { version = "0.2.4", optional = true }
4646

4747
[dev-dependencies]
4848
femme = "1.2.0"

src/sync/barrier.rs

+57
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::sync::Mutex;
3232
/// # });
3333
/// # }
3434
/// ```
35+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
3536
#[derive(Debug)]
3637
pub struct Barrier {
3738
state: Mutex<BarrierState>,
@@ -60,6 +61,7 @@ struct BarrierState {
6061
/// let barrier = Barrier::new(1);
6162
/// let barrier_wait_result = barrier.wait();
6263
/// ```
64+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
6365
#[derive(Debug, Clone)]
6466
pub struct BarrierWaitResult(bool);
6567

@@ -172,3 +174,58 @@ impl BarrierWaitResult {
172174
self.0
173175
}
174176
}
177+
178+
#[cfg(test)]
179+
mod test {
180+
use futures_channel::mpsc::unbounded;
181+
use futures_util::sink::SinkExt;
182+
use futures_util::stream::StreamExt;
183+
184+
use crate::sync::{Arc, Barrier};
185+
use crate::task;
186+
187+
#[test]
188+
fn test_barrier() {
189+
// NOTE(dignifiedquire): Based on the test in std, I was seeing some
190+
// race conditions, so running it in a loop to make sure things are
191+
// solid.
192+
193+
for _ in 0..1_000 {
194+
task::block_on(async move {
195+
const N: usize = 10;
196+
197+
let barrier = Arc::new(Barrier::new(N));
198+
let (tx, mut rx) = unbounded();
199+
200+
for _ in 0..N - 1 {
201+
let c = barrier.clone();
202+
let mut tx = tx.clone();
203+
task::spawn(async move {
204+
let res = c.wait().await;
205+
206+
tx.send(res.is_leader()).await.unwrap();
207+
});
208+
}
209+
210+
// At this point, all spawned threads should be blocked,
211+
// so we shouldn't get anything from the port
212+
let res = rx.try_next();
213+
assert!(match res {
214+
Err(_err) => true,
215+
_ => false,
216+
});
217+
218+
let mut leader_found = barrier.wait().await.is_leader();
219+
220+
// Now, the barrier is cleared and we should get data.
221+
for _ in 0..N - 1 {
222+
if rx.next().await.unwrap() {
223+
assert!(!leader_found);
224+
leader_found = true;
225+
}
226+
}
227+
assert!(leader_found);
228+
});
229+
}
230+
}
231+
}

src/sync/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232
#[doc(inline)]
3333
pub use std::sync::{Arc, Weak};
3434

35+
#[cfg(any(feature = "unstable", feature = "docs"))]
3536
pub use barrier::{Barrier, BarrierWaitResult};
37+
3638
pub use mutex::{Mutex, MutexGuard};
3739
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
3840

41+
#[cfg(any(feature = "unstable", feature = "docs"))]
42+
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
3943
mod barrier;
4044
mod mutex;
4145
mod rwlock;

tests/barrier.rs

-52
This file was deleted.

0 commit comments

Comments
 (0)