Permalink
Browse files

[change] Introduce XbmcThreads::InversePredicate as a helper for thre…

…ading mechanisms that deal with predicates.

This is a slight modification that makes the code more readable and provides for a general mechanism to use condition variables in a more straightforward manner. This currently effects CSharedSection and CApplication. With respect to the use of TightConditionVariable, they should be cleaner now.
  • Loading branch information...
1 parent 79bca05 commit ff3c7162f8d2e02d4edbc04b4f20fc5a607e10e3 Jim Carroll committed Jul 24, 2011
Showing with 24 additions and 17 deletions.
  1. +1 −2 xbmc/Application.cpp
  2. +0 −9 xbmc/Application.h
  3. +17 −0 xbmc/threads/Helpers.h
  4. +6 −6 xbmc/threads/SharedSection.h
@@ -1888,8 +1888,7 @@ bool CApplication::WaitFrame(unsigned int timeout)
CSingleLock lock(m_frameMutex);
//wait until event is set, but modify remaining time
- NotFrameCount nfc(this);
- TightConditionVariable<NotFrameCount&> cv(m_frameCond, nfc);
+ TightConditionVariable<InversePredicate<int&> > cv(m_frameCond, InversePredicate<int&>(m_frameCount));
cv.wait(lock,timeout);
done = m_frameCount == 0;
View
@@ -388,15 +388,6 @@ class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMs
std::map<std::string, std::map<int, float> > m_lastAxisMap;
#endif
- class NotFrameCount
- {
- CApplication* ths;
- public:
- inline NotFrameCount(CApplication* o) : ths(o) {}
- inline bool operator!() { return !(ths->m_frameCount); }
- };
-
- friend class NotFrameCount;
};
extern CApplication g_application;
@@ -34,5 +34,22 @@ namespace XbmcThreads
inline NonCopyable() {}
};
+ /**
+ * This will create a new predicate from an old predicate P with
+ * inverse truth value. This predicate is safe to use in a
+ * TightConditionVariable<P>
+ */
+ template <class P> class InversePredicate
+ {
+ P predicate;
+
+ public:
+ inline InversePredicate(P predicate_) : predicate(predicate_) {}
+ inline InversePredicate(const InversePredicate<P>& other) : predicate(other.predicate) {}
+ inline InversePredicate<P>& operator=(InversePredicate<P>& other) { predicate = other.predicate; }
+
+ inline bool operator!() const { return !(!predicate); }
+ };
+
}
@@ -23,6 +23,7 @@
#include "threads/Condition.h"
#include "threads/SingleLock.h"
+#include "threads/Helpers.h"
/**
* A CSharedSection is a mutex that satisfies the Shared Lockable concept (see Lockables.h).
@@ -31,21 +32,20 @@ class CSharedSection
{
CCriticalSection sec;
XbmcThreads::ConditionVariable actualCv;
- XbmcThreads::TightConditionVariable<bool&> cond;
+ XbmcThreads::TightConditionVariable<XbmcThreads::InversePredicate<unsigned int&> > cond;
unsigned int sharedCount;
- bool noShared;
public:
- inline CSharedSection() : cond(actualCv,noShared), sharedCount(0), noShared(true) {}
+ inline CSharedSection() : cond(actualCv,XbmcThreads::InversePredicate<unsigned int&>(sharedCount)), sharedCount(0) {}
inline void lock() { CSingleLock l(sec); if (sharedCount) cond.wait(l); sec.lock(); }
inline bool try_lock() { return (sec.try_lock() ? ((sharedCount == 0) ? true : (sec.unlock(), false)) : false); }
inline void unlock() { sec.unlock(); }
- inline void lock_shared() { CSingleLock l(sec); sharedCount++; noShared = false; }
- inline bool try_lock_shared() { return (sec.try_lock() ? sharedCount++, noShared = false, sec.unlock(), true : false); }
- inline void unlock_shared() { CSingleLock l(sec); sharedCount--; if (!sharedCount) { noShared = true; cond.notifyAll(); } }
+ inline void lock_shared() { CSingleLock l(sec); sharedCount++; }
+ inline bool try_lock_shared() { return (sec.try_lock() ? sharedCount++, sec.unlock(), true : false); }
+ inline void unlock_shared() { CSingleLock l(sec); sharedCount--; if (!sharedCount) { cond.notifyAll(); } }
};
class CSharedLock : public XbmcThreads::SharedLock<CSharedSection>

0 comments on commit ff3c716

Please sign in to comment.