use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant};
pub const DEADLINE: Duration = Duration::from_secs(4);
static BEGAN: OnceLock<(Instant, &'static str)> = OnceLock::new();
static STEPS: Mutex<Vec<(&'static str, Instant, Option<Duration>)>> = Mutex::new(Vec::new());
static HIDDEN: Mutex<usize> = Mutex::new(0);
static ENDING: AtomicBool = AtomicBool::new(false);
pub(crate) fn is_closing() -> bool {
BEGAN.get().is_some()
}
pub fn begin(why: &'static str) {
if BEGAN.set((Instant::now(), why)).is_err() {
return;
}
let hidden = hide_windows();
if let Ok(mut h) = HIDDEN.lock() {
*h = hidden;
}
sk_desktop::launch::closing();
sk_tree::log::record(
"shutdown",
format!("[shutdown] {why}: hid {hidden} window(s); tearing down"),
);
let _ = std::thread::Builder::new()
.name("sk-shutdown-deadline".into())
.spawn(|| {
std::thread::sleep(DEADLINE);
if ENDING.swap(true, Ordering::SeqCst) {
return;
}
if crate::embed::active() {
let line = timeline();
eprintln!(
"[shutdown] the teardown passed its {} s deadline. {line}",
DEADLINE.as_secs()
);
crate::embed::finished(false, line);
return;
}
eprintln!(
"[shutdown] the teardown passed its {} s deadline; ending the process. {}",
DEADLINE.as_secs(),
timeline()
);
terminate();
});
}
pub fn step<T>(name: &'static str, f: impl FnOnce() -> T) -> T {
let t = Instant::now();
let index = STEPS.lock().map(|mut s| {
s.push((name, t, None));
s.len() - 1
});
let out = f();
if let (Ok(i), Ok(mut s)) = (index, STEPS.lock()) {
s[i].2 = Some(t.elapsed());
}
out
}
pub fn finish() -> ! {
begin("quit");
if ENDING.swap(true, Ordering::SeqCst) {
loop {
std::thread::park();
}
}
eprintln!("[shutdown] closed. {}", timeline());
if crate::embed::active() {
crate::embed::finished(true, timeline());
loop {
std::thread::park();
}
}
terminate()
}
pub fn install_test_delay(cx: &mut gpui::App) {
let Some(ms) = std::env::var("SK_SHUTDOWN_DELAY_MS")
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
else {
return;
};
std::mem::forget(cx.on_app_quit(move |_| {
step("SK_SHUTDOWN_DELAY_MS", || {
std::thread::sleep(Duration::from_millis(ms))
});
std::future::ready(())
}));
}
fn timeline() -> String {
let Some((t0, why)) = BEGAN.get() else {
return String::new();
};
let hidden = HIDDEN.lock().map(|h| *h).unwrap_or(0);
let steps = STEPS
.lock()
.map(|s| {
s.iter()
.map(|(n, began, took)| match took {
Some(d) => format!("{n} {} ms", d.as_millis()),
None => format!("{n} still running after {} ms", began.elapsed().as_millis()),
})
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
format!(
"{why}: hid {hidden} window(s); quit hooks: {}; ended at +{} ms",
if steps.is_empty() { "none ran" } else { &steps },
t0.elapsed().as_millis()
)
}
#[cfg(target_os = "windows")]
fn hide_windows() -> usize {
use windows::core::BOOL;
use windows::Win32::Foundation::{HWND, LPARAM};
use windows::Win32::System::Threading::GetCurrentThreadId;
use windows::Win32::UI::WindowsAndMessaging::{
EnumThreadWindows, IsWindowVisible, ShowWindow, SW_HIDE,
};
unsafe extern "system" fn hide(hwnd: HWND, count: LPARAM) -> BOOL {
unsafe {
if IsWindowVisible(hwnd).as_bool() {
let _ = ShowWindow(hwnd, SW_HIDE);
*(count.0 as *mut usize) += 1;
}
}
true.into()
}
let mut count = 0usize;
unsafe {
let _ = EnumThreadWindows(
GetCurrentThreadId(),
Some(hide),
LPARAM(&mut count as *mut usize as isize),
);
}
count
}
#[cfg(not(target_os = "windows"))]
fn hide_windows() -> usize {
0
}
fn terminate() -> ! {
#[cfg(target_os = "windows")]
unsafe {
use windows::Win32::System::Threading::{GetCurrentProcess, TerminateProcess};
let _ = TerminateProcess(GetCurrentProcess(), 0);
}
std::process::exit(0)
}