1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-19 12:12:37 +01:00
actix-net/actix-rt/src/system.rs

132 lines
3.3 KiB
Rust
Raw Normal View History

2021-01-26 10:46:14 +01:00
use std::{
cell::RefCell,
io,
sync::atomic::{AtomicUsize, Ordering},
};
2018-12-10 04:55:40 +01:00
2021-01-29 03:21:06 +01:00
use tokio::sync::mpsc::UnboundedSender;
2018-12-10 04:55:40 +01:00
2021-01-26 10:46:14 +01:00
use crate::{
arbiter::{Arbiter, SystemCommand},
builder::{Builder, SystemRunner},
};
2018-12-10 04:55:40 +01:00
static SYSTEM_COUNT: AtomicUsize = AtomicUsize::new(0);
2018-12-10 04:55:40 +01:00
/// System is a runtime manager.
#[derive(Clone, Debug)]
pub struct System {
id: usize,
2021-01-29 03:21:06 +01:00
tx: UnboundedSender<SystemCommand>,
2018-12-10 04:55:40 +01:00
arbiter: Arbiter,
stop_on_panic: bool,
}
thread_local!(
static CURRENT: RefCell<Option<System>> = RefCell::new(None);
);
impl System {
/// Constructs new system and sets it as current
pub(crate) fn construct(
sys: UnboundedSender<SystemCommand>,
arbiter: Arbiter,
stop_on_panic: bool,
) -> Self {
let sys = System {
2021-01-29 03:21:06 +01:00
tx: sys,
2018-12-10 04:55:40 +01:00
arbiter,
stop_on_panic,
id: SYSTEM_COUNT.fetch_add(1, Ordering::SeqCst),
2018-12-10 04:55:40 +01:00
};
System::set_current(sys.clone());
sys
}
/// Build a new system with a customized tokio runtime.
///
2021-01-26 10:46:14 +01:00
/// This allows to customize the runtime. See [`Builder`] for more information.
2018-12-10 04:55:40 +01:00
pub fn builder() -> Builder {
Builder::new()
}
/// Create new system.
///
/// This method panics if it can not create tokio runtime
2021-01-26 10:46:14 +01:00
#[allow(clippy::new_ret_no_self)]
2021-01-29 03:21:06 +01:00
pub fn new(name: impl Into<String>) -> SystemRunner {
2018-12-10 04:55:40 +01:00
Self::builder().name(name).build()
}
/// Get current running system.
pub fn current() -> System {
CURRENT.with(|cell| match *cell.borrow() {
Some(ref sys) => sys.clone(),
None => panic!("System is not running"),
})
}
2021-01-26 10:46:14 +01:00
/// Check if current system has started.
pub fn is_set() -> bool {
2018-12-10 04:55:40 +01:00
CURRENT.with(|cell| cell.borrow().is_some())
}
/// Set current running system.
#[doc(hidden)]
pub fn set_current(sys: System) {
CURRENT.with(|s| {
*s.borrow_mut() = Some(sys);
})
}
/// Execute function with system reference.
pub fn with_current<F, R>(f: F) -> R
where
F: FnOnce(&System) -> R,
{
CURRENT.with(|cell| match *cell.borrow() {
Some(ref sys) => f(sys),
None => panic!("System is not running"),
})
}
2021-01-26 10:46:14 +01:00
/// Numeric system ID.
pub fn id(&self) -> usize {
self.id
}
2021-01-26 10:46:14 +01:00
/// Stop the system (with code 0).
2018-12-10 04:55:40 +01:00
pub fn stop(&self) {
self.stop_with_code(0)
}
/// Stop the system with a particular exit code.
pub fn stop_with_code(&self, code: i32) {
2021-01-29 03:21:06 +01:00
let _ = self.tx.send(SystemCommand::Exit(code));
2018-12-10 04:55:40 +01:00
}
2021-01-29 03:21:06 +01:00
pub(crate) fn tx(&self) -> &UnboundedSender<SystemCommand> {
&self.tx
2018-12-10 04:55:40 +01:00
}
/// Return status of 'stop_on_panic' option which controls whether the System is stopped when an
/// uncaught panic is thrown from a worker thread.
2021-01-26 10:46:14 +01:00
pub(crate) fn stop_on_panic(&self) -> bool {
2018-12-10 04:55:40 +01:00
self.stop_on_panic
}
2021-01-26 10:46:14 +01:00
/// Get shared reference to system arbiter.
2018-12-10 04:55:40 +01:00
pub fn arbiter(&self) -> &Arbiter {
&self.arbiter
}
2021-01-26 10:46:14 +01:00
/// This function will start tokio runtime and will finish once the `System::stop()` message
/// is called. Function `f` is called within tokio runtime context.
pub fn run<F>(f: F) -> io::Result<()>
2018-12-10 04:55:40 +01:00
where
F: FnOnce(),
2018-12-10 04:55:40 +01:00
{
Self::builder().run(f)
}
}