From: Lars Op den Kamp Date: Thu, 9 Feb 2012 11:59:21 +0000 (+0100) Subject: cec: added CTryLockObject X-Git-Tag: upstream/2.2.0~1^2~35^2~131 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c183fa4b134719cfdba7d0a4128b4a741a8e1eb3;p=deb_libcec.git cec: added CTryLockObject --- 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: