File: OpenmpException.h

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (66 lines) | stat: -rw-r--r-- 1,343 bytes parent folder | download | duplicates (3)
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