mirror of
https://github.com/erg-lang/erg.git
synced 2025-07-07 21:25:31 +00:00
52 lines
1.4 KiB
Rust
52 lines
1.4 KiB
Rust
#[cfg(all(unix, any(feature = "debug", feature = "backtrace")))]
|
|
pub use backtrace_on_stack_overflow;
|
|
use std::thread::{self, JoinHandle};
|
|
|
|
const STACK_SIZE: usize = if cfg!(feature = "large_thread") {
|
|
8 * 1024 * 1024
|
|
} else {
|
|
4 * 1024 * 1024
|
|
};
|
|
|
|
#[macro_export]
|
|
macro_rules! enable_overflow_stacktrace {
|
|
() => {
|
|
#[cfg(all(unix, any(feature = "debug", feature = "backtrace")))]
|
|
unsafe {
|
|
$crate::spawn::backtrace_on_stack_overflow::enable()
|
|
};
|
|
};
|
|
}
|
|
|
|
/// Execute the function in a new thread.
|
|
/// The default stack size is 4MB, and with the `large_thread` flag, the stack size is 8MB.
|
|
pub fn exec_new_thread<F, T>(run: F, name: &str) -> T
|
|
where
|
|
F: FnOnce() -> T + Send + 'static,
|
|
T: Send + 'static,
|
|
{
|
|
enable_overflow_stacktrace!();
|
|
let child = thread::Builder::new()
|
|
.name(name.to_string())
|
|
.stack_size(STACK_SIZE)
|
|
.spawn(run)
|
|
.unwrap();
|
|
// Wait for thread to join
|
|
child.join().unwrap_or_else(|err| {
|
|
eprintln!("Thread panicked: {err:?}");
|
|
std::process::exit(1);
|
|
})
|
|
}
|
|
|
|
pub fn spawn_new_thread<F, T>(run: F, name: &str) -> JoinHandle<T>
|
|
where
|
|
F: FnOnce() -> T + Send + 'static,
|
|
T: Send + 'static,
|
|
{
|
|
enable_overflow_stacktrace!();
|
|
thread::Builder::new()
|
|
.name(name.to_string())
|
|
.stack_size(STACK_SIZE)
|
|
.spawn(run)
|
|
.unwrap()
|
|
}
|