Forráskód Böngészése

add CriticalLock that unlocks automatically if the current scope is left

Kolja Strohm 6 hónapja
szülő
commit
dcc59f9131
2 módosított fájl, 26 hozzáadás és 2 törlés
  1. 13 2
      Critical.cpp
  2. 13 0
      Critical.h

+ 13 - 2
Critical.cpp

@@ -26,7 +26,7 @@ Critical::~Critical()
 
 pthread_t CachedCurrentThread()
 {
-    volatile thread_local static  pthread_t t = GetCurrentThread();
+    volatile thread_local static pthread_t t = GetCurrentThread();
     return (pthread_t)t;
 }
 
@@ -151,4 +151,15 @@ void Synchronizer::notifyAll()
 int Synchronizer::getNumberOfWaitingThreads() const
 {
     return numWaiting;
-}
+}
+
+Framework::CriticalLock::CriticalLock(Critical* critical)
+{
+    this->critical = critical;
+    critical->lock();
+}
+
+Framework::CriticalLock::~CriticalLock()
+{
+    critical->unlock();
+}

+ 13 - 0
Critical.h

@@ -34,6 +34,19 @@ namespace Framework
         DLLEXPORT const Thread* zOwner() const;
     };
 
+    class CriticalLock
+    {
+    private:
+        Critical* critical;
+
+    public:
+        DLLEXPORT CriticalLock(Critical* critical);
+        DLLEXPORT ~CriticalLock();
+    };
+
+#define LOCK(x)     CriticalLock _lock(x)
+#define LOCKN(x, i) CriticalLock _lock_##i(x)
+
     class Synchronizer
     {
     private: