Skip to content

Commit

Permalink
Handle exceptions thrown by call_in_main_thread in the caller thread
Browse files Browse the repository at this point in the history
Also removes an unnecessary ctor and adds docs.
  • Loading branch information
Vultraz committed May 27, 2018
1 parent 183bc27 commit 8679265
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/events.cpp
Expand Up @@ -43,18 +43,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;
}
};
Expand Down Expand Up @@ -752,7 +759,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);
Expand All @@ -765,6 +772,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

0 comments on commit 8679265

Please sign in to comment.