-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <exception> | ||
#include <mutex> | ||
#include <queue> | ||
|
||
namespace mrc { | ||
|
||
/** | ||
* @brief A utility for catching out-of-stack exceptions in a thread-safe manner such that they | ||
* can be checked and throw from a parent thread. | ||
*/ | ||
class ExceptionCatcher | ||
{ | ||
public: | ||
/** | ||
* @brief "catches" an exception to the catcher | ||
*/ | ||
void push_exception(std::exception_ptr ex); | ||
|
||
/** | ||
* @brief checks to see if any exceptions have been "caught" by the catcher. | ||
*/ | ||
bool has_exception(); | ||
|
||
/** | ||
* @brief rethrows the next exception (in the order in which it was "caught"). | ||
*/ | ||
void rethrow_next_exception(); | ||
|
||
private: | ||
std::mutex m_mutex{}; | ||
std::queue<std::exception_ptr> m_exceptions{}; | ||
}; | ||
|
||
} // namespace mrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <mrc/exceptions/exception_catcher.hpp> | ||
|
||
namespace mrc { | ||
|
||
void ExceptionCatcher::push_exception(std::exception_ptr ex) | ||
{ | ||
auto lock = std::lock_guard(m_mutex); | ||
m_exceptions.push(ex); | ||
} | ||
|
||
bool ExceptionCatcher::has_exception() | ||
{ | ||
auto lock = std::lock_guard(m_mutex); | ||
return not m_exceptions.empty(); | ||
} | ||
|
||
void ExceptionCatcher::rethrow_next_exception() | ||
{ | ||
auto lock = std::lock_guard(m_mutex); | ||
|
||
if (m_exceptions.empty()) | ||
{ | ||
return; | ||
} | ||
|
||
auto ex = m_exceptions.front(); | ||
|
||
m_exceptions.pop(); | ||
|
||
std::rethrow_exception(ex); | ||
} | ||
|
||
} // namespace mrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters