monitor and threads
This commit is contained in:
@@ -149,6 +149,46 @@ inline ScopedTimedLock makeScopedMutexLock(std::timed_mutex& mutex, int timeout_
|
||||
__ulib_lock; \
|
||||
__ulib_lock.unlock())
|
||||
|
||||
/**
|
||||
* @brief RecursiveMutex class wraps std::recursive_timed_mutex.
|
||||
*/
|
||||
class RecursiveMutex {
|
||||
public:
|
||||
RecursiveMutex() = default;
|
||||
~RecursiveMutex() = default;
|
||||
|
||||
/** @brief Locks the mutex, blocking if necessary. */
|
||||
void Lock() { m_Mutex.lock(); }
|
||||
|
||||
/** @brief Unlocks the mutex. */
|
||||
void Unlock() { m_Mutex.unlock(); }
|
||||
|
||||
/** @brief Tries to lock the mutex without blocking. */
|
||||
bool TryLock() { return m_Mutex.try_lock(); }
|
||||
|
||||
/** @brief Tries to lock the mutex within a timeout in milliseconds. */
|
||||
bool TryLockFor(int timeout_ms) {
|
||||
if (timeout_ms < 0) { Lock(); return true; }
|
||||
return m_Mutex.try_lock_for(std::chrono::milliseconds(timeout_ms));
|
||||
}
|
||||
|
||||
/** @brief RAII helper for scoped locking. */
|
||||
class ScopedLock {
|
||||
public:
|
||||
ScopedLock(RecursiveMutex &mutex) : m_Mutex(mutex) { m_Mutex.Lock(); }
|
||||
~ScopedLock() { m_Mutex.Unlock(); }
|
||||
private:
|
||||
RecursiveMutex &m_Mutex;
|
||||
ScopedLock(const ScopedLock&) = delete;
|
||||
ScopedLock& operator=(const ScopedLock&) = delete;
|
||||
};
|
||||
|
||||
private:
|
||||
std::recursive_timed_mutex m_Mutex;
|
||||
RecursiveMutex(const RecursiveMutex &) = delete;
|
||||
RecursiveMutex &operator=(const RecursiveMutex &) = delete;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Monitor class provides a base for objects that need thread-safe access.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user