Merge 13831:14550 from trunk
[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 = NULL;
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 = NULL;
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 = NULL;
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 DPRINT1("(%s:%d) Too many objects!\n", __FILE__, __LINE__);
404 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
405 }
406
407 /* Use the Thread's Wait Block */
408 WaitBlockArray = &CurrentThread->WaitBlock[0];
409
410 } else {
411
412 /* Using our own Block Array. Check in regards to System Object Limit */
413 if (Count > MAXIMUM_WAIT_OBJECTS) {
414
415 DPRINT1("(%s:%d) Too many objects!\n", __FILE__, __LINE__);
416 KEBUGCHECK(MAXIMUM_WAIT_OBJECTS_EXCEEDED);
417 }
418 }
419
420 /* Start the actual Loop */
421 do {
422
423 /* Get the current Wait Status */
424 WaitStatus = CurrentThread->WaitStatus;
425
426 /* Append wait block to the KTHREAD wait block list */
427 CurrentThread->WaitBlockList = WaitBlock = WaitBlockArray;
428
429 /* Check if the wait is (already) satisfied */
430 AllObjectsSignaled = TRUE;
431
432 /* First, we'll try to satisfy the wait directly */
433 for (WaitIndex = 0; WaitIndex < Count; WaitIndex++) {
434
435 /* Get the Current Object */
436 CurrentObject = (PDISPATCHER_HEADER)Object[WaitIndex];
437
438 /* FIXME:
439 * Temporary hack until my Object Manager re-write. Basically some objects, like
440 * the File Object, but also LPCs and others, are actually waitable on their event.
441 * The Object Manager sets this up in The ObjectTypeInformation->DefaultObject member,
442 * by using pretty much the same kind of hack as us. Normal objects point to themselves
443 * in that pointer. Then, NtWaitForXXX will populate the WaitList that gets sent to us by
444 * using ->DefaultObject, so the proper actual objects will be sent to us. Until then however,
445 * I will keep this hack here, since there's no need to make an interim hack until the rewrite
446 * -- Alex Ionescu 24/02/05
447 */
448 if (CurrentObject->Type == IO_TYPE_FILE) {
449
450 DPRINT1("Hack used: %x\n", &((PFILE_OBJECT)CurrentObject)->Event);
451 CurrentObject = (PDISPATCHER_HEADER)(&((PFILE_OBJECT)CurrentObject)->Event);
452 }
453
454 /* Check if the Object is Signaled */
455 if (KiIsObjectSignaled(CurrentObject, CurrentThread)) {
456
457 /* Check what kind of wait this is */
458 if (WaitType == WaitAny) {
459
460 /* This is a Wait Any, so just unwait this guy and exit */
461 if (CurrentObject->SignalState != MINLONG) {
462
463 /* It has a normal signal state, so unwait it and return */
464 KiSatisfyObjectWait(CurrentObject, CurrentThread);
465 Status = STATUS_WAIT_0 | WaitIndex;
466 goto WaitDone;
467
468 } else {
469
470 /* Is this a Mutant? */
471 if (CurrentObject->Type == MutantObject) {
472
473 /* According to wasm.ru, we must raise this exception (tested and true) */
474 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
475 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED);
476 }
477 }
478 }
479
480 } else {
481
482 /* One of the objects isn't signaled... if this is a WaitAll, we will fail later */
483 AllObjectsSignaled = FALSE;
484 }
485
486 /* Set up a Wait Block for this Object */
487 WaitBlock->Object = CurrentObject;
488 WaitBlock->Thread = CurrentThread;
489 WaitBlock->WaitKey = (USHORT)(STATUS_WAIT_0 + WaitIndex);
490 WaitBlock->WaitType = (USHORT)WaitType;
491 WaitBlock->NextWaitBlock = WaitBlock + 1;
492
493 /* Move to the next Wait Block */
494 WaitBlock = WaitBlock->NextWaitBlock;
495 }
496
497 /* Return to the Root Wait Block */
498 WaitBlock--;
499 WaitBlock->NextWaitBlock = NULL;
500
501 /* Check if this is a Wait All and all the objects are signaled */
502 if ((WaitType == WaitAll) && (AllObjectsSignaled)) {
503
504 /* Return to the Root Wait Block */
505 WaitBlock = CurrentThread->WaitBlockList;
506
507 /* Satisfy their Waits and return to the caller */
508 KiSatisifyMultipleObjectWaits(WaitBlock);
509 Status = STATUS_WAIT_0;
510 goto WaitDone;
511 }
512
513 /* Make sure we can satisfy the Alertable request */
514 KiCheckAlertability(Alertable, CurrentThread, WaitMode, &Status);
515
516 /* Set the Wait Status */
517 CurrentThread->WaitStatus = Status;
518
519 /* Enable the Timeout Timer if there was any specified */
520 if (Timeout != NULL) {
521
522 /* However if 0 timeout was specified, then we must fail since we need to peform a wait */
523 if (!Timeout->QuadPart) {
524
525 /* Return a timeout */
526 Status = STATUS_TIMEOUT;
527 goto WaitDone;
528 }
529
530 /* Point to Timer Wait Block and Thread Timer */
531 TimerWaitBlock = &CurrentThread->WaitBlock[TIMER_WAIT_BLOCK];
532 ThreadTimer = &CurrentThread->Timer;
533
534 /* Connect the Timer Wait Block */
535 WaitBlock->NextWaitBlock = TimerWaitBlock;
536
537 /* Set up the Timer Wait Block */
538 TimerWaitBlock->Object = (PVOID)ThreadTimer;
539 TimerWaitBlock->Thread = CurrentThread;
540 TimerWaitBlock->WaitKey = STATUS_TIMEOUT;
541 TimerWaitBlock->WaitType = WaitAny;
542 TimerWaitBlock->NextWaitBlock = NULL;
543
544 /* Link the timer to this Wait Block */
545 InitializeListHead(&ThreadTimer->Header.WaitListHead);
546
547 /* Insert the Timer into the Timer Lists and enable it */
548 if (!KiInsertTimer(ThreadTimer, *Timeout)) {
549
550 /* Return a timeout if we couldn't insert the timer for some reason */
551 Status = STATUS_TIMEOUT;
552 goto WaitDone;
553 }
554 }
555
556 /* Insert into Object's Wait List*/
557 WaitBlock = CurrentThread->WaitBlockList;
558 while (WaitBlock) {
559
560 /* Get the Current Object */
561 CurrentObject = WaitBlock->Object;
562
563 /* Link the Object to this Wait Block */
564 InsertTailList(&CurrentObject->WaitListHead, &WaitBlock->WaitListEntry);
565
566 /* Move to the next Wait Block */
567 WaitBlock = WaitBlock->NextWaitBlock;
568 }
569
570 /* Handle Kernel Queues */
571 if (CurrentThread->Queue) {
572
573 DPRINT("Waking Queue\n");
574 KiWakeQueue(CurrentThread->Queue);
575 }
576
577 /* Block the Thread */
578 DPRINT("Blocking the Thread: %d, %d, %d, %x\n", Alertable, WaitMode, WaitReason, KeGetCurrentThread());
579 KiBlockThread(&Status,
580 Alertable,
581 WaitMode,
582 (UCHAR)WaitReason);
583
584 /* Check if we were executing an APC */
585 if (Status != STATUS_KERNEL_APC) {
586
587 /* Return Status */
588 return Status;
589 }
590
591 DPRINT("Looping Again\n");
592 CurrentThread->WaitIrql = KeAcquireDispatcherDatabaseLock();
593
594 } while (TRUE);
595
596 WaitDone:
597 /* Release the Lock, we are done */
598 DPRINT("Returning from KeWaitForMultipleObjects(), %x. Status: %d\n", KeGetCurrentThread(), Status);
599 KeReleaseDispatcherDatabaseLock(CurrentThread->WaitIrql);
600 return Status;
601 }
602
603 VOID
604 FASTCALL
605 KiSatisfyObjectWait(PDISPATCHER_HEADER Object,
606 PKTHREAD Thread)
607
608 {
609 /* Special case for Mutants */
610 if (Object->Type == MutantObject) {
611
612 /* Decrease the Signal State */
613 Object->SignalState--;
614
615 /* Check if it's now non-signaled */
616 if (Object->SignalState == 0) {
617
618 /* Set the Owner Thread */
619 ((PKMUTANT)Object)->OwnerThread = Thread;
620
621 /* Disable APCs if needed */
622 Thread->KernelApcDisable -= ((PKMUTANT)Object)->ApcDisable;
623
624 /* Check if it's abandoned */
625 if (((PKMUTANT)Object)->Abandoned) {
626
627 /* Unabandon it */
628 ((PKMUTANT)Object)->Abandoned = FALSE;
629
630 /* Return Status */
631 Thread->WaitStatus = STATUS_ABANDONED;
632 }
633
634 /* Insert it into the Mutant List */
635 InsertHeadList(&Thread->MutantListHead, &((PKMUTANT)Object)->MutantListEntry);
636 }
637
638 } else if ((Object->Type & TIMER_OR_EVENT_TYPE) == EventSynchronizationObject) {
639
640 /* These guys (Syncronization Timers and Events) just get un-signaled */
641 Object->SignalState = 0;
642
643 } else if (Object->Type == SemaphoreObject) {
644
645 /* These ones can have multiple signalings, so we only decrease it */
646 Object->SignalState--;
647 }
648 }
649
650 VOID
651 FASTCALL
652 KiWaitTest(PDISPATCHER_HEADER Object,
653 KPRIORITY Increment)
654 {
655 PLIST_ENTRY WaitEntry;
656 PLIST_ENTRY WaitList;
657 PKWAIT_BLOCK CurrentWaitBlock;
658 PKWAIT_BLOCK NextWaitBlock;
659
660 /* Loop the Wait Entries */
661 DPRINT("KiWaitTest for Object: %x\n", Object);
662 WaitList = &Object->WaitListHead;
663 WaitEntry = WaitList->Flink;
664 while ((WaitEntry != WaitList) && (Object->SignalState > 0)) {
665
666 /* Get the current wait block */
667 CurrentWaitBlock = CONTAINING_RECORD(WaitEntry, KWAIT_BLOCK, WaitListEntry);
668
669 /* Check the current Wait Mode */
670 if (CurrentWaitBlock->WaitType == WaitAny) {
671
672 /* Easy case, satisfy only this wait */
673 DPRINT("Satisfiying a Wait any\n");
674 WaitEntry = WaitEntry->Blink;
675 KiSatisfyObjectWait(Object, CurrentWaitBlock->Thread);
676
677 } else {
678
679 /* Everything must be satisfied */
680 DPRINT("Checking for a Wait All\n");
681 NextWaitBlock = CurrentWaitBlock->NextWaitBlock;
682
683 /* Loop first to make sure they are valid */
684 while (NextWaitBlock) {
685
686 /* Check if the object is signaled */
687 if (!KiIsObjectSignaled(Object, CurrentWaitBlock->Thread)) {
688
689 /* It's not, move to the next one */
690 DPRINT1("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(CurrentWaitBlock->Thread, 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 == THREAD_STATE_BLOCKED) == (Thread->WaitBlockList != NULL));
728
729 /* Remove the Wait Blocks from the list */
730 DPRINT("Removing waits\n");
731 WaitBlock = Thread->WaitBlockList;
732 while (WaitBlock) {
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 };
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->ObjectType == ExEventObjectType ||
803 Header->ObjectType == ExIoCompletionType ||
804 Header->ObjectType == ExMutantObjectType ||
805 Header->ObjectType == ExSemaphoreObjectType ||
806 Header->ObjectType == ExTimerType ||
807 Header->ObjectType == PsProcessType ||
808 Header->ObjectType == PsThreadType ||
809 Header->ObjectType == 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 PKTHREAD WaitThread = WaitBlock->Thread;
825
826 /* Loop through all the Wait Blocks, and wake each Object */
827 while (WaitBlock) {
828
829 /* Wake the Object */
830 KiSatisfyObjectWait(WaitBlock->Object, WaitThread);
831 WaitBlock = WaitBlock->NextWaitBlock;
832 }
833 }
834
835 VOID
836 inline
837 FASTCALL
838 KeInitializeDispatcherHeader(DISPATCHER_HEADER* Header,
839 ULONG Type,
840 ULONG Size,
841 ULONG SignalState)
842 {
843 Header->Type = (UCHAR)Type;
844 Header->Absolute = 0;
845 Header->Inserted = 0;
846 Header->Size = (UCHAR)Size;
847 Header->SignalState = SignalState;
848 InitializeListHead(&(Header->WaitListHead));
849 }
850
851 KIRQL
852 inline
853 FASTCALL
854 KeAcquireDispatcherDatabaseLock(VOID)
855 {
856 KIRQL OldIrql;
857
858 KeAcquireSpinLock (&DispatcherDatabaseLock, &OldIrql);
859 return OldIrql;
860 }
861
862 VOID
863 inline
864 FASTCALL
865 KeAcquireDispatcherDatabaseLockAtDpcLevel(VOID)
866 {
867 KeAcquireSpinLockAtDpcLevel (&DispatcherDatabaseLock);
868 }
869
870 VOID
871 inline
872 FASTCALL
873 KeInitializeDispatcher(VOID)
874 {
875 /* Initialize the Dispatcher Lock */
876 KeInitializeSpinLock(&DispatcherDatabaseLock);
877 }
878
879 VOID
880 inline
881 FASTCALL
882 KeReleaseDispatcherDatabaseLock(KIRQL OldIrql)
883 {
884 /* If it's the idle thread, dispatch */
885 if (!KeIsExecutingDpc() && OldIrql < DISPATCH_LEVEL && KeGetCurrentThread() != NULL &&
886 KeGetCurrentThread() == KeGetCurrentPrcb()->IdleThread) {
887
888 KiDispatchThreadNoLock(THREAD_STATE_READY);
889 KeLowerIrql(OldIrql);
890
891 } else {
892
893 /* Just release the spin lock */
894 KeReleaseSpinLock(&DispatcherDatabaseLock, OldIrql);
895 }
896 }
897
898 VOID
899 inline
900 FASTCALL
901 KeReleaseDispatcherDatabaseLockFromDpcLevel(VOID)
902 {
903 KeReleaseSpinLockFromDpcLevel(&DispatcherDatabaseLock);
904 }
905
906 /* EOF */