Protected the handle table.
Modified: trunk/reactos/subsys/csrss/api/handle.c
Modified: trunk/reactos/subsys/csrss/api/process.c
Modified: trunk/reactos/subsys/csrss/include/api.h

Modified: trunk/reactos/subsys/csrss/api/handle.c
--- trunk/reactos/subsys/csrss/api/handle.c	2005-05-26 17:17:29 UTC (rev 15525)
+++ trunk/reactos/subsys/csrss/api/handle.c	2005-05-26 19:22:45 UTC (rev 15526)
@@ -123,8 +123,10 @@
     }
 
   Status = CsrReleaseObjectByPointer(ProcessData->HandleTable[h]);
+
+  RtlEnterCriticalSection(&ProcessData->HandleTableLock);
   ProcessData->HandleTable[h] = 0;
-
+  RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
   return Status;
 }
 
@@ -138,33 +140,36 @@
       return STATUS_INVALID_PARAMETER;
    }
 
+   RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+
    for (i = 0; i < ProcessData->HandleTableSize; i++)
      {
 	if (ProcessData->HandleTable[i] == NULL)
 	  {
-	     ProcessData->HandleTable[i] = Object;
-	     *Handle = (HANDLE)(((i + 1) << 2) | 0x3);
-	     InterlockedIncrement( &Object->ReferenceCount );
-	     return(STATUS_SUCCESS);
+            break;
 	  }
      }
-   NewBlock = RtlAllocateHeap(CsrssApiHeap,
-			      HEAP_ZERO_MEMORY,
-			      (ProcessData->HandleTableSize + 64) *
-			      sizeof(HANDLE));
-   if (NewBlock == NULL)
+   if (i >= ProcessData->HandleTableSize)
      {
-	return(STATUS_UNSUCCESSFUL);
+       NewBlock = RtlAllocateHeap(CsrssApiHeap,
+			          HEAP_ZERO_MEMORY,
+			          (ProcessData->HandleTableSize + 64) * sizeof(HANDLE));
+       if (NewBlock == NULL)
+         {
+           RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+	   return(STATUS_UNSUCCESSFUL);
+         }
+       RtlCopyMemory(NewBlock,
+		     ProcessData->HandleTable,
+		     ProcessData->HandleTableSize * sizeof(HANDLE));
+       RtlFreeHeap( CsrssApiHeap, 0, ProcessData->HandleTable );
+       ProcessData->HandleTable = (Object_t **)NewBlock;
+       ProcessData->HandleTableSize += 64;
      }
-   RtlCopyMemory(NewBlock,
-		 ProcessData->HandleTable,
-		 ProcessData->HandleTableSize * sizeof(HANDLE));
-   RtlFreeHeap( CsrssApiHeap, 0, ProcessData->HandleTable );
-   ProcessData->HandleTable = (Object_t **)NewBlock;
    ProcessData->HandleTable[i] = Object;
    *Handle = (HANDLE)(((i + 1) << 2) | 0x3);
    InterlockedIncrement( &Object->ReferenceCount );
-   ProcessData->HandleTableSize = ProcessData->HandleTableSize + 64;
+   RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
    return(STATUS_SUCCESS);
 }
 

Modified: trunk/reactos/subsys/csrss/api/process.c
--- trunk/reactos/subsys/csrss/api/process.c	2005-05-26 17:17:29 UTC (rev 15525)
+++ trunk/reactos/subsys/csrss/api/process.c	2005-05-26 19:22:45 UTC (rev 15526)
@@ -103,7 +103,8 @@
             ProcessData[hash] = pProcessData->next;
 	    RtlFreeHeap(CsrssApiHeap, 0, pProcessData);
 	    pProcessData = NULL;
-        }
+         }
+         RtlInitializeCriticalSection(&pProcessData->HandleTableLock);
       }
    }
    else
@@ -160,6 +161,7 @@
             }
           RtlFreeHeap(CsrssApiHeap, 0, pProcessData->HandleTable);
         }
+      RtlDeleteCriticalSection(&pProcessData->HandleTableLock);
       if (pProcessData->Console)
         {
           CsrReleaseObjectByPointer((Object_t *) pProcessData->Console);
@@ -254,7 +256,6 @@
        if( !NT_SUCCESS( Status ) )
 	 {
 	   DbgPrint( "CSR: NtDuplicateObject() failed: %x\n", Status );
-	   InterlockedDecrement( &(NewProcessData->Console->Header.ReferenceCount) );
 	   CsrFreeProcessData( NewProcessData->ProcessId );
 	   Reply->Status = Status;
 	   return Status;

Modified: trunk/reactos/subsys/csrss/include/api.h
--- trunk/reactos/subsys/csrss/include/api.h	2005-05-26 17:17:29 UTC (rev 15525)
+++ trunk/reactos/subsys/csrss/include/api.h	2005-05-26 19:22:45 UTC (rev 15526)
@@ -34,6 +34,7 @@
 typedef struct _CSRSS_PROCESS_DATA
 {
   PCSRSS_CONSOLE Console;
+  RTL_CRITICAL_SECTION HandleTableLock;
   ULONG HandleTableSize;
   Object_t ** HandleTable;
   HANDLE ProcessId;