https://git.reactos.org/?p=reactos.git;a=commitdiff;h=2486558ae1d15e878e2df4d067858a710f168580

commit 2486558ae1d15e878e2df4d067858a710f168580
Author:     Jérôme Gardou <[email protected]>
AuthorDate: Fri Jan 29 18:15:17 2021 +0100
Commit:     Jérôme Gardou <[email protected]>
CommitDate: Fri Jan 29 18:18:09 2021 +0100

    [RTL] Do not mess with critical section lock when there is no reason to.
    
     - When process is shutting down.
     - When the caller is so drunk that they leave twice the pub altough they 
entered it only once.
---
 sdk/lib/rtl/critical.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/sdk/lib/rtl/critical.c b/sdk/lib/rtl/critical.c
index c9b9c68ff6d..0911a8ce76f 100644
--- a/sdk/lib/rtl/critical.c
+++ b/sdk/lib/rtl/critical.c
@@ -114,12 +114,6 @@ RtlpWaitForCriticalSection(PRTL_CRITICAL_SECTION 
CriticalSection)
     EXCEPTION_RECORD ExceptionRecord;
     BOOLEAN LastChance = FALSE;
 
-    /* Do we have an Event yet? */
-    if (!CriticalSection->LockSemaphore)
-    {
-        RtlpCreateCriticalSectionSem(CriticalSection);
-    }
-
     /* Increase the Debug Entry count */
     DPRINT("Waiting on Critical Section Event: %p %p\n",
             CriticalSection,
@@ -136,10 +130,15 @@ RtlpWaitForCriticalSection(PRTL_CRITICAL_SECTION 
CriticalSection)
         LdrpShutdownThreadId == NtCurrentTeb()->RealClientId.UniqueThread)
     {
         DPRINT("Forcing ownership of critical section %p\n", CriticalSection);
-        CriticalSection->LockCount = 0;
         return STATUS_SUCCESS;
     }
 
+    /* Do we have an Event yet? */
+    if (!CriticalSection->LockSemaphore)
+    {
+        RtlpCreateCriticalSectionSem(CriticalSection);
+    }
+
     for (;;)
     {
         /* Increase the number of times we've had contention */
@@ -715,9 +714,13 @@ RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION 
CriticalSection)
      */
     if (--CriticalSection->RecursionCount)
     {
+        if (CriticalSection->RecursionCount < 0)
+        {
+            DPRINT1("CRITICAL SECTION MESS: Section %p is not acquired!\n", 
CriticalSection);
+            return STATUS_UNSUCCESSFUL;
+        }
         /* Someone still owns us, but we are free. This needs to be done 
atomically. */
         InterlockedDecrement(&CriticalSection->LockCount);
-
     }
     else
     {

Reply via email to