1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
pub mod backend;
mod buffer;
pub mod bus;
pub mod collections;
mod controller;
mod task;
mod trace;
mod worker;

pub use buffer::*;
pub use controller::Controller;
pub use task::{
    group::TaskGroup,
    switcher::{TaskSwitcher, TaskSwitcherBranch, TaskSwitcherChild},
    Task,
};
pub use trace::*;
pub use worker::{
    BusChannelControl, BusControl, BusEvent, WorkerInner, WorkerInnerInput, WorkerInnerOutput,
    WorkerStats,
};

#[macro_export]
macro_rules! return_if_none {
    ($option:expr) => {
        match $option {
            Some(val) => val,
            None => return,
        }
    };
}

#[macro_export]
macro_rules! return_if_some {
    ($option:expr) => {
        let out = $option;
        if out.is_some() {
            return out;
        }
    };
}

#[macro_export]
macro_rules! return_if_err {
    ($option:expr) => {
        match $option {
            Ok(val) => val,
            Err(_) => return,
        }
    };
}

#[macro_export]
macro_rules! group_owner_type {
    ($name:ident) => {
        #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
        pub struct $name(usize);

        #[allow(dead_code)]
        impl $name {
            fn build(index: usize) -> Self {
                Self(index)
            }
            pub fn index(&self) -> usize {
                self.0
            }
        }

        impl From<usize> for $name {
            fn from(value: usize) -> Self {
                Self(value)
            }
        }
    };
}

#[macro_export]
macro_rules! group_task {
    ($name:ident, $task:ty, $input:ty, $output:ty) => {
        pub struct $name {
            tasks: Vec<Option<$task>>,
            switcher: TaskSwitcher,
        }

        impl Default for $name {
            fn default() -> Self {
                Self {
                    tasks: Vec::new(),
                    switcher: TaskSwitcher::new(0),
                }
            }
        }

        impl $name {
            /// Returns the number of tasks in the group.
            pub fn tasks(&self) -> usize {
                //count all task which not None
                let tasks = self.tasks.iter().filter(|x| x.is_some()).count();
                tasks
            }

            /// Check if we have task with index
            pub fn has_task(&self, index: usize) -> bool {
                matches!(self.tasks.get(index), Some(Some(_)))
            }

            /// Adds a task to the group.
            pub fn add_task(&mut self, task: $task) -> usize {
                for (index, slot) in self.tasks.iter_mut().enumerate() {
                    if slot.is_none() {
                        *slot = Some(task);
                        return index;
                    }
                }

                self.tasks.push(Some(task));
                self.switcher.set_tasks(self.tasks.len());
                self.tasks.len() - 1
            }

            /// Remove a task from the group
            pub fn remove_task(&mut self, index: usize) {
                self.tasks
                    .get_mut(index)
                    .expect("Should have task when remove")
                    .take();
                while let Some(None) = self.tasks.last() {
                    self.tasks.pop();
                }
                self.switcher.set_tasks(self.tasks.len());
            }

            pub fn on_tick(&mut self, now: std::time::Instant) {
                self.switcher.flag_all();
                for index in 0..self.switcher.tasks() {
                    if let Some(Some(task)) = self.tasks.get_mut(index) {
                        task.on_tick(now);
                    }
                }
            }

            pub fn on_event<'a>(&mut self, now: std::time::Instant, index: usize, input: $input) {
                if let Some(Some(task)) = self.tasks.get_mut(index) {
                    self.switcher.flag_task(index);
                    task.on_event(now, input);
                }
            }

            pub fn pop_output<'a>(&mut self, now: std::time::Instant) -> Option<(usize, $output)> {
                while let Some(index) = self.switcher.current() {
                    let slot = self.tasks.get_mut(index);
                    if let Some(Some(slot)) = slot {
                        if let Some(out) = slot.pop_output(now) {
                            return Some((index, out));
                        } else {
                            self.switcher.finished(index);
                        }
                    } else {
                        self.switcher.finished(index);
                    }
                }
                None
            }

            /// Gracefully destroys the task group.
            pub fn on_shutdown(&mut self, now: std::time::Instant) {
                self.switcher.flag_all();
                for index in 0..self.switcher.tasks() {
                    log::info!("Group kill tasks {}/{}", index, self.switcher.tasks());
                    if let Some(Some(task)) = self.tasks.get_mut(index) {
                        task.on_shutdown(now);
                    }
                }
            }
        }
    };
}