From b3b8e285f304bad3fbd15fcdae495ddd8dabfef9 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Tue, 23 Aug 2022 10:07:02 -0700 Subject: [PATCH 1/2] use parking crate instead of std thread --- Cargo.toml | 4 +++- src/lib.rs | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 24f1e7e..f21492e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,8 @@ keywords = ["condvar", "eventcount", "wake", "blocking", "park"] categories = ["asynchronous", "concurrency"] exclude = ["/.*"] +[dependencies] +parking = "2.0.0" + [dev-dependencies] -futures = { version = "0.3", default-features = false, features = ["std"] } waker-fn = "1" diff --git a/src/lib.rs b/src/lib.rs index 20047be..7cd4e5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,10 +73,11 @@ use std::ptr::{self, NonNull}; use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, MutexGuard}; use std::task::{Context, Poll, Waker}; -use std::thread::{self, Thread}; use std::time::{Duration, Instant}; use std::usize; +use parking::Unparker; + /// Inner state of [`Event`]. struct Inner { /// The number of notified entries, or `usize::MAX` if all of them have been notified. @@ -598,6 +599,7 @@ impl EventListener { None => unreachable!("cannot wait twice on an `EventListener`"), Some(entry) => entry, }; + let (parker, unparker) = parking::pair(); // Set this listener's state to `Waiting`. { @@ -612,14 +614,14 @@ impl EventListener { return true; } // Otherwise, set the state to `Waiting`. - _ => e.state.set(State::Waiting(thread::current())), + _ => e.state.set(State::Waiting(unparker)), } } // Wait until a notification is received or the timeout is reached. loop { match deadline { - None => thread::park(), + None => parker.park(), Some(deadline) => { // Check for timeout. @@ -634,7 +636,7 @@ impl EventListener { } // Park until the deadline. - thread::park_timeout(deadline - now); + parker.park_timeout(deadline - now); } } @@ -776,7 +778,7 @@ enum State { Polling(Waker), /// A thread is blocked on it. - Waiting(Thread), + Waiting(Unparker), } impl State { @@ -792,7 +794,7 @@ impl State { /// An entry representing a registered listener. struct Entry { - /// THe state of this listener. + /// The state of this listener. state: Cell, /// Previous entry in the linked list. @@ -928,7 +930,7 @@ impl List { State::Notified(_) => {} State::Created => {} State::Polling(w) => w.wake(), - State::Waiting(t) => t.unpark(), + State::Waiting(t) => { t.unpark(); }, } // Update the counter. @@ -957,7 +959,7 @@ impl List { State::Notified(_) => {} State::Created => {} State::Polling(w) => w.wake(), - State::Waiting(t) => t.unpark(), + State::Waiting(t) => { t.unpark(); }, } // Update the counter. From 6d1725ba457fa7c5c4dd269eb465f86e357b43af Mon Sep 17 00:00:00 2001 From: jtnunley Date: Tue, 23 Aug 2022 13:55:00 -0700 Subject: [PATCH 2/2] bump MSRV --- .github/workflows/ci.yml | 2 +- src/lib.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 988524f..2ab378b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,7 @@ jobs: matrix: # When updating this, the reminder to update the minimum supported # Rust version in Cargo.toml and .clippy.toml. - rust: ['1.36'] + rust: ['1.39'] steps: - uses: actions/checkout@v3 - name: Install Rust diff --git a/src/lib.rs b/src/lib.rs index 7cd4e5b..0290a48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -930,7 +930,9 @@ impl List { State::Notified(_) => {} State::Created => {} State::Polling(w) => w.wake(), - State::Waiting(t) => { t.unpark(); }, + State::Waiting(t) => { + t.unpark(); + } } // Update the counter. @@ -959,7 +961,9 @@ impl List { State::Notified(_) => {} State::Created => {} State::Polling(w) => w.wake(), - State::Waiting(t) => { t.unpark(); }, + State::Waiting(t) => { + t.unpark(); + } } // Update the counter.