Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/*
|
1998-08-25 04:27:26 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2006-01-03 21:34:19 +00:00
|
|
|
* PROJECT: ReactOS Kernel
|
2005-01-26 13:58:37 +00:00
|
|
|
* FILE: ntoskrnl/ex/work.c
|
2006-01-03 21:34:19 +00:00
|
|
|
* PURPOSE: Manage system work queues and worker threads
|
|
|
|
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
|
1998-08-25 04:27:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2001-06-04 11:26:13 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
1998-08-25 04:27:26 +00:00
|
|
|
|
2005-11-28 23:25:31 +00:00
|
|
|
#if defined (ALLOC_PRAGMA)
|
|
|
|
#pragma alloc_text(INIT, ExpInitializeWorkerThreads)
|
|
|
|
#endif
|
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* DATA **********************************************************************/
|
2000-01-26 10:07:30 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Number of worker threads for each Queue */
|
2006-01-04 20:45:58 +00:00
|
|
|
#define EX_HYPERCRITICAL_WORK_THREADS 1
|
|
|
|
#define EX_DELAYED_WORK_THREADS 3
|
|
|
|
#define EX_CRITICAL_WORK_THREADS 5
|
2000-01-26 10:07:30 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Magic flag for dynamic worker threads */
|
2006-01-04 20:45:58 +00:00
|
|
|
#define EX_DYNAMIC_WORK_THREAD 0x80000000
|
1998-08-25 04:27:26 +00:00
|
|
|
|
2006-01-04 20:45:58 +00:00
|
|
|
/* Worker thread priority increments (added to base priority) */
|
|
|
|
#define EX_HYPERCRITICAL_QUEUE_PRIORITY_INCREMENT 7
|
|
|
|
#define EX_CRITICAL_QUEUE_PRIORITY_INCREMENT 5
|
|
|
|
#define EX_DELAYED_QUEUE_PRIORITY_INCREMENT 4
|
1998-08-25 04:27:26 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* The actual worker queue array */
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
EX_WORK_QUEUE ExWorkerQueue[MaximumWorkQueue];
|
1998-08-25 04:27:26 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Accounting of the total threads and registry hacked threads */
|
|
|
|
ULONG ExpCriticalWorkerThreads;
|
|
|
|
ULONG ExpDelayedWorkerThreads;
|
|
|
|
ULONG ExpAdditionalCriticalWorkerThreads;
|
|
|
|
ULONG ExpAdditionalDelayedWorkerThreads;
|
1998-08-25 04:27:26 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Future support for stack swapping worker threads */
|
|
|
|
BOOLEAN ExpWorkersCanSwap;
|
|
|
|
LIST_ENTRY ExpWorkerListHead;
|
2010-03-08 20:37:24 +00:00
|
|
|
FAST_MUTEX ExpWorkerSwapinMutex;
|
2006-01-03 21:34:19 +00:00
|
|
|
|
|
|
|
/* The worker balance set manager events */
|
|
|
|
KEVENT ExpThreadSetManagerEvent;
|
|
|
|
KEVENT ExpThreadSetManagerShutdownEvent;
|
|
|
|
|
|
|
|
/* Thread pointers for future worker thread shutdown support */
|
|
|
|
PETHREAD ExpWorkerThreadBalanceManagerPtr;
|
|
|
|
PETHREAD ExpLastWorkerThread;
|
|
|
|
|
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name ExpWorkerThreadEntryPoint
|
|
|
|
*
|
|
|
|
* The ExpWorkerThreadEntryPoint routine is the entrypoint for any new
|
|
|
|
* worker thread created by teh system.
|
|
|
|
*
|
|
|
|
* @param Context
|
|
|
|
* Contains the work queue type masked with a flag specifing whether the
|
|
|
|
* thread is dynamic or not.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks A dynamic thread can timeout after 10 minutes of waiting on a queue
|
|
|
|
* while a static thread will never timeout.
|
|
|
|
*
|
|
|
|
* Worker threads must return at IRQL == PASSIVE_LEVEL, must not have
|
|
|
|
* active impersonation info, and must not have disabled APCs.
|
|
|
|
*
|
|
|
|
* NB: We will re-enable APCs for broken threads but all other cases
|
|
|
|
* will generate a bugcheck.
|
|
|
|
*
|
|
|
|
*--*/
|
2005-05-09 01:38:29 +00:00
|
|
|
VOID
|
2006-01-03 21:34:19 +00:00
|
|
|
NTAPI
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
ExpWorkerThreadEntryPoint(IN PVOID Context)
|
1998-08-25 04:27:26 +00:00
|
|
|
{
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
PWORK_QUEUE_ITEM WorkItem;
|
|
|
|
PLIST_ENTRY QueueEntry;
|
|
|
|
WORK_QUEUE_TYPE WorkQueueType;
|
|
|
|
PEX_WORK_QUEUE WorkQueue;
|
2006-01-03 21:34:19 +00:00
|
|
|
LARGE_INTEGER Timeout;
|
|
|
|
PLARGE_INTEGER TimeoutPointer = NULL;
|
|
|
|
PETHREAD Thread = PsGetCurrentThread();
|
|
|
|
KPROCESSOR_MODE WaitMode;
|
|
|
|
EX_QUEUE_WORKER_INFO OldValue, NewValue;
|
|
|
|
|
|
|
|
/* Check if this is a dyamic thread */
|
|
|
|
if ((ULONG_PTR)Context & EX_DYNAMIC_WORK_THREAD)
|
|
|
|
{
|
|
|
|
/* It is, which means we will eventually time out after 10 minutes */
|
|
|
|
Timeout.QuadPart = Int32x32To64(10, -10000000 * 60);
|
|
|
|
TimeoutPointer = &Timeout;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/* Get Queue Type and Worker Queue */
|
2006-01-03 21:34:19 +00:00
|
|
|
WorkQueueType = (WORK_QUEUE_TYPE)((ULONG_PTR)Context &
|
|
|
|
~EX_DYNAMIC_WORK_THREAD);
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
WorkQueue = &ExWorkerQueue[WorkQueueType];
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Select the wait mode */
|
|
|
|
WaitMode = (UCHAR)WorkQueue->Info.WaitMode;
|
|
|
|
|
|
|
|
/* Nobody should have initialized this yet, do it now */
|
|
|
|
ASSERT(Thread->ExWorkerCanWaitUser == 0);
|
|
|
|
if (WaitMode == UserMode) Thread->ExWorkerCanWaitUser = TRUE;
|
|
|
|
|
|
|
|
/* If we shouldn't swap, disable that feature */
|
|
|
|
if (!ExpWorkersCanSwap) KeSetKernelStackSwapEnable(FALSE);
|
|
|
|
|
|
|
|
/* Set the worker flags */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Check if the queue is being disabled */
|
|
|
|
if (WorkQueue->Info.QueueDisabled)
|
|
|
|
{
|
|
|
|
/* Re-enable stack swapping and kill us */
|
|
|
|
KeSetKernelStackSwapEnable(TRUE);
|
|
|
|
PsTerminateSystemThread(STATUS_SYSTEM_SHUTDOWN);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Increase the worker count */
|
|
|
|
OldValue = WorkQueue->Info;
|
|
|
|
NewValue = OldValue;
|
|
|
|
NewValue.WorkerCount++;
|
|
|
|
}
|
|
|
|
while (InterlockedCompareExchange((PLONG)&WorkQueue->Info,
|
|
|
|
*(PLONG)&NewValue,
|
|
|
|
*(PLONG)&OldValue) != *(PLONG)&OldValue);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Success, you are now officially a worker thread! */
|
|
|
|
Thread->ActiveExWorker = TRUE;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Loop forever */
|
|
|
|
ProcessLoop:
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Wait for Something to Happen on the Queue */
|
|
|
|
QueueEntry = KeRemoveQueue(&WorkQueue->WorkerQueue,
|
|
|
|
WaitMode,
|
|
|
|
TimeoutPointer);
|
|
|
|
|
|
|
|
/* Check if we timed out and quit this loop in that case */
|
2010-01-13 22:35:43 +00:00
|
|
|
if ((NTSTATUS)(ULONG_PTR)QueueEntry == STATUS_TIMEOUT) break;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/* Increment Processed Work Items */
|
2005-05-01 19:30:06 +00:00
|
|
|
InterlockedIncrement((PLONG)&WorkQueue->WorkItemsProcessed);
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
|
|
|
|
/* Get the Work Item */
|
|
|
|
WorkItem = CONTAINING_RECORD(QueueEntry, WORK_QUEUE_ITEM, List);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Make sure nobody is trying to play smart with us */
|
|
|
|
ASSERT((ULONG_PTR)WorkItem->WorkerRoutine > MmUserProbeAddress);
|
|
|
|
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/* Call the Worker Routine */
|
|
|
|
WorkItem->WorkerRoutine(WorkItem->Parameter);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Make sure APCs are not disabled */
|
|
|
|
if (Thread->Tcb.SpecialApcDisable)
|
|
|
|
{
|
|
|
|
/* We're nice and do it behind your back */
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT1("Warning: Broken Worker Thread: %p %p %p came back "
|
2006-01-03 21:34:19 +00:00
|
|
|
"with APCs disabled!\n",
|
|
|
|
WorkItem->WorkerRoutine,
|
|
|
|
WorkItem->Parameter,
|
|
|
|
WorkItem);
|
|
|
|
Thread->Tcb.SpecialApcDisable = 0;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Make sure it returned at right IRQL */
|
|
|
|
if (KeGetCurrentIrql() != PASSIVE_LEVEL)
|
|
|
|
{
|
|
|
|
/* It didn't, bugcheck! */
|
2008-08-24 15:48:05 +00:00
|
|
|
KeBugCheckEx(WORKER_THREAD_RETURNED_AT_BAD_IRQL,
|
2005-11-08 20:07:41 +00:00
|
|
|
(ULONG_PTR)WorkItem->WorkerRoutine,
|
|
|
|
KeGetCurrentIrql(),
|
|
|
|
(ULONG_PTR)WorkItem->Parameter,
|
|
|
|
(ULONG_PTR)WorkItem);
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/* Make sure it returned with Impersionation Disabled */
|
2006-01-03 21:34:19 +00:00
|
|
|
if (Thread->ActiveImpersonationInfo)
|
|
|
|
{
|
|
|
|
/* It didn't, bugcheck! */
|
2008-08-24 15:48:05 +00:00
|
|
|
KeBugCheckEx(IMPERSONATING_WORKER_THREAD,
|
2005-11-08 20:07:41 +00:00
|
|
|
(ULONG_PTR)WorkItem->WorkerRoutine,
|
|
|
|
(ULONG_PTR)WorkItem->Parameter,
|
|
|
|
(ULONG_PTR)WorkItem,
|
|
|
|
0);
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-03 21:34:19 +00:00
|
|
|
|
|
|
|
/* This is a dynamic thread. Terminate it unless IRPs are pending */
|
|
|
|
if (!IsListEmpty(&Thread->IrpList)) goto ProcessLoop;
|
|
|
|
|
|
|
|
/* Don't terminate it if the queue is disabled either */
|
|
|
|
if (WorkQueue->Info.QueueDisabled) goto ProcessLoop;
|
|
|
|
|
|
|
|
/* Set the worker flags */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Decrease the worker count */
|
|
|
|
OldValue = WorkQueue->Info;
|
|
|
|
NewValue = OldValue;
|
|
|
|
NewValue.WorkerCount--;
|
|
|
|
}
|
|
|
|
while (InterlockedCompareExchange((PLONG)&WorkQueue->Info,
|
|
|
|
*(PLONG)&NewValue,
|
|
|
|
*(PLONG)&OldValue) != *(PLONG)&OldValue);
|
|
|
|
|
|
|
|
/* Decrement dynamic thread count */
|
|
|
|
InterlockedDecrement(&WorkQueue->DynamicThreadCount);
|
|
|
|
|
|
|
|
/* We're not a worker thread anymore */
|
|
|
|
Thread->ActiveExWorker = FALSE;
|
|
|
|
|
|
|
|
/* Re-enable the stack swap */
|
|
|
|
KeSetKernelStackSwapEnable(TRUE);
|
|
|
|
return;
|
1998-08-25 04:27:26 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/*++
|
|
|
|
* @name ExpCreateWorkerThread
|
|
|
|
*
|
|
|
|
* The ExpCreateWorkerThread routine creates a new worker thread for the
|
|
|
|
* specified queue.
|
|
|
|
**
|
|
|
|
* @param QueueType
|
|
|
|
* Type of the queue to use for this thread. Valid values are:
|
|
|
|
* - DelayedWorkQueue
|
|
|
|
* - CriticalWorkQueue
|
|
|
|
* - HyperCriticalWorkQueue
|
|
|
|
*
|
|
|
|
* @param Dynamic
|
|
|
|
* Specifies whether or not this thread is a dynamic thread.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks HyperCritical work threads run at priority 7; Critical work threads
|
|
|
|
* run at priority 5, and delayed work threads run at priority 4.
|
|
|
|
*
|
|
|
|
* This, worker threads cannot pre-empty a normal user-mode thread.
|
|
|
|
*
|
|
|
|
*--*/
|
2005-05-09 01:38:29 +00:00
|
|
|
VOID
|
2006-01-03 21:34:19 +00:00
|
|
|
NTAPI
|
|
|
|
ExpCreateWorkerThread(WORK_QUEUE_TYPE WorkQueueType,
|
|
|
|
IN BOOLEAN Dynamic)
|
1998-08-25 04:27:26 +00:00
|
|
|
{
|
2005-03-13 14:21:47 +00:00
|
|
|
PETHREAD Thread;
|
|
|
|
HANDLE hThread;
|
2006-01-03 21:34:19 +00:00
|
|
|
ULONG Context;
|
|
|
|
KPRIORITY Priority;
|
|
|
|
|
|
|
|
/* Check if this is going to be a dynamic thread */
|
|
|
|
Context = WorkQueueType;
|
|
|
|
|
|
|
|
/* Add the dynamic mask */
|
|
|
|
if (Dynamic) Context |= EX_DYNAMIC_WORK_THREAD;
|
|
|
|
|
|
|
|
/* Create the System Thread */
|
|
|
|
PsCreateSystemThread(&hThread,
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
ExpWorkerThreadEntryPoint,
|
2010-01-13 22:35:43 +00:00
|
|
|
UlongToPtr(Context));
|
2006-01-03 21:34:19 +00:00
|
|
|
|
|
|
|
/* If the thread is dynamic */
|
|
|
|
if (Dynamic)
|
|
|
|
{
|
|
|
|
/* Increase the count */
|
|
|
|
InterlockedIncrement(&ExWorkerQueue[WorkQueueType].DynamicThreadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the priority */
|
|
|
|
if (WorkQueueType == DelayedWorkQueue)
|
|
|
|
{
|
|
|
|
/* Priority == 4 */
|
2006-01-04 20:45:58 +00:00
|
|
|
Priority = EX_DELAYED_QUEUE_PRIORITY_INCREMENT;
|
2006-01-03 21:34:19 +00:00
|
|
|
}
|
|
|
|
else if (WorkQueueType == CriticalWorkQueue)
|
|
|
|
{
|
|
|
|
/* Priority == 5 */
|
2006-01-04 20:45:58 +00:00
|
|
|
Priority = EX_CRITICAL_QUEUE_PRIORITY_INCREMENT;
|
2006-01-03 21:34:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Priority == 7 */
|
2006-01-04 20:45:58 +00:00
|
|
|
Priority = EX_HYPERCRITICAL_QUEUE_PRIORITY_INCREMENT;
|
2006-01-03 21:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the Thread */
|
|
|
|
ObReferenceObjectByHandle(hThread,
|
|
|
|
THREAD_SET_INFORMATION,
|
|
|
|
PsThreadType,
|
|
|
|
KernelMode,
|
|
|
|
(PVOID*)&Thread,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Set the Priority */
|
2006-01-04 20:45:58 +00:00
|
|
|
KeSetBasePriorityThread(&Thread->Tcb, Priority);
|
2006-01-03 21:34:19 +00:00
|
|
|
|
|
|
|
/* Dereference and close handle */
|
|
|
|
ObDereferenceObject(Thread);
|
2010-01-23 21:27:26 +00:00
|
|
|
ObCloseHandle(hThread, KernelMode);
|
2006-01-03 21:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name ExpCheckDynamicThreadCount
|
|
|
|
*
|
|
|
|
* The ExpCheckDynamicThreadCount routine checks every queue and creates a
|
|
|
|
* dynamic thread if the queue seems to be deadlocked.
|
|
|
|
*
|
|
|
|
* @param None
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks The algorithm for deciding if a new thread must be created is
|
|
|
|
* based on wether the queue has processed no new items in the last
|
|
|
|
* second, and new items are still enqueued.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpDetectWorkerThreadDeadlock(VOID)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
PEX_WORK_QUEUE Queue;
|
|
|
|
|
|
|
|
/* Loop the 3 queues */
|
|
|
|
for (i = 0; i < MaximumWorkQueue; i++)
|
|
|
|
{
|
|
|
|
/* Get the queue */
|
|
|
|
Queue = &ExWorkerQueue[i];
|
|
|
|
ASSERT(Queue->DynamicThreadCount <= 16);
|
|
|
|
|
|
|
|
/* Check if stuff is on the queue that still is unprocessed */
|
|
|
|
if ((Queue->QueueDepthLastPass) &&
|
|
|
|
(Queue->WorkItemsProcessed == Queue->WorkItemsProcessedLastPass) &&
|
|
|
|
(Queue->DynamicThreadCount < 16))
|
|
|
|
{
|
|
|
|
/* Stuff is still on the queue and nobody did anything about it */
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT1("EX: Work Queue Deadlock detected: %lu\n", i);
|
2006-01-03 21:34:19 +00:00
|
|
|
ExpCreateWorkerThread(i, TRUE);
|
|
|
|
DPRINT1("Dynamic threads queued %d\n", Queue->DynamicThreadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update our data */
|
|
|
|
Queue->WorkItemsProcessedLastPass = Queue->WorkItemsProcessed;
|
|
|
|
Queue->QueueDepthLastPass = KeReadStateQueue(&Queue->WorkerQueue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name ExpCheckDynamicThreadCount
|
|
|
|
*
|
|
|
|
* The ExpCheckDynamicThreadCount routine checks every queue and creates a
|
|
|
|
* dynamic thread if the queue requires one.
|
|
|
|
*
|
|
|
|
* @param None
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks The algorithm for deciding if a new thread must be created is
|
|
|
|
* documented in the ExQueueWorkItem routine.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpCheckDynamicThreadCount(VOID)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
PEX_WORK_QUEUE Queue;
|
|
|
|
|
|
|
|
/* Loop the 3 queues */
|
|
|
|
for (i = 0; i < MaximumWorkQueue; i++)
|
|
|
|
{
|
|
|
|
/* Get the queue */
|
|
|
|
Queue = &ExWorkerQueue[i];
|
|
|
|
|
|
|
|
/* Check if still need a new thread. See ExQueueWorkItem */
|
|
|
|
if ((Queue->Info.MakeThreadsAsNecessary) &&
|
|
|
|
(!IsListEmpty(&Queue->WorkerQueue.EntryListHead)) &&
|
|
|
|
(Queue->WorkerQueue.CurrentCount <
|
|
|
|
Queue->WorkerQueue.MaximumCount) &&
|
|
|
|
(Queue->DynamicThreadCount < 16))
|
|
|
|
{
|
|
|
|
/* Create a new thread */
|
|
|
|
DPRINT1("EX: Creating new dynamic thread as requested\n");
|
|
|
|
ExpCreateWorkerThread(i, TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/*++
|
|
|
|
* @name ExpWorkerThreadBalanceManager
|
|
|
|
*
|
|
|
|
* The ExpWorkerThreadBalanceManager routine is the entrypoint for the
|
|
|
|
* worker thread balance set manager.
|
|
|
|
*
|
|
|
|
* @param Context
|
|
|
|
* Unused.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks The worker thread balance set manager listens every second, but can
|
|
|
|
* also be woken up by an event when a new thread is needed, or by the
|
|
|
|
* special shutdown event. This thread runs at priority 7.
|
|
|
|
*
|
|
|
|
* This routine must run at IRQL == PASSIVE_LEVEL.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpWorkerThreadBalanceManager(IN PVOID Context)
|
|
|
|
{
|
|
|
|
KTIMER Timer;
|
|
|
|
LARGE_INTEGER Timeout;
|
|
|
|
NTSTATUS Status;
|
2006-01-05 06:24:07 +00:00
|
|
|
PVOID WaitEvents[3];
|
2006-01-03 21:34:19 +00:00
|
|
|
PAGED_CODE();
|
|
|
|
UNREFERENCED_PARAMETER(Context);
|
|
|
|
|
|
|
|
/* Raise our priority above all other worker threads */
|
|
|
|
KeSetBasePriorityThread(KeGetCurrentThread(),
|
2006-01-04 20:45:58 +00:00
|
|
|
EX_CRITICAL_QUEUE_PRIORITY_INCREMENT + 1);
|
2006-01-03 21:34:19 +00:00
|
|
|
|
|
|
|
/* Setup the timer */
|
|
|
|
KeInitializeTimer(&Timer);
|
|
|
|
Timeout.QuadPart = Int32x32To64(-1, 10000000);
|
|
|
|
|
|
|
|
/* We'll wait on the periodic timer and also the emergency event */
|
|
|
|
WaitEvents[0] = &Timer;
|
|
|
|
WaitEvents[1] = &ExpThreadSetManagerEvent;
|
|
|
|
WaitEvents[2] = &ExpThreadSetManagerShutdownEvent;
|
|
|
|
|
|
|
|
/* Start wait loop */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Wait for the timer */
|
|
|
|
KeSetTimer(&Timer, Timeout, NULL);
|
|
|
|
Status = KeWaitForMultipleObjects(3,
|
|
|
|
WaitEvents,
|
|
|
|
WaitAny,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
if (Status == 0)
|
|
|
|
{
|
|
|
|
/* Our timer expired. Check for deadlocks */
|
|
|
|
ExpDetectWorkerThreadDeadlock();
|
|
|
|
}
|
|
|
|
else if (Status == 1)
|
|
|
|
{
|
|
|
|
/* Someone notified us, verify if we should create a new thread */
|
|
|
|
ExpCheckDynamicThreadCount();
|
|
|
|
}
|
|
|
|
else if (Status == 2)
|
|
|
|
{
|
|
|
|
/* We are shutting down. Cancel the timer */
|
|
|
|
DPRINT1("System shutdown\n");
|
|
|
|
KeCancelTimer(&Timer);
|
|
|
|
|
|
|
|
/* Make sure we have a final thread */
|
|
|
|
ASSERT(ExpLastWorkerThread);
|
|
|
|
|
|
|
|
/* Wait for it */
|
|
|
|
KeWaitForSingleObject(ExpLastWorkerThread,
|
|
|
|
Executive,
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
KernelMode,
|
2006-01-03 21:34:19 +00:00
|
|
|
FALSE,
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
NULL);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Dereference it and kill us */
|
|
|
|
ObDereferenceObject(ExpLastWorkerThread);
|
|
|
|
PsTerminateSystemThread(STATUS_SYSTEM_SHUTDOWN);
|
|
|
|
}
|
2005-03-13 14:21:47 +00:00
|
|
|
}
|
1998-08-25 04:27:26 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/*++
|
|
|
|
* @name ExpInitializeWorkerThreads
|
|
|
|
*
|
|
|
|
* The ExpInitializeWorkerThreads routine initializes worker thread and
|
|
|
|
* work queue support.
|
|
|
|
*
|
|
|
|
* @param None.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks This routine is only called once during system initialization.
|
|
|
|
*
|
|
|
|
*--*/
|
2005-05-09 01:38:29 +00:00
|
|
|
VOID
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
INIT_FUNCTION
|
2006-01-03 21:34:19 +00:00
|
|
|
NTAPI
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
ExpInitializeWorkerThreads(VOID)
|
1998-08-25 04:27:26 +00:00
|
|
|
{
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
ULONG WorkQueueType;
|
2006-01-03 21:34:19 +00:00
|
|
|
ULONG CriticalThreads, DelayedThreads;
|
|
|
|
HANDLE ThreadHandle;
|
|
|
|
PETHREAD Thread;
|
|
|
|
ULONG i;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Setup the stack swap support */
|
2010-03-08 20:37:24 +00:00
|
|
|
ExInitializeFastMutex(&ExpWorkerSwapinMutex);
|
2006-01-03 21:34:19 +00:00
|
|
|
InitializeListHead(&ExpWorkerListHead);
|
|
|
|
ExpWorkersCanSwap = TRUE;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Set the number of critical and delayed threads. We shouldn't hardcode */
|
|
|
|
DelayedThreads = EX_DELAYED_WORK_THREADS;
|
|
|
|
CriticalThreads = EX_CRITICAL_WORK_THREADS;
|
|
|
|
|
|
|
|
/* Protect against greedy registry modifications */
|
|
|
|
ExpAdditionalDelayedWorkerThreads =
|
2006-01-04 22:46:45 +00:00
|
|
|
min(ExpAdditionalDelayedWorkerThreads, 16);
|
2006-01-03 21:34:19 +00:00
|
|
|
ExpAdditionalCriticalWorkerThreads =
|
|
|
|
min(ExpAdditionalCriticalWorkerThreads, 16);
|
|
|
|
|
|
|
|
/* Calculate final count */
|
|
|
|
DelayedThreads += ExpAdditionalDelayedWorkerThreads;
|
|
|
|
CriticalThreads += ExpAdditionalCriticalWorkerThreads;
|
|
|
|
|
|
|
|
/* Initialize the Array */
|
|
|
|
for (WorkQueueType = 0; WorkQueueType < MaximumWorkQueue; WorkQueueType++)
|
|
|
|
{
|
|
|
|
/* Clear the structure and initialize the queue */
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
RtlZeroMemory(&ExWorkerQueue[WorkQueueType], sizeof(EX_WORK_QUEUE));
|
|
|
|
KeInitializeQueue(&ExWorkerQueue[WorkQueueType].WorkerQueue, 0);
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Dynamic threads are only used for the critical queue */
|
|
|
|
ExWorkerQueue[CriticalWorkQueue].Info.MakeThreadsAsNecessary = TRUE;
|
|
|
|
|
|
|
|
/* Initialize the balance set manager events */
|
|
|
|
KeInitializeEvent(&ExpThreadSetManagerEvent, SynchronizationEvent, FALSE);
|
|
|
|
KeInitializeEvent(&ExpThreadSetManagerShutdownEvent,
|
|
|
|
NotificationEvent,
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
/* Create the built-in worker threads for the critical queue */
|
|
|
|
for (i = 0; i < CriticalThreads; i++)
|
|
|
|
{
|
|
|
|
/* Create the thread */
|
|
|
|
ExpCreateWorkerThread(CriticalWorkQueue, FALSE);
|
|
|
|
ExpCriticalWorkerThreads++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the built-in worker threads for the delayed queue */
|
|
|
|
for (i = 0; i < DelayedThreads; i++)
|
|
|
|
{
|
|
|
|
/* Create the thread */
|
|
|
|
ExpCreateWorkerThread(DelayedWorkQueue, FALSE);
|
|
|
|
ExpDelayedWorkerThreads++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the built-in worker thread for the hypercritical queue */
|
|
|
|
ExpCreateWorkerThread(HyperCriticalWorkQueue, FALSE);
|
|
|
|
|
|
|
|
/* Create the balance set manager thread */
|
|
|
|
PsCreateSystemThread(&ThreadHandle,
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
ExpWorkerThreadBalanceManager,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Get a pointer to it for the shutdown process */
|
|
|
|
ObReferenceObjectByHandle(ThreadHandle,
|
|
|
|
THREAD_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
KernelMode,
|
|
|
|
(PVOID*)&Thread,
|
|
|
|
NULL);
|
|
|
|
ExpWorkerThreadBalanceManagerPtr = Thread;
|
|
|
|
|
|
|
|
/* Close the handle and return */
|
2010-01-23 21:27:26 +00:00
|
|
|
ObCloseHandle(ThreadHandle, KernelMode);
|
1998-08-25 04:27:26 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:37:24 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExpSetSwappingKernelApc(IN PKAPC Apc,
|
|
|
|
OUT PKNORMAL_ROUTINE *NormalRoutine,
|
|
|
|
IN OUT PVOID *NormalContext,
|
|
|
|
IN OUT PVOID *SystemArgument1,
|
|
|
|
IN OUT PVOID *SystemArgument2)
|
|
|
|
{
|
|
|
|
PBOOLEAN AllowSwap;
|
|
|
|
PKEVENT Event = (PKEVENT)*SystemArgument1;
|
|
|
|
|
|
|
|
/* Make sure it's an active worker */
|
|
|
|
if (PsGetCurrentThread()->ActiveExWorker)
|
|
|
|
{
|
|
|
|
/* Read the setting from the context flag */
|
|
|
|
AllowSwap = (PBOOLEAN)NormalContext;
|
|
|
|
KeSetKernelStackSwapEnable(*AllowSwap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let caller know that we're done */
|
|
|
|
KeSetEvent(Event, 0, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
ExSwapinWorkerThreads(IN BOOLEAN AllowSwap)
|
|
|
|
{
|
|
|
|
KEVENT Event;
|
|
|
|
PETHREAD CurrentThread = PsGetCurrentThread(), Thread;
|
|
|
|
PEPROCESS Process = PsInitialSystemProcess;
|
|
|
|
KAPC Apc;
|
|
|
|
PAGED_CODE();
|
|
|
|
|
|
|
|
/* Initialize an event so we know when we're done */
|
|
|
|
KeInitializeEvent(&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
/* Lock this routine */
|
|
|
|
ExAcquireFastMutex(&ExpWorkerSwapinMutex);
|
|
|
|
|
|
|
|
/* New threads cannot swap anymore */
|
|
|
|
ExpWorkersCanSwap = AllowSwap;
|
|
|
|
|
|
|
|
/* Loop all threads in the system process */
|
|
|
|
Thread = PsGetNextProcessThread(Process, NULL);
|
|
|
|
while (Thread)
|
|
|
|
{
|
|
|
|
/* Skip threads with explicit permission to do this */
|
|
|
|
if (Thread->ExWorkerCanWaitUser) goto Next;
|
|
|
|
|
|
|
|
/* Check if we reached ourselves */
|
|
|
|
if (Thread == CurrentThread)
|
|
|
|
{
|
|
|
|
/* Do it inline */
|
|
|
|
KeSetKernelStackSwapEnable(AllowSwap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Queue an APC */
|
|
|
|
KeInitializeApc(&Apc,
|
|
|
|
&Thread->Tcb,
|
|
|
|
InsertApcEnvironment,
|
|
|
|
ExpSetSwappingKernelApc,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
KernelMode,
|
|
|
|
&AllowSwap);
|
|
|
|
if (KeInsertQueueApc(&Apc, &Event, NULL, 3))
|
|
|
|
{
|
|
|
|
/* Wait for the APC to run */
|
|
|
|
KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
|
|
|
|
KeClearEvent(&Event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next thread */
|
|
|
|
Next:
|
|
|
|
Thread = PsGetNextProcessThread(Process, Thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the lock */
|
|
|
|
ExReleaseFastMutex(&ExpWorkerSwapinMutex);
|
|
|
|
}
|
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* PUBLIC FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name ExQueueWorkItem
|
|
|
|
* @implemented NT4
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
*
|
2006-01-03 21:34:19 +00:00
|
|
|
* The ExQueueWorkItem routine acquires rundown protection for
|
|
|
|
* the specified descriptor.
|
|
|
|
*
|
|
|
|
* @param WorkItem
|
|
|
|
* Pointer to an initialized Work Queue Item structure. This structure
|
|
|
|
* must be located in nonpaged pool memory.
|
|
|
|
*
|
|
|
|
* @param QueueType
|
|
|
|
* Type of the queue to use for this item. Can be one of the following:
|
|
|
|
* - DelayedWorkQueue
|
|
|
|
* - CriticalWorkQueue
|
|
|
|
* - HyperCriticalWorkQueue
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks This routine is obsolete. Use IoQueueWorkItem instead.
|
|
|
|
*
|
|
|
|
* Callers of this routine must be running at IRQL <= DISPATCH_LEVEL.
|
|
|
|
*
|
|
|
|
*--*/
|
2005-05-09 01:38:29 +00:00
|
|
|
VOID
|
2006-01-03 21:34:19 +00:00
|
|
|
NTAPI
|
2007-01-15 07:33:42 +00:00
|
|
|
ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem,
|
|
|
|
IN WORK_QUEUE_TYPE QueueType)
|
1998-08-25 04:27:26 +00:00
|
|
|
{
|
2006-01-03 21:34:19 +00:00
|
|
|
PEX_WORK_QUEUE WorkQueue = &ExWorkerQueue[QueueType];
|
|
|
|
ASSERT(QueueType < MaximumWorkQueue);
|
2004-12-24 02:09:12 +00:00
|
|
|
ASSERT(WorkItem->List.Flink == NULL);
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-03 21:34:19 +00:00
|
|
|
/* Don't try to trick us */
|
|
|
|
if ((ULONG_PTR)WorkItem->WorkerRoutine < MmUserProbeAddress)
|
|
|
|
{
|
|
|
|
/* Bugcheck the system */
|
2008-08-24 15:48:05 +00:00
|
|
|
KeBugCheckEx(WORKER_INVALID,
|
2006-01-03 21:34:19 +00:00
|
|
|
1,
|
|
|
|
(ULONG_PTR)WorkItem,
|
|
|
|
(ULONG_PTR)WorkItem->WorkerRoutine,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
Dispatching & Queue Rewrite II:
- Rewrote wait code. It is now cleaner, more optimized and faster. All waiting
functions are now clearly differentiated instead of sharing code. These functions
are called up to a dozen times a second, so having dedicated code for each of
them is a real boost in speed.
- Fixed several queue issues, made a dedicated queue wait/wake function (you are not
supposed to use KeWaitFor on a queue, and this is also a speed boost), and make it
compatible with new wait code.
- Optimized Work Queue code to be much smaller and better organized, by using an
array instead of hard-coded multiple static variables. Also, add support for the
real NT structures and implementation, paving the road for Dynamic Work Items, which
also have timeouts, and deadlock dection + debug info.
- Simplified PsBlockThread and made it compatible with wait code.
- Added support for priority boosting when unwaiting a thread; will use later, as well
as put proper boosting for dispatch objects.
- Inlined all dispatcher lock functions and header initialization for speed.
- Moved executive wait code into ob.
svn path=/trunk/; revision=14047
2005-03-14 05:54:32 +00:00
|
|
|
/* Insert the Queue */
|
2006-01-03 21:34:19 +00:00
|
|
|
KeInsertQueue(&WorkQueue->WorkerQueue, &WorkItem->List);
|
|
|
|
ASSERT(!WorkQueue->Info.QueueDisabled);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we need a new thread. Our decision is as follows:
|
|
|
|
* - This queue type must support Dynamic Threads (duh!)
|
|
|
|
* - It actually has to have unprocessed items
|
|
|
|
* - We have CPUs which could be handling another thread
|
|
|
|
* - We haven't abused our usage of dynamic threads.
|
|
|
|
*/
|
|
|
|
if ((WorkQueue->Info.MakeThreadsAsNecessary) &&
|
|
|
|
(!IsListEmpty(&WorkQueue->WorkerQueue.EntryListHead)) &&
|
|
|
|
(WorkQueue->WorkerQueue.CurrentCount <
|
|
|
|
WorkQueue->WorkerQueue.MaximumCount) &&
|
|
|
|
(WorkQueue->DynamicThreadCount < 16))
|
|
|
|
{
|
|
|
|
/* Let the balance manager know about it */
|
2013-08-31 16:02:13 +00:00
|
|
|
DPRINT1("Requesting a new thread. CurrentCount: %lu. MaxCount: %lu\n",
|
2006-01-03 21:34:19 +00:00
|
|
|
WorkQueue->WorkerQueue.CurrentCount,
|
|
|
|
WorkQueue->WorkerQueue.MaximumCount);
|
|
|
|
KeSetEvent(&ExpThreadSetManagerEvent, 0, FALSE);
|
|
|
|
}
|
1998-08-25 04:27:26 +00:00
|
|
|
}
|
2000-06-07 13:05:09 +00:00
|
|
|
|
|
|
|
/* EOF */
|
2006-01-03 21:34:19 +00:00
|
|
|
|