Skip to content

Commit

Permalink
Port to event-listener v5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Feb 12, 2024
1 parent fc7d911 commit f1f60bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ categories = ["asynchronous", "concurrency"]
keywords = ["atomic", "sync", "async", "waitgroup", "futures"]

[dependencies]
event-listener = { version = "4.0.0", default-features = false }
event-listener-strategy = { version = "0.4.0", default-features = false }
futures-core = { version = "0.3.5", default-features = false }
pin-project-lite = "0.2.11"
event-listener = { version = "5.0.0", default-features = false }
event-listener-strategy = { version = "0.5.0", default-features = false }
futures-core = { version = "0.3.30", default-features = false }
pin-project-lite = "0.2.13"

[dev-dependencies]
futures-util = "0.3"
pin-utils = "0.1"
tokio = { version = "1.35.1", features = ["macros", "rt"] }
tokio = { version = "1.36", features = ["macros", "rt"] }

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# async-waitgroup

[![Build Status](https://github.com/maolonglong/async-waitgroup/workflows/CI/badge.svg)](https://github.com/maolonglong/async-waitgroup/actions)
[![CI](https://github.com/maolonglong/async-waitgroup/actions/workflows/ci.yml/badge.svg)](https://github.com/maolonglong/async-waitgroup/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/license-MIT_OR_Apache--2.0-blue.svg)](https://github.com/maolonglong/async-waitgroup#license)
[![Cargo](https://img.shields.io/crates/v/async-waitgroup.svg)](https://crates.io/crates/async-waitgroup)
[![Documentation](https://docs.rs/async-waitgroup/badge.svg)](https://docs.rs/async-waitgroup)
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
extern crate alloc;

use alloc::sync::Arc;
use core::marker::PhantomPinned;
use core::pin::Pin;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::task::Poll;
Expand All @@ -16,6 +17,7 @@ use std::fmt;
use event_listener::{Event, EventListener};
use event_listener_strategy::{easy_wrapper, EventListenerFuture, Strategy};
use futures_core::ready;
use pin_project_lite::pin_project;

/// Enables tasks to synchronize the beginning or end of some computation.
///
Expand Down Expand Up @@ -104,7 +106,8 @@ impl WaitGroup {
pub fn wait(self) -> Wait {
Wait::_new(WaitInner {
wg: self.inner.clone(),
listener: EventListener::new(),
listener: None,
_pin: PhantomPinned,
})
}

Expand Down Expand Up @@ -142,11 +145,13 @@ easy_wrapper! {
pub(crate) wait();
}

pin_project_lite::pin_project! {
pin_project! {
#[project(!Unpin)]
struct WaitInner {
wg: Arc<WgInner>,
listener: Option<EventListener>,
#[pin]
listener: EventListener,
_pin: PhantomPinned
}
}

Expand All @@ -158,18 +163,18 @@ impl EventListenerFuture for WaitInner {
strategy: &mut S,
context: &mut S::Context,
) -> Poll<Self::Output> {
let mut this = self.project();
let this = self.project();

if this.wg.count.load(Ordering::SeqCst) == 0 {
return Poll::Ready(());
}

let mut count = this.wg.count.load(Ordering::SeqCst);
while count > 0 {
if this.listener.is_listening() {
ready!(strategy.poll(this.listener.as_mut(), context))
if this.listener.is_some() {
ready!(strategy.poll(&mut *this.listener, context))
} else {
this.listener.as_mut().listen(&this.wg.drop_ops);
*this.listener = Some(this.wg.drop_ops.listen());
}
count = this.wg.count.load(Ordering::SeqCst);
}
Expand Down Expand Up @@ -212,7 +217,7 @@ mod tests {

#[tokio::test]
async fn test_wait() {
const LOOP: usize = 10_000;
const LOOP: usize = if cfg!(miri) { 100 } else { 10_000 };

let wg = WaitGroup::new();
let cnt = Arc::new(AtomicUsize::new(0));
Expand Down
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use async_waitgroup::WaitGroup;
use std::thread;

fn main() {
loop {
let wg = WaitGroup::new();

thread::spawn({
let wg = wg.clone();
move || {
println!("child waiting");
wg.wait_blocking();
}
});

println!("parent waiting");
wg.wait_blocking();
println!("done");
}
}

0 comments on commit f1f60bf

Please sign in to comment.