Skip to content

Commit a01518f

Browse files
authored
Use the parking crate instead of threading APIs (#27)
1 parent a38fcb4 commit a01518f

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
matrix:
3737
# When updating this, the reminder to update the minimum supported
3838
# Rust version in Cargo.toml and .clippy.toml.
39-
rust: ['1.36']
39+
rust: ['1.39']
4040
steps:
4141
- uses: actions/checkout@v3
4242
- name: Install Rust

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ keywords = ["condvar", "eventcount", "wake", "blocking", "park"]
1414
categories = ["asynchronous", "concurrency"]
1515
exclude = ["/.*"]
1616

17+
[dependencies]
18+
parking = "2.0.0"
19+
1720
[dev-dependencies]
18-
futures = { version = "0.3", default-features = false, features = ["std"] }
1921
waker-fn = "1"

src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ use std::ptr::{self, NonNull};
7373
use std::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
7474
use std::sync::{Arc, Mutex, MutexGuard};
7575
use std::task::{Context, Poll, Waker};
76-
use std::thread::{self, Thread};
7776
use std::time::{Duration, Instant};
7877
use std::usize;
7978

79+
use parking::Unparker;
80+
8081
/// Inner state of [`Event`].
8182
struct Inner {
8283
/// The number of notified entries, or `usize::MAX` if all of them have been notified.
@@ -598,6 +599,7 @@ impl EventListener {
598599
None => unreachable!("cannot wait twice on an `EventListener`"),
599600
Some(entry) => entry,
600601
};
602+
let (parker, unparker) = parking::pair();
601603

602604
// Set this listener's state to `Waiting`.
603605
{
@@ -612,14 +614,14 @@ impl EventListener {
612614
return true;
613615
}
614616
// Otherwise, set the state to `Waiting`.
615-
_ => e.state.set(State::Waiting(thread::current())),
617+
_ => e.state.set(State::Waiting(unparker)),
616618
}
617619
}
618620

619621
// Wait until a notification is received or the timeout is reached.
620622
loop {
621623
match deadline {
622-
None => thread::park(),
624+
None => parker.park(),
623625

624626
Some(deadline) => {
625627
// Check for timeout.
@@ -634,7 +636,7 @@ impl EventListener {
634636
}
635637

636638
// Park until the deadline.
637-
thread::park_timeout(deadline - now);
639+
parker.park_timeout(deadline - now);
638640
}
639641
}
640642

@@ -776,7 +778,7 @@ enum State {
776778
Polling(Waker),
777779

778780
/// A thread is blocked on it.
779-
Waiting(Thread),
781+
Waiting(Unparker),
780782
}
781783

782784
impl State {
@@ -792,7 +794,7 @@ impl State {
792794

793795
/// An entry representing a registered listener.
794796
struct Entry {
795-
/// THe state of this listener.
797+
/// The state of this listener.
796798
state: Cell<State>,
797799

798800
/// Previous entry in the linked list.
@@ -928,7 +930,9 @@ impl List {
928930
State::Notified(_) => {}
929931
State::Created => {}
930932
State::Polling(w) => w.wake(),
931-
State::Waiting(t) => t.unpark(),
933+
State::Waiting(t) => {
934+
t.unpark();
935+
}
932936
}
933937

934938
// Update the counter.
@@ -957,7 +961,9 @@ impl List {
957961
State::Notified(_) => {}
958962
State::Created => {}
959963
State::Polling(w) => w.wake(),
960-
State::Waiting(t) => t.unpark(),
964+
State::Waiting(t) => {
965+
t.unpark();
966+
}
961967
}
962968

963969
// Update the counter.

0 commit comments

Comments
 (0)