use std::cell::RefCell;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_KEY: AtomicU64 = AtomicU64::new(1);
thread_local! {
static POLLING: RefCell<Vec<u64>> = const { RefCell::new(Vec::new()) };
}
#[cfg_attr(
not(any(
feature = "boa",
feature = "quickjs",
feature = "v8",
feature = "fuzzing",
test
)),
expect(dead_code, reason = "only the host bridge and worker spawning use it")
)]
pub(crate) fn new_key() -> u64 {
NEXT_KEY.fetch_add(1, Ordering::Relaxed)
}
pub(crate) fn is_polling(key: u64) -> bool {
POLLING.with(|polling| polling.borrow().contains(&key))
}
#[cfg_attr(
not(any(
feature = "boa",
feature = "quickjs",
feature = "v8",
feature = "fuzzing",
test
)),
expect(dead_code, reason = "only the host bridge polls host futures")
)]
pub(crate) fn enter(key: u64) -> Polling {
POLLING.with(|polling| polling.borrow_mut().push(key));
Polling { key }
}
pub(crate) struct Polling {
key: u64,
}
impl Drop for Polling {
fn drop(&mut self) {
POLLING.with(|polling| {
let mut polling = polling.borrow_mut();
if let Some(index) = polling.iter().rposition(|key| *key == self.key) {
polling.remove(index);
}
});
}
}