use std::{
num::NonZeroUsize,
thread::{self, JoinHandle, Thread},
};
use crate::{Clock, Duty, Waiter};
use super::{
GroupControl, ReactorGroupExit, ReactorGroupHandle, ReactorGroupLimits,
ReactorGroupTermination, ReactorId, Supervised,
};
#[must_use = "dropping the group detaches observation and does not terminate its reactors"]
pub struct ReactorGroup<D, C, W, T>
where
D: Duty,
C: Clock,
W: Waiter<D>,
{
pub(super) limits: ReactorGroupLimits,
pub(super) reactors: NonZeroUsize,
pub(super) ingress: ReactorGroupHandle<T>,
pub(super) control: GroupControl<T>,
pub(super) threads: Box<[Thread]>,
pub(super) supervisor: JoinHandle<Supervised<D, C, W>>,
}
impl<D, C, W, T> ReactorGroup<D, C, W, T>
where
D: Duty,
C: Clock,
W: Waiter<D>,
{
pub const fn limits(&self) -> ReactorGroupLimits {
self.limits
}
pub const fn reactors(&self) -> NonZeroUsize {
self.reactors
}
pub fn handle(&self) -> ReactorGroupHandle<T> {
self.ingress.clone()
}
pub fn request_termination(&self) -> ReactorGroupTermination {
self.control.request_termination()
}
pub fn reactor_thread(&self, reactor: ReactorId) -> Option<&Thread> {
reactor
.position()
.and_then(|position| self.threads.get(position))
}
pub fn is_finished(&self) -> bool {
self.supervisor.is_finished()
}
pub fn join(self) -> thread::Result<ReactorGroupExit<D, C, W>> {
self.supervisor.join().map(ReactorGroupExit::new)
}
}
impl<D, C, W, T> core::fmt::Debug for ReactorGroup<D, C, W, T>
where
D: Duty,
C: Clock,
W: Waiter<D>,
{
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter
.debug_struct("ReactorGroup")
.field("limits", &self.limits)
.field("reactors", &self.reactors)
.field("ingress", &self.ingress)
.field("finished", &self.is_finished())
.finish_non_exhaustive()
}
}