From c183fa4b134719cfdba7d0a4128b4a741a8e1eb3 Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Thu, 9 Feb 2012 12:59:21 +0100 Subject: [PATCH] cec: added CTryLockObject --- src/lib/platform/threads/mutex.h | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/lib/platform/threads/mutex.h b/src/lib/platform/threads/mutex.h index d44c0f1..e848101 100644 --- a/src/lib/platform/threads/mutex.h +++ b/src/lib/platform/threads/mutex.h @@ -163,6 +163,64 @@ namespace PLATFORM bool m_bClearOnExit; }; + class CTryLockObject : public PreventCopy + { + public: + inline CTryLockObject(CMutex &mutex, bool bClearOnExit = false) : + m_mutex(mutex), + m_bClearOnExit(bClearOnExit), + m_bIsLocked(m_mutex.TryLock()) + { + } + + inline ~CTryLockObject(void) + { + if (m_bClearOnExit) + Clear(); + else if (m_bIsLocked) + Unlock(); + } + + inline bool TryLock(void) + { + bool bReturn = m_mutex.TryLock(); + m_bIsLocked |= bReturn; + return bReturn; + } + + inline void Unlock(void) + { + if (m_bIsLocked) + { + m_bIsLocked = false; + m_mutex.Unlock(); + } + } + + inline bool Clear(void) + { + m_bIsLocked = false; + return m_mutex.Clear(); + } + + inline bool Lock(void) + { + bool bReturn = m_mutex.Lock(); + m_bIsLocked |= bReturn; + return bReturn; + } + + inline bool IsLocked(void) const + { + return m_bIsLocked; + } + + private: + CMutex & m_mutex; + bool m_bClearOnExit; + volatile bool m_bIsLocked; + }; + class CCondition : public PreventCopy { public: -- 2.34.1