bronzeflow_core/executor/
mod.rs

1#[cfg(feature = "async_tokio")]
2pub mod tokio_executor;
3
4use crate::prelude::{BronzeRuntime, TriggerCaller};
5use crate::runtime::{Runnable, ThreadRuntime};
6use bronzeflow_utils::debug;
7
8pub trait Executor: TriggerCaller {
9    fn submit(&self, runnable: &mut impl Runnable, report_msg: bool);
10
11    fn submit_safe<F>(&self, mut runnable: F, report_msg: bool)
12    where
13        F: Runnable + Send + Sync + 'static,
14    {
15        self.submit(&mut runnable, report_msg)
16    }
17
18    #[inline(always)]
19    fn support_async(&self) -> bool {
20        false
21    }
22
23    #[inline(always)]
24    fn check_async_support(&self, runnable: &impl Runnable) {
25        if runnable.is_async() && !self.support_async() {
26            panic!("Current executor not supported async runnable");
27        }
28    }
29}
30
31impl<T: Executor> TriggerCaller for T {
32    #[inline(always)]
33    fn trigger(&self, runnable: &mut impl Runnable, report_msg: bool) {
34        self.check_async_support(runnable);
35        debug!(
36            "Begin run {}, {:?}",
37            runnable.run_type_name(),
38            runnable.run_type_id()
39        );
40        self.submit(runnable, report_msg);
41    }
42
43    #[inline(always)]
44    fn trigger_safe<F>(&self, runnable: F, report_msg: bool)
45    where
46        F: Runnable + Send + Sync + 'static,
47    {
48        debug!(
49            "Begin run {}, {:?}",
50            runnable.run_type_name(),
51            runnable.run_type_id()
52        );
53        self.check_async_support(&runnable);
54        self.submit_safe(runnable, report_msg);
55    }
56}
57
58#[derive(Default)]
59pub struct DefaultExecutor {}
60
61impl DefaultExecutor {
62    pub fn new() -> Self {
63        DefaultExecutor {}
64    }
65}
66
67impl Executor for DefaultExecutor {
68    #[inline(always)]
69    fn submit(&self, runnable: &mut impl Runnable, _: bool) {
70        runnable.run();
71    }
72}
73
74#[derive(Default)]
75pub struct ThreadExecutor {
76    runtime: ThreadRuntime,
77}
78
79impl Executor for ThreadExecutor {
80    fn submit(&self, _: &mut impl Runnable, _: bool) {
81        // self.runtime.run(runnable, report_msg);
82        panic!("Not supported submit in `ThreadExecutor`")
83    }
84
85    #[inline(always)]
86    fn submit_safe<F>(&self, runnable: F, report_msg: bool)
87    where
88        F: Runnable + Send + Sync + 'static,
89    {
90        self.runtime.run_safe(runnable, report_msg);
91    }
92}