CatapultServer  v0.5.0.1 (Elephant)
ReentrancyCheckReaderNotificationPolicy.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "SpinLock.h"
23 #include "catapult/exceptions.h"
24 #include <thread>
25 #include <unordered_set>
26 
27 namespace catapult { namespace utils {
28 
31  public:
33  };
34 
37  public:
39  void readerAcquired() {
40  executeSynchronized([this](auto id) {
41  if (m_threadIds.cend() != m_threadIds.find(id))
42  CATAPULT_THROW_AND_LOG_1(reader_reentrancy_error, "reader reentrancy detected", id);
43 
44  m_threadIds.insert(id);
45  });
46  }
47 
49  void readerReleased() {
50  executeSynchronized([this](auto id) {
51  m_threadIds.erase(id);
52  });
53  }
54 
55  private:
56  template<typename TAction>
57  void executeSynchronized(TAction action) {
58  auto id = std::this_thread::get_id();
59  SpinLockGuard lock(m_mutex);
60 
61  action(id);
62  }
63 
64  private:
66  std::unordered_set<std::thread::id> m_threadIds;
67  };
68 }}
catapult::utils::reader_reentrancy_error
Exception class that is thrown when reader reentrancy is detected.
Definition: ReentrancyCheckReaderNotificationPolicy.h:30
SpinLock.h
exceptions.h
catapult::utils::ReentrancyCheckReaderNotificationPolicy::readerReleased
void readerReleased()
A reader was released by the current thread.
Definition: ReentrancyCheckReaderNotificationPolicy.h:49
catapult::utils::ReentrancyCheckReaderNotificationPolicy::m_mutex
SpinLock m_mutex
Definition: ReentrancyCheckReaderNotificationPolicy.h:65
catapult::utils::SpinLock
Definition: SpinLock.h:31
catapult::utils::ReentrancyCheckReaderNotificationPolicy::m_threadIds
std::unordered_set< std::thread::id > m_threadIds
Definition: ReentrancyCheckReaderNotificationPolicy.h:66
catapult::utils::ReentrancyCheckReaderNotificationPolicy::executeSynchronized
void executeSynchronized(TAction action)
Definition: ReentrancyCheckReaderNotificationPolicy.h:57
catapult::utils::ReentrancyCheckReaderNotificationPolicy
A reentrancy check reader notification policy.
Definition: ReentrancyCheckReaderNotificationPolicy.h:36
catapult::catapult_runtime_error
catapult_error< std::runtime_error > catapult_runtime_error
Definition: exceptions.h:87
CATAPULT_THROW_AND_LOG_1
#define CATAPULT_THROW_AND_LOG_1(TYPE, MESSAGE, PARAM1)
Macro used to throw a catapult exception with one parameter.
Definition: exceptions.h:152
catapult::action
std::function< void()> action
An action function.
Definition: functions.h:27
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::catapult_error
Base class for all catapult exceptions that derives from both std::exception and boost::exception.
Definition: exceptions.h:42
catapult::utils::SpinLockGuard
std::lock_guard< SpinLock > SpinLockGuard
A spin lock guard.
Definition: SpinLock.h:60
catapult::utils::ReentrancyCheckReaderNotificationPolicy::readerAcquired
void readerAcquired()
A reader was acquried by the current thread.
Definition: ReentrancyCheckReaderNotificationPolicy.h:39