3 * Copyright (C) 2000, 1999, 1998 David Welch <welch@cwcom.net>,
4 * Philip Susi <phreak@iag.net>,
5 * Eric Kohl <ekohl@abo.rhein-zeitung.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * COPYRIGHT: See COPYING in the top level directory
24 * PROJECT: ReactOS kernel
25 * FILE: ntoskrnl/ke/dpc.c
26 * PURPOSE: Handle DPCs (Delayed Procedure Calls)
27 * PROGRAMMER: David Welch (welch@mcmail.com)
30 * 12/3/99: Phillip Susi: Fixed IRQL problem
34 * NOTE: See also the higher level support routines in ntoskrnl/io/dpc.c
37 /* INCLUDES ***************************************************************/
39 #include <ddk/ntddk.h>
40 #include <internal/ps.h>
43 #include <internal/debug.h>
45 /* TYPES *******************************************************************/
47 /* GLOBALS ******************************************************************/
51 static LIST_ENTRY DpcQueueHead; /* Head of the list of pending DPCs */
52 #endif /* LIBCAPTIVE */
53 static KSPIN_LOCK DpcQueueLock; /* Lock for the above list */
55 * Number of pending DPCs. This is inspected by
56 * the idle thread to determine if the queue needs to
59 ULONG DpcQueueSize = 0;
61 /* FUNCTIONS ****************************************************************/
67 KeInitializeDpc (PKDPC Dpc,
68 PKDEFERRED_ROUTINE DeferredRoutine,
69 PVOID DeferredContext)
71 * FUNCTION: Initalizes a DPC
73 * Dpc = Caller supplied DPC to be initialized
74 * DeferredRoutine = Associated DPC callback
75 * DeferredContext = Parameter to be passed to the callback
76 * NOTE: Callers must be running at IRQL PASSIVE_LEVEL
80 Dpc->DeferredRoutine = DeferredRoutine;
81 Dpc->DeferredContext = DeferredContext;
91 KiDispatchInterrupt(VOID)
93 * FUNCTION: Called to execute queued dpcs
96 PLIST_ENTRY current_entry;
100 assert_irql(DISPATCH_LEVEL);
102 if (DpcQueueSize == 0)
107 KeRaiseIrql(HIGH_LEVEL, &oldlvl);
108 KeAcquireSpinLockAtDpcLevel(&DpcQueueLock);
110 while (!IsListEmpty(&DpcQueueHead))
112 current_entry = RemoveHeadList(&DpcQueueHead);
114 current = CONTAINING_RECORD(current_entry,KDPC,DpcListEntry);
116 KeReleaseSpinLock(&DpcQueueLock, oldlvl);
117 current->DeferredRoutine(current,current->DeferredContext,
118 current->SystemArgument1,
119 current->SystemArgument2);
121 KeRaiseIrql(HIGH_LEVEL, &oldlvl);
122 KeAcquireSpinLockAtDpcLevel(&DpcQueueLock);
124 KeReleaseSpinLock(&DpcQueueLock, oldlvl);
127 #endif /* LIBCAPTIVE */
133 KeRemoveQueueDpc (PKDPC Dpc)
135 * FUNCTION: Removes DPC object from the system dpc queue
137 * Dpc = DPC to remove
138 * RETURNS: TRUE if the DPC was in the queue
144 KeRaiseIrql(HIGH_LEVEL, &oldIrql);
145 KeAcquireSpinLockAtDpcLevel(&DpcQueueLock);
148 KeReleaseSpinLock(&DpcQueueLock, oldIrql);
151 RemoveEntryList(&Dpc->DpcListEntry);
154 KeReleaseSpinLock(&DpcQueueLock, oldIrql);
164 KeInsertQueueDpc (PKDPC Dpc,
165 PVOID SystemArgument1,
166 PVOID SystemArgument2)
168 * FUNCTION: Queues a DPC for execution when the IRQL of a processor
169 * drops below DISPATCH_LEVEL
171 * Dpc = Initalizes DPC
172 * SystemArguments[1-2] = Undocumented
173 * RETURNS: TRUE if the DPC object wasn't already in the queue
178 DPRINT("KeInsertQueueDpc(dpc %x, SystemArgument1 %x, SystemArgument2 %x)\n",
179 Dpc, SystemArgument1, SystemArgument2);
181 assert(KeGetCurrentIrql()>=DISPATCH_LEVEL);
184 Dpc->Importance=MediumImportance;
185 Dpc->SystemArgument1=SystemArgument1;
186 Dpc->SystemArgument2=SystemArgument2;
191 KeRaiseIrql(HIGH_LEVEL, &oldlvl);
192 KeAcquireSpinLockAtDpcLevel(&DpcQueueLock);
193 InsertHeadList(&DpcQueueHead,&Dpc->DpcListEntry);
194 DPRINT("Dpc->DpcListEntry.Flink %x\n", Dpc->DpcListEntry.Flink);
197 KeReleaseSpinLock( &DpcQueueLock, oldlvl );
198 DPRINT("DpcQueueHead.Flink %x\n",DpcQueueHead.Flink);
199 DPRINT("Leaving KeInsertQueueDpc()\n",0);
204 * FUNCTION: Specifies the DPCs importance
206 * Dpc = Initalizes DPC
207 * Importance = DPC importance
213 KeSetImportanceDpc (IN PKDPC Dpc,
214 IN KDPC_IMPORTANCE Importance)
216 Dpc->Importance = Importance;
220 * FUNCTION: Specifies on which processor the DPC will run
222 * Dpc = Initalizes DPC
223 * Number = Processor number
229 KeSetTargetProcessorDpc (IN PKDPC Dpc,
238 * FUNCTION: Initialize DPC handling
241 InitializeListHead(&DpcQueueHead);
242 KeInitializeSpinLock(&DpcQueueLock);
245 #endif /* LIBCAPTIVE */