From 86792656aed349b6ddc67e66b98abdee5fb310e2 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Sun, 27 May 2018 17:35:14 +1100 Subject: [PATCH] Handle exceptions thrown by call_in_main_thread in the caller thread Also removes an unnecessary ctor and adds docs. --- src/events.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/events.cpp b/src/events.cpp index 4c93401bab1b..4efdc605a7fb 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -43,18 +43,25 @@ namespace { struct invoked_function_data { - bool finished; + /** The actual function to call. */ const std::function& f; - invoked_function_data(bool finished_, const std::function& func) - : finished(finished_) - , f(func) - { - } + /** Whether execution in the main thread is complete. */ + bool finished = false; + + /** Stores any exception thrown during the execution of @ref f. */ + std::exception_ptr thrown_exception; void call() { - f(); + try { + f(); + } catch(const CVideo::quit&) { + // Handle this exception in the main thread. + } catch(...) { + thrown_exception = std::current_exception(); + } + finished = true; } }; @@ -752,7 +759,7 @@ void call_in_main_thread(const std::function& f) return; } - invoked_function_data fdata{false, f}; + invoked_function_data fdata{f}; SDL_Event sdl_event; sdl::UserEvent sdl_userevent(INVOKE_FUNCTION_EVENT, &fdata); @@ -765,6 +772,10 @@ void call_in_main_thread(const std::function& f) while(!fdata.finished) { SDL_Delay(10); } + + if(fdata.thrown_exception) { + std::rethrow_exception(fdata.thrown_exception); + } } } // end events namespace