SPH
CheckFunction.cpp
Go to the documentation of this file.
1 #include "thread/CheckFunction.h"
2 #include "common/Assert.h"
3 #include <thread>
4 
6 
7 const std::thread::id MAIN_THREAD_ID = std::this_thread::get_id();
8 
9 FunctionChecker::FunctionChecker(std::atomic<Size>& reentrantCnt,
10  std::atomic<Size>& totalCnt,
11  const Flags<CheckFunction> flags)
12  : reentrantCnt(reentrantCnt)
13  , flags(flags) {
14  reentrantCnt++;
15  totalCnt++;
16 
17  if (flags.has(CheckFunction::MAIN_THREAD)) {
18  SPH_ASSERT(std::this_thread::get_id() == MAIN_THREAD_ID, "Called from different thread");
19  }
21  SPH_ASSERT(std::this_thread::get_id() != MAIN_THREAD_ID, "Called from main thread");
22  }
23  if (flags.has(CheckFunction::NON_REENRANT)) {
24  SPH_ASSERT(reentrantCnt == 1, "Reentrant " + std::to_string(reentrantCnt));
25  }
26  if (flags.has(CheckFunction::ONCE)) {
27  SPH_ASSERT(totalCnt == 1, "Called more than once");
28  }
29 }
30 
32  reentrantCnt--;
33 
34  if (flags.has(CheckFunction::NO_THROW)) {
35  SPH_ASSERT(!std::uncaught_exception(), "Function threw an exception");
36  }
37 }
38 
39 bool isMainThread() {
40  return std::this_thread::get_id() == MAIN_THREAD_ID;
41 }
42 
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
NAMESPACE_SPH_BEGIN const std::thread::id MAIN_THREAD_ID
bool isMainThread()
Checks if the calling thread is the main thred.
Helper functions to check the internal consistency of the code.
@ NOT_MAIN_THREAD
Function cannot be called from main thread.
@ NO_THROW
Function cannot throw exceptions.
@ MAIN_THREAD
Function can only be executed from main thread.
@ ONCE
Function can be executed only once in the application.
@ NON_REENRANT
Function can be executed by any thread, but only once at a time.
#define NAMESPACE_SPH_END
Definition: Object.h:12
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
Definition: Flags.h:77
FunctionChecker(std::atomic< Size > &reentrantCnt, std::atomic< Size > &totalCnt, const Flags< CheckFunction > flags)