Skip to content

feat: implement Barrier using Condvar #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ default = [
"pin-project-lite",
]
docs = ["attributes", "unstable", "default"]
unstable = ["std", "broadcaster"]
unstable = ["std"]
attributes = ["async-attributes"]
std = [
"alloc",
Expand All @@ -55,7 +55,6 @@ alloc = [
[dependencies]
async-attributes = { version = "1.1.1", optional = true }
async-task = { version = "3.0.0", optional = true }
broadcaster = { version = "1.0.0", optional = true }
crossbeam-utils = { version = "0.7.2", optional = true }
futures-core = { version = "0.3.4", optional = true, default-features = false }
futures-io = { version = "0.3.4", optional = true }
Expand Down
59 changes: 15 additions & 44 deletions src/sync/barrier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use broadcaster::BroadcastChannel;

use crate::sync::Mutex;
use crate::sync::{Condvar,Mutex};

/// A barrier enables multiple tasks to synchronize the beginning
/// of some computation.
Expand Down Expand Up @@ -36,14 +34,13 @@ use crate::sync::Mutex;
#[derive(Debug)]
pub struct Barrier {
state: Mutex<BarrierState>,
wait: BroadcastChannel<(usize, usize)>,
n: usize,
cvar: Condvar,
num_tasks: usize,
}

// The inner state of a double barrier
#[derive(Debug)]
struct BarrierState {
waker: BroadcastChannel<(usize, usize)>,
count: usize,
generation_id: usize,
}
Expand Down Expand Up @@ -81,25 +78,14 @@ impl Barrier {
///
/// let barrier = Barrier::new(10);
/// ```
pub fn new(mut n: usize) -> Barrier {
let waker = BroadcastChannel::new();
let wait = waker.clone();

if n == 0 {
// if n is 0, it's not clear what behavior the user wants.
// in std::sync::Barrier, an n of 0 exhibits the same behavior as n == 1, where every
// .wait() immediately unblocks, so we adopt that here as well.
n = 1;
}

pub fn new(n: usize) -> Barrier {
Barrier {
state: Mutex::new(BarrierState {
waker,
count: 0,
generation_id: 1,
}),
n,
wait,
cvar: Condvar::new(),
num_tasks: n,
}
}

Expand Down Expand Up @@ -143,35 +129,20 @@ impl Barrier {
/// # });
/// ```
pub async fn wait(&self) -> BarrierWaitResult {
let mut lock = self.state.lock().await;
let local_gen = lock.generation_id;

lock.count += 1;
let mut state = self.state.lock().await;
let local_gen = state.generation_id;
state.count += 1;

if lock.count < self.n {
let mut wait = self.wait.clone();

let mut generation_id = lock.generation_id;
let mut count = lock.count;

drop(lock);

while local_gen == generation_id && count < self.n {
let (g, c) = wait.recv().await.expect("sender has not been closed");
generation_id = g;
count = c;
if state.count < self.num_tasks {
while local_gen == state.generation_id && state.count < self.num_tasks {
state = self.cvar.wait(state).await;
}

BarrierWaitResult(false)
} else {
lock.count = 0;
lock.generation_id = lock.generation_id.wrapping_add(1);

lock.waker
.send(&(lock.generation_id, lock.count))
.await
.expect("there should be at least one receiver");

state.count = 0;
state.generation_id = state.generation_id.wrapping_add(1);
self.cvar.notify_all();
BarrierWaitResult(true)
}
}
Expand Down