00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
#include "ki.h"
00027
00028
00029
00030
00031
00032
00033 #define ASSERT_QUEUE(Q) ASSERT((Q)->Header.Type == QueueObject);
00034
00035
VOID
00036 KeInitializeQueue (
00037 IN
PRKQUEUE Queue,
00038 IN ULONG Count OPTIONAL
00039 )
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 {
00062
00063
00064
00065
00066
00067
00068 Queue->Header.Type =
QueueObject;
00069 Queue->Header.Size =
sizeof(
KQUEUE) /
sizeof(LONG);
00070 Queue->Header.SignalState = 0;
00071 InitializeListHead(&Queue->Header.WaitListHead);
00072
00073
00074
00075
00076
00077
00078 InitializeListHead(&Queue->EntryListHead);
00079 InitializeListHead(&Queue->ThreadListHead);
00080 Queue->CurrentCount = 0;
00081
if (ARGUMENT_PRESENT((PVOID)(ULONG_PTR)
Count)) {
00082 Queue->MaximumCount =
Count;
00083
00084 }
else {
00085 Queue->MaximumCount =
KeNumberProcessors;
00086 }
00087
00088
return;
00089 }
00090
00091 LONG
00092 KeReadStateQueue (
00093 IN
PRKQUEUE Queue
00094 )
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 {
00113
00114
ASSERT_QUEUE(Queue);
00115
00116
00117
00118
00119
00120
return Queue->Header.SignalState;
00121 }
00122
00123 LONG
00124 KeInsertQueue (
00125 IN
PRKQUEUE Queue,
00126 IN PLIST_ENTRY Entry
00127 )
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 {
00152
00153 KIRQL OldIrql;
00154 LONG OldState;
00155
00156
ASSERT_QUEUE(Queue);
00157
ASSERT(KeGetCurrentIrql() <=
DISPATCH_LEVEL);
00158
00159
00160
00161
00162
00163
KiLockDispatcherDatabase(&OldIrql);
00164
00165
00166
00167
00168
00169 OldState =
KiInsertQueue(Queue, Entry,
FALSE);
00170
00171
00172
00173
00174
00175
00176
KiUnlockDispatcherDatabase(OldIrql);
00177
return OldState;
00178 }
00179
00180 LONG
00181 KeInsertHeadQueue (
00182 IN
PRKQUEUE Queue,
00183 IN PLIST_ENTRY Entry
00184 )
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 {
00209
00210 KIRQL OldIrql;
00211 LONG OldState;
00212
00213
ASSERT_QUEUE(Queue);
00214
ASSERT(KeGetCurrentIrql() <=
DISPATCH_LEVEL);
00215
00216
00217
00218
00219
00220
KiLockDispatcherDatabase(&OldIrql);
00221
00222
00223
00224
00225
00226 OldState =
KiInsertQueue(Queue, Entry,
TRUE);
00227
00228
00229
00230
00231
00232
00233
KiUnlockDispatcherDatabase(OldIrql);
00234
return OldState;
00235 }
00236
00237 PLIST_ENTRY
00238 KeRemoveQueue (
00239 IN
PRKQUEUE Queue,
00240 IN KPROCESSOR_MODE WaitMode,
00241 IN PLARGE_INTEGER Timeout OPTIONAL
00242 )
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 {
00274
00275 LARGE_INTEGER DueTime;
00276 PLIST_ENTRY Entry;
00277
PRKTHREAD NextThread;
00278 LARGE_INTEGER NewTime;
00279 KIRQL OldIrql;
00280
PRKQUEUE OldQueue;
00281 PLARGE_INTEGER OriginalTime;
00282
PRKTHREAD Thread;
00283
PRKTIMER Timer;
00284
PRKWAIT_BLOCK WaitBlock;
00285 LONG_PTR WaitStatus;
00286
00287
ASSERT_QUEUE(Queue);
00288
ASSERT(KeGetCurrentIrql() <=
DISPATCH_LEVEL);
00289
00290
00291
00292
00293
00294
00295
00296 Thread =
KeGetCurrentThread();
00297
if (Thread->
WaitNext) {
00298 Thread->
WaitNext =
FALSE;
00299
00300 }
else {
00301
KiLockDispatcherDatabase(&OldIrql);
00302 Thread->
WaitIrql = OldIrql;
00303 }
00304
00305
00306
00307
00308
00309
00310 OldQueue = Thread->
Queue;
00311 Thread->
Queue = Queue;
00312
if (Queue != OldQueue) {
00313
00314
00315
00316
00317
00318
00319
00320 Entry = &Thread->
QueueListEntry;
00321
if (OldQueue !=
NULL) {
00322 RemoveEntryList(Entry);
00323
KiActivateWaiterQueue(OldQueue);
00324 }
00325
00326
00327
00328
00329
00330
00331 InsertTailList(&Queue->ThreadListHead, Entry);
00332
00333 }
else {
00334
00335
00336
00337
00338
00339
00340 Queue->CurrentCount -= 1;
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 OriginalTime = Timeout;
00358
do {
00359
00360
00361
00362
00363
00364
00365
00366 Entry = Queue->EntryListHead.Flink;
00367
if ((Entry != &Queue->EntryListHead) &&
00368 (Queue->CurrentCount < Queue->MaximumCount)) {
00369
00370
00371
00372
00373
00374
00375
00376 Queue->Header.SignalState -= 1;
00377 Queue->CurrentCount += 1;
00378
if ((Entry->Flink ==
NULL) || (Entry->Blink ==
NULL)) {
00379
KeBugCheckEx(INVALID_WORK_QUEUE_ITEM,
00380 (ULONG_PTR)Entry,
00381 (ULONG_PTR)Queue,
00382 (ULONG_PTR)&
ExWorkerQueue[0],
00383 (ULONG_PTR)((
PWORK_QUEUE_ITEM)Entry)->WorkerRoutine);
00384 }
00385
00386 RemoveEntryList(Entry);
00387 Entry->Flink =
NULL;
00388
break;
00389
00390 }
else {
00391
00392
00393
00394
00395
00396 Thread->
WaitBlockList = &Thread->
WaitBlock[0];
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
if (Thread->
ApcState.
KernelApcPending && (Thread->
WaitIrql <
APC_LEVEL)) {
00410
00411
00412
00413
00414
00415
00416
00417
00418 Queue->CurrentCount += 1;
00419
KiUnlockDispatcherDatabase(Thread->
WaitIrql);
00420
00421 }
else {
00422
00423
00424
00425
00426
00427
if ((WaitMode !=
KernelMode) && (Thread->
ApcState.
UserApcPending)) {
00428 Entry = (PLIST_ENTRY)ULongToPtr(STATUS_USER_APC);
00429 Queue->CurrentCount += 1;
00430
break;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 Thread->
WaitStatus = (
NTSTATUS)0;
00442 WaitBlock = &Thread->
WaitBlock[0];
00443 WaitBlock->
Object = (PVOID)Queue;
00444 WaitBlock->
WaitKey = (CSHORT)(STATUS_SUCCESS);
00445 WaitBlock->
WaitType = WaitAny;
00446 WaitBlock->
Thread = Thread;
00447
00448
00449
00450
00451
00452
if (ARGUMENT_PRESENT(Timeout)) {
00453
00454
00455
00456
00457
00458
00459
if (!(Timeout->LowPart | Timeout->HighPart)) {
00460 Entry = (PLIST_ENTRY)ULongToPtr(STATUS_TIMEOUT);
00461 Queue->CurrentCount += 1;
00462
break;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471 Timer = &Thread->
Timer;
00472 WaitBlock->
NextWaitBlock = &Thread->
WaitBlock[1];
00473 WaitBlock = &Thread->
WaitBlock[1];
00474 WaitBlock->
Object = (PVOID)Timer;
00475 WaitBlock->
WaitKey = (CSHORT)(STATUS_TIMEOUT);
00476 WaitBlock->
WaitType = WaitAny;
00477 WaitBlock->
Thread = Thread;
00478 Timer->
Header.
WaitListHead.Flink = &WaitBlock->
WaitListEntry;
00479 Timer->
Header.
WaitListHead.Blink = &WaitBlock->
WaitListEntry;
00480 WaitBlock->
WaitListEntry.Flink = &Timer->
Header.
WaitListHead;
00481 WaitBlock->
WaitListEntry.Blink = &Timer->
Header.
WaitListHead;
00482
if (
KiInsertTreeTimer(Timer, *Timeout) ==
FALSE) {
00483 Entry = (PLIST_ENTRY)ULongToPtr(STATUS_TIMEOUT);
00484 Queue->CurrentCount += 1;
00485
break;
00486 }
00487
00488 DueTime.QuadPart = Timer->
DueTime.QuadPart;
00489 }
00490
00491
00492
00493
00494
00495 WaitBlock->
NextWaitBlock = &Thread->
WaitBlock[0];
00496
00497
00498
00499
00500
00501 WaitBlock = &Thread->
WaitBlock[0];
00502 InsertTailList(&Queue->Header.WaitListHead, &WaitBlock->
WaitListEntry);
00503
00504
00505
00506
00507
00508
00509 Thread->
Alertable =
FALSE;
00510 Thread->
WaitMode = WaitMode;
00511 Thread->
WaitReason =
WrQueue;
00512 Thread->
WaitTime = KiQueryLowTickCount();
00513 Thread->
State =
Waiting;
00514
KiInsertWaitList(WaitMode, Thread);
00515
00516
00517
00518
00519
00520
00521
00522
ASSERT(Thread->
WaitIrql <=
DISPATCH_LEVEL);
00523
00524 WaitStatus =
KiSwapThread();
00525
00526
00527
00528
00529
00530
00531 Thread->
WaitReason = 0;
00532
if (WaitStatus != STATUS_KERNEL_APC) {
00533
return (PLIST_ENTRY)WaitStatus;
00534 }
00535
00536
if (ARGUMENT_PRESENT(Timeout)) {
00537
00538
00539
00540
00541
00542 Timeout =
KiComputeWaitInterval(OriginalTime,
00543 &DueTime,
00544 &NewTime);
00545 }
00546 }
00547
00548
00549
00550
00551
00552
00553
KiLockDispatcherDatabase(&OldIrql);
00554 Thread->
WaitIrql = OldIrql;
00555 Queue->CurrentCount -= 1;
00556 }
00557
00558 }
while (
TRUE);
00559
00560
00561
00562
00563
00564
00565
KiUnlockDispatcherDatabase(Thread->
WaitIrql);
00566
return Entry;
00567 }
00568
00569 PLIST_ENTRY
00570 KeRundownQueue (
00571 IN
PRKQUEUE Queue
00572 )
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 {
00596
00597 PLIST_ENTRY Entry;
00598 PLIST_ENTRY FirstEntry;
00599 KIRQL OldIrql;
00600
PKTHREAD Thread;
00601
00602
ASSERT_QUEUE(Queue);
00603
ASSERT(KeGetCurrentIrql() <=
DISPATCH_LEVEL);
00604
00605
00606
00607
00608
00609
KiLockDispatcherDatabase(&OldIrql);
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 FirstEntry = Queue->EntryListHead.Flink;
00620
if (FirstEntry == &Queue->EntryListHead) {
00621 FirstEntry =
NULL;
00622
00623 }
else {
00624 RemoveEntryList(&Queue->EntryListHead);
00625 }
00626
00627
00628
00629
00630
00631
while (Queue->ThreadListHead.Flink != &Queue->ThreadListHead) {
00632 Entry = Queue->ThreadListHead.Flink;
00633 Thread = CONTAINING_RECORD(Entry,
KTHREAD, QueueListEntry);
00634 Thread->
Queue =
NULL;
00635 RemoveEntryList(Entry);
00636 }
00637
00638
00639
00640
00641
00642
00643
KiUnlockDispatcherDatabase(OldIrql);
00644
return FirstEntry;
00645 }
00646
00647
VOID
00648
FASTCALL
00649 KiActivateWaiterQueue (
00650 IN
PRKQUEUE Queue
00651 )
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 {
00675
00676 PRLIST_ENTRY Entry;
00677
PRKTHREAD Thread;
00678
PRKWAIT_BLOCK WaitBlock;
00679 PRLIST_ENTRY WaitEntry;
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690 Queue->CurrentCount -= 1;
00691
if (Queue->CurrentCount < Queue->MaximumCount) {
00692 Entry = Queue->EntryListHead.Flink;
00693 WaitEntry = Queue->Header.WaitListHead.Blink;
00694
if ((Entry != &Queue->EntryListHead) &&
00695 (WaitEntry != &Queue->Header.WaitListHead)) {
00696 RemoveEntryList(Entry);
00697 Entry->Flink =
NULL;
00698 Queue->Header.SignalState -= 1;
00699 WaitBlock = CONTAINING_RECORD(WaitEntry,
KWAIT_BLOCK, WaitListEntry);
00700 Thread = WaitBlock->
Thread;
00701
KiUnwaitThread(Thread, (LONG_PTR)Entry, 0);
00702 }
00703 }
00704
00705
return;
00706 }
00707
00708 LONG
00709
FASTCALL
00710 KiInsertQueue (
00711 IN
PRKQUEUE Queue,
00712 IN PLIST_ENTRY Entry,
00713 IN BOOLEAN Head
00714 )
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 {
00743
00744 LONG OldState;
00745
PRKTHREAD Thread;
00746
PKTIMER Timer;
00747
PKWAIT_BLOCK WaitBlock;
00748 PLIST_ENTRY WaitEntry;
00749
00750
ASSERT_QUEUE(Queue);
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764 OldState = Queue->Header.SignalState;
00765 Thread =
KeGetCurrentThread();
00766 WaitEntry = Queue->Header.WaitListHead.Blink;
00767
if ((WaitEntry != &Queue->Header.WaitListHead) &&
00768 (Queue->CurrentCount < Queue->MaximumCount) &&
00769 ((Thread->
Queue != Queue) ||
00770 (Thread->
WaitReason !=
WrQueue))) {
00771
00772
00773
00774
00775
00776
00777 RemoveEntryList(WaitEntry);
00778 WaitBlock = CONTAINING_RECORD(WaitEntry,
KWAIT_BLOCK, WaitListEntry);
00779 Thread = WaitBlock->
Thread;
00780
00781
00782
00783
00784
00785
00786
00787 Thread->
WaitStatus = (LONG_PTR)Entry;
00788 RemoveEntryList(&Thread->
WaitListEntry);
00789 Queue->CurrentCount += 1;
00790 Thread->
WaitReason = 0;
00791
00792
00793
00794
00795
00796 Timer = &Thread->
Timer;
00797
if (Timer->
Header.
Inserted ==
TRUE) {
00798
KiRemoveTreeTimer(Timer);
00799 }
00800
00801
00802
00803
00804
00805
KiReadyThread(Thread);
00806
00807 }
else {
00808 Queue->Header.SignalState += 1;
00809
if (Head !=
FALSE) {
00810 InsertHeadList(&Queue->EntryListHead, Entry);
00811
00812 }
else {
00813 InsertTailList(&Queue->EntryListHead, Entry);
00814 }
00815 }
00816
00817
return OldState;
00818 }
00819