use core::{
pin::Pin,
task::{Context, Poll},
};
use crate::{
core_impl::{WaitGroupUtil, WaitGroupWrapper},
state::SharedWgInner,
twin_ref::{ClonableTwinRef, TwinRef},
};
#[cfg(feature = "compact-mono")]
type MonoInner = crate::state::MonoWgInner;
#[cfg(not(feature = "compact-mono"))]
type MonoInner = crate::state::SharedWgInner;
#[must_use]
#[derive(Debug)]
pub struct WaitGroup(WaitGroupWrapper<TwinRef<SharedWgInner>>);
#[must_use]
#[derive(Debug)]
pub struct MonoWaitGroup(WaitGroupWrapper<TwinRef<MonoInner>>);
#[must_use]
#[derive(Clone, Debug)]
pub struct WorkerHandle {
_handle: ClonableTwinRef<SharedWgInner>,
}
#[must_use]
#[derive(Debug)]
pub struct MonoWorkerHandle(TwinRef<MonoInner>);
impl WaitGroup {
pub fn new() -> (Self, WorkerHandle) {
let inner = SharedWgInner::new();
let (wg, handle) = TwinRef::new_clonable(inner);
(
Self(WaitGroupWrapper::new(wg)),
WorkerHandle { _handle: handle },
)
}
#[inline]
pub fn is_done(&self) -> bool {
self.0.is_done()
}
}
impl MonoWaitGroup {
pub fn new() -> (Self, MonoWorkerHandle) {
let inner = MonoInner::new();
let (wg, handle) = TwinRef::new_mono(inner);
(Self(WaitGroupWrapper::new(wg)), MonoWorkerHandle(handle))
}
#[inline]
pub fn is_done(&self) -> bool {
self.0.is_done()
}
}
impl Future for WaitGroup {
type Output = ();
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx)
}
}
impl Future for MonoWaitGroup {
type Output = ();
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx)
}
}
#[cfg(feature = "futures-core")]
impl futures_core::FusedFuture for WaitGroup {
#[inline]
fn is_terminated(&self) -> bool {
self.0.is_terminated()
}
}
#[cfg(feature = "futures-core")]
impl futures_core::FusedFuture for MonoWaitGroup {
#[inline]
fn is_terminated(&self) -> bool {
self.0.is_terminated()
}
}
impl WorkerHandle {
#[inline]
pub fn done(self) {
drop(self);
}
}
impl MonoWorkerHandle {
#[inline]
pub fn done(self) {
drop(self);
}
}
impl Drop for MonoWorkerHandle {
#[inline]
fn drop(&mut self) {
unsafe {
self.0.send_done();
}
}
}