1use super::{sched::ctrl::SubContext, sys::system::SystemId};
2use crate::ds::ManagedConstPtr;
3use std::{any::Any, fmt};
4
5pub mod prelude {
6 pub use super::Work;
7}
8
9pub trait Work {
30 fn unpark(&mut self, cx: ManagedConstPtr<SubContext>) -> bool;
33
34 fn park(&mut self) -> bool;
39
40 fn name(&self) -> &str;
42}
43
44#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
45pub(crate) struct WorkerId {
46 id: u32,
47 group_index: u16,
48 worker_index: u16,
49}
50
51impl WorkerId {
52 const DUMMY: Self = Self {
53 id: u32::MAX,
54 group_index: u16::MAX,
55 worker_index: u16::MAX,
56 };
57
58 pub(crate) const fn new(id: u32, group_index: u16, worker_index: u16) -> Self {
59 Self {
60 id,
61 group_index,
62 worker_index,
63 }
64 }
65
66 pub(crate) const fn dummy() -> Self {
67 Self::DUMMY
68 }
69
70 pub(crate) const fn group_index(&self) -> u16 {
71 self.group_index
72 }
73
74 pub(crate) const fn worker_index(&self) -> u16 {
75 self.worker_index
76 }
77}
78
79pub(crate) enum Message {
80 Handle(WorkerId),
81 Fin(WorkerId, SystemId),
85
86 Aborted(WorkerId, SystemId),
87
88 Panic(PanicMessage),
90}
91
92impl fmt::Debug for Message {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 Self::Handle(wid) => write!(f, "Message::Handle({wid:?})"),
96 Self::Fin(wid, sid) => write!(f, "Message::Fin({wid:?}, {sid:?})"),
97 Self::Aborted(wid, sid) => write!(f, "Message::Aborted({wid:?}, {sid:?})"),
98 Self::Panic(msg) => write!(f, "Message::Panic({msg:?})"),
99 }
100 }
101}
102
103pub(crate) struct PanicMessage {
104 pub(crate) wid: WorkerId,
105 pub(crate) sid: SystemId,
106 pub(crate) payload: Box<dyn Any + Send>,
107 pub(crate) unrecoverable: bool,
108}
109
110impl fmt::Debug for PanicMessage {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 f.debug_struct("PanicMessage")
113 .field("wid", &self.wid)
114 .field("sid", &self.sid)
115 .field("unrecoverable", &self.unrecoverable)
116 .finish_non_exhaustive()
117 }
118}