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;
65 KeReadStateQueue(IN PKQUEUE Queue)
67 return(Queue->Header.SignalState);
80 DPRINT("KiInsertQueue(Queue %x, Entry %x)\n", Queue, Entry);
82 KeAcquireDispatcherDatabaseLock(FALSE);
84 InitialState = Queue->Header.SignalState;
85 Queue->Header.SignalState++;
89 InsertHeadList(&Queue->EntryListHead, Entry);
93 InsertTailList(&Queue->EntryListHead, Entry);
96 if (Queue->CurrentCount < Queue->MaximumCount && InitialState == 0)
98 KeDispatcherObjectWake(&Queue->Header);
101 KeReleaseDispatcherDatabaseLock(FALSE);
111 KeInsertHeadQueue(IN PKQUEUE Queue,
112 IN PLIST_ENTRY Entry)
114 return KiInsertQueue(Queue,Entry,TRUE);
122 KeInsertQueue(IN PKQUEUE Queue,
123 IN PLIST_ENTRY Entry)
125 return KiInsertQueue(Queue,Entry,FALSE);
133 KeRemoveQueue(IN PKQUEUE Queue,
134 IN KPROCESSOR_MODE WaitMode,
135 IN PLARGE_INTEGER Timeout OPTIONAL)
137 PLIST_ENTRY ListEntry;
139 PKTHREAD Thread = KeGetCurrentThread();
141 KeAcquireDispatcherDatabaseLock(FALSE);
143 //assiciate new thread with queue?
144 if (Thread->Queue != Queue)
146 //remove association from other queue
147 if (!IsListEmpty(&Thread->QueueListEntry))
149 RemoveEntryList(&Thread->QueueListEntry);
152 //associate with this queue
153 InsertHeadList(&Queue->ThreadListHead, &Thread->QueueListEntry);
154 Queue->CurrentCount++;
155 Thread->Queue = Queue;
158 if (Queue->CurrentCount <= Queue->MaximumCount && !IsListEmpty(&Queue->EntryListHead))
160 ListEntry = RemoveHeadList(&Queue->EntryListHead);
161 Queue->Header.SignalState--;
162 KeReleaseDispatcherDatabaseLock(FALSE);
166 //need to wait for it...
167 KeReleaseDispatcherDatabaseLock(FALSE);
169 Status = KeWaitForSingleObject(Queue,
175 if (Status == STATUS_TIMEOUT || Status == STATUS_USER_APC)
177 return (PVOID)Status;
181 KeAcquireDispatcherDatabaseLock(FALSE);
182 ListEntry = RemoveHeadList(&Queue->EntryListHead);
183 KeReleaseDispatcherDatabaseLock(FALSE);
194 KeRundownQueue(IN PKQUEUE Queue)
196 PLIST_ENTRY EnumEntry;
199 DPRINT("KeRundownQueue(Queue %x)\n", Queue);
201 //FIXME: should we wake thread waiting on a queue?
203 KeAcquireDispatcherDatabaseLock(FALSE);
205 // Clear Queue and QueueListEntry members of all threads associated with this queue
206 while (!IsListEmpty(&Queue->ThreadListHead))
208 EnumEntry = RemoveHeadList(&Queue->ThreadListHead);
209 InitializeListHead(EnumEntry);
210 Thread = CONTAINING_RECORD(EnumEntry, KTHREAD, QueueListEntry);
211 Thread->Queue = NULL;
214 if (!IsListEmpty(&Queue->EntryListHead))
215 EnumEntry = Queue->EntryListHead.Flink;
219 KeReleaseDispatcherDatabaseLock(FALSE);
224 #endif /* LIBCAPTIVE */