upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index f01997775..eb04c4f34 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -231,15 +231,36 @@ int usage() {
231// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it 231// The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it
232// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles). 232// here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles).
233constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s 233constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s
234// For a build still running on a title thread, then the session cache write (rt::d3d12::save_pipeline_caches_at_exit).
235constexpr unsigned kExitCacheWaitMs = 1000;
234 236
235static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) { 237static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) {
236 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) { 238 if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) {
237 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer 239 rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer
238 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); 240 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
241 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
239 } 242 }
240 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before 243 return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before
241} 244}
242 245
246// A run that ends in an exception nothing handles (a fault in the title or one of its DLLs, the int3 after a device
247// loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again. The device is
248// left alone apart from that write; the exception then goes on as before (the previous filter, or Windows' own
249// handling), so the exit code is the exception's. Not for a stack overflow: no stack is left to write with.
250static LPTOP_LEVEL_EXCEPTION_FILTER previous_fatal_filter = nullptr;
251
252static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) {
253 const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0;
254 if (code != EXCEPTION_STACK_OVERFLOW) {
255 std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the "
256 "process ends\n", static_cast<unsigned long>(code));
257 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
258 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
259 std::fflush(stderr);
260 }
261 return previous_fatal_filter ? previous_fatal_filter(info) : EXCEPTION_CONTINUE_SEARCH;
262}
263
243static BOOL WINAPI show_package_window(HWND window, int command) { 264static BOOL WINAPI show_package_window(HWND window, int command) {
244 const BOOL previous = ShowWindow(window, command); 265 const BOOL previous = ShowWindow(window, command);
245 const DWORD error = GetLastError(); 266 const DWORD error = GetLastError();
@@ -516,6 +537,7 @@ int wmain(int argc, wchar_t** argv) {
516 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) 537 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic)
517#ifdef LEONIDA_VULKAN_DEVICE 538#ifdef LEONIDA_VULKAN_DEVICE
518 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); 539 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE);
540 previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception);
519#endif 541#endif
520 int code; 542 int code;
521 { 543 {
@@ -536,6 +558,7 @@ int wmain(int argc, wchar_t** argv) {
536 rt::d3d12_xs::stop_list_values(500); 558 rt::d3d12_xs::stop_list_values(500);
537#ifdef LEONIDA_VULKAN_DEVICE 559#ifdef LEONIDA_VULKAN_DEVICE
538 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); 560 rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs);
561 rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs);
539#endif 562#endif
540 rt::cleanup_tls_clones(); 563 rt::cleanup_tls_clones();
541 ExitProcess(static_cast<UINT>(code)); 564 ExitProcess(static_cast<UINT>(code));