SharedException.h
Go to the documentation of this file.
1 #ifndef art_Framework_Core_SharedException_h
2 #define art_Framework_Core_SharedException_h
3 
4 #include <atomic>
5 #include <exception>
6 #include <thread>
7 
8 // ============================================================================
9 // The SharedException class exists to facilitate the communication of
10 // exceptions produced on worker threads to the main thread of
11 // execution. The pattern is that any thread may store its exception
12 // in an object of this type; however, the exception must be thrown
13 // from the same thread in which the SharedException object has been
14 // created.
15 // ============================================================================
16 
17 namespace art {
19  public:
20  void
21  store(std::exception_ptr ex_ptr)
22  {
23  bool expected = false;
24  if (cachedExceptionStored_.compare_exchange_strong(expected, true)) {
25  // Put the exception where the main thread can get at it.
26  cachedException_ = std::move(ex_ptr);
27  }
28  }
29 
30  void
32  {
33  store(std::current_exception());
34  }
35 
36  template <typename T, typename... Args>
37  void
38  store(Args&&... args)
39  {
40  store(std::make_exception_ptr(T{std::forward<Args>(args)...}));
41  }
42 
43  void
45  {
46  assert(std::this_thread::get_id() == ownerThread_);
47  if (cachedExceptionStored_.load()) {
48  std::rethrow_exception(cachedException_);
49  }
50  }
51 
52  private:
53  std::thread::id const ownerThread_{std::this_thread::get_id()};
54  std::atomic<bool> cachedExceptionStored_{false};
55  std::exception_ptr cachedException_{};
56  };
57 }
58 
59 #endif /* art_Framework_Core_SharedException_h */
60 
61 // Local Variables:
62 // mode: c++
63 // End:
const char expected[]
Definition: Exception_t.cc:22
std::thread::id const ownerThread_
static QCString args
Definition: declinfo.cpp:674
void store(std::exception_ptr ex_ptr)
std::atomic< bool > cachedExceptionStored_
def move(depos, offset)
Definition: depos.py:107
std::exception_ptr cachedException_
void store(Args &&...args)