use pi_async_runtime_core::task::TaskQuery;
use pi_async_task::definitions::TaskId;
use std::{
cell::UnsafeCell,
collections::HashMap,
marker::{PhantomData, PhantomPinned},
ptr::NonNull,
rc::Rc,
sync::{
atomic::{AtomicPtr, AtomicUsize},
Arc,
},
};
pub struct TaskRegistryHandle {
shared: Arc<RegistryShared>,
}
pub(crate) struct TaskRegistry {
shared: Arc<RegistryShared>,
index: HashMap<TaskId, NonNull<RegistryNode>>,
pending_index: *mut RegistryNode,
index_first: bool,
retired: *mut RegistryNode,
cursor: *mut RegistryNode,
previous: *mut RegistryNode,
local: PhantomData<Rc<()>>,
}
struct RegistryShared {
head: AtomicPtr<RegistryNode>,
incoming: AtomicPtr<RegistryNode>,
participants: AtomicUsize,
final_retired: UnsafeCell<*mut RegistryNode>,
}
struct RegistryNode {
query: TaskQuery,
next: AtomicPtr<RegistryNode>,
incoming_next: UnsafeCell<*mut RegistryNode>,
retired_next: UnsafeCell<*mut RegistryNode>,
indexed: UnsafeCell<bool>,
pin: PhantomPinned,
}
struct RegistryReadGuard<'a> {
shared: &'a RegistryShared,
}
struct RegistryReclaimGuard {
remaining: *mut RegistryNode,
}
struct RegistryIndexGuard<'a> {
shared: &'a RegistryShared,
remaining: *mut RegistryNode,
}
const REGISTRY_SEALED: usize = 1usize << (usize::BITS - 1);
const REGISTRY_READERS: usize = REGISTRY_SEALED - 1;
unsafe impl Send for RegistryShared {}
unsafe impl Sync for RegistryShared {}
impl TaskRegistry {
pub(crate) fn new() -> Self {
registry_new_inner()
}
pub(crate) fn handle(&self) -> TaskRegistryHandle {
registry_handle_inner(self)
}
pub(crate) fn refresh_index(&mut self) {
registry_index_inner(self)
}
pub(crate) fn query(&mut self, id: TaskId) -> Option<&TaskQuery> {
registry_query_inner(self, id)
}
pub(crate) fn maintain(&mut self, max_nodes: usize) {
registry_maintain_inner(self, max_nodes)
}
pub(crate) fn unconfirmed_counts(
&self,
owner: pi_async_task::definitions::TaskOwnerId,
) -> (usize, usize) {
registry_unconfirmed_inner(self, owner)
}
}
unsafe fn registry_index_record(
index: &mut HashMap<TaskId, NonNull<RegistryNode>>,
node: NonNull<RegistryNode>,
) -> *mut RegistryNode {
unsafe { registry_index_record_inner(index, node) }
}
impl Drop for TaskRegistry {
fn drop(&mut self) {
registry_owner_drop_inner(self)
}
}
impl Clone for TaskRegistryHandle {
fn clone(&self) -> Self {
registry_clone_inner(self)
}
}
impl TaskRegistryHandle {
pub(crate) fn register(&self, query: TaskQuery) {
registry_register_inner(self, query)
}
pub fn waiting_for_wake_count(&self) -> usize {
registry_waiting_inner(self)
}
pub fn pending_cancel_count(&self) -> usize {
registry_cancelling_inner(self)
}
}
impl RegistryShared {
fn enter(&self) -> Option<RegistryReadGuard<'_>> {
registry_enter_inner(self)
}
fn collect_final(&self) {
registry_final_inner(self)
}
}
impl Drop for RegistryReadGuard<'_> {
fn drop(&mut self) {
registry_read_drop_inner(self)
}
}
impl Drop for RegistryReclaimGuard {
fn drop(&mut self) {
registry_reclaim_drop_inner(self)
}
}
impl Drop for RegistryIndexGuard<'_> {
fn drop(&mut self) {
registry_index_drop_inner(self)
}
}
include!("registry_inner.rs");
include!("member_query_registration.rs");