Skip to content

Commit

Permalink
Use the parking crate instead of threading APIs (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull authored Aug 24, 2022
1 parent a38fcb4 commit a01518f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
{
Expand All @@ -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.
Expand All @@ -634,7 +636,7 @@ impl EventListener {
}

// Park until the deadline.
thread::park_timeout(deadline - now);
parker.park_timeout(deadline - now);
}
}

Expand Down Expand Up @@ -776,7 +778,7 @@ enum State {
Polling(Waker),

/// A thread is blocked on it.
Waiting(Thread),
Waiting(Unparker),
}

impl State {
Expand All @@ -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<State>,

/// Previous entry in the linked list.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a01518f

Please sign in to comment.