Skip to content

Barrier Synchronization for async/await #230

@ytakano

Description

@ytakano

Implement a barrier synchronization for async/await.

I implemented a barrier synchronization as follows.

#[derive(Clone)]
struct Barrier {
count: Arc<AtomicUsize>,
tx: Arc<Publisher<()>>,
rx: Subscriber<()>,
}
impl Barrier {
fn new(count: usize) -> Self {
let attr = Attribute {
queue_size: 1,
..Attribute::default()
};
let (tx, rx) = pubsub::create_pubsub(attr);
Self {
count: Arc::new(AtomicUsize::new(count)),
tx: Arc::new(tx),
rx,
}
}
async fn wait(&mut self) {
if self
.count
.fetch_sub(1, core::sync::atomic::Ordering::Relaxed)
== 1
{
self.tx.send(()).await;
} else {
self.rx.recv().await;
}
}
}

However, the APIs are not compatible with other libraries like Tokio.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions