From d0ebb4154748166a4ba07baa4b424a1c45efd219 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Mon, 16 Nov 2020 22:49:35 +0200 Subject: sync: add `Notify::notify_waiters` (#3098) This PR makes `Notify::notify_waiters` public. The method already exists, but it changes the way `notify_waiters`, is used. Previously in order for the consumer to register interest, in a notification triggered by `notify_waiters`, the `Notified` future had to be polled. This introduced friction when using the api as the future had to be pinned before polled. This change introduces a counter that tracks how many times `notified_waiters` has been called. Upon creation of the future the number of times is loaded. When first polled the future compares this number with the count state of the `Notify` type. This avoids the need for registering the waiter upfront. Fixes: #3066 --- tokio/src/sync/tests/loom_notify.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tokio/src/sync/tests') diff --git a/tokio/src/sync/tests/loom_notify.rs b/tokio/src/sync/tests/loom_notify.rs index 79a5bf89..4be949a3 100644 --- a/tokio/src/sync/tests/loom_notify.rs +++ b/tokio/src/sync/tests/loom_notify.rs @@ -21,6 +21,27 @@ fn notify_one() { }); } +#[test] +fn notify_waiters() { + loom::model(|| { + let notify = Arc::new(Notify::new()); + let tx = notify.clone(); + let notified1 = notify.notified(); + let notified2 = notify.notified(); + + let th = thread::spawn(move || { + tx.notify_waiters(); + }); + + th.join().unwrap(); + + block_on(async { + notified1.await; + notified2.await; + }); + }); +} + #[test] fn notify_multi() { loom::model(|| { -- cgit v1.2.3