fn signal_check_background_loop()

in hf_xet/src/runtime.rs [72:102]


fn signal_check_background_loop() {
    const SIGNAL_CHECK_INTERVAL: Duration = Duration::from_millis(250);

    loop {
        std::thread::sleep(SIGNAL_CHECK_INTERVAL);

        let shutdown_runtime = SIGINT_DETECTED.load(Ordering::SeqCst);

        // The keyboard interrupt was raised, so shut down things in a reasonable amount of time and return the runtime
        // to the uninitialized state.
        if shutdown_runtime {
            // Acquire exclusive access to the runtime.  This will only be released once the runtime is shut down,
            // meaining that all the tasks have completed or been cancelled.
            let maybe_runtime = MULTITHREADED_RUNTIME.write().unwrap().take();

            if let Some(ref runtime) = maybe_runtime {
                // See if we have anything going on that needs to be shut down.
                if runtime.external_executor_count() != 0 {
                    eprintln!("Cancellation requested; stopping current tasks.");
                    runtime.perform_sigint_shutdown();
                }
            }

            // Clear the flag; we're good to go now.
            SIGINT_DETECTED.store(false, Ordering::SeqCst);

            // Exits this background thread.
            break;
        }
    }
}