Added process ids.
[reactos.git] / reactos / ntoskrnl / ps / thread.c
index b376880..285c7f2 100644 (file)
@@ -26,6 +26,7 @@
 #include <internal/string.h>
 #include <internal/hal.h>
 #include <internal/ps.h>
+#include <internal/ob.h>
 
 #define NDEBUG
 #include <internal/debug.h>
@@ -51,7 +52,7 @@ ULONG PiNrRunnableThreads = 0;
 
 static PETHREAD CurrentThread = NULL;
 
-static ULONG NextThreadUniqueId = 0;
+static ULONG NextUniqueThreadId = 0;
 
 /* FUNCTIONS ***************************************************************/
 
@@ -62,7 +63,7 @@ PKTHREAD KeGetCurrentThread(VOID)
 
 PETHREAD PsGetCurrentThread(VOID)
 {
-   return((PETHREAD)KeGetCurrentThread());
+   return(CurrentThread);
 }
 
 VOID PiTerminateProcessThreads(PEPROCESS Process, NTSTATUS ExitStatus)
@@ -112,7 +113,7 @@ VOID PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext)
    KeReleaseSpinLock(&ThreadListLock,PASSIVE_LEVEL);
    Ret = StartRoutine(StartContext);
    PsTerminateSystemThread(Ret);
-   for(;;);
+   KeBugCheck(0);
 }
 
 static PETHREAD PsScanThreadList(KPRIORITY Priority)
@@ -179,7 +180,7 @@ VOID PsDispatchThread(VOID)
          {
              DPRINT("Scheduling current thread\n");
              KeQueryTickCount(&TickCount);
-             CurrentThread->Tcb.LastTick = GET_LARGE_INTEGER_LOW_PART(TickCount);
+             CurrentThread->Tcb.LastTick = TickCount.LowPart;
             CurrentThread->Tcb.ThreadState = THREAD_STATE_RUNNING;
             KeReleaseSpinLock(&ThreadListLock,irql);
             return;
@@ -191,7 +192,7 @@ VOID PsDispatchThread(VOID)
             Candidate->Tcb.ThreadState = THREAD_STATE_RUNNING;
             
             KeQueryTickCount(&TickCount);
-            CurrentThread->Tcb.LastTick = GET_LARGE_INTEGER_LOW_PART(TickCount);
+             CurrentThread->Tcb.LastTick = TickCount.LowPart;
             
             CurrentThread = Candidate;
             
@@ -226,7 +227,7 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle,
    Thread->Tcb.CurrentPriority=THREAD_PRIORITY_NORMAL;
    Thread->Tcb.ApcList=ExAllocatePool(NonPagedPool,sizeof(LIST_ENTRY));
    Thread->Tcb.SuspendCount = 1;
-   if (ProcessHandle!=NULL)
+   if (ProcessHandle != NULL)
      {
        Status = ObReferenceObjectByHandle(ProcessHandle,
                                           PROCESS_CREATE_THREAD,
@@ -242,7 +243,7 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle,
      }
    else
      {
-       Thread->ThreadsProcess=SystemProcess;
+       Thread->ThreadsProcess = SystemProcess;
        ObReferenceObjectByPointer(Thread->ThreadsProcess,
                                   PROCESS_CREATE_THREAD,
                                   PsProcessType,
@@ -255,7 +256,10 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle,
    InitializeListHead(Thread->Tcb.ApcList);
    InitializeListHead(&(Thread->IrpList));
    Thread->Cid.UniqueThread = (HANDLE)InterlockedIncrement(
-                                                         &NextThreadUniqueId);
+                                                          &NextUniqueThreadId);
+   Thread->Cid.UniqueProcess = (HANDLE)Thread->ThreadsProcess->UniqueProcessId;
+   DbgPrint("Thread->Cid.UniqueThread %d\nThread->Cid.UniqueProcess %d\n",
+            Thread->Cid.UniqueThread, Thread->Cid.UniqueThread);
    ObReferenceObjectByPointer(Thread,
                              THREAD_ALL_ACCESS,
                              PsThreadType,
@@ -264,19 +268,15 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle,
    
    *ThreadPtr = Thread;
    
-   ObDereferenceObject(Thread->ThreadsProcess);   
+   ObDereferenceObject(Thread->ThreadsProcess);
    return(STATUS_SUCCESS);
 }
 
 VOID PsResumeThread(PETHREAD Thread)
 {
    DPRINT("PsResumeThread(Thread %x)\n",Thread);
-   
    Thread->Tcb.SuspendCount--;
-   DPRINT("Thread->Tcb.SuspendCount %d\n",Thread->Tcb.SuspendCount);
-   DPRINT("Thread->Tcb.ThreadState %d THREAD_STATE_RUNNING %d\n",
-           Thread->Tcb.ThreadState,THREAD_STATE_RUNNING);
-   if (Thread->Tcb.SuspendCount <= 0 && 
+   if (Thread->Tcb.SuspendCount <= 0 &&
        Thread->Tcb.ThreadState != THREAD_STATE_RUNNING)
      {
         DPRINT("Setting thread to runnable\n");
@@ -299,7 +299,12 @@ VOID PsSuspendThread(PETHREAD Thread)
      }
 }
 
-void PsInitThreadManagment(void)
+VOID PiDeleteThread(PVOID ObjectBody)
+{
+   DbgPrint("PiDeleteThread(ObjectBody %x)\n",ObjectBody);
+}
+
+VOID PsInitThreadManagment(VOID)
 /*
  * FUNCTION: Initialize thread managment
  */
@@ -329,7 +334,7 @@ void PsInitThreadManagment(void)
    PsThreadType->Dump = NULL;
    PsThreadType->Open = NULL;
    PsThreadType->Close = NULL;
-   PsThreadType->Delete = NULL;
+   PsThreadType->Delete = PiDeleteThread;
    PsThreadType->Parse = NULL;
    PsThreadType->Security = NULL;
    PsThreadType->QueryName = NULL;
@@ -384,12 +389,16 @@ NTSTATUS ZwCreateThread(PHANDLE ThreadHandle,
    
    Status = PsInitializeThread(ProcessHandle,&Thread,ThreadHandle,
                               DesiredAccess,ObjectAttributes);
-   if (Status != STATUS_SUCCESS)
+   if (!NT_SUCCESS(Status))
      {
        return(Status);
      }
    
-   HalInitTaskWithContext(Thread,ThreadContext);
+   Status = HalInitTaskWithContext(Thread,ThreadContext);
+   if (!NT_SUCCESS(Status))
+     {
+       return(Status);
+     }
    Thread->StartAddress=NULL;
 
    if (Client!=NULL)
@@ -438,13 +447,17 @@ NTSTATUS PsCreateSystemThread(PHANDLE ThreadHandle,
    
    Status = PsInitializeThread(ProcessHandle,&Thread,ThreadHandle,
                               DesiredAccess,ObjectAttributes);
-   if (Status != STATUS_SUCCESS)
+   if (!NT_SUCCESS(Status))
      {
        return(Status);
      }
    
    Thread->StartAddress=StartRoutine;
-   HalInitTask(Thread,StartRoutine,StartContext);
+   Status = HalInitTask(Thread,StartRoutine,StartContext);
+   if (!NT_SUCCESS(Status))
+     {
+       return(Status);
+     }
 
    if (ClientId!=NULL)
      {