const WINDOWS_CLI_PARSE_STACK_FLOOR_BYTES: usize = 16 * 1024 * 1024;
pub const CLI_THREAD_STACK_BYTES: usize = WINDOWS_CLI_PARSE_STACK_FLOOR_BYTES;
const _: () = assert!(CLI_THREAD_STACK_BYTES >= WINDOWS_CLI_PARSE_STACK_FLOOR_BYTES);
pub fn run_on_cli_stack<F, T>(name: &'static str, f: F) -> T
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
std::thread::Builder::new()
.name(name.into())
.stack_size(CLI_THREAD_STACK_BYTES)
.spawn(f)
.unwrap_or_else(|e| {
panic!(
"spawn {name} thread with {} MiB stack: {e}",
CLI_THREAD_STACK_BYTES / 1024 / 1024
)
})
.join()
.unwrap_or_else(|payload| std::panic::resume_unwind(payload))
}
pub fn build_cli_runtime() -> anyhow::Result<tokio::runtime::Runtime> {
Ok(tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_stack_size(CLI_THREAD_STACK_BYTES)
.build()?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn run_on_cli_stack_returns_the_worker_value() {
let got = run_on_cli_stack("newt-stack-test", || 42);
assert_eq!(got, 42);
}
}