88#pragma once
99
1010#include < atomic>
11- #include < shared_mutex >
11+ #include < memory >
1212#include < mutex>
13+ #include < shared_mutex>
1314#include < thread>
1415#include < unordered_map>
15- #include < memory>
1616
1717// Compile-time configuration for forcing thread safety
1818#ifndef REACTION_FORCE_THREAD_SAFETY
2222// Thread safety mode detection
2323#ifndef REACTION_THREAD_SAFETY_MODE
2424#if REACTION_FORCE_THREAD_SAFETY
25- #define REACTION_THREAD_SAFETY_MODE 1 // Always thread-safe
25+ #define REACTION_THREAD_SAFETY_MODE 1 // Always thread-safe
2626#else
27- #define REACTION_THREAD_SAFETY_MODE 0 // Auto-detect
27+ #define REACTION_THREAD_SAFETY_MODE 0 // Auto-detect
2828#endif
2929#endif
3030
@@ -48,7 +48,7 @@ class ThreadSafetyManager {
4848 * @brief Get the singleton instance.
4949 * @return ThreadSafetyManager& Reference to the singleton instance.
5050 */
51- static ThreadSafetyManager& getInstance () noexcept {
51+ static ThreadSafetyManager & getInstance () noexcept {
5252 static ThreadSafetyManager instance;
5353 return instance;
5454 }
@@ -148,12 +148,12 @@ class ThreadSafetyManager {
148148private:
149149 ThreadSafetyManager () = default ;
150150 ~ThreadSafetyManager () = default ;
151- ThreadSafetyManager (const ThreadSafetyManager&) = delete ;
152- ThreadSafetyManager& operator =(const ThreadSafetyManager&) = delete ;
151+ ThreadSafetyManager (const ThreadSafetyManager &) = delete ;
152+ ThreadSafetyManager & operator =(const ThreadSafetyManager &) = delete ;
153153
154154 std::atomic<bool > m_threadSafetyEnabled{REACTION_THREAD_SAFETY_MODE};
155155 std::atomic<std::thread::id> m_firstThreadId{};
156- std::atomic<uint32_t > m_safetyVersion{1 }; // Version counter for cache invalidation
156+ std::atomic<uint32_t > m_safetyVersion{1 }; // Version counter for cache invalidation
157157};
158158
159159/* *
@@ -177,7 +177,7 @@ class ThreadRegistrationGuard {
177177 *
178178 * @tparam MutexType The underlying mutex type (std::mutex, std::shared_mutex, etc.)
179179 */
180- template <typename MutexType>
180+ template <typename MutexType>
181181class ConditionalMutexWrapper {
182182public:
183183 // / @brief Acquire lock if thread safety is enabled.
@@ -203,23 +203,23 @@ class ConditionalMutexWrapper {
203203 }
204204
205205 // Shared mutex operations (SFINAE-enabled for types that support them)
206- template <typename T = MutexType>
206+ template <typename T = MutexType>
207207 auto lock_shared () noexcept (noexcept (std::declval<T>().lock_shared()))
208208 -> decltype(std::declval<T>().lock_shared(), void()) {
209209 if (ThreadSafetyManager::getInstance ().isThreadSafetyEnabled ()) [[likely]] {
210210 m_mutex.lock_shared ();
211211 }
212212 }
213213
214- template <typename T = MutexType>
214+ template <typename T = MutexType>
215215 auto unlock_shared () noexcept (noexcept (std::declval<T>().unlock_shared()))
216216 -> decltype(std::declval<T>().unlock_shared(), void()) {
217217 if (ThreadSafetyManager::getInstance ().isThreadSafetyEnabled ()) [[likely]] {
218218 m_mutex.unlock_shared ();
219219 }
220220 }
221221
222- template <typename T = MutexType>
222+ template <typename T = MutexType>
223223 auto try_lock_shared () noexcept (noexcept (std::declval<T>().try_lock_shared()))
224224 -> decltype(std::declval<T>().try_lock_shared()) {
225225 if (ThreadSafetyManager::getInstance ().isThreadSafetyEnabled ()) [[likely]] {
@@ -251,26 +251,26 @@ using ConditionalMutex = ConditionalMutexWrapper<std::mutex>;
251251 * @param LockMethod The locking method name
252252 * @param UnlockMethod The unlocking method name
253253 */
254- #define REACTION_DEFINE_LOCK_GUARD (GuardName, LockMethod, UnlockMethod ) \
255- template <typename Mutex> \
256- class GuardName { \
257- public: \
258- explicit GuardName (Mutex& mutex) noexcept (noexcept (mutex.LockMethod())) \
259- : m_mutex(mutex) { \
260- m_mutex.LockMethod (); \
261- } \
262- \
263- ~GuardName () noexcept (noexcept (m_mutex.UnlockMethod())) { \
264- m_mutex.UnlockMethod (); \
265- } \
266- \
267- GuardName (const GuardName&) = delete ; \
268- GuardName& operator =(const GuardName&) = delete ; \
269- GuardName (GuardName&&) = delete ; \
270- GuardName& operator =(GuardName&&) = delete ; \
271- \
272- private: \
273- Mutex& m_mutex; \
254+ #define REACTION_DEFINE_LOCK_GUARD (GuardName, LockMethod, UnlockMethod ) \
255+ template <typename Mutex> \
256+ class GuardName { \
257+ public: \
258+ explicit GuardName (Mutex & mutex) noexcept (noexcept (mutex.LockMethod())) \
259+ : m_mutex(mutex) { \
260+ m_mutex.LockMethod (); \
261+ } \
262+ \
263+ ~GuardName () noexcept (noexcept (m_mutex.UnlockMethod())) { \
264+ m_mutex.UnlockMethod (); \
265+ } \
266+ \
267+ GuardName (const GuardName &) = delete ; \
268+ GuardName & operator =(const GuardName &) = delete ; \
269+ GuardName (GuardName &&) = delete ; \
270+ GuardName & operator =(GuardName &&) = delete ; \
271+ \
272+ private: \
273+ Mutex & m_mutex; \
274274 }
275275
276276// Generate optimized lock guards
@@ -280,21 +280,21 @@ REACTION_DEFINE_LOCK_GUARD(ConditionalSharedLockGuard, lock_shared, unlock_share
280280/* *
281281 * @brief RAII shared lock guard for conditional shared mutex.
282282 */
283- template <typename Mutex>
283+ template <typename Mutex>
284284using ConditionalSharedLock = ConditionalSharedLockGuard<Mutex>;
285285
286286/* *
287287 * @brief RAII unique lock guard for conditional mutex.
288288 */
289- template <typename Mutex>
289+ template <typename Mutex>
290290using ConditionalUniqueLock = ConditionalLockGuard<Mutex>;
291291
292292// Helper macros for thread registration with better performance
293- #define REACTION_REGISTER_THREAD () \
294- do { \
293+ #define REACTION_REGISTER_THREAD () \
294+ do { \
295295 static thread_local reaction::ThreadRegistrationGuard thread_guard_##__COUNTER__; \
296- (void )thread_guard_##__COUNTER__; \
297- } while (0 )
296+ (void )thread_guard_##__COUNTER__; \
297+ } while (0 )
298298
299299// Cleanup macro to avoid pollution
300300#undef REACTION_DEFINE_LOCK_GUARD
0 commit comments