1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifdef _OPENMP
#ifndef OPENMPEXCEPTION_H
#define OPENMPEXCEPTION_H
#include <omp.h>
#include <mutex>
/** \file OPNEMPException.h
* \brief Use to generate exception in parallelized block with OPENMP
* \author Xavier Warin
*/
namespace StOpt
{
/// \class OpenmpException
/// To generate and catch exceptions
class OpenmpException
{
std::exception_ptr Ptr;
std::mutex Lock;
public:
/// \brief Constructor
OpenmpException(): Ptr(nullptr) {}
/// \brief destructor
~OpenmpException()
{
this->rethrow();
}
/// \brief Rethrow
void rethrow()
{
if (auto tmp = this->Ptr)
{
this->Ptr = nullptr;
std::rethrow_exception(tmp);
}
}
/// \brief Capture
void captureException()
{
std::unique_lock<std::mutex> guard(this->Lock);
this->Ptr = std::current_exception();
}
/// \brief Elegant way to run the test
template <typename Function, typename... Parameters>
void run(Function f, Parameters... params)
{
try
{
f(params...);
}
catch (...)
{
captureException();
}
}
};
}
#endif
#endif
|