SignalObjectAndWait

Windows Kernel Synchronization API Reference

SignalObjectAndWait

The SignalObjectAndWait function signals one or more synchronization objects and then waits on one or more other synchronization objects. This function allows a thread to signal an event or semaphore and then wait for another event or semaphore without entering a user-mode wait state between the signal and the wait. This is particularly useful for avoiding race conditions and for improving the performance of certain synchronization scenarios.

Note: This function is intended for use by kernel-mode drivers and is not generally available to user-mode applications. For user-mode equivalents, consider using separate SetEvent (or equivalent) and wait functions.

Syntax


PWIN32_NTSTATUS WINAPI SignalObjectAndWait(
  _In_ HANDLE hObjectToSignal,
  _In_ HANDLE hObjectToWait,
  _In_ PLARGE_INTEGER pTimeOut,
  _In_ BOOL bAlertable
);
                    

Parameters

hObjectToSignal

A handle to the synchronization object to be signaled. This object can be an event, a semaphore, a mutex, or a timer.

hObjectToWait

A handle to the synchronization object on which to wait. This object can be an event, a semaphore, a mutex, or a timer.

pTimeOut

A pointer to a LARGE_INTEGER structure that specifies the absolute or relative time out interval for the wait. If pTimeOut is a negative value, the interval is relative to the current time. If pTimeOut is zero, the function will always return immediately. If pTimeOut is NULL, the function will wait indefinitely.

bAlertable

If this parameter is TRUE, the function returns if the thread receives an I/O completion packet or if the thread is in an alertable wait state and the thread's APC queue becomes non-empty. Otherwise, this parameter is FALSE.

Return Value

STATUS_SUCCESS if the function succeeds.

An appropriate NTSTATUS code if the function fails. Common failure codes include:

  • STATUS_INVALID_HANDLE: One of the handles provided is invalid.
  • STATUS_TIMEOUT: The wait timed out.
  • STATUS_ALERTED: The wait was alertable and the thread was alerted.

Remarks

The SignalObjectAndWait function is a powerful tool for managing inter-thread communication and synchronization in kernel-mode. By combining a signal operation with a wait operation atomically, it helps prevent race conditions that could occur if these operations were performed separately.

It's crucial to ensure that the handles provided are valid and that the objects are compatible for signaling and waiting.

See Also