- Set the right Thread->WaitTime dring waits
- Initialize WaitBlock->Thread during thread creation.
- Make APCs queuable for the thread after it's created
- Enable Timer Block optimization since it works now. This allows us not to always set-up for each wait, since most of its fields can remain static.
- Properly link wait block together with the waitlist of the timer.
Modified: trunk/reactos/ntoskrnl/ke/gate.c
Modified: trunk/reactos/ntoskrnl/ke/kthread.c
Modified: trunk/reactos/ntoskrnl/ke/queue.c
Modified: trunk/reactos/ntoskrnl/ke/wait.c

Modified: trunk/reactos/ntoskrnl/ke/gate.c
--- trunk/reactos/ntoskrnl/ke/gate.c	2006-01-06 22:41:42 UTC (rev 20631)
+++ trunk/reactos/ntoskrnl/ke/gate.c	2006-01-06 22:57:21 UTC (rev 20632)
@@ -63,13 +63,12 @@
         GateWaitBlock->Thread = CurrentThread;
 
         /* Set the Thread Wait Data */
-        CurrentThread->WaitReason = WaitReason;
-        CurrentThread->WaitMode = WaitMode;
         CurrentThread->WaitIrql = OldIrql;
         CurrentThread->GateObject = Gate;
 
         /* Insert into the Wait List */
-        InsertTailList(&Gate->Header.WaitListHead, &GateWaitBlock->WaitListEntry);
+        InsertTailList(&Gate->Header.WaitListHead,
+                       &GateWaitBlock->WaitListEntry);
 
         /* Handle Kernel Queues */
         if (CurrentThread->Queue)
@@ -81,7 +80,7 @@
         /* Setup the wait information */
         CurrentThread->WaitMode = WaitMode;
         CurrentThread->WaitReason = WaitReason;
-        CurrentThread->WaitTime = 0;
+        CurrentThread->WaitTime = ((PLARGE_INTEGER)&KeTickCount)->LowPart;
         CurrentThread->State = Waiting;
 
         /* Find a new thread to run */

Modified: trunk/reactos/ntoskrnl/ke/kthread.c
--- trunk/reactos/ntoskrnl/ke/kthread.c	2006-01-06 22:41:42 UTC (rev 20631)
+++ trunk/reactos/ntoskrnl/ke/kthread.c	2006-01-06 22:57:21 UTC (rev 20632)
@@ -19,7 +19,9 @@
 #define THREAD_ALERT_INCREMENT 2
 
 extern EX_WORK_QUEUE ExWorkerQueue[MaximumWorkQueue];
+#define TIMER_WAIT_BLOCK 0x3L
 
+
 /*
  * PURPOSE: List of threads associated with each priority level
  */
@@ -789,6 +791,10 @@
                    PVOID Teb,
                    PVOID KernelStack)
 {
+    ULONG i;
+    PKWAIT_BLOCK TimerWaitBlock;
+    PKTIMER Timer;
+
     /* Initalize the Dispatcher Header */
     DPRINT("Initializing Dispatcher Header for New Thread: %x in Process: %x\n", Thread, Process);
     KeInitializeDispatcherHeader(&Thread->DispatcherHeader,
@@ -803,6 +809,13 @@
     /* Initialize the Mutant List */
     InitializeListHead(&Thread->MutantListHead);
 
+    /* Initialize the wait blocks */
+    for (i = 0; i< (THREAD_WAIT_OBJECTS + 1); i++)
+    {
+        /* Put our pointer */
+        Thread->WaitBlock[i].Thread = Thread;
+    }
+
     /* Setup the Service Descriptor Table for Native Calls */
     Thread->ServiceTable = KeServiceDescriptorTable;
 
@@ -813,6 +826,7 @@
     Thread->ApcStatePointer[OriginalApcEnvironment] = &Thread->ApcState;
     Thread->ApcStatePointer[AttachedApcEnvironment] = &Thread->SavedApcState;
     Thread->ApcStateIndex = OriginalApcEnvironment;
+    Thread->ApcQueueable = TRUE;
     KeInitializeSpinLock(&Thread->ApcQueueLock);
 
     /* Initialize the Suspend APC */
@@ -829,17 +843,18 @@
     KeInitializeSemaphore(&Thread->SuspendSemaphore, 0, 128);
 
     /* FIXME OPTIMIZATION OF DOOM. DO NOT ENABLE FIXME */
-#if 0
-    Thread->WaitBlock[3].Object = (PVOID)&Thread->Timer;
-    Thread->WaitBlock[3].Thread = Thread;
-    Thread->WaitBlock[3].WaitKey = STATUS_TIMEOUT;
-    Thread->WaitBlock[3].WaitType = WaitAny;
-    Thread->WaitBlock[3].NextWaitBlock = NULL;
-    InsertTailList(&Thread->Timer.Header.WaitListHead,
-                   &Thread->WaitBlock[3].WaitListEntry);
-#endif
-    KeInitializeTimer(&Thread->Timer);
+    Timer = &Thread->Timer;
+    KeInitializeTimer(Timer);
+    TimerWaitBlock = &Thread->WaitBlock[TIMER_WAIT_BLOCK];
+    TimerWaitBlock->Object = Timer;
+    TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
+    TimerWaitBlock->WaitType = WaitAny;
+    TimerWaitBlock->NextWaitBlock = NULL;
 
+    /* Link the two wait lists together */
+    TimerWaitBlock->WaitListEntry.Flink = &Timer->Header.WaitListHead;
+    TimerWaitBlock->WaitListEntry.Blink = &Timer->Header.WaitListHead;
+
     /* Set the TEB */
     Thread->Teb = Teb;
 

Modified: trunk/reactos/ntoskrnl/ke/queue.c
--- trunk/reactos/ntoskrnl/ke/queue.c	2006-01-06 22:41:42 UTC (rev 20631)
+++ trunk/reactos/ntoskrnl/ke/queue.c	2006-01-06 22:57:21 UTC (rev 20632)
@@ -270,8 +270,8 @@
                 WaitBlock->WaitType = WaitAny;
 
                 /* Link the timer to this Wait Block */
-                InitializeListHead(&Timer->Header.WaitListHead);
-                InsertTailList(&Timer->Header.WaitListHead, &WaitBlock->WaitListEntry);
+                Timer->Header.WaitListHead.Flink = &WaitBlock->WaitListEntry;
+                Timer->Header.WaitListHead.Blink = &WaitBlock->WaitListEntry;
 
                 /* Create Timer */
                 DPRINT("Creating Timer with timeout %I64d\n", *Timeout);
@@ -290,7 +290,7 @@
             Thread->WaitMode = WaitMode;
             Thread->WaitReason = WrQueue;
             Thread->Alertable = FALSE;
-            Thread->WaitTime = 0;
+            Thread->WaitTime = ((PLARGE_INTEGER)&KeTickCount)->LowPart;
             Thread->State = Waiting;
 
             /* Find a new thread to run */

Modified: trunk/reactos/ntoskrnl/ke/wait.c
--- trunk/reactos/ntoskrnl/ke/wait.c	2006-01-06 22:41:42 UTC (rev 20631)
+++ trunk/reactos/ntoskrnl/ke/wait.c	2006-01-06 22:57:21 UTC (rev 20632)
@@ -148,15 +148,11 @@
 
         /* Setup the Wait Block */
         CurrentThread->WaitBlockList = TimerWaitBlock;
-        TimerWaitBlock->Object = (PVOID)ThreadTimer;
-        TimerWaitBlock->Thread = CurrentThread;
-        TimerWaitBlock->WaitKey = (USHORT)STATUS_TIMEOUT;
-        TimerWaitBlock->WaitType = WaitAny;
         TimerWaitBlock->NextWaitBlock = TimerWaitBlock;
 
         /* Link the timer to this Wait Block */
-        InitializeListHead(&ThreadTimer->Header.WaitListHead);
-        InsertTailList(&ThreadTimer->Header.WaitListHead, &TimerWaitBlock->WaitListEntry);
+        ThreadTimer->Header.WaitListHead.Flink = &TimerWaitBlock->WaitListEntry;
+        ThreadTimer->Header.WaitListHead.Blink = &TimerWaitBlock->WaitListEntry;
 
         /* Insert the Timer into the Timer Lists and enable it */
         if (!KiInsertTimer(ThreadTimer, *Interval))
@@ -177,7 +173,7 @@
         CurrentThread->Alertable = Alertable;
         CurrentThread->WaitMode = WaitMode;
         CurrentThread->WaitReason = DelayExecution;
-        CurrentThread->WaitTime = 0;
+        CurrentThread->WaitTime = ((PLARGE_INTEGER)&KeTickCount)->LowPart;
         CurrentThread->State = Waiting;
 
         /* Find a new thread to run */
@@ -336,16 +332,11 @@
             WaitBlock->NextWaitBlock = TimerWaitBlock;
 
             /* Set up the Timer Wait Block */
-            TimerWaitBlock->Object = (PVOID)ThreadTimer;
-            TimerWaitBlock->Thread = CurrentThread;
-            TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
-            TimerWaitBlock->WaitType = WaitAny;
             TimerWaitBlock->NextWaitBlock = WaitBlock;
 
             /* Link the timer to this Wait Block */
-            InitializeListHead(&ThreadTimer->Header.WaitListHead);
-            InsertTailList(&ThreadTimer->Header.WaitListHead,
-                           &TimerWaitBlock->WaitListEntry);
+            ThreadTimer->Header.WaitListHead.Flink = &TimerWaitBlock->WaitListEntry;
+            ThreadTimer->Header.WaitListHead.Blink = &TimerWaitBlock->WaitListEntry;
 
             /* Insert the Timer into the Timer Lists and enable it */
             if (!KiInsertTimer(ThreadTimer, *Timeout))
@@ -371,7 +362,7 @@
         CurrentThread->Alertable = Alertable;
         CurrentThread->WaitMode = WaitMode;
         CurrentThread->WaitReason = WaitReason;
-        CurrentThread->WaitTime = 0;
+        CurrentThread->WaitTime = ((PLARGE_INTEGER)&KeTickCount)->LowPart;
         CurrentThread->State = Waiting;
 
         /* Find a new thread to run */
@@ -610,13 +601,9 @@
             WaitBlock->NextWaitBlock = TimerWaitBlock;
 
             /* Set up the Timer Wait Block */
-            TimerWaitBlock->Object = (PVOID)ThreadTimer;
-            TimerWaitBlock->Thread = CurrentThread;
-            TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
-            TimerWaitBlock->WaitType = WaitAny;
             TimerWaitBlock->NextWaitBlock = WaitBlockArray;
 
-            /* Link the timer to this Wait Block */
+            /* Initialize the list head */
             InitializeListHead(&ThreadTimer->Header.WaitListHead);
 
             /* Insert the Timer into the Timer Lists and enable it */
@@ -655,7 +642,7 @@
         CurrentThread->Alertable = Alertable;
         CurrentThread->WaitMode = WaitMode;
         CurrentThread->WaitReason = WaitReason;
-        CurrentThread->WaitTime = 0;
+        CurrentThread->WaitTime = ((PLARGE_INTEGER)&KeTickCount)->LowPart;
         CurrentThread->State = Waiting;
 
         /* Find a new thread to run */