1 #pragma once
2 
3 /// \file CheckFunction.h
4 /// \brief Helper functions to check the internal consistency of the code
5 /// \author Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
6 /// \date 2016-2021
7 
8 #include "common/Globals.h"
9 #include "objects/wrappers/Flags.h"
10 #include <atomic>
11 
12 NAMESPACE_SPH_BEGIN
13 
14 enum class CheckFunction {
15     NON_REENRANT = 1 << 0,    ///< Function can be executed by any thread, but only once at a time
16     MAIN_THREAD = 1 << 1,     ///< Function can only be executed from main thread
17     NOT_MAIN_THREAD = 1 << 2, ///< Function cannot be called from main thread
18     ONCE = 1 << 3,            ///< Function can be executed only once in the application
19     NO_THROW = 1 << 4,        ///< Function cannot throw exceptions
20 };
21 
22 class FunctionChecker {
23     std::atomic<Size>& reentrantCnt;
24     Flags<CheckFunction> flags;
25 
26 public:
27     FunctionChecker(std::atomic<Size>& reentrantCnt,
28         std::atomic<Size>& totalCnt,
29         const Flags<CheckFunction> flags);
30 
31     ~FunctionChecker();
32 };
33 
34 #ifdef SPH_DEBUG
35 #define CHECK_FUNCTION(flags)                                                                                \
36     static std::atomic<Size> __reentrantCnt;                                                                 \
37     static std::atomic<Size> __totalCnt;                                                                     \
38     FunctionChecker __checker(__reentrantCnt, __totalCnt, flags);
39 #else
40 #define CHECK_FUNCTION(flags)
41 #endif
42 
43 
44 /// Checks if the calling thread is the main thred
45 bool isMainThread();
46 
47 NAMESPACE_SPH_END
48