Author: fireball
Date: Sun Mar 25 17:23:30 2007
New Revision: 26165
URL: 
http://svn.reactos.org/svn/reactos?rev=26165&view=rev
Log:
- Fix (and optimize) KeRemoveByKeyDeviceQueue() routine.
- Add DPRINTs for easier testing/debugging.
Modified:
    trunk/reactos/ntoskrnl/ke/devqueue.c
Modified: trunk/reactos/ntoskrnl/ke/devqueue.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ke/devqueue.c?rev…
==============================================================================
--- trunk/reactos/ntoskrnl/ke/devqueue.c (original)
+++ trunk/reactos/ntoskrnl/ke/devqueue.c Sun Mar 25 17:23:30 2007
@@ -45,6 +45,8 @@
     BOOLEAN Inserted;
     ASSERT_DEVICE_QUEUE(DeviceQueue);
+    DPRINT("KeInsertDeviceQueue() DevQueue %p, Entry %p\n", DeviceQueue,
DeviceQueueEntry);
+
     /* Lock the queue */
     KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
@@ -85,6 +87,8 @@
     KLOCK_QUEUE_HANDLE DeviceLock;
     BOOLEAN Inserted;
     ASSERT_DEVICE_QUEUE(DeviceQueue);
+
+    DPRINT("KeInsertByKeyDeviceQueue() DevQueue %p, Entry %p, SortKey 0x%x\n",
DeviceQueue, DeviceQueueEntry, SortKey);
     /* Lock the queue */
     KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
@@ -129,6 +133,8 @@
     KLOCK_QUEUE_HANDLE DeviceLock;
     ASSERT_DEVICE_QUEUE(DeviceQueue);
+    DPRINT("KeRemoveDeviceQueue() DevQueue %p\n", DeviceQueue);
+
     /* Lock the queue */
     KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
     ASSERT(DeviceQueue->Busy);
@@ -172,12 +178,92 @@
     KLOCK_QUEUE_HANDLE DeviceLock;
     ASSERT_DEVICE_QUEUE(DeviceQueue);
+    DPRINT("KeRemoveByKeyDeviceQueue() DevQueue %p, SortKey 0x%x\n",
DeviceQueue, SortKey);
+
     /* Lock the queue */
     KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
     ASSERT(DeviceQueue->Busy);
     /* Check if this is an empty queue */
     if (IsListEmpty(&DeviceQueue->DeviceListHead))
+    {
+        /* Set it to idle and return nothing*/
+        DeviceQueue->Busy = FALSE;
+        ReturnEntry = NULL;
+    }
+    else
+    {
+        /* If SortKey is greater than the last key, then return the first entry right
away */
+        ListEntry = &DeviceQueue->DeviceListHead;
+        ReturnEntry = CONTAINING_RECORD(ListEntry->Blink,
+                                        KDEVICE_QUEUE_ENTRY,
+                                        DeviceListEntry);
+
+        if (SortKey >= ReturnEntry->SortKey)
+        {
+            ReturnEntry = CONTAINING_RECORD(ListEntry->Flink,
+                                            KDEVICE_QUEUE_ENTRY,
+                                            DeviceListEntry);
+
+            /* Remove it from the list */
+            RemoveEntryList(&ReturnEntry->DeviceListEntry);
+        }
+        else
+        {
+            /* Find entry with SortKey greater than or equal to the passed-in SortKey */
+            LIST_FOR_EACH(ReturnEntry, &DeviceQueue->DeviceListHead,
KDEVICE_QUEUE_ENTRY, DeviceListEntry)
+            {
+                /* Check if keys match */
+                if (ReturnEntry->SortKey >= SortKey)
+                {
+                    /* We found it, so just remove it */
+                    RemoveEntryList(&ReturnEntry->DeviceListEntry);
+                    break;
+                }
+            }
+
+            /* Check if we found something */
+            if (!ReturnEntry)
+            {
+                /*  Not found, return the first entry */
+                ListEntry = RemoveHeadList(&DeviceQueue->DeviceListHead);
+                ReturnEntry = CONTAINING_RECORD(ListEntry,
+                                                KDEVICE_QUEUE_ENTRY,
+                                                DeviceListEntry);
+            }
+        }
+
+        /* Set it as non-inserted */
+        ReturnEntry->Inserted = FALSE;
+    }
+
+    /* Release the lock */
+    KiReleaseDeviceQueueLock(&DeviceLock);
+
+    /* Return the entry */
+    return ReturnEntry;
+}
+
+/*
+ * @implemented
+ */
+PKDEVICE_QUEUE_ENTRY
+NTAPI
+KeRemoveByKeyDeviceQueueIfBusy(IN PKDEVICE_QUEUE DeviceQueue,
+                               IN ULONG SortKey)
+{
+    PLIST_ENTRY ListEntry;
+    PKDEVICE_QUEUE_ENTRY ReturnEntry;
+    KLOCK_QUEUE_HANDLE DeviceLock;
+    ASSERT_DEVICE_QUEUE(DeviceQueue);
+
+    DPRINT("KeRemoveByKeyDeviceQueueIfBusy() DevQueue %p, SortKey 0x%x\n",
DeviceQueue, SortKey);
+
+    /* Lock the queue */
+    KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
+
+    /* Check if this is an empty or idle queue */
+    if (!(DeviceQueue->Busy) || (IsListEmpty(&DeviceQueue->DeviceListHead)))
     {
         /* Set it to idle and return nothing*/
         DeviceQueue->Busy = FALSE;
@@ -221,64 +307,6 @@
 /*
  * @implemented
  */
-PKDEVICE_QUEUE_ENTRY
-NTAPI
-KeRemoveByKeyDeviceQueueIfBusy(IN PKDEVICE_QUEUE DeviceQueue,
-                               IN ULONG SortKey)
-{
-    PLIST_ENTRY ListEntry;
-    PKDEVICE_QUEUE_ENTRY ReturnEntry;
-    KLOCK_QUEUE_HANDLE DeviceLock;
-    ASSERT_DEVICE_QUEUE(DeviceQueue);
-
-    /* Lock the queue */
-    KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);
-
-    /* Check if this is an empty or idle queue */
-    if (!(DeviceQueue->Busy) || (IsListEmpty(&DeviceQueue->DeviceListHead)))
-    {
-        /* Set it to idle and return nothing*/
-        DeviceQueue->Busy = FALSE;
-        ReturnEntry = NULL;
-    }
-    else
-    {
-        /* Find entry with SortKey greater than or equal to the passed-in SortKey */
-        LIST_FOR_EACH(ReturnEntry, &DeviceQueue->DeviceListHead,
KDEVICE_QUEUE_ENTRY, DeviceListEntry)
-        {
-            /* Check if keys match */
-            if (ReturnEntry->SortKey >= SortKey)
-            {
-               /* We found it, so just remove it */
-               RemoveEntryList(&ReturnEntry->DeviceListEntry);
-               break;
-            }
-        }
-
-        /* Check if we found something */
-        if (!ReturnEntry)
-        {
-            /*  Not found, return the first entry */
-            ListEntry = RemoveHeadList(&DeviceQueue->DeviceListHead);
-            ReturnEntry = CONTAINING_RECORD(ListEntry,
-                                            KDEVICE_QUEUE_ENTRY,
-                                            DeviceListEntry);
-        }
-
-        /* Set it as non-inserted */
-        ReturnEntry->Inserted = FALSE;
-    }
-
-    /* Release the lock */
-    KiReleaseDeviceQueueLock(&DeviceLock);
-
-    /* Return the entry */
-    return ReturnEntry;
-}
-
-/*
- * @implemented
- */
 BOOLEAN
 NTAPI
 KeRemoveEntryDeviceQueue(IN PKDEVICE_QUEUE DeviceQueue,
@@ -287,6 +315,8 @@
     BOOLEAN OldState;
     KLOCK_QUEUE_HANDLE DeviceLock;
     ASSERT_DEVICE_QUEUE(DeviceQueue);
+
+    DPRINT("KeRemoveEntryDeviceQueue() DevQueue %p, Entry %p\n", DeviceQueue,
DeviceQueueEntry);
     /* Lock the queue */
     KiAcquireDeviceQueueLock(DeviceQueue, &DeviceLock);