Removed debug messages from queuing code (sorry)
[reactos.git] / reactos / ntoskrnl / ke / kqueue.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PURPOSE: ReactOS kernel
4 * FILE: ntoskrnl/ke/kqueue.c
5 * PURPOSE: Implement device queues
6 * PROGRAMMER: David Welch (welch@mcmail.com)
7 * REVISION HISTORY:
8 * 08/07/98: Created
9 */
10
11 /* INCLUDES ****************************************************************/
12
13 #include <ddk/ntddk.h>
14
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* FUNCTIONS *****************************************************************/
19
20 VOID InsertBeforeEntryInList(PLIST_ENTRY Head, PLIST_ENTRY After,
21 PLIST_ENTRY Entry)
22 {
23 InsertHeadList(After, Entry);
24 }
25
26 BOOLEAN KeInsertByKeyDeviceQueue(PKDEVICE_QUEUE DeviceQueue,
27 PKDEVICE_QUEUE_ENTRY DeviceQueueEntry,
28 ULONG SortKey)
29 {
30 KIRQL oldlvl;
31 PLIST_ENTRY current;
32 PKDEVICE_QUEUE_ENTRY entry;
33
34 DPRINT("KeInsertByKeyDeviceQueue()\n");
35
36 DeviceQueueEntry->Key=SortKey;
37
38 KeAcquireSpinLock(&DeviceQueue->Lock,&oldlvl);
39
40 if (!DeviceQueue->Busy)
41 {
42 DeviceQueue->Busy=TRUE;
43 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
44 return(FALSE);
45 }
46
47 current=DeviceQueue->ListHead.Flink;
48 while (current!=(&DeviceQueue->ListHead))
49 {
50 entry = CONTAINING_RECORD(current,KDEVICE_QUEUE_ENTRY,Entry);
51 if (entry->Key < SortKey)
52 {
53 InsertBeforeEntryInList(&DeviceQueue->ListHead,
54 &DeviceQueueEntry->Entry,
55 current);
56 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
57 return(TRUE);
58 }
59 current = current->Flink;
60 }
61 InsertTailList(&DeviceQueue->ListHead,&DeviceQueueEntry->Entry);
62
63 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
64 return(TRUE);
65 }
66
67 PKDEVICE_QUEUE_ENTRY KeRemoveByKeyDeviceQueue(PKDEVICE_QUEUE DeviceQueue,
68 ULONG SortKey)
69 {
70 KIRQL oldlvl;
71 PLIST_ENTRY current;
72 PKDEVICE_QUEUE_ENTRY entry;
73
74 assert_irql(DISPATCH_LEVEL);
75 assert(DeviceQueue!=NULL);
76 assert(DeviceQueue->Busy);
77
78 KeAcquireSpinLock(&DeviceQueue->Lock,&oldlvl);
79
80 current = DeviceQueue->ListHead.Flink;
81 while (current != &DeviceQueue->ListHead)
82 {
83 entry = CONTAINING_RECORD(current,KDEVICE_QUEUE_ENTRY,Entry);
84 if (entry->Key < SortKey ||
85 current->Flink == &DeviceQueue->ListHead)
86 {
87 RemoveEntryList(current);
88 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
89 return(entry);
90 }
91 current = current->Flink;
92 }
93 DeviceQueue->Busy = FALSE;
94 KeReleaseSpinLock(&DeviceQueue->Lock, oldlvl);
95 return(NULL);
96 }
97
98 PKDEVICE_QUEUE_ENTRY KeRemoveDeviceQueue(PKDEVICE_QUEUE DeviceQueue)
99 /*
100 * FUNCTION: Removes an entry from a device queue
101 * ARGUMENTS:
102 * DeviceQueue = Queue to remove the entry
103 * RETURNS: The removed entry
104 */
105 {
106 KIRQL oldlvl;
107 PLIST_ENTRY list_entry;
108 PKDEVICE_QUEUE_ENTRY entry;
109
110 DPRINT("KeRemoveDeviceQueue(DeviceQueue %x)\n",DeviceQueue);
111
112 assert_irql(DISPATCH_LEVEL);
113 assert(DeviceQueue!=NULL);
114 assert(DeviceQueue->Busy);
115
116 KeAcquireSpinLock(&DeviceQueue->Lock,&oldlvl);
117
118 list_entry = RemoveHeadList(&DeviceQueue->ListHead);
119 if (list_entry==(&DeviceQueue->ListHead))
120 {
121 DeviceQueue->Busy=FALSE;
122 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
123 return(NULL);
124 }
125 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
126
127 entry = CONTAINING_RECORD(list_entry,KDEVICE_QUEUE_ENTRY,Entry);
128 return(entry);
129 }
130
131 VOID KeInitializeDeviceQueue(PKDEVICE_QUEUE DeviceQueue)
132 /*
133 * FUNCTION: Intializes a device queue
134 * ARGUMENTS:
135 * DeviceQueue = Device queue to initialize
136 */
137 {
138 assert(DeviceQueue!=NULL);
139 InitializeListHead(&DeviceQueue->ListHead);
140 DeviceQueue->Busy=FALSE;
141 KeInitializeSpinLock(&DeviceQueue->Lock);
142 }
143
144 BOOLEAN KeInsertDeviceQueue(PKDEVICE_QUEUE DeviceQueue,
145 PKDEVICE_QUEUE_ENTRY DeviceQueueEntry)
146 /*
147 * FUNCTION: Inserts an entry in a device queue
148 * ARGUMENTS:
149 * DeviceQueue = Queue to insert the entry in
150 * DeviceQueueEntry = Entry to insert
151 * RETURNS: False is the device queue wasn't busy
152 * True otherwise
153 */
154 {
155 KIRQL oldlvl;
156
157 KeAcquireSpinLock(&DeviceQueue->Lock,&oldlvl);
158
159 if (!DeviceQueue->Busy)
160 {
161 DeviceQueue->Busy=TRUE;
162 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
163 return(FALSE);
164 }
165
166 InsertTailList(&DeviceQueue->ListHead,
167 &DeviceQueueEntry->Entry);
168 DeviceQueueEntry->Key=0;
169
170 KeReleaseSpinLock(&DeviceQueue->Lock,oldlvl);
171 return(TRUE);
172 }