Remove RefCell<Option> from PARKER

This commit is contained in:
Taiki Endo 2024-01-07 14:15:30 +09:00
parent b1d437ae83
commit 5f5135ed81
1 changed files with 7 additions and 13 deletions

View File

@ -1045,28 +1045,22 @@ impl<T, B: Borrow<Inner<T>> + Unpin> Listener<T, B> {
/// Wait until the provided deadline.
#[cfg(all(feature = "std", not(target_family = "wasm")))]
fn wait_internal(mut self: Pin<&mut Self>, deadline: Option<Instant>) -> Option<T> {
use std::cell::RefCell;
fn parker_and_task() -> (Parker, Task) {
let parker = Parker::new();
let unparker = parker.unparker();
(parker, Task::Unparker(unparker))
}
std::thread_local! {
/// Cached thread-local parker/unparker pair.
static PARKER: RefCell<Option<(Parker, Task)>> = RefCell::new(None);
static PARKER: (Parker, Task) = parker_and_task();
}
// Try to borrow the thread-local parker/unparker pair.
PARKER
.try_with({
let this = self.as_mut();
|parker| {
let mut pair = parker
.try_borrow_mut()
.expect("Shouldn't be able to borrow parker reentrantly");
let (parker, unparker) = pair.get_or_insert_with(|| {
let (parker, unparker) = parking::pair();
(parker, Task::Unparker(unparker))
});
this.wait_with_parker(deadline, parker, unparker.as_task_ref())
}
|(parker, unparker)| this.wait_with_parker(deadline, parker, unparker.as_task_ref())
})
.unwrap_or_else(|_| {
// If the pair isn't accessible, we may be being called in a destructor.