Go to the documentation of this file.
5 #ifndef SHARED_RECURSIVE_MUTEX_H
6 #define SHARED_RECURSIVE_MUTEX_H
7 #include <shared_mutex>
17 template<
typename PhantomType>
92 std::shared_mutex m_sharedMtx;
93 static inline thread_local uint32_t g_readers = 0;
94 static inline thread_local uint32_t g_writers = 0;
97 template<
typename PhantomType>
100 if (g_writers == 0 && g_readers == 0)
104 else if (g_writers == 0 && g_readers > 0)
106 m_sharedMtx.unlock_shared();
111 template<
typename PhantomType>
119 else if (g_readers > 0)
123 else if (g_readers == 0)
125 m_sharedMtx.lock_shared();
129 template<
typename PhantomType>
137 m_sharedMtx.unlock();
139 m_sharedMtx.lock_shared();
142 template<
typename PhantomType>
154 m_sharedMtx.unlock_shared();
157 template<
typename PhantomType>
172 const bool aquiredLock = m_sharedMtx.try_lock();
180 template<
typename PhantomType>
184 if (g_writers > 0 || g_readers > 0)
189 const bool aquiredLock = m_sharedMtx.try_lock_shared();
196 template<
typename PhantomType>
199 return g_writers > 0;
201 template<
typename PhantomType>
204 return g_readers > 0 && g_writers == 0;
211 #endif //SHARED_RECURSIVE_MUTEX_H
static shared_recursive_mutex_t & instance()
The shared_recursive_mutex_t is relying on thread local storage, so there can only be 1 valid instanc...
Definition: shared_recursive_mutex.h:28
bool is_locked() const
Returns if this thread has write ownership.
Definition: shared_recursive_mutex.h:197
shared_recursive_mutex_t & operator=(const shared_recursive_mutex_t &)=delete
void unlock_shared()
Unlocks the mutex for this thread if its level of ownership is 1. Otherwise reduces the level of owne...
Definition: shared_recursive_mutex.h:143
void unlock()
Unlocks the mutex for this thread if its level of write ownership is 1 and has no read ownership....
Definition: shared_recursive_mutex.h:130
Definition: shared_recursive_mutex.h:9
bool try_lock()
Tries to get write ownership if possible. If the thread has read (but no write) ownership this functi...
Definition: shared_recursive_mutex.h:158
Implementation of a fast shared_recursive_mutex Thanks to https://github.com/KonanM/shared_recursive_...
Definition: shared_recursive_mutex.h:18
void lock_shared()
Locks the mutex for sharable read access. Blocks execution as long as read access is not available:
Definition: shared_recursive_mutex.h:112
bool is_locked_shared() const
Returns true if this thread has only read ownership.
Definition: shared_recursive_mutex.h:202
bool try_lock_shared()
Tries to get read ownership if possible.
Definition: shared_recursive_mutex.h:181
void lock()
Locks the mutex for exclusive write access for this thread. Blocks execution as long as write access ...
Definition: shared_recursive_mutex.h:98