[RTL]
[reactos.git] / reactos / lib / rtl / critical.c
index 4a6dd3f..8270b46 100644 (file)
@@ -59,15 +59,15 @@ RtlpCreateCriticalSectionSem(PRTL_CRITICAL_SECTION CriticalSection)
 
                 /* We failed, this is bad... */
                 DPRINT1("Failed to Create Event!\n");
-                _InterlockedDecrement(&CriticalSection->LockCount);
+                InterlockedDecrement(&CriticalSection->LockCount);
                 RtlRaiseStatus(Status);
                 return;
         }
         DPRINT("Created Event: %p \n", hNewEvent);
 
-        if ((hEvent = _InterlockedCompareExchangePointer((PVOID*)&CriticalSection->LockSemaphore,
-                                                         (PVOID)hNewEvent,
-                                                         0))) {
+        if (InterlockedCompareExchangePointer((PVOID*)&CriticalSection->LockSemaphore,
+                                              (PVOID)hNewEvent,
+                                               0)) {
 
             /* Some just created an event */
             DPRINT("Closing already created event: %p\n", hNewEvent);
@@ -117,12 +117,15 @@ RtlpWaitForCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
     DPRINT("Waiting on Critical Section Event: %p %p\n",
             CriticalSection,
             CriticalSection->LockSemaphore);
-    CriticalSection->DebugInfo->EntryCount++;
+
+    if (CriticalSection->DebugInfo)
+        CriticalSection->DebugInfo->EntryCount++;
 
     for (;;) {
 
         /* Increase the number of times we've had contention */
-        CriticalSection->DebugInfo->ContentionCount++;
+        if (CriticalSection->DebugInfo)
+            CriticalSection->DebugInfo->ContentionCount++;
 
         /* Wait on the Event */
         Status = NtWaitForSingleObject(CriticalSection->LockSemaphore,
@@ -310,7 +313,7 @@ RtlpFreeDebugInfo(PRTL_CRITICAL_SECTION_DEBUG DebugInfo)
         DPRINT("Freeing from Buffer: %p. Entry: %lu inside Process: %p\n",
                DebugInfo,
                EntryId,
-               NtCurrentTeb()->Cid.UniqueProcess);
+               NtCurrentTeb()->ClientId.UniqueProcess);
         RtlpDebugInfoFreeList[EntryId] = FALSE;
 
     } else {
@@ -318,7 +321,7 @@ RtlpFreeDebugInfo(PRTL_CRITICAL_SECTION_DEBUG DebugInfo)
         /* It's a dynamic one, so free from the heap */
         DPRINT("Freeing from Heap: %p inside Process: %p\n",
                DebugInfo,
-               NtCurrentTeb()->Cid.UniqueProcess);
+               NtCurrentTeb()->ClientId.UniqueProcess);
         RtlFreeHeap(NtCurrentPeb()->ProcessHeap, 0, DebugInfo);
 
     }
@@ -358,14 +361,21 @@ RtlDeleteCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
     /* Protect List */
     RtlEnterCriticalSection(&RtlCriticalSectionLock);
 
-    /* Remove it from the list */
-    RemoveEntryList(&CriticalSection->DebugInfo->ProcessLocksList);
+    if (CriticalSection->DebugInfo)
+    {
+        /* Remove it from the list */
+        RemoveEntryList(&CriticalSection->DebugInfo->ProcessLocksList);
+        RtlZeroMemory(CriticalSection->DebugInfo, sizeof(RTL_CRITICAL_SECTION_DEBUG));
+    }
 
     /* Unprotect */
     RtlLeaveCriticalSection(&RtlCriticalSectionLock);
 
-    /* Free it */
-    RtlpFreeDebugInfo(CriticalSection->DebugInfo);
+    if (CriticalSection->DebugInfo)
+    {
+        /* Free it */
+        RtlpFreeDebugInfo(CriticalSection->DebugInfo);
+    }
 
     /* Wipe it out */
     RtlZeroMemory(CriticalSection, sizeof(RTL_CRITICAL_SECTION));
@@ -424,10 +434,10 @@ NTSTATUS
 NTAPI
 RtlEnterCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
 {
-    HANDLE Thread = (HANDLE)NtCurrentTeb()->Cid.UniqueThread;
+    HANDLE Thread = (HANDLE)NtCurrentTeb()->ClientId.UniqueThread;
 
     /* Try to Lock it */
-    if (_InterlockedIncrement(&CriticalSection->LockCount) != 0) {
+    if (InterlockedIncrement(&CriticalSection->LockCount) != 0) {
 
         /*
          * We've failed to lock it! Does this thread
@@ -522,7 +532,7 @@ RtlInitializeCriticalSectionAndSpinCount(PRTL_CRITICAL_SECTION CriticalSection,
     CritcalSectionDebugData = RtlpAllocateDebugInfo();
     DPRINT("Allocated Debug Data: %p inside Process: %p\n",
            CritcalSectionDebugData,
-           NtCurrentTeb()->Cid.UniqueProcess);
+           NtCurrentTeb()->ClientId.UniqueProcess);
 
     if (!CritcalSectionDebugData) {
 
@@ -594,7 +604,7 @@ NTAPI
 RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
 {
 #ifndef NDEBUG
-    HANDLE Thread = (HANDLE)NtCurrentTeb()->Cid.UniqueThread;
+    HANDLE Thread = (HANDLE)NtCurrentTeb()->ClientId.UniqueThread;
 
     /* In win this case isn't checked. However it's a valid check so it should only
        be performed in debug builds! */
@@ -611,7 +621,7 @@ RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
     if (--CriticalSection->RecursionCount) {
 
         /* Someone still owns us, but we are free. This needs to be done atomically. */
-        _InterlockedDecrement(&CriticalSection->LockCount);
+        InterlockedDecrement(&CriticalSection->LockCount);
 
     } else {
 
@@ -620,7 +630,7 @@ RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
         CriticalSection->OwningThread = 0;
 
         /* Was someone wanting us? This needs to be done atomically. */
-        if (-1 != _InterlockedDecrement(&CriticalSection->LockCount)) {
+        if (-1 != InterlockedDecrement(&CriticalSection->LockCount)) {
 
             /* Let him have us */
             RtlpUnWaitCriticalSection(CriticalSection);
@@ -652,19 +662,19 @@ NTAPI
 RtlTryEnterCriticalSection(PRTL_CRITICAL_SECTION CriticalSection)
 {
     /* Try to take control */
-    if (_InterlockedCompareExchange(&CriticalSection->LockCount,
+    if (InterlockedCompareExchange(&CriticalSection->LockCount,
                                     0,
                                     -1) == -1) {
 
         /* It's ours */
-        CriticalSection->OwningThread =  NtCurrentTeb()->Cid.UniqueThread;
+        CriticalSection->OwningThread =  NtCurrentTeb()->ClientId.UniqueThread;
         CriticalSection->RecursionCount = 1;
         return TRUE;
 
-   } else if (CriticalSection->OwningThread == NtCurrentTeb()->Cid.UniqueThread) {
+   } else if (CriticalSection->OwningThread == NtCurrentTeb()->ClientId.UniqueThread) {
 
         /* It's already ours */
-        _InterlockedIncrement(&CriticalSection->LockCount);
+        InterlockedIncrement(&CriticalSection->LockCount);
         CriticalSection->RecursionCount++;
         return TRUE;
     }