提交 95caa6dc authored 作者: Serhij S's avatar Serhij S

mock all methods on non-linux platforms

上级 f0f993eb
...@@ -7,16 +7,18 @@ use std::{ ...@@ -7,16 +7,18 @@ use std::{
time::Duration, time::Duration,
}; };
#[cfg(target_os = "linux")]
use crate::suicide;
use crate::{ use crate::{
critical, critical,
hub::Hub, hub::Hub,
suicide,
supervisor::Supervisor, supervisor::Supervisor,
thread_rt::{Builder, RTParams, Scheduling}, thread_rt::{Builder, RTParams, Scheduling},
Error, Result, Error, Result,
}; };
pub use roboplc_derive::WorkerOpts; pub use roboplc_derive::WorkerOpts;
use rtsc::data_policy::DataDeliveryPolicy; use rtsc::data_policy::DataDeliveryPolicy;
#[cfg(target_os = "linux")]
use signal_hook::{ use signal_hook::{
consts::{SIGINT, SIGTERM}, consts::{SIGINT, SIGTERM},
iterator::Signals, iterator::Signals,
...@@ -203,7 +205,7 @@ where ...@@ -203,7 +205,7 @@ where
pub fn register_signals_with_shutdown_handler<H>( pub fn register_signals_with_shutdown_handler<H>(
&mut self, &mut self,
handle_fn: H, handle_fn: H,
shutdown_timeout: Duration, #[allow(unused_variables)] shutdown_timeout: Duration,
) -> Result<()> ) -> Result<()>
where where
H: Fn(&Context<D, V>) + Send + Sync + 'static, H: Fn(&Context<D, V>) + Send + Sync + 'static,
...@@ -218,22 +220,30 @@ where ...@@ -218,22 +220,30 @@ where
builder.park_on_errors = true; builder.park_on_errors = true;
macro_rules! sig_handler { macro_rules! sig_handler {
($handler: expr) => {{ ($handler: expr) => {{
let context = self.context(); #[cfg(target_os = "linux")]
let mut signals = Signals::new([SIGTERM, SIGINT])?; {
move || { let context = self.context();
if let Some(sig) = signals.forever().next() { let mut signals = Signals::new([SIGTERM, SIGINT])?;
match sig { move || {
SIGTERM | SIGINT => { if let Some(sig) = signals.forever().next() {
suicide(shutdown_timeout, true); match sig {
$handler(&context); SIGTERM | SIGINT => {
context.terminate(); suicide(shutdown_timeout, true);
$handler(&context);
context.terminate();
}
_ => unreachable!(),
} }
_ => unreachable!(),
} }
} }
} }
#[cfg(not(target_os = "linux"))]
{
move || {}
}
}}; }};
} }
#[allow(unused_variables)]
let h = handler.clone(); let h = handler.clone();
if let Err(e) = self.supervisor.spawn(builder.clone(), sig_handler!(h)) { if let Err(e) = self.supervisor.spawn(builder.clone(), sig_handler!(h)) {
if !matches!(e, Error::RTSchedSetSchduler(_)) { if !matches!(e, Error::RTSchedSetSchduler(_)) {
......
...@@ -6,7 +6,6 @@ use std::panic::PanicInfo; ...@@ -6,7 +6,6 @@ use std::panic::PanicInfo;
use std::{env, sync::Arc, time::Duration}; use std::{env, sync::Arc, time::Duration};
use colored::Colorize as _; use colored::Colorize as _;
#[cfg(target_os = "linux")]
use thread_rt::{RTParams, Scheduling}; use thread_rt::{RTParams, Scheduling};
pub use log::LevelFilter; pub use log::LevelFilter;
...@@ -90,7 +89,6 @@ pub use rtsc::data_policy::{DataDeliveryPolicy, DeliveryPolicy}; ...@@ -90,7 +89,6 @@ pub use rtsc::data_policy::{DataDeliveryPolicy, DeliveryPolicy};
/// Reliable TCP/Serial communications /// Reliable TCP/Serial communications
pub mod comm; pub mod comm;
/// Controller and workers /// Controller and workers
#[cfg(target_os = "linux")]
pub mod controller; pub mod controller;
/// In-process data communication pub/sub hub, synchronous edition /// In-process data communication pub/sub hub, synchronous edition
pub mod hub; pub mod hub;
...@@ -100,10 +98,8 @@ pub mod hub_async; ...@@ -100,10 +98,8 @@ pub mod hub_async;
/// I/O /// I/O
pub mod io; pub mod io;
/// Task supervisor to manage real-time threads /// Task supervisor to manage real-time threads
#[cfg(target_os = "linux")]
pub mod supervisor; pub mod supervisor;
/// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone /// Real-time thread functions to work with [`supervisor::Supervisor`] and standalone, Linux only
#[cfg(target_os = "linux")]
pub mod thread_rt; pub mod thread_rt;
/// The crate result type /// The crate result type
...@@ -245,7 +241,6 @@ impl Error { ...@@ -245,7 +241,6 @@ impl Error {
} }
/// Immediately kills the current process and all its subprocesses with a message to stderr /// Immediately kills the current process and all its subprocesses with a message to stderr
#[cfg(target_os = "linux")]
pub fn critical(msg: &str) -> ! { pub fn critical(msg: &str) -> ! {
eprintln!("{}", msg.red().bold()); eprintln!("{}", msg.red().bold());
thread_rt::suicide_myself(Duration::from_secs(0), false); thread_rt::suicide_myself(Duration::from_secs(0), false);
...@@ -257,7 +252,6 @@ pub fn critical(msg: &str) -> ! { ...@@ -257,7 +252,6 @@ pub fn critical(msg: &str) -> ! {
/// period of time. /// period of time.
/// ///
/// Prints warnings to STDOUT if warn is true /// Prints warnings to STDOUT if warn is true
#[cfg(target_os = "linux")]
pub fn suicide(delay: Duration, warn: bool) { pub fn suicide(delay: Duration, warn: bool) {
let mut builder = thread_rt::Builder::new().name("suicide").rt_params( let mut builder = thread_rt::Builder::new().name("suicide").rt_params(
RTParams::new() RTParams::new()
...@@ -336,14 +330,12 @@ pub fn metrics_exporter_install( ...@@ -336,14 +330,12 @@ pub fn metrics_exporter_install(
/// Sets panic handler to immediately kill the process and its childs with SIGKILL. The process is /// Sets panic handler to immediately kill the process and its childs with SIGKILL. The process is
/// killed when panic happens in ANY thread /// killed when panic happens in ANY thread
#[cfg(target_os = "linux")]
pub fn setup_panic() { pub fn setup_panic() {
std::panic::set_hook(Box::new(move |info: &PanicInfo| { std::panic::set_hook(Box::new(move |info: &PanicInfo| {
panic(info); panic(info);
})); }));
} }
#[cfg(target_os = "linux")]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
eprintln!("{}", info.to_string().red().bold()); eprintln!("{}", info.to_string().red().bold());
thread_rt::suicide_myself(Duration::from_secs(0), false); thread_rt::suicide_myself(Duration::from_secs(0), false);
...@@ -372,13 +364,10 @@ pub fn configure_logger(filter: LevelFilter) { ...@@ -372,13 +364,10 @@ pub fn configure_logger(filter: LevelFilter) {
/// Prelude module /// Prelude module
pub mod prelude { pub mod prelude {
#[cfg(target_os = "linux")]
pub use super::suicide; pub use super::suicide;
#[cfg(target_os = "linux")]
pub use crate::controller::*; pub use crate::controller::*;
pub use crate::hub::prelude::*; pub use crate::hub::prelude::*;
pub use crate::io::prelude::*; pub use crate::io::prelude::*;
#[cfg(target_os = "linux")]
pub use crate::supervisor::prelude::*; pub use crate::supervisor::prelude::*;
pub use crate::time::DurationRT; pub use crate::time::DurationRT;
pub use bma_ts::{Monotonic, Timestamp}; pub use bma_ts::{Monotonic, Timestamp};
......
...@@ -29,8 +29,12 @@ impl<T> Default for Supervisor<T> { ...@@ -29,8 +29,12 @@ impl<T> Default for Supervisor<T> {
macro_rules! vacant_entry { macro_rules! vacant_entry {
($self:ident, $builder:ident) => {{ ($self:ident, $builder:ident) => {{
let Some(name) = $builder.name.clone() else { return Err(Error::SupervisorNameNotSpecified); }; let Some(name) = $builder.name.clone() else {
let btree_map::Entry::Vacant(entry) = $self.tasks.entry(name.clone()) else { return Err(Error::SupervisorDuplicateTask(name)); }; return Err(Error::SupervisorNameNotSpecified);
};
let btree_map::Entry::Vacant(entry) = $self.tasks.entry(name.clone()) else {
return Err(Error::SupervisorDuplicateTask(name));
};
entry entry
}}; }};
} }
......
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论