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/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..0290a48 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,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. @@ -957,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.