Treelite
omp_exception.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_OMP_EXCEPTION_H_
8 #define TREELITE_OMP_EXCEPTION_H_
9 
10 #include <treelite/error.h>
11 #include <exception>
12 #include <mutex>
13 
14 namespace treelite {
15 
19 class OMPException {
20  private:
21  // exception_ptr member to store the exception
22  std::exception_ptr omp_exception_;
23  // mutex to be acquired during catch to set the exception_ptr
24  std::mutex mutex_;
25 
26  public:
30  template <typename Function, typename... Parameters>
31  void Run(Function f, Parameters... params) {
32  try {
33  f(params...);
34  } catch (treelite::Error &ex) {
35  std::lock_guard<std::mutex> lock(mutex_);
36  if (!omp_exception_) {
37  omp_exception_ = std::current_exception();
38  }
39  } catch (std::exception &ex) {
40  std::lock_guard<std::mutex> lock(mutex_);
41  if (!omp_exception_) {
42  omp_exception_ = std::current_exception();
43  }
44  }
45  }
46 
50  void Rethrow() {
51  if (this->omp_exception_) {
52  std::rethrow_exception(this->omp_exception_);
53  }
54  }
55 };
56 
57 } // namespace treelite
58 
59 #endif // TREELITE_OMP_EXCEPTION_H_
Exception class that will be thrown by Treelite.
Definition: error.h:18
Exception class used throughout the Treelite codebase.
void Run(Function f, Parameters... params)
Parallel OMP blocks should be placed within Run to save exception.
Definition: omp_exception.h:31
OMP Exception class catches, saves and rethrows exception from OMP blocks.
Definition: omp_exception.h:19
void Rethrow()
should be called from the main thread to rethrow the exception
Definition: omp_exception.h:50