2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS project
4 * FILE: ntoskrnl/ke/wait.c
5 * PURPOSE: Manages non-busy waiting
7 * PROGRAMMERS: Alex Ionescu - Fixes and optimization.
8 * Gunnar Dalsnes - Implementation
11 /* INCLUDES ******************************************************************/
16 #include <internal/debug.h>
18 /* GLOBALS ******************************************************************/
20 static KSPIN_LOCK DispatcherDatabaseLock
;
22 /* Tells us if the Timer or Event is a Syncronization or Notification Object */
23 #define TIMER_OR_EVENT_TYPE 0x7L
25 /* One of the Reserved Wait Blocks, this one is for the Thread's Timer */
26 #define TIMER_WAIT_BLOCK 0x3L
28 /* FUNCTIONS *****************************************************************/
33 KiCheckAlertability(BOOLEAN Alertable
,
34 PKTHREAD CurrentThread
,
35 KPROCESSOR_MODE WaitMode
,
38 /* At this point, we have to do a wait, so make sure we can make the thread Alertable if requested */
41 /* If the Thread is Alerted, set the Wait Status accordingly */
42 if (CurrentThread
->Alerted
[(int)WaitMode
]) {
44 CurrentThread
->Alerted
[(int)WaitMode
] = FALSE
;
45 DPRINT("Thread was Alerted\n");
46 *Status
= STATUS_ALERTED
;
48 /* If there are User APCs Pending, then we can't really be alertable */
49 } else if ((!IsListEmpty(&CurrentThread
->ApcState
.ApcListHead
[UserMode
])) &&
50 (WaitMode
== UserMode
)) {
52 DPRINT("APCs are Pending\n");
53 CurrentThread
->ApcState
.UserApcPending
= TRUE
;
54 *Status
= STATUS_USER_APC
;
57 /* If there are User APCs Pending and we are waiting in usermode, then we must notify the caller */
58 } else if ((CurrentThread
->ApcState
.UserApcPending
) && (WaitMode
== UserMode
)) {
59 DPRINT("APCs are Pending\n");
60 *Status
= STATUS_USER_APC
;
67 * FUNCTION: Puts the current thread into an alertable or nonalertable
68 * wait state for a given internal
70 * WaitMode = Processor mode in which the caller is waiting
71 * Altertable = Specifies if the wait is alertable
72 * Interval = Specifies the interval to wait
77 KeDelayExecutionThread(KPROCESSOR_MODE WaitMode
,
79 PLARGE_INTEGER Interval
)
81 PKWAIT_BLOCK TimerWaitBlock
;
83 PKTHREAD CurrentThread
= KeGetCurrentThread();
86 DPRINT("Entering KeDelayExecutionThread\n");
88 /* Check if the lock is already held */
89 if (CurrentThread
->WaitNext
) {
91 /* Lock is held, disable Wait Next */
92 DPRINT("Lock is held\n");
93 CurrentThread
->WaitNext
= FALSE
;
97 /* Lock not held, acquire it */
98 DPRINT("Lock is not held, acquiring\n");
99 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
102 /* Use built-in Wait block */
103 TimerWaitBlock
= &CurrentThread
->WaitBlock
[TIMER_WAIT_BLOCK
];
105 /* Start Wait Loop */
108 /* We are going to wait no matter what (that's the point), so test Alertability */
109 KiCheckAlertability(Alertable
, CurrentThread
, KernelMode
, &Status
);
112 ThreadTimer
= &CurrentThread
->Timer
;
114 /* Setup the Wait Block */
115 CurrentThread
->WaitBlockList
= TimerWaitBlock
;
116 TimerWaitBlock
->Object
= (PVOID
)ThreadTimer
;
117 TimerWaitBlock
->Thread
= CurrentThread
;
118 TimerWaitBlock
->WaitKey
= (USHORT
)STATUS_TIMEOUT
;
119 TimerWaitBlock
->WaitType
= WaitAny
;
120 TimerWaitBlock
->NextWaitBlock
= TimerWaitBlock
;
122 /* Link the timer to this Wait Block */
123 InitializeListHead(&ThreadTimer
->Header
.WaitListHead
);
124 InsertTailList(&ThreadTimer
->Header
.WaitListHead
, &TimerWaitBlock
->WaitListEntry
);
126 /* Insert the Timer into the Timer Lists and enable it */
127 if (!KiInsertTimer(ThreadTimer
, *Interval
)) {
129 /* FIXME: The timer already expired, we should find a new ready thread */
130 Status
= STATUS_SUCCESS
;
134 /* Handle Kernel Queues */
135 if (CurrentThread
->Queue
) {
137 DPRINT("Waking Queue\n");
138 KiWakeQueue(CurrentThread
->Queue
);
141 /* Block the Thread */
142 DPRINT("Blocking the Thread: %d, %d, %x\n", Alertable
, WaitMode
, KeGetCurrentThread());
143 KiBlockThread(&Status
,
148 /* Check if we were executing an APC or if we timed out */
149 if (Status
!= STATUS_KERNEL_APC
) {
151 /* This is a good thing */
152 if (Status
== STATUS_TIMEOUT
) Status
= STATUS_SUCCESS
;
158 DPRINT("Looping Again\n");
159 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
163 /* Release the Lock, we are done */
164 DPRINT("Returning from KeDelayExecutionThread(), %x. Status: %d\n", KeGetCurrentThread(), Status
);
165 KeReleaseDispatcherDatabaseLock(CurrentThread
->WaitIrql
);
172 * FUNCTION: Puts the current thread into a wait state until the
173 * given dispatcher object is set to signalled
175 * Object = Object to wait on
176 * WaitReason = Reason for the wait (debugging aid)
177 * WaitMode = Can be KernelMode or UserMode, if UserMode then
178 * user-mode APCs can be delivered and the thread's
179 * stack can be paged out
180 * Altertable = Specifies if the wait is a alertable
181 * Timeout = Optional timeout value
186 KeWaitForSingleObject(PVOID Object
,
187 KWAIT_REASON WaitReason
,
188 KPROCESSOR_MODE WaitMode
,
190 PLARGE_INTEGER Timeout
)
192 PDISPATCHER_HEADER CurrentObject
;
193 PKWAIT_BLOCK WaitBlock
;
194 PKWAIT_BLOCK TimerWaitBlock
;
196 PKTHREAD CurrentThread
= KeGetCurrentThread();
200 DPRINT("Entering KeWaitForSingleObject\n");
202 /* Check if the lock is already held */
203 if (CurrentThread
->WaitNext
) {
205 /* Lock is held, disable Wait Next */
206 DPRINT("Lock is held\n");
207 CurrentThread
->WaitNext
= FALSE
;
211 /* Lock not held, acquire it */
212 DPRINT("Lock is not held, acquiring\n");
213 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
216 /* Start the actual Loop */
219 /* Get the current Wait Status */
220 WaitStatus
= CurrentThread
->WaitStatus
;
222 /* Append wait block to the KTHREAD wait block list */
223 CurrentThread
->WaitBlockList
= WaitBlock
= &CurrentThread
->WaitBlock
[0];
225 /* Get the Current Object */
226 CurrentObject
= (PDISPATCHER_HEADER
)Object
;
229 * Temporary hack until my Object Manager re-write. Basically some objects, like
230 * the File Object, but also LPCs and others, are actually waitable on their event.
231 * The Object Manager sets this up in The ObjectTypeInformation->DefaultObject member,
232 * by using pretty much the same kind of hack as us. Normal objects point to themselves
233 * in that pointer. Then, NtWaitForXXX will populate the WaitList that gets sent to us by
234 * using ->DefaultObject, so the proper actual objects will be sent to us. Until then however,
235 * I will keep this hack here, since there's no need to make an interim hack until the rewrite
236 * -- Alex Ionescu 24/02/05
238 if (CurrentObject
->Type
== IO_TYPE_FILE
) {
240 DPRINT1("Hack used: %x\n", &((PFILE_OBJECT
)CurrentObject
)->Event
);
241 CurrentObject
= (PDISPATCHER_HEADER
)(&((PFILE_OBJECT
)CurrentObject
)->Event
);
244 /* Check if the Object is Signaled */
245 if (KiIsObjectSignaled(CurrentObject
, CurrentThread
)) {
247 /* Just unwait this guy and exit */
248 if (CurrentObject
->SignalState
!= MINLONG
) {
250 /* It has a normal signal state, so unwait it and return */
251 KiSatisfyObjectWait(CurrentObject
, CurrentThread
);
252 Status
= STATUS_WAIT_0
;
257 /* Is this a Mutant? */
258 if (CurrentObject
->Type
== MutantObject
) {
260 /* According to wasm.ru, we must raise this exception (tested and true) */
261 KeReleaseDispatcherDatabaseLock(CurrentThread
->WaitIrql
);
262 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
267 /* Set up the Wait Block */
268 WaitBlock
->Object
= CurrentObject
;
269 WaitBlock
->Thread
= CurrentThread
;
270 WaitBlock
->WaitKey
= (USHORT
)(STATUS_WAIT_0
);
271 WaitBlock
->WaitType
= WaitAny
;
272 WaitBlock
->NextWaitBlock
= WaitBlock
;
274 /* Make sure we can satisfy the Alertable request */
275 KiCheckAlertability(Alertable
, CurrentThread
, WaitMode
, &Status
);
277 /* Set the Wait Status */
278 CurrentThread
->WaitStatus
= Status
;
280 /* Enable the Timeout Timer if there was any specified */
281 if (Timeout
!= NULL
) {
283 /* However if 0 timeout was specified, then we must fail since we need to peform a wait */
284 if (!Timeout
->QuadPart
) {
286 /* Return a timeout */
287 Status
= STATUS_TIMEOUT
;
291 /* Point to Timer Wait Block and Thread Timer */
292 TimerWaitBlock
= &CurrentThread
->WaitBlock
[TIMER_WAIT_BLOCK
];
293 ThreadTimer
= &CurrentThread
->Timer
;
295 /* Connect the Timer Wait Block */
296 WaitBlock
->NextWaitBlock
= TimerWaitBlock
;
298 /* Set up the Timer Wait Block */
299 TimerWaitBlock
->Object
= (PVOID
)ThreadTimer
;
300 TimerWaitBlock
->Thread
= CurrentThread
;
301 TimerWaitBlock
->WaitKey
= STATUS_TIMEOUT
;
302 TimerWaitBlock
->WaitType
= WaitAny
;
303 TimerWaitBlock
->NextWaitBlock
= WaitBlock
;
305 /* Link the timer to this Wait Block */
306 InitializeListHead(&ThreadTimer
->Header
.WaitListHead
);
307 InsertTailList(&ThreadTimer
->Header
.WaitListHead
, &TimerWaitBlock
->WaitListEntry
);
309 /* Insert the Timer into the Timer Lists and enable it */
310 if (!KiInsertTimer(ThreadTimer
, *Timeout
)) {
312 /* Return a timeout if we couldn't insert the timer for some reason */
313 Status
= STATUS_TIMEOUT
;
318 /* Link the Object to this Wait Block */
319 InsertTailList(&CurrentObject
->WaitListHead
, &WaitBlock
->WaitListEntry
);
321 /* Handle Kernel Queues */
322 if (CurrentThread
->Queue
) {
324 DPRINT("Waking Queue\n");
325 KiWakeQueue(CurrentThread
->Queue
);
328 /* Block the Thread */
329 DPRINT("Blocking the Thread: %d, %d, %d, %x\n", Alertable
, WaitMode
, WaitReason
, KeGetCurrentThread());
330 KiBlockThread(&Status
,
335 /* Check if we were executing an APC */
336 if (Status
!= STATUS_KERNEL_APC
) {
342 DPRINT("Looping Again\n");
343 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
348 /* Release the Lock, we are done */
349 DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n", KeGetCurrentThread(), Status
);
350 KeReleaseDispatcherDatabaseLock(CurrentThread
->WaitIrql
);
358 KeWaitForMultipleObjects(ULONG Count
,
361 KWAIT_REASON WaitReason
,
362 KPROCESSOR_MODE WaitMode
,
364 PLARGE_INTEGER Timeout
,
365 PKWAIT_BLOCK WaitBlockArray
)
367 PDISPATCHER_HEADER CurrentObject
;
368 PKWAIT_BLOCK WaitBlock
;
369 PKWAIT_BLOCK TimerWaitBlock
;
371 PKTHREAD CurrentThread
= KeGetCurrentThread();
372 ULONG AllObjectsSignaled
;
377 DPRINT("Entering KeWaitForMultipleObjects(Count %lu Object[] %p) "
378 "PsGetCurrentThread() %x, Timeout %x\n", Count
, Object
, PsGetCurrentThread(), Timeout
);
380 /* Set the Current Thread */
381 CurrentThread
= KeGetCurrentThread();
383 /* Check if the lock is already held */
384 if (CurrentThread
->WaitNext
) {
386 /* Lock is held, disable Wait Next */
387 DPRINT("Lock is held\n");
388 CurrentThread
->WaitNext
= FALSE
;
392 /* Lock not held, acquire it */
393 DPRINT("Lock is not held, acquiring\n");
394 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
397 /* Make sure the Wait Count is valid for the Thread and Maximum Wait Objects */
398 if (!WaitBlockArray
) {
400 /* Check in regards to the Thread Object Limit */
401 if (Count
> THREAD_WAIT_OBJECTS
) {
403 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
406 /* Use the Thread's Wait Block */
407 WaitBlockArray
= &CurrentThread
->WaitBlock
[0];
411 /* Using our own Block Array. Check in regards to System Object Limit */
412 if (Count
> MAXIMUM_WAIT_OBJECTS
) {
414 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
418 /* Start the actual Loop */
421 /* Get the current Wait Status */
422 WaitStatus
= CurrentThread
->WaitStatus
;
424 /* Append wait block to the KTHREAD wait block list */
425 CurrentThread
->WaitBlockList
= WaitBlock
= WaitBlockArray
;
427 /* Check if the wait is (already) satisfied */
428 AllObjectsSignaled
= TRUE
;
430 /* First, we'll try to satisfy the wait directly */
431 for (WaitIndex
= 0; WaitIndex
< Count
; WaitIndex
++) {
433 /* Get the Current Object */
434 CurrentObject
= (PDISPATCHER_HEADER
)Object
[WaitIndex
];
437 * Temporary hack until my Object Manager re-write. Basically some objects, like
438 * the File Object, but also LPCs and others, are actually waitable on their event.
439 * The Object Manager sets this up in The ObjectTypeInformation->DefaultObject member,
440 * by using pretty much the same kind of hack as us. Normal objects point to themselves
441 * in that pointer. Then, NtWaitForXXX will populate the WaitList that gets sent to us by
442 * using ->DefaultObject, so the proper actual objects will be sent to us. Until then however,
443 * I will keep this hack here, since there's no need to make an interim hack until the rewrite
444 * -- Alex Ionescu 24/02/05
446 if (CurrentObject
->Type
== IO_TYPE_FILE
) {
448 DPRINT1("Hack used: %x\n", &((PFILE_OBJECT
)CurrentObject
)->Event
);
449 CurrentObject
= (PDISPATCHER_HEADER
)(&((PFILE_OBJECT
)CurrentObject
)->Event
);
452 /* Check if the Object is Signaled */
453 if (KiIsObjectSignaled(CurrentObject
, CurrentThread
)) {
455 /* Check what kind of wait this is */
456 if (WaitType
== WaitAny
) {
458 /* This is a Wait Any, so just unwait this guy and exit */
459 if (CurrentObject
->SignalState
!= MINLONG
) {
461 /* It has a normal signal state, so unwait it and return */
462 KiSatisfyObjectWait(CurrentObject
, CurrentThread
);
463 Status
= STATUS_WAIT_0
| WaitIndex
;
468 /* Is this a Mutant? */
469 if (CurrentObject
->Type
== MutantObject
) {
471 /* According to wasm.ru, we must raise this exception (tested and true) */
472 KeReleaseDispatcherDatabaseLock(CurrentThread
->WaitIrql
);
473 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
480 /* One of the objects isn't signaled... if this is a WaitAll, we will fail later */
481 AllObjectsSignaled
= FALSE
;
484 /* Set up a Wait Block for this Object */
485 WaitBlock
->Object
= CurrentObject
;
486 WaitBlock
->Thread
= CurrentThread
;
487 WaitBlock
->WaitKey
= (USHORT
)(STATUS_WAIT_0
+ WaitIndex
);
488 WaitBlock
->WaitType
= (USHORT
)WaitType
;
489 WaitBlock
->NextWaitBlock
= WaitBlock
+ 1;
491 /* Move to the next Wait Block */
492 WaitBlock
= WaitBlock
->NextWaitBlock
;
495 /* Return to the Root Wait Block */
497 WaitBlock
->NextWaitBlock
= WaitBlockArray
;
499 /* Check if this is a Wait All and all the objects are signaled */
500 if ((WaitType
== WaitAll
) && (AllObjectsSignaled
)) {
502 /* Return to the Root Wait Block */
503 WaitBlock
= CurrentThread
->WaitBlockList
;
505 /* Satisfy their Waits and return to the caller */
506 KiSatisifyMultipleObjectWaits(WaitBlock
);
507 Status
= STATUS_WAIT_0
;
511 /* Make sure we can satisfy the Alertable request */
512 KiCheckAlertability(Alertable
, CurrentThread
, WaitMode
, &Status
);
514 /* Set the Wait Status */
515 CurrentThread
->WaitStatus
= Status
;
517 /* Enable the Timeout Timer if there was any specified */
518 if (Timeout
!= NULL
) {
520 /* However if 0 timeout was specified, then we must fail since we need to peform a wait */
521 if (!Timeout
->QuadPart
) {
523 /* Return a timeout */
524 Status
= STATUS_TIMEOUT
;
528 /* Point to Timer Wait Block and Thread Timer */
529 TimerWaitBlock
= &CurrentThread
->WaitBlock
[TIMER_WAIT_BLOCK
];
530 ThreadTimer
= &CurrentThread
->Timer
;
532 /* Connect the Timer Wait Block */
533 WaitBlock
->NextWaitBlock
= TimerWaitBlock
;
535 /* Set up the Timer Wait Block */
536 TimerWaitBlock
->Object
= (PVOID
)ThreadTimer
;
537 TimerWaitBlock
->Thread
= CurrentThread
;
538 TimerWaitBlock
->WaitKey
= STATUS_TIMEOUT
;
539 TimerWaitBlock
->WaitType
= WaitAny
;
540 TimerWaitBlock
->NextWaitBlock
= WaitBlockArray
;
542 /* Link the timer to this Wait Block */
543 InitializeListHead(&ThreadTimer
->Header
.WaitListHead
);
545 /* Insert the Timer into the Timer Lists and enable it */
546 if (!KiInsertTimer(ThreadTimer
, *Timeout
)) {
548 /* Return a timeout if we couldn't insert the timer for some reason */
549 Status
= STATUS_TIMEOUT
;
554 /* Insert into Object's Wait List*/
555 WaitBlock
= CurrentThread
->WaitBlockList
;
558 /* Get the Current Object */
559 CurrentObject
= WaitBlock
->Object
;
561 /* Link the Object to this Wait Block */
562 InsertTailList(&CurrentObject
->WaitListHead
, &WaitBlock
->WaitListEntry
);
564 /* Move to the next Wait Block */
565 WaitBlock
= WaitBlock
->NextWaitBlock
;
566 } while (WaitBlock
!= WaitBlockArray
);
568 /* Handle Kernel Queues */
569 if (CurrentThread
->Queue
) {
571 DPRINT("Waking Queue\n");
572 KiWakeQueue(CurrentThread
->Queue
);
575 /* Block the Thread */
576 DPRINT("Blocking the Thread: %d, %d, %d, %x\n", Alertable
, WaitMode
, WaitReason
, KeGetCurrentThread());
577 KiBlockThread(&Status
,
582 /* Check if we were executing an APC */
583 if (Status
!= STATUS_KERNEL_APC
) {
589 DPRINT("Looping Again\n");
590 CurrentThread
->WaitIrql
= KeAcquireDispatcherDatabaseLock();
595 /* Release the Lock, we are done */
596 DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n", KeGetCurrentThread(), Status
);
597 KeReleaseDispatcherDatabaseLock(CurrentThread
->WaitIrql
);
603 KiSatisfyObjectWait(PDISPATCHER_HEADER Object
,
607 /* Special case for Mutants */
608 if (Object
->Type
== MutantObject
) {
610 /* Decrease the Signal State */
611 Object
->SignalState
--;
613 /* Check if it's now non-signaled */
614 if (Object
->SignalState
== 0) {
616 /* Set the Owner Thread */
617 ((PKMUTANT
)Object
)->OwnerThread
= Thread
;
619 /* Disable APCs if needed */
620 Thread
->KernelApcDisable
-= ((PKMUTANT
)Object
)->ApcDisable
;
622 /* Check if it's abandoned */
623 if (((PKMUTANT
)Object
)->Abandoned
) {
626 ((PKMUTANT
)Object
)->Abandoned
= FALSE
;
629 Thread
->WaitStatus
= STATUS_ABANDONED
;
632 /* Insert it into the Mutant List */
633 InsertHeadList(&Thread
->MutantListHead
, &((PKMUTANT
)Object
)->MutantListEntry
);
636 } else if ((Object
->Type
& TIMER_OR_EVENT_TYPE
) == EventSynchronizationObject
) {
638 /* These guys (Syncronization Timers and Events) just get un-signaled */
639 Object
->SignalState
= 0;
641 } else if (Object
->Type
== SemaphoreObject
) {
643 /* These ones can have multiple signalings, so we only decrease it */
644 Object
->SignalState
--;
650 KiWaitTest(PDISPATCHER_HEADER Object
,
653 PLIST_ENTRY WaitEntry
;
654 PLIST_ENTRY WaitList
;
655 PKWAIT_BLOCK CurrentWaitBlock
;
656 PKWAIT_BLOCK NextWaitBlock
;
659 /* Loop the Wait Entries */
660 DPRINT("KiWaitTest for Object: %x\n", Object
);
661 WaitList
= &Object
->WaitListHead
;
662 WaitEntry
= WaitList
->Flink
;
663 while ((WaitEntry
!= WaitList
) && (Object
->SignalState
> 0)) {
665 /* Get the current wait block */
666 CurrentWaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
667 WaitThread
= CurrentWaitBlock
->Thread
;
669 /* Check the current Wait Mode */
670 if (CurrentWaitBlock
->WaitType
== WaitAny
) {
672 /* Easy case, satisfy only this wait */
673 DPRINT("Satisfiying a Wait any\n");
674 WaitEntry
= WaitEntry
->Blink
;
675 KiSatisfyObjectWait(Object
, WaitThread
);
679 /* Everything must be satisfied */
680 DPRINT("Checking for a Wait All\n");
681 NextWaitBlock
= CurrentWaitBlock
->NextWaitBlock
;
683 /* Loop first to make sure they are valid */
684 while (NextWaitBlock
!= CurrentWaitBlock
) {
686 /* Check if the object is signaled */
687 DPRINT("Checking: %x %d\n", NextWaitBlock
->Object
, Object
->SignalState
);
688 if (!KiIsObjectSignaled(NextWaitBlock
->Object
, WaitThread
)) {
690 /* It's not, move to the next one */
691 DPRINT("One of the object is non-signaled, sorry.\n");
695 /* Go to the next Wait block */
696 NextWaitBlock
= NextWaitBlock
->NextWaitBlock
;
699 /* All the objects are signaled, we can satisfy */
700 DPRINT("Satisfiying a Wait All\n");
701 WaitEntry
= WaitEntry
->Blink
;
702 KiSatisifyMultipleObjectWaits(CurrentWaitBlock
);
705 /* All waits satisfied, unwait the thread */
706 DPRINT("Unwaiting the Thread\n");
707 KiAbortWaitThread(WaitThread
, CurrentWaitBlock
->WaitKey
, Increment
);
711 WaitEntry
= WaitEntry
->Flink
;
717 /* Must be called with the dispatcher lock held */
720 KiAbortWaitThread(PKTHREAD Thread
,
724 PKWAIT_BLOCK WaitBlock
;
726 /* If we are blocked, we must be waiting on something also */
727 DPRINT("KiAbortWaitThread: %x, Status: %x, %x \n", Thread
, WaitStatus
, Thread
->WaitBlockList
);
728 ASSERT((Thread
->State
== THREAD_STATE_BLOCKED
) == (Thread
->WaitBlockList
!= NULL
));
730 /* Remove the Wait Blocks from the list */
731 DPRINT("Removing waits\n");
732 WaitBlock
= Thread
->WaitBlockList
;
736 DPRINT("Removing Waitblock: %x, %x\n", WaitBlock
, WaitBlock
->NextWaitBlock
);
737 RemoveEntryList(&WaitBlock
->WaitListEntry
);
739 /* Go to the next one */
740 WaitBlock
= WaitBlock
->NextWaitBlock
;
741 } while (WaitBlock
!= Thread
->WaitBlockList
);
743 /* Check if there's a Thread Timer */
744 if (Thread
->Timer
.Header
.Inserted
) {
746 /* Cancel the Thread Timer with the no-lock fastpath */
747 DPRINT("Removing the Thread's Timer\n");
748 Thread
->Timer
.Header
.Inserted
= FALSE
;
749 RemoveEntryList(&Thread
->Timer
.TimerListEntry
);
752 /* Increment the Queue's active threads */
755 DPRINT("Incrementing Queue's active threads\n");
756 Thread
->Queue
->CurrentCount
++;
759 /* Reschedule the Thread */
760 DPRINT("Unblocking the Thread\n");
761 KiUnblockThread(Thread
, &WaitStatus
, 0);
767 KiIsObjectSignaled(PDISPATCHER_HEADER Object
,
770 /* Mutants are...well...mutants! */
771 if (Object
->Type
== MutantObject
) {
774 * Because Cutler hates mutants, they are actually signaled if the Signal State is <= 0
775 * Well, only if they are recursivly acquired (i.e if we own it right now).
776 * Of course, they are also signaled if their signal state is 1.
778 if ((Object
->SignalState
<= 0 && ((PKMUTANT
)Object
)->OwnerThread
== Thread
) ||
779 (Object
->SignalState
== 1)) {
781 /* Signaled Mutant */
786 /* Unsignaled Mutant */
791 /* Any other object is not a mutated freak, so let's use logic */
792 return (!Object
->SignalState
<= 0);
798 KiIsObjectWaitable(PVOID Object
)
800 POBJECT_HEADER Header
;
801 Header
= BODY_TO_HEADER(Object
);
803 if (Header
->ObjectType
== ExEventObjectType
||
804 Header
->ObjectType
== ExIoCompletionType
||
805 Header
->ObjectType
== ExMutantObjectType
||
806 Header
->ObjectType
== ExSemaphoreObjectType
||
807 Header
->ObjectType
== ExTimerType
||
808 Header
->ObjectType
== PsProcessType
||
809 Header
->ObjectType
== PsThreadType
||
810 Header
->ObjectType
== IoFileObjectType
) {
823 KiSatisifyMultipleObjectWaits(PKWAIT_BLOCK WaitBlock
)
825 PKWAIT_BLOCK FirstBlock
= WaitBlock
;
826 PKTHREAD WaitThread
= WaitBlock
->Thread
;
828 /* Loop through all the Wait Blocks, and wake each Object */
831 /* Wake the Object */
832 KiSatisfyObjectWait(WaitBlock
->Object
, WaitThread
);
833 WaitBlock
= WaitBlock
->NextWaitBlock
;
834 } while (WaitBlock
!= FirstBlock
);
840 KeInitializeDispatcherHeader(DISPATCHER_HEADER
* Header
,
845 Header
->Type
= (UCHAR
)Type
;
846 Header
->Absolute
= 0;
847 Header
->Inserted
= 0;
848 Header
->Size
= (UCHAR
)Size
;
849 Header
->SignalState
= SignalState
;
850 InitializeListHead(&(Header
->WaitListHead
));
856 KeAcquireDispatcherDatabaseLock(VOID
)
860 KeAcquireSpinLock (&DispatcherDatabaseLock
, &OldIrql
);
867 KeAcquireDispatcherDatabaseLockAtDpcLevel(VOID
)
869 KeAcquireSpinLockAtDpcLevel (&DispatcherDatabaseLock
);
875 KeInitializeDispatcher(VOID
)
877 /* Initialize the Dispatcher Lock */
878 KeInitializeSpinLock(&DispatcherDatabaseLock
);
884 KeReleaseDispatcherDatabaseLock(KIRQL OldIrql
)
886 /* If it's the idle thread, dispatch */
887 if (!KeIsExecutingDpc() && OldIrql
< DISPATCH_LEVEL
&& KeGetCurrentThread() != NULL
&&
888 KeGetCurrentThread() == KeGetCurrentPrcb()->IdleThread
) {
890 KiDispatchThreadNoLock(THREAD_STATE_READY
);
891 KeLowerIrql(OldIrql
);
895 /* Just release the spin lock */
896 KeReleaseSpinLock(&DispatcherDatabaseLock
, OldIrql
);
903 KeReleaseDispatcherDatabaseLockFromDpcLevel(VOID
)
905 KeReleaseSpinLockFromDpcLevel(&DispatcherDatabaseLock
);