[CONSRV]
[reactos.git] / subsystems / win32 / csrsrv / thredsup.c
index 90b3bda..d7238d2 100644 (file)
@@ -123,11 +123,11 @@ CsrAllocateThread(IN PCSR_PROCESS CsrProcess)
 
     /* Allocate the structure */
     CsrThread = RtlAllocateHeap(CsrHeap, HEAP_ZERO_MEMORY, sizeof(CSR_THREAD));
-    if (!CsrThread) return(NULL);
+    if (!CsrThread) return NULL;
 
     /* Reference the Thread and Process */
-    CsrThread->ReferenceCount++;
-    CsrProcess->ReferenceCount++;
+    CsrLockedReferenceThread(CsrThread);
+    CsrLockedReferenceProcess(CsrProcess);
 
     /* Set the Parent Process */
     CsrThread->Process = CsrProcess;
@@ -201,9 +201,13 @@ CsrLocateThreadByClientId(OUT PCSR_PROCESS *Process OPTIONAL,
         /* Get the thread */
         FoundThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, HashLinks);
 
+        /* Move to the next entry */
+        NextEntry = NextEntry->Flink;
+
         /* Compare the CID */
-        // FIXME: if (*(PULONGLONG)&FoundThread->ClientId == *(PULONGLONG)ClientId)
-        if (FoundThread->ClientId.UniqueThread == ClientId->UniqueThread)
+        // if (*(PULONGLONG)&FoundThread->ClientId == *(PULONGLONG)ClientId)
+        if ( FoundThread->ClientId.UniqueProcess == ClientId->UniqueProcess &&
+             FoundThread->ClientId.UniqueThread  == ClientId->UniqueThread )
         {
             /* Match found, return the process */
             *Process = FoundThread->Process;
@@ -212,9 +216,6 @@ CsrLocateThreadByClientId(OUT PCSR_PROCESS *Process OPTIONAL,
 //            DPRINT1("Found: %p %p\n", FoundThread, FoundThread->Process);
             return FoundThread;
         }
-
-        /* Next */
-        NextEntry = NextEntry->Flink;
     }
 
     /* Nothing found */
@@ -267,7 +268,7 @@ CsrLocateThreadInProcess(IN PCSR_PROCESS CsrProcess OPTIONAL,
         /* Check for TID Match */
         if (FoundThread->ClientId.UniqueThread == Cid->UniqueThread) break;
 
-        /* Next entry */
+        /* Move to the next entry */
         NextEntry = NextEntry->Flink;
     }
 
@@ -464,8 +465,8 @@ CsrLockedDereferenceThread(IN PCSR_THREAD CsrThread)
     if (!LockCount)
     {
         /* Call the generic cleanup code */
-        CsrThreadRefcountZero(CsrThread);
         CsrAcquireProcessLock();
+        CsrThreadRefcountZero(CsrThread);
     }
 }
 
@@ -579,7 +580,7 @@ CsrCreateRemoteThread(IN HANDLE hThread,
     }
 
     /* Lock the Owner Process */
-    Status = CsrLockProcessByClientId(&ClientId->UniqueProcess, &CsrProcess);
+    Status = CsrLockProcessByClientId(ClientId->UniqueProcess, &CsrProcess);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("No known process for %lx\n", ClientId->UniqueProcess);
@@ -978,7 +979,7 @@ CsrLockThreadByClientId(IN HANDLE Tid,
             break;
         }
 
-        /* Next entry */
+        /* Move to the next entry */
         NextEntry = NextEntry->Flink;
     }
 
@@ -990,7 +991,7 @@ CsrLockThreadByClientId(IN HANDLE Tid,
     {
         /* Reference the found thread */
         Status = STATUS_SUCCESS;
-        CurrentThread->ReferenceCount++;
+        CsrLockedReferenceThread(CurrentThread);
         *CsrThread = CurrentThread;
     }
     else