[change] Introduce XbmcThreads::InversePredicate as a helper for threading mechanisms...
authorJim Carroll <thecarrolls@jiminger.com>
Sun, 24 Jul 2011 13:53:41 +0000 (09:53 -0400)
committerJim Carroll <thecarrolls@jiminger.com>
Sun, 31 Jul 2011 14:03:57 +0000 (10:03 -0400)
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.

xbmc/Application.cpp
xbmc/Application.h
xbmc/threads/Helpers.h
xbmc/threads/SharedSection.h

index 97f9a86..95b6fb9 100644 (file)
@@ -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;
 
index 3b0e588..78e8607 100644 (file)
@@ -388,15 +388,6 @@ protected:
   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;
index 19fbba4..6efdd88 100644 (file)
@@ -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); }
+  };
+
 }
 
index c18da74..fb915e6 100644 (file)
@@ -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>