Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the parking crate instead of threading APIs #27

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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