3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/queue.c
23 * PURPOSE: Implements kernel queues
24 * PROGRAMMER: Eric Kohl (ekohl@rz-online.de)
29 /* INCLUDES *****************************************************************/
31 #include <ddk/ntddk.h>
33 #include <internal/ke.h>
34 #include <internal/id.h>
35 #include <internal/ps.h>
38 #include <internal/debug.h>
40 /* FUNCTIONS *****************************************************************/
46 KeInitializeQueue(IN PKQUEUE Queue,
47 IN ULONG Count OPTIONAL)
49 KeInitializeDispatcherHeader(&Queue->Header,
51 sizeof(KQUEUE)/sizeof(ULONG),
53 InitializeListHead(&Queue->EntryListHead);
54 InitializeListHead(&Queue->ThreadListHead);
55 Queue->CurrentCount = 0;
56 Queue->MaximumCount = (Count == 0) ? (ULONG) KeNumberProcessors : Count;
64 KeReadStateQueue(IN PKQUEUE Queue)
66 return(Queue->Header.SignalState);
79 DPRINT("KiInsertQueue(Queue %x, Entry %x)\n", Queue, Entry);
81 KeAcquireDispatcherDatabaseLock(FALSE);
83 InitialState = Queue->Header.SignalState;
84 Queue->Header.SignalState++;
88 InsertHeadList(&Queue->EntryListHead, Entry);
92 InsertTailList(&Queue->EntryListHead, Entry);
95 if (Queue->CurrentCount < Queue->MaximumCount && InitialState == 0)
97 KeDispatcherObjectWake(&Queue->Header);
100 KeReleaseDispatcherDatabaseLock(FALSE);
110 KeInsertHeadQueue(IN PKQUEUE Queue,
111 IN PLIST_ENTRY Entry)
113 return KiInsertQueue(Queue,Entry,TRUE);
121 KeInsertQueue(IN PKQUEUE Queue,
122 IN PLIST_ENTRY Entry)
124 return KiInsertQueue(Queue,Entry,FALSE);
132 KeRemoveQueue(IN PKQUEUE Queue,
133 IN KPROCESSOR_MODE WaitMode,
134 IN PLARGE_INTEGER Timeout OPTIONAL)
136 PLIST_ENTRY ListEntry;
138 PKTHREAD Thread = KeGetCurrentThread();
140 KeAcquireDispatcherDatabaseLock(FALSE);
142 //assiciate new thread with queue?
143 if (Thread->Queue != Queue)
145 //remove association from other queue
146 if (!IsListEmpty(&Thread->QueueListEntry))
148 RemoveEntryList(&Thread->QueueListEntry);
151 //associate with this queue
152 InsertHeadList(&Queue->ThreadListHead, &Thread->QueueListEntry);
153 Queue->CurrentCount++;
154 Thread->Queue = Queue;
157 if (Queue->CurrentCount <= Queue->MaximumCount && !IsListEmpty(&Queue->EntryListHead))
159 ListEntry = RemoveHeadList(&Queue->EntryListHead);
160 Queue->Header.SignalState--;
161 KeReleaseDispatcherDatabaseLock(FALSE);
165 //need to wait for it...
166 KeReleaseDispatcherDatabaseLock(FALSE);
168 Status = KeWaitForSingleObject(Queue,
174 if (Status == STATUS_TIMEOUT || Status == STATUS_USER_APC)
176 return (PVOID)Status;
180 KeAcquireDispatcherDatabaseLock(FALSE);
181 ListEntry = RemoveHeadList(&Queue->EntryListHead);
182 KeReleaseDispatcherDatabaseLock(FALSE);
193 KeRundownQueue(IN PKQUEUE Queue)
195 PLIST_ENTRY EnumEntry;
198 DPRINT("KeRundownQueue(Queue %x)\n", Queue);
200 //FIXME: should we wake thread waiting on a queue?
202 KeAcquireDispatcherDatabaseLock(FALSE);
204 // Clear Queue and QueueListEntry members of all threads associated with this queue
205 while (!IsListEmpty(&Queue->ThreadListHead))
207 EnumEntry = RemoveHeadList(&Queue->ThreadListHead);
208 InitializeListHead(EnumEntry);
209 Thread = CONTAINING_RECORD(EnumEntry, KTHREAD, QueueListEntry);
210 Thread->Queue = NULL;
213 if (!IsListEmpty(&Queue->EntryListHead))
214 EnumEntry = Queue->EntryListHead.Flink;
218 KeReleaseDispatcherDatabaseLock(FALSE);