Decouple emulator wake pipe from window subsystem

CU
1d ago
+507-99

The self-pipe trick (wake_fd) was previously coupled to the window backend: wake_fd was only
declared under VIRTIOINPUT || VIRTIOGPU, pipe creation required g_window.window_main_loop, and poll/drain/signal-wakeup handling shared the window guards.

When building without SDL2 (ENABLE_SDL=0), the Makefile disables VIRTIOGPU and VIRTIOINPUT while other background threads such as VIRTIOSND and signal handlers still need to unblock poll(-1) in the SMP scheduler. In addition, virtio-snd never notified wake_fd on TX worker completions, and unmasked helper threads spawned by virtio_snd_init() / PortAudio could handle SIGINT/SIGTERM after teardown closed the pipe.

Changes

  • Decouple wake-pipe lifecycle: Make wake_fd[2], pipe setup under vm->n_hart > 1 (both ends non-blocking), signal_wake_fd, semu_close_wake_pipe(), semu_cleanup_backends(), and semu_run() poll/drain paths unconditional while keeping g_window calls under VIRTIOINPUT || VIRTIOGPU.
  • Drain and exit ordering: Drain all queued wake bytes in semu_run(), run emu_tick_peripherals() immediately, and break cleanly on emu->stopped || signal_received.
  • Plain teardown: Close wake_fd with plain close() and reset fields to -1 in semu_close_wake_pipe(), relying on callers running on the emulator thread or after pthread_join().
  • VirtIO-Sound wakeup & signal masking: Expose virtio_snd_set_wake_fd(), write one wake byte from the virtio-snd TX worker when completions or virtio_snd_set_fail() raise InterruptStatus, and block SIGINT/SIGTERM around pthread_create(), Pa_Initialize(), Pa_OpenStream(), and Pa_StartStream().
  • Unit test & CI: Add tests/test_vsnd_wake.c, PA_LDFLAGS / unit-test targets in Makefile, .ci/test-unit.sh, Linux/macOS CI steps in .github/workflows/main.yml, and .gitignore entries for test artifacts.

Summary by cubic

Decouples the emulator wake pipe from the window subsystem so VIRTIOSND and other async devices can wake the SMP scheduler's poll(-1) in headless or ENABLE_SDL=0 builds.

Previously the wake pipe was only created when a window backend was present (g_window.window_main_loop and VIRTIOINPUT/VIRTIOGPU), so builds without SDL2 had no way to unblock the emulator's idle wait from background threads.

  • Creates the wake pipe whenever the SMP scheduler runs (vm->n_hart > 1), marks both ends non-blocking, and drains all queued wake bytes before ticking peripherals.
  • Wires the VIRTIOSND TX worker to write a wake byte on completions and device-fail through virtio_snd_set_wake_fd().
  • Blocks SIGINT/SIGTERM around helper-thread creation (window init, virtio-snd TX worker, PortAudio init/open/start) so spawned threads inherit the mask; signals stay unblocked only while the emulator or window loop runs, and are re-blocked before teardown.
  • Keeps all g_window calls under VIRTIOINPUT || VIRTIOGPU, with teardown through semu_close_wake_pipe() and semu_cleanup_backends().
  • Adds a unit-test Makefile target with a test_vsnd_wake harness, run in Linux and macOS CI.