Implementing Signaling

The PAL provides a number of waiting functions for threads. All of these (WaitForSingleObject, WaitForMultipleObjects, Sleep, and SleepEx) are implemented using WaitForMultipleObjectsEx . Because of this, this section will step through only WaitForMultipleObjectsEx. You will understand how signal waiting is done in the PAL if you understand how this function, defined in pal/unix/sync/wait.c, works.

Warning

The code for WaitForMultipleObjectsEx can be tricky to trace at runtime, due to its use of indirect recursion, as well as its use of a worker thread when waiting for a process to terminate.

Here is the function prototype for WaitForMultipleObjectsEx:

    WaitForMultipleObjectsEx(
           IN DWORD nCount,
           IN CONST HANDLE *lpHandles,
           IN BOOL bWaitAll,
           IN DWORD dwMilliseconds,
           IN BOOL bAlertable)

The arguments lpHandles and nCount combine to specify an array containing the handles of all objects to wait on. If all objects must signal before the function returns, bWaitAll is set to true; otherwise, the first object to signal will cause the function to return. The dwMilliseconds argument is used to indicate how long a timeout to use before returning, regardless of success. This argument may be INFINITE, in which case the wait will return only when all wait conditions have been met. Finally, bAlertable indicates that the wait may be terminated by a queued APC, which is discussed in the later section "Asynchronous Procedure Calls.”

The first thing done in WaitForMultipleObjectsEx ...

Get Shared Source CLI Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.