Jelajahi Sumber

HPCC-11966 Extend locking primitives to add assertLocked()

Handle nested case

Signed-off-by: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 11 tahun lalu
induk
melakukan
cd949479ab
1 mengubah file dengan 16 tambahan dan 4 penghapusan
  1. 16 4
      system/jlib/jmutex.hpp

+ 16 - 4
system/jlib/jmutex.hpp

@@ -200,20 +200,22 @@ private:
     CRITICAL_SECTION flags;
 #ifdef _ASSERT_LOCK_SUPPORT
     ThreadId owner;
+    unsigned depth;
 #endif
-    inline CriticalSection(CriticalSection & value) { assert(false); } // dummy to prevent inadvetant use as block
+    inline CriticalSection(CriticalSection & value) { assert(false); } // dummy to prevent inadvertant use as block
 public:
     inline CriticalSection()
     {
         InitializeCriticalSection(&flags);
 #ifdef _ASSERT_LOCK_SUPPORT
         owner = 0;
+        depth = 0;
 #endif
     };
     inline ~CriticalSection()
     {
 #ifdef _ASSERT_LOCK_SUPPORT
-        assertex(owner==0);
+        assertex(owner==0 && depth==0);
 #endif
         DeleteCriticalSection(&flags);
     };
@@ -221,13 +223,23 @@ public:
     {
         EnterCriticalSection(&flags);
 #ifdef _ASSERT_LOCK_SUPPORT
-        owner = GetCurrentThreadId();
+        if (owner)
+        {
+            assertex(owner==GetCurrentThreadId());
+            depth++;
+        }
+        else
+            owner = GetCurrentThreadId();
 #endif
     };
     inline void leave()
     {
 #ifdef _ASSERT_LOCK_SUPPORT
-        owner = 0;
+        assertex(owner==GetCurrentThreadId());
+        if (depth)
+            depth--;
+        else
+            owner = 0;
 #endif
         LeaveCriticalSection(&flags);
     };