Handle exceptions thrown by call_in_main_thread in the caller thread

Also removes an unnecessary ctor and adds docs.
This commit is contained in:
Charles Dang 2018-05-27 17:35:14 +11:00
parent 78ba4582a1
commit b06855e604

View file

@ -42,18 +42,25 @@ namespace
{
struct invoked_function_data
{
bool finished;
/** The actual function to call. */
const std::function<void(void)>& f;
invoked_function_data(bool finished_, const std::function<void(void)>& 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;
}
};
@ -748,7 +755,7 @@ void call_in_main_thread(const std::function<void(void)>& 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);
@ -761,6 +768,10 @@ void call_in_main_thread(const std::function<void(void)>& f)
while(!fdata.finished) {
SDL_Delay(10);
}
if(fdata.thrown_exception) {
std::rethrow_exception(fdata.thrown_exception);
}
}
} // end events namespace