03057c82b9b94aa6af624e1f663e187b3b53f93c
[reactos.git] / reactos / ntoskrnl / ke / wait.c
1 /*
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
6 *
7 * PROGRAMMERS: Alex Ionescu - Fixes and optimization.
8 * Gunnar Dalsnes - Implementation
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* GLOBALS ******************************************************************/
19
20 static KSPIN_LOCK DispatcherDatabaseLock;
21
22 /* Tells us if the Timer or Event is a Syncronization or Notification Object */
23 #define TIMER_OR_EVENT_TYPE 0x7L
24
25 /* One of the Reserved Wait Blocks, this one is for the Thread's Timer */
26 #define TIMER_WAIT_BLOCK 0x3L
27
28 /* FUNCTIONS *****************************************************************/
29
30 VOID
31 inline
32 FASTCALL
33 KiCheckAlertability(BOOLEAN Alertable,
34 PKTHREAD CurrentThread,
35 KPROCESSOR_MODE WaitMode,
36 PNTSTATUS Status)
37 {
38 /* At this point, we have to do a wait, so make sure we can make the thread Alertable if requested */
39 if (Alertable) {
40
41 /* If the Thread is Alerted, set the Wait Status accordingly */
42 if (CurrentThread->Alerted[(int)WaitMode]) {
43
44 CurrentThread->Alerted[(int)WaitMode] = FALSE;
45 DPRINT("Thread was Alerted\n");
46 *Status = STATUS_ALERTED;
47
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)) {
51
52 DPRINT("APCs are Pending\n");
53 CurrentThread->ApcState.UserApcPending = TRUE;
54 *Status = STATUS_USER_APC;
55 }
56
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;
61 }
62 }
63
64 /*
65 * @implemented
66 *
67 * FUNCTION: Puts the current thread into an alertable or nonalertable
68 * wait state for a given internal
69 * ARGUMENTS:
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
73 * RETURNS: Status
74 */
75 NTSTATUS
76 STDCALL
77 KeDelayExecutionThread(KPROCESSOR_MODE WaitMode,
78 BOOLEAN Alertable,
79 PLARGE_INTEGER Interval)
80 {
81 PKWAIT_BLOCK TimerWaitBlock;
82 PKTIMER ThreadTimer;
83 PKTHREAD CurrentThread = KeGetCurrentThread();
84 NTSTATUS Status;
85
86 DPRINT("Entering KeDelayExecutionThread\n");
87
88 /* Check if the lock is already held */
89 if (CurrentThread->WaitNext) {
90
91 /* Lock is held, disable Wait Next */
92 DPRINT("Lock is held\n");
93 CurrentThread->WaitNext = FALSE;
94
95 } else {
96
97 /* Lock not held, acquire it */
98 DPRINT("Lock is not held, acquiring\n");
99 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
100 }
101
102 /* Use built-in Wait block */
103 TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
104
105 /* Start Wait Loop */
106 do {
107
108 /* We are going to wait no matter what (that's the point), so test Alertability */
109 KiCheckAlertability(Alertable, CurrentThread, KernelMode, &Status);
110
111 /* Set Timer */
112 ThreadTimer = &CurrentThread->Timer;
113
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;
121
122 /* Link the timer to this Wait Block */
123 InitializeListHead(&ThreadTimer->Header.WaitListHead);
124 InsertTailList(&ThreadTimer->Header.WaitListHead, &TimerWaitBlock->WaitListEntry);
125
126 /* Insert the Timer into the Timer Lists and enable it */
127 if (!KiInsertTimer(ThreadTimer, *Interval)) {
128
129 /* FIXME: The timer already expired, we should find a new ready thread */
130 Status = STATUS_SUCCESS;
131 break;
132 }
133
134 /* Handle Kernel Queues */
135 if (CurrentThread->Queue) {
136
137 DPRINT("Waking Queue\n");
138 KiWakeQueue(CurrentThread->Queue);
139 }
140
141 /* Block the Thread */
142 DPRINT("Blocking the Thread: %d, %d, %x\n", Alertable, WaitMode, KeGetCurrentThread());
143 KiBlockThread(&Status,
144 Alertable,
145 WaitMode,
146 DelayExecution);
147
148 /* Check if we were executing an APC or if we timed out */
149 if (Status != STATUS_KERNEL_APC) {
150
151 /* This is a good thing */
152 if (Status == STATUS_TIMEOUT) Status = STATUS_SUCCESS;
153
154 /* Return Status */
155 return Status;
156 }
157
158 DPRINT("Looping Again\n");
159 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
160
161 } while (TRUE);
162
163 /* Release the Lock, we are done */
164 DPRINT("Returning from KeDelayExecutionThread(), %x. Status: %d\n", KeGetCurrentThread(), Status);
165 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
166 return Status;
167 }
168
169 /*
170 * @implemented
171 *
172 * FUNCTION: Puts the current thread into a wait state until the
173 * given dispatcher object is set to signalled
174 * ARGUMENTS:
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
182 * RETURNS: Status
183 */
184 NTSTATUS
185 STDCALL
186 KeWaitForSingleObject(PVOID Object,
187 KWAIT_REASON WaitReason,
188 KPROCESSOR_MODE WaitMode,
189 BOOLEAN Alertable,
190 PLARGE_INTEGER Timeout)
191 {
192 PDISPATCHER_HEADER CurrentObject;
193 PKWAIT_BLOCK WaitBlock;
194 PKWAIT_BLOCK TimerWaitBlock;
195 PKTIMER ThreadTimer;
196 PKTHREAD CurrentThread = KeGetCurrentThread();
197 NTSTATUS Status;
198 NTSTATUS WaitStatus;
199
200 DPRINT("Entering KeWaitForSingleObject\n");
201
202 /* Check if the lock is already held */
203 if (CurrentThread->WaitNext) {
204
205 /* Lock is held, disable Wait Next */
206 DPRINT("Lock is held\n");
207 CurrentThread->WaitNext = FALSE;
208
209 } else {
210
211 /* Lock not held, acquire it */
212 DPRINT("Lock is not held, acquiring\n");
213 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
214 }
215
216 /* Start the actual Loop */
217 do {
218
219 /* Get the current Wait Status */
220 WaitStatus = CurrentThread->WaitStatus;
221
222 /* Append wait block to the KTHREAD wait block list */
223 CurrentThread->WaitBlockList = WaitBlock = &CurrentThread->WaitBlock[0];
224
225 /* Get the Current Object */
226 CurrentObject = (PDISPATCHER_HEADER)Object;
227
228 /* FIXME:
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
237 */
238 if (CurrentObject->Type == IO_TYPE_FILE) {
239
240 DPRINT1("Hack used: %x\n", &((PFILE_OBJECT)CurrentObject)->Event);
241 CurrentObject = (PDISPATCHER_HEADER)(&((PFILE_OBJECT)CurrentObject)->Event);
242 }
243
244 /* Check if the Object is Signaled */
245 if (KiIsObjectSignaled(CurrentObject, CurrentThread)) {
246
247 /* Just unwait this guy and exit */
248 if (CurrentObject->SignalState != MINLONG) {
249
250 /* It has a normal signal state, so unwait it and return */
251 KiSatisfyObjectWait(CurrentObject, CurrentThread);
252 Status = STATUS_WAIT_0;
253 goto WaitDone;
254
255 } else {
256
257 /* Is this a Mutant? */
258 if (CurrentObject->Type == MutantObject) {
259
260 /* According to wasm.ru, we must raise this exception (tested and true) */
261 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
262 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
263 }
264 }
265 }
266
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;
273
274 /* Make sure we can satisfy the Alertable request */
275 KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status);
276
277 /* Set the Wait Status */
278 CurrentThread->WaitStatus = Status;
279
280 /* Enable the Timeout Timer if there was any specified */
281 if (Timeout != NULL) {
282
283 /* However if 0 timeout was specified, then we must fail since we need to peform a wait */
284 if (!Timeout->QuadPart) {
285
286 /* Return a timeout */
287 Status = STATUS_TIMEOUT;
288 goto WaitDone;
289 }
290
291 /* Point to Timer Wait Block and Thread Timer */
292 TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
293 ThreadTimer = &CurrentThread->Timer;
294
295 /* Connect the Timer Wait Block */
296 WaitBlock->NextWaitBlock = TimerWaitBlock;
297
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;
304
305 /* Link the timer to this Wait Block */
306 InitializeListHead(&ThreadTimer->Header.WaitListHead);
307 InsertTailList(&ThreadTimer->Header.WaitListHead, &TimerWaitBlock->WaitListEntry);
308
309 /* Insert the Timer into the Timer Lists and enable it */
310 if (!KiInsertTimer(ThreadTimer, *Timeout)) {
311
312 /* Return a timeout if we couldn't insert the timer for some reason */
313 Status = STATUS_TIMEOUT;
314 goto WaitDone;
315 }
316 }
317
318 /* Link the Object to this Wait Block */
319 InsertTailList(&CurrentObject->WaitListHead, &WaitBlock->WaitListEntry);
320
321 /* Handle Kernel Queues */
322 if (CurrentThread->Queue) {
323
324 DPRINT("Waking Queue\n");
325 KiWakeQueue(CurrentThread->Queue);
326 }
327
328 /* Block the Thread */
329 DPRINT("Blocking the Thread: %d, %d, %d, %x\n", Alertable, WaitMode, WaitReason, KeGetCurrentThread());
330 KiBlockThread(&Status,
331 Alertable,
332 WaitMode,
333 (UCHAR)WaitReason);
334
335 /* Check if we were executing an APC */
336 if (Status != STATUS_KERNEL_APC) {
337
338 /* Return Status */
339 return Status;
340 }
341
342 DPRINT("Looping Again\n");
343 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
344
345 } while (TRUE);
346
347 WaitDone:
348 /* Release the Lock, we are done */
349 DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n", KeGetCurrentThread(), Status);
350 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
351 return Status;
352 }
353
354 /*
355 * @implemented
356 */
357 NTSTATUS STDCALL
358 KeWaitForMultipleObjects(ULONG Count,
359 PVOID Object[],
360 WAIT_TYPE WaitType,
361 KWAIT_REASON WaitReason,
362 KPROCESSOR_MODE WaitMode,
363 BOOLEAN Alertable,
364 PLARGE_INTEGER Timeout,
365 PKWAIT_BLOCK WaitBlockArray)
366 {
367 PDISPATCHER_HEADER CurrentObject;
368 PKWAIT_BLOCK WaitBlock;
369 PKWAIT_BLOCK TimerWaitBlock;
370 PKTIMER ThreadTimer;
371 PKTHREAD CurrentThread = KeGetCurrentThread();
372 ULONG AllObjectsSignaled;
373 ULONG WaitIndex;
374 NTSTATUS Status;
375 NTSTATUS WaitStatus;
376
377 DPRINT("Entering KeWaitForMultipleObjects(Count %lu Object[] %p) "
378 "PsGetCurrentThread() %x, Timeout %x\n", Count, Object, PsGetCurrentThread(), Timeout);
379
380 /* Set the Current Thread */
381 CurrentThread = KeGetCurrentThread();
382
383 /* Check if the lock is already held */
384 if (CurrentThread->WaitNext) {
385
386 /* Lock is held, disable Wait Next */
387 DPRINT("Lock is held\n");
388 CurrentThread->WaitNext = FALSE;
389
390 } else {
391
392 /* Lock not held, acquire it */
393 DPRINT("Lock is not held, acquiring\n");
394 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
395 }
396
397 /* Make sure the Wait Count is valid for the Thread and Maximum Wait Objects */
398 if (!WaitBlockArray) {
399
400 /* Check in regards to the Thread Object Limit */
401 if (Count > THREAD_WAIT_OBJECTS) {
402
403 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
404 }
405
406 /* Use the Thread's Wait Block */
407 WaitBlockArray = &CurrentThread->WaitBlock[0];
408
409 } else {
410
411 /* Using our own Block Array. Check in regards to System Object Limit */
412 if (Count > MAXIMUM_WAIT_OBJECTS) {
413
414 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
415 }
416 }
417
418 /* Start the actual Loop */
419 do {
420
421 /* Get the current Wait Status */
422 WaitStatus = CurrentThread->WaitStatus;
423
424 /* Append wait block to the KTHREAD wait block list */
425 CurrentThread->WaitBlockList = WaitBlock = WaitBlockArray;
426
427 /* Check if the wait is (already) satisfied */
428 AllObjectsSignaled = TRUE;
429
430 /* First, we'll try to satisfy the wait directly */
431 for (WaitIndex = 0; WaitIndex < Count; WaitIndex++) {
432
433 /* Get the Current Object */
434 CurrentObject = (PDISPATCHER_HEADER)Object[WaitIndex];
435
436 /* FIXME:
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
445 */
446 if (CurrentObject->Type == IO_TYPE_FILE) {
447
448 CurrentObject = (PDISPATCHER_HEADER)(&((PFILE_OBJECT)CurrentObject)->Event);
449 }
450
451 /* Check if the Object is Signaled */
452 if (KiIsObjectSignaled(CurrentObject, CurrentThread)) {
453
454 /* Check what kind of wait this is */
455 if (WaitType == WaitAny) {
456
457 /* This is a Wait Any, so just unwait this guy and exit */
458 if (CurrentObject->SignalState != MINLONG) {
459
460 /* It has a normal signal state, so unwait it and return */
461 KiSatisfyObjectWait(CurrentObject, CurrentThread);
462 Status = STATUS_WAIT_0 | WaitIndex;
463 goto WaitDone;
464
465 } else {
466
467 /* Is this a Mutant? */
468 if (CurrentObject->Type == MutantObject) {
469
470 /* According to wasm.ru, we must raise this exception (tested and true) */
471 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
472 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
473 }
474 }
475 }
476
477 } else {
478
479 /* One of the objects isn't signaled... if this is a WaitAll, we will fail later */
480 AllObjectsSignaled = FALSE;
481 }
482
483 /* Set up a Wait Block for this Object */
484 WaitBlock->Object = CurrentObject;
485 WaitBlock->Thread = CurrentThread;
486 WaitBlock->WaitKey = (USHORT)(STATUS_WAIT_0 + WaitIndex);
487 WaitBlock->WaitType = (USHORT)WaitType;
488 WaitBlock->NextWaitBlock = WaitBlock + 1;
489
490 /* Move to the next Wait Block */
491 WaitBlock = WaitBlock->NextWaitBlock;
492 }
493
494 /* Return to the Root Wait Block */
495 WaitBlock--;
496 WaitBlock->NextWaitBlock = WaitBlockArray;
497
498 /* Check if this is a Wait All and all the objects are signaled */
499 if ((WaitType == WaitAll) && (AllObjectsSignaled)) {
500
501 /* Return to the Root Wait Block */
502 WaitBlock = CurrentThread->WaitBlockList;
503
504 /* Satisfy their Waits and return to the caller */
505 KiSatisifyMultipleObjectWaits(WaitBlock);
506 Status = STATUS_WAIT_0;
507 goto WaitDone;
508 }
509
510 /* Make sure we can satisfy the Alertable request */
511 KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status);
512
513 /* Set the Wait Status */
514 CurrentThread->WaitStatus = Status;
515
516 /* Enable the Timeout Timer if there was any specified */
517 if (Timeout != NULL) {
518
519 /* However if 0 timeout was specified, then we must fail since we need to peform a wait */
520 if (!Timeout->QuadPart) {
521
522 /* Return a timeout */
523 Status = STATUS_TIMEOUT;
524 goto WaitDone;
525 }
526
527 /* Point to Timer Wait Block and Thread Timer */
528 TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
529 ThreadTimer = &CurrentThread->Timer;
530
531 /* Connect the Timer Wait Block */
532 WaitBlock->NextWaitBlock = TimerWaitBlock;
533
534 /* Set up the Timer Wait Block */
535 TimerWaitBlock->Object = (PVOID)ThreadTimer;
536 TimerWaitBlock->Thread = CurrentThread;
537 TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
538 TimerWaitBlock->WaitType = WaitAny;
539 TimerWaitBlock->NextWaitBlock = WaitBlockArray;
540
541 /* Link the timer to this Wait Block */
542 InitializeListHead(&ThreadTimer->Header.WaitListHead);
543
544 /* Insert the Timer into the Timer Lists and enable it */
545 if (!KiInsertTimer(ThreadTimer, *Timeout)) {
546
547 /* Return a timeout if we couldn't insert the timer for some reason */
548 Status = STATUS_TIMEOUT;
549 goto WaitDone;
550 }
551 }
552
553 /* Insert into Object's Wait List*/
554 WaitBlock = CurrentThread->WaitBlockList;
555 do {
556
557 /* Get the Current Object */
558 CurrentObject = WaitBlock->Object;
559
560 /* Link the Object to this Wait Block */
561 InsertTailList(&CurrentObject->WaitListHead, &WaitBlock->WaitListEntry);
562
563 /* Move to the next Wait Block */
564 WaitBlock = WaitBlock->NextWaitBlock;
565 } while (WaitBlock != WaitBlockArray);
566
567 /* Handle Kernel Queues */
568 if (CurrentThread->Queue) {
569
570 DPRINT("Waking Queue\n");
571 KiWakeQueue(CurrentThread->Queue);
572 }
573
574 /* Block the Thread */
575 DPRINT("Blocking the Thread: %d, %d, %d, %x\n", Alertable, WaitMode, WaitReason, KeGetCurrentThread());
576 KiBlockThread(&Status,
577 Alertable,
578 WaitMode,
579 (UCHAR)WaitReason);
580
581 /* Check if we were executing an APC */
582 if (Status != STATUS_KERNEL_APC) {
583
584 /* Return Status */
585 return Status;
586 }
587
588 DPRINT("Looping Again\n");
589 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
590
591 } while (TRUE);
592
593 WaitDone:
594 /* Release the Lock, we are done */
595 DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n", KeGetCurrentThread(), Status);
596 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
597 return Status;
598 }
599
600 VOID
601 FASTCALL
602 KiSatisfyObjectWait(PDISPATCHER_HEADER Object,
603 PKTHREAD Thread)
604
605 {
606 /* Special case for Mutants */
607 if (Object->Type == MutantObject) {
608
609 /* Decrease the Signal State */
610 Object->SignalState--;
611
612 /* Check if it's now non-signaled */
613 if (Object->SignalState == 0) {
614
615 /* Set the Owner Thread */
616 ((PKMUTANT)Object)->OwnerThread = Thread;
617
618 /* Disable APCs if needed */
619 Thread->KernelApcDisable -= ((PKMUTANT)Object)->ApcDisable;
620
621 /* Check if it's abandoned */
622 if (((PKMUTANT)Object)->Abandoned) {
623
624 /* Unabandon it */
625 ((PKMUTANT)Object)->Abandoned = FALSE;
626
627 /* Return Status */
628 Thread->WaitStatus = STATUS_ABANDONED;
629 }
630
631 /* Insert it into the Mutant List */
632 InsertHeadList(&Thread->MutantListHead, &((PKMUTANT)Object)->MutantListEntry);
633 }
634
635 } else if ((Object->Type & TIMER_OR_EVENT_TYPE) == EventSynchronizationObject) {
636
637 /* These guys (Syncronization Timers and Events) just get un-signaled */
638 Object->SignalState = 0;
639
640 } else if (Object->Type == SemaphoreObject) {
641
642 /* These ones can have multiple signalings, so we only decrease it */
643 Object->SignalState--;
644 }
645 }
646
647 VOID
648 FASTCALL
649 KiWaitTest(PDISPATCHER_HEADER Object,
650 KPRIORITY Increment)
651 {
652 PLIST_ENTRY WaitEntry;
653 PLIST_ENTRY WaitList;
654 PKWAIT_BLOCK CurrentWaitBlock;
655 PKWAIT_BLOCK NextWaitBlock;
656 PKTHREAD WaitThread;
657
658 /* Loop the Wait Entries */
659 DPRINT("KiWaitTest for Object: %x\n", Object);
660 WaitList = &Object->WaitListHead;
661 WaitEntry = WaitList->Flink;
662 while ((WaitEntry != WaitList) && (Object->SignalState > 0)) {
663
664 /* Get the current wait block */
665 CurrentWaitBlock = CONTAINING_RECORD(WaitEntry, KWAIT_BLOCK, WaitListEntry);
666 WaitThread = CurrentWaitBlock->Thread;
667
668 /* Check the current Wait Mode */
669 if (CurrentWaitBlock->WaitType == WaitAny) {
670
671 /* Easy case, satisfy only this wait */
672 DPRINT("Satisfiying a Wait any\n");
673 WaitEntry = WaitEntry->Blink;
674 KiSatisfyObjectWait(Object, WaitThread);
675
676 } else {
677
678 /* Everything must be satisfied */
679 DPRINT("Checking for a Wait All\n");
680 NextWaitBlock = CurrentWaitBlock->NextWaitBlock;
681
682 /* Loop first to make sure they are valid */
683 while (NextWaitBlock != CurrentWaitBlock) {
684
685 /* Check if the object is signaled */
686 DPRINT("Checking: %x %d\n", NextWaitBlock->Object, Object->SignalState);
687 if (!KiIsObjectSignaled(NextWaitBlock->Object, WaitThread)) {
688
689 /* It's not, move to the next one */
690 DPRINT("One of the object is non-signaled, sorry.\n");
691 goto SkipUnwait;
692 }
693
694 /* Go to the next Wait block */
695 NextWaitBlock = NextWaitBlock->NextWaitBlock;
696 }
697
698 /* All the objects are signaled, we can satisfy */
699 DPRINT("Satisfiying a Wait All\n");
700 WaitEntry = WaitEntry->Blink;
701 KiSatisifyMultipleObjectWaits(CurrentWaitBlock);
702 }
703
704 /* All waits satisfied, unwait the thread */
705 DPRINT("Unwaiting the Thread\n");
706 KiAbortWaitThread(WaitThread, CurrentWaitBlock->WaitKey, Increment);
707
708 SkipUnwait:
709 /* Next entry */
710 WaitEntry = WaitEntry->Flink;
711 }
712
713 DPRINT("Done\n");
714 }
715
716 /* Must be called with the dispatcher lock held */
717 VOID
718 FASTCALL
719 KiAbortWaitThread(PKTHREAD Thread,
720 NTSTATUS WaitStatus,
721 KPRIORITY Increment)
722 {
723 PKWAIT_BLOCK WaitBlock;
724
725 /* If we are blocked, we must be waiting on something also */
726 DPRINT("KiAbortWaitThread: %x, Status: %x, %x \n", Thread, WaitStatus, Thread->WaitBlockList);
727 ASSERT((Thread->State == Waiting) == (Thread->WaitBlockList != NULL));
728
729 /* Remove the Wait Blocks from the list */
730 DPRINT("Removing waits\n");
731 WaitBlock = Thread->WaitBlockList;
732 do {
733
734 /* Remove it */
735 DPRINT("Removing Waitblock: %x, %x\n", WaitBlock, WaitBlock->NextWaitBlock);
736 RemoveEntryList(&WaitBlock->WaitListEntry);
737
738 /* Go to the next one */
739 WaitBlock = WaitBlock->NextWaitBlock;
740 } while (WaitBlock != Thread->WaitBlockList);
741
742 /* Check if there's a Thread Timer */
743 if (Thread->Timer.Header.Inserted) {
744
745 /* Cancel the Thread Timer with the no-lock fastpath */
746 DPRINT("Removing the Thread's Timer\n");
747 Thread->Timer.Header.Inserted = FALSE;
748 RemoveEntryList(&Thread->Timer.TimerListEntry);
749 }
750
751 /* Increment the Queue's active threads */
752 if (Thread->Queue) {
753
754 DPRINT("Incrementing Queue's active threads\n");
755 Thread->Queue->CurrentCount++;
756 }
757
758 /* Reschedule the Thread */
759 DPRINT("Unblocking the Thread\n");
760 KiUnblockThread(Thread, &WaitStatus, 0);
761 }
762
763 BOOLEAN
764 inline
765 FASTCALL
766 KiIsObjectSignaled(PDISPATCHER_HEADER Object,
767 PKTHREAD Thread)
768 {
769 /* Mutants are...well...mutants! */
770 if (Object->Type == MutantObject) {
771
772 /*
773 * Because Cutler hates mutants, they are actually signaled if the Signal State is <= 0
774 * Well, only if they are recursivly acquired (i.e if we own it right now).
775 * Of course, they are also signaled if their signal state is 1.
776 */
777 if ((Object->SignalState <= 0 && ((PKMUTANT)Object)->OwnerThread == Thread) ||
778 (Object->SignalState == 1)) {
779
780 /* Signaled Mutant */
781 return (TRUE);
782
783 } else {
784
785 /* Unsignaled Mutant */
786 return (FALSE);
787 }
788 }
789
790 /* Any other object is not a mutated freak, so let's use logic */
791 return (!Object->SignalState <= 0);
792 }
793
794 BOOL
795 inline
796 FASTCALL
797 KiIsObjectWaitable(PVOID Object)
798 {
799 POBJECT_HEADER Header;
800 Header = BODY_TO_HEADER(Object);
801
802 if (Header->Type == ExEventObjectType ||
803 Header->Type == IoCompletionType ||
804 Header->Type == ExMutantObjectType ||
805 Header->Type == ExSemaphoreObjectType ||
806 Header->Type == ExTimerType ||
807 Header->Type == PsProcessType ||
808 Header->Type == PsThreadType ||
809 Header->Type == IoFileObjectType) {
810
811 return TRUE;
812
813 } else {
814
815 return FALSE;
816 }
817 }
818
819 VOID
820 inline
821 FASTCALL
822 KiSatisifyMultipleObjectWaits(PKWAIT_BLOCK WaitBlock)
823 {
824 PKWAIT_BLOCK FirstBlock = WaitBlock;
825 PKTHREAD WaitThread = WaitBlock->Thread;
826
827 /* Loop through all the Wait Blocks, and wake each Object */
828 do {
829
830 /* Wake the Object */
831 KiSatisfyObjectWait(WaitBlock->Object, WaitThread);
832 WaitBlock = WaitBlock->NextWaitBlock;
833 } while (WaitBlock != FirstBlock);
834 }
835
836 VOID
837 inline
838 FASTCALL
839 KeInitializeDispatcherHeader(DISPATCHER_HEADER* Header,
840 ULONG Type,
841 ULONG Size,
842 ULONG SignalState)
843 {
844 Header->Type = (UCHAR)Type;
845 Header->Absolute = 0;
846 Header->Inserted = 0;
847 Header->Size = (UCHAR)Size;
848 Header->SignalState = SignalState;
849 InitializeListHead(&(Header->WaitListHead));
850 }
851
852 KIRQL
853 inline
854 FASTCALL
855 KeAcquireDispatcherDatabaseLock(VOID)
856 {
857 KIRQL OldIrql;
858
859 KeAcquireSpinLock (&DispatcherDatabaseLock, &OldIrql);
860 return OldIrql;
861 }
862
863 VOID
864 inline
865 FASTCALL
866 KeAcquireDispatcherDatabaseLockAtDpcLevel(VOID)
867 {
868 KeAcquireSpinLockAtDpcLevel (&DispatcherDatabaseLock);
869 }
870
871 VOID
872 inline
873 FASTCALL
874 KeInitializeDispatcher(VOID)
875 {
876 /* Initialize the Dispatcher Lock */
877 KeInitializeSpinLock(&DispatcherDatabaseLock);
878 }
879
880 VOID
881 inline
882 FASTCALL
883 KeReleaseDispatcherDatabaseLock(KIRQL OldIrql)
884 {
885 /* If it's the idle thread, dispatch */
886 if (!KeIsExecutingDpc() && OldIrql < DISPATCH_LEVEL && KeGetCurrentThread() != NULL &&
887 KeGetCurrentThread() == KeGetCurrentPrcb()->IdleThread) {
888
889 KiDispatchThreadNoLock(Ready);
890 KeLowerIrql(OldIrql);
891
892 } else {
893
894 /* Just release the spin lock */
895 KeReleaseSpinLock(&DispatcherDatabaseLock, OldIrql);
896 }
897 }
898
899 VOID
900 inline
901 FASTCALL
902 KeReleaseDispatcherDatabaseLockFromDpcLevel(VOID)
903 {
904 KeReleaseSpinLockFromDpcLevel(&DispatcherDatabaseLock);
905 }
906
907 /* EOF */