Thread Synchronization

< BACK  NEXT >
[oR]

Like user-mode threads in a Win32 application, system threads may need to suspend their execution until some other condition has been satisfied. This section describes the basic synchronization techniques available to system threads.

Time Synchronization

The simplest kind of synchronization involves stopping a thread's execution until a specific time interval elapses. Although Timer objects, described later in this chapter, can be used, the kernel provides a convenient function, KeDelayExecutionThread (illustrated in Table 14.3) that is easier to use.

Table 14.3. KeDelayExecutionThread Function Prototype
NTSTATUS KeDelayExecutionThread IRQL == PASSIVE_LEVEL
Parameter Description
IN KPROCESSOR_MODE WaitMode KernelMode for drivers
IN BOOLEAN bAlertable FALSE for drivers
IN PLARGE_INTEGER Interval Absolute or relative due time
Return value STATUS_SUCCESS: wait completed

General Synchronization

System threads can synchronize their activities in more general ways by waiting for dispatcher objects. Thread synchronization depends on the fact that a dispatcher object is always in either the signaled or nonsignaled state. When a thread asks to wait for a nonsignaled dispatcher object, the thread's execution stops until the object becomes signaled. (Waiting for a dispatcher object that is already Signaled is a no-op.) There are two different functions that can be used to wait for a dispatcher object.

KeWaitForSingleObject

This function, described in Table 14.4, puts the calling thread into a wait state until a specific dispatcher object is set to the signaled state.

Table 14.4. KeWaitForSingleObject Function Prototype
NTSTATUS KeWaitForSingleObject
Parameter Description
IN PVOID Object Pointer to an initialized dispatcher object
IN KWAIT_REASON Reason Executive for drivers
IN KPROCESSOR_MODE WaitMode KernelMode for drivers
IN BOOLEAN Alertable False for drivers
IN PLARGE_INTEGER Timeout Absolute or relative timeout value
NULL for an infinite wait
Return value STATUS_SUCCESS
STATUS_ALERTED
STATUS TIMEOUT

Optionally, a timeout value may be specified that causes the thread to awaken even if the dispatcher object is nonsignaled. If the timeout argument is NULL, KeWaitForSingleObject waits indefinitely.

KeWaitForMultipleObjects

This function, described in Table 14.5, puts the calling thread into a wait state until any or all of a group of dispatcher objects are set to the signaled state. Again, a timeout value for the wait may be specified.

Table 14.5. KeWaitForMultipleObjects Function Prototype
NTSTATUS KeWaitForMultipleObjects
Parameter Description
IN ULONG Count Number of objects to wait for
IN PVOID Object [ ] Array of pointers to dispatcher objects
IN WAIT_TYPE WaitType WaitAll: wait until all are signaled
WaitAny: wait until one is signaled
IN KWAIT_REASON Reason Executive for drivers
IN KPROCESSOR_MODE WaitMode KernelMode for drivers
IN BOOLEAN Alertable FALSE for drivers
IN PLARGE_INTEGER Timeout Absolute or relative timeout value
NULL for an infinite wait
IN PKWAIT_BLOCK WaitBlocks [ ] Array of wait blocks for this operation
Return value STATUS_SUCCESS
STATUS_ALERTED
STATUS_TIMEOUT

Be aware that there are limits on how many objects the thread can wait for at one time. Each thread has a built-in array of Wait blocks that it uses for concurrent wait operations. The thread can use this array to wait for up to THREAD_WAIT_OBJECTS number of objects. If the number THREAD_WAIT_ OBJECTS is insufficient, a driver-supplied array of Wait blocks must be included in the call to KeWaitForMultipleObjects. Regardless, the number of objects waited upon cannot exceed MAXIMUM_WAIT_OBJECTS.

The KeWaitForXxx functions may be called from either PASSIVE_ LEVEL or DISPATCH_LEVEL IRQL. However, from DISPATCH_LEVEL IRQL, a zero timeout value must be specified. At DISPATCH_LEVEL IRQL, the calls are effectively used as a polling mechanism for Signaled objects.

< BACK  NEXT >


The Windows 2000 Device Driver Book(c) A Guide for Programmers
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
ISBN: 0130204315
EAN: 2147483647
Year: 2000
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net