2012-02-16 06:57:27 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2012-10-25 20:40:41 +00:00
|
|
|
* PROJECT: ReactOS Client/Server Runtime SubSystem
|
|
|
|
* FILE: subsystems/win32/csrsrv/wait.c
|
2012-02-16 06:57:27 +00:00
|
|
|
* PURPOSE: CSR Server DLL Wait Implementation
|
|
|
|
* PROGRAMMERS: Emanuele Aliberti
|
|
|
|
* Alex Ionescu (alex@relsoft.net)
|
|
|
|
*/
|
|
|
|
|
2012-10-27 14:30:32 +00:00
|
|
|
/* INCLUDES *******************************************************************/
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
#include "srv.h"
|
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-10-27 14:30:32 +00:00
|
|
|
/* DATA ***********************************************************************/
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
RTL_CRITICAL_SECTION CsrWaitListsLock;
|
|
|
|
|
2012-10-27 14:30:32 +00:00
|
|
|
/* PRIVATE FUNCTIONS **********************************************************/
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrInitializeWait
|
|
|
|
*
|
|
|
|
* The CsrInitializeWait routine initializes a CSR Wait Object.
|
|
|
|
*
|
|
|
|
* @param WaitFunction
|
|
|
|
* Pointer to the function that will handle this wait.
|
|
|
|
*
|
|
|
|
* @param CsrWaitThread
|
|
|
|
* Pointer to the CSR Thread that will perform the wait.
|
|
|
|
*
|
|
|
|
* @param WaitApiMessage
|
|
|
|
* Pointer to the CSR API Message associated to this wait.
|
|
|
|
*
|
|
|
|
* @param WaitContext
|
|
|
|
* Pointer to a user-defined parameter associated to this wait.
|
|
|
|
*
|
|
|
|
* @param NewWaitBlock
|
|
|
|
* Pointed to the initialized CSR Wait Block for this wait.
|
|
|
|
*
|
2012-10-13 20:32:44 +00:00
|
|
|
* @return TRUE in case of success, FALSE otherwise.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CsrInitializeWait(IN CSR_WAIT_FUNCTION WaitFunction,
|
|
|
|
IN PCSR_THREAD CsrWaitThread,
|
|
|
|
IN OUT PCSR_API_MESSAGE WaitApiMessage,
|
|
|
|
IN PVOID WaitContext,
|
|
|
|
OUT PCSR_WAIT_BLOCK *NewWaitBlock)
|
|
|
|
{
|
|
|
|
ULONG Size;
|
|
|
|
PCSR_WAIT_BLOCK WaitBlock;
|
|
|
|
|
|
|
|
/* Calculate the size of the wait block */
|
|
|
|
Size = sizeof(CSR_WAIT_BLOCK) -
|
|
|
|
sizeof(WaitBlock->WaitApiMessage) +
|
|
|
|
WaitApiMessage->Header.u1.s1.TotalLength;
|
|
|
|
|
|
|
|
/* Allocate the Wait Block */
|
2013-01-26 21:23:10 +00:00
|
|
|
WaitBlock = RtlAllocateHeap(CsrHeap, HEAP_ZERO_MEMORY, Size);
|
2012-02-16 06:57:27 +00:00
|
|
|
if (!WaitBlock)
|
|
|
|
{
|
|
|
|
/* Fail */
|
|
|
|
WaitApiMessage->Status = STATUS_NO_MEMORY;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize it */
|
2013-10-05 20:06:47 +00:00
|
|
|
WaitBlock->Size = Size;
|
|
|
|
WaitBlock->WaitThread = CsrWaitThread;
|
|
|
|
WaitBlock->WaitContext = WaitContext;
|
|
|
|
WaitBlock->WaitFunction = WaitFunction;
|
2013-09-12 21:05:58 +00:00
|
|
|
WaitBlock->WaitList.Flink = NULL;
|
|
|
|
WaitBlock->WaitList.Blink = NULL;
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
/* Copy the message */
|
2013-09-12 21:05:58 +00:00
|
|
|
RtlCopyMemory(&WaitBlock->WaitApiMessage,
|
2012-02-16 06:57:27 +00:00
|
|
|
WaitApiMessage,
|
|
|
|
WaitApiMessage->Header.u1.s1.TotalLength);
|
|
|
|
|
|
|
|
/* Return the block */
|
|
|
|
*NewWaitBlock = WaitBlock;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrNotifyWaitBlock
|
|
|
|
*
|
|
|
|
* The CsrNotifyWaitBlock routine calls the wait function for a registered
|
|
|
|
* CSR Wait Block, and replies to the attached CSR API Message, if any.
|
|
|
|
*
|
|
|
|
* @param WaitBlock
|
2013-10-05 20:06:47 +00:00
|
|
|
* Pointer to the CSR Wait Block.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitList
|
|
|
|
* Pointer to the wait list for this wait.
|
|
|
|
*
|
|
|
|
* @param WaitArgument[1-2]
|
|
|
|
* User-defined values to pass to the wait function.
|
|
|
|
*
|
|
|
|
* @param WaitFlags
|
|
|
|
* Wait flags for this wait.
|
|
|
|
*
|
|
|
|
* @param DereferenceThread
|
|
|
|
* Specifies whether the CSR Thread should be dereferenced at the
|
|
|
|
* end of this wait.
|
|
|
|
*
|
|
|
|
* @return TRUE in case of success, FALSE otherwise.
|
|
|
|
*
|
|
|
|
* @remarks After a wait block is notified, the wait function becomes invalid.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CsrNotifyWaitBlock(IN PCSR_WAIT_BLOCK WaitBlock,
|
|
|
|
IN PLIST_ENTRY WaitList,
|
|
|
|
IN PVOID WaitArgument1,
|
|
|
|
IN PVOID WaitArgument2,
|
|
|
|
IN ULONG WaitFlags,
|
|
|
|
IN BOOLEAN DereferenceThread)
|
|
|
|
{
|
|
|
|
/* Call the wait function */
|
2013-01-03 17:18:19 +00:00
|
|
|
if (WaitBlock->WaitFunction(WaitList,
|
|
|
|
WaitBlock->WaitThread,
|
|
|
|
&WaitBlock->WaitApiMessage,
|
|
|
|
WaitBlock->WaitContext,
|
|
|
|
WaitArgument1,
|
|
|
|
WaitArgument2,
|
|
|
|
WaitFlags))
|
2012-02-16 06:57:27 +00:00
|
|
|
{
|
|
|
|
/* The wait is done, clear the block */
|
|
|
|
WaitBlock->WaitThread->WaitBlock = NULL;
|
|
|
|
|
|
|
|
/* Check for captured arguments */
|
|
|
|
if (WaitBlock->WaitApiMessage.CsrCaptureData)
|
|
|
|
{
|
|
|
|
/* Release them */
|
|
|
|
CsrReleaseCapturedArguments(&WaitBlock->WaitApiMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reply to the port */
|
|
|
|
NtReplyPort(WaitBlock->WaitThread->Process->ClientPort,
|
2013-10-05 20:06:47 +00:00
|
|
|
&WaitBlock->WaitApiMessage.Header);
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
/* Check if we should dereference the thread */
|
|
|
|
if (DereferenceThread)
|
|
|
|
{
|
|
|
|
/* Remove it from the Wait List */
|
|
|
|
if (WaitBlock->WaitList.Flink)
|
|
|
|
{
|
|
|
|
RemoveEntryList(&WaitBlock->WaitList);
|
|
|
|
}
|
|
|
|
|
2012-11-20 19:34:03 +00:00
|
|
|
/* Dereference the thread */
|
2012-02-16 06:57:27 +00:00
|
|
|
CsrDereferenceThread(WaitBlock->WaitThread);
|
|
|
|
|
|
|
|
/* Free the wait block */
|
|
|
|
RtlFreeHeap(CsrHeap, 0, WaitBlock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The wait is complete, but the thread is being kept alive */
|
|
|
|
WaitBlock->WaitFunction = NULL;
|
|
|
|
}
|
2013-01-03 17:18:19 +00:00
|
|
|
|
|
|
|
/* The wait succeeded */
|
2012-02-16 06:57:27 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-03 17:18:19 +00:00
|
|
|
|
2012-02-16 06:57:27 +00:00
|
|
|
/* The wait failed */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:32 +00:00
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
2012-02-16 06:57:27 +00:00
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrCreateWait
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrCreateWait routine creates a CSR Wait.
|
|
|
|
*
|
|
|
|
* @param WaitList
|
2013-10-05 20:06:47 +00:00
|
|
|
* Pointer to a wait list in which the wait will be added.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitFunction
|
|
|
|
* Pointer to the function that will handle this wait.
|
|
|
|
*
|
|
|
|
* @param CsrWaitThread
|
|
|
|
* Pointer to the CSR Thread that will perform the wait.
|
|
|
|
*
|
|
|
|
* @param WaitApiMessage
|
|
|
|
* Pointer to the CSR API Message associated to this wait.
|
|
|
|
*
|
|
|
|
* @param WaitContext
|
|
|
|
* Pointer to a user-defined parameter associated to this wait.
|
|
|
|
*
|
|
|
|
* @return TRUE in case of success, FALSE otherwise.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CsrCreateWait(IN PLIST_ENTRY WaitList,
|
|
|
|
IN CSR_WAIT_FUNCTION WaitFunction,
|
|
|
|
IN PCSR_THREAD CsrWaitThread,
|
|
|
|
IN OUT PCSR_API_MESSAGE WaitApiMessage,
|
2013-09-12 21:05:58 +00:00
|
|
|
IN PVOID WaitContext)
|
2012-02-16 06:57:27 +00:00
|
|
|
{
|
|
|
|
PCSR_WAIT_BLOCK WaitBlock;
|
|
|
|
|
|
|
|
/* Initialize the wait */
|
|
|
|
if (!CsrInitializeWait(WaitFunction,
|
|
|
|
CsrWaitThread,
|
|
|
|
WaitApiMessage,
|
|
|
|
WaitContext,
|
|
|
|
&WaitBlock))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Acquire the Wait Lock */
|
|
|
|
CsrAcquireWaitLock();
|
|
|
|
|
|
|
|
/* Make sure the thread wasn't destroyed */
|
|
|
|
if (CsrWaitThread->Flags & CsrThreadTerminated)
|
|
|
|
{
|
|
|
|
/* Fail the wait */
|
|
|
|
RtlFreeHeap(CsrHeap, 0, WaitBlock);
|
|
|
|
CsrReleaseWaitLock();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-05 20:06:47 +00:00
|
|
|
/* Associate the newly created wait to the waiting thread */
|
|
|
|
CsrWaitThread->WaitBlock = WaitBlock;
|
|
|
|
|
2012-02-16 06:57:27 +00:00
|
|
|
/* Insert the wait in the queue */
|
|
|
|
InsertTailList(WaitList, &WaitBlock->WaitList);
|
|
|
|
|
|
|
|
/* Return */
|
|
|
|
CsrReleaseWaitLock();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrDereferenceWait
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
2012-10-13 20:32:44 +00:00
|
|
|
* The CsrDereferenceWait routine dereferences a CSR Wait Block.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitList
|
|
|
|
* Pointer to the Wait List associated to the wait.
|
|
|
|
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrDereferenceWait(IN PLIST_ENTRY WaitList)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PCSR_WAIT_BLOCK WaitBlock;
|
|
|
|
|
|
|
|
/* Acquire the Process and Wait Locks */
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
CsrAcquireWaitLock();
|
|
|
|
|
|
|
|
/* Set the list pointers */
|
|
|
|
NextEntry = WaitList->Flink;
|
|
|
|
|
|
|
|
/* Start the loop */
|
|
|
|
while (NextEntry != WaitList)
|
|
|
|
{
|
|
|
|
/* Get the wait block */
|
|
|
|
WaitBlock = CONTAINING_RECORD(NextEntry, CSR_WAIT_BLOCK, WaitList);
|
|
|
|
|
|
|
|
/* Move to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
2013-01-03 17:18:19 +00:00
|
|
|
/* Check if there's no Wait Routine (satisfied wait) */
|
|
|
|
if (WaitBlock->WaitFunction == NULL)
|
2012-02-16 06:57:27 +00:00
|
|
|
{
|
|
|
|
/* Remove it from the Wait List */
|
|
|
|
if (WaitBlock->WaitList.Flink)
|
|
|
|
{
|
|
|
|
RemoveEntryList(&WaitBlock->WaitList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dereference the thread waiting on it */
|
|
|
|
CsrDereferenceThread(WaitBlock->WaitThread);
|
|
|
|
|
|
|
|
/* Free the block */
|
|
|
|
RtlFreeHeap(CsrHeap, 0, WaitBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the locks */
|
|
|
|
CsrReleaseWaitLock();
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrMoveSatisfiedWait
|
|
|
|
* @implemented NT5
|
|
|
|
*
|
2013-10-05 20:06:47 +00:00
|
|
|
* The CsrMoveSatisfiedWait routine moves satisfied waits from
|
|
|
|
* a wait list to another list.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
2013-10-05 20:06:47 +00:00
|
|
|
* @param DestinationList
|
|
|
|
* Pointer to a list in which the satisfied waits will be added.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitList
|
2013-10-05 20:06:47 +00:00
|
|
|
* Pointer to the wait list whose satisfied wait blocks
|
|
|
|
* will be moved away.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2013-10-05 20:06:47 +00:00
|
|
|
CsrMoveSatisfiedWait(IN PLIST_ENTRY DestinationList,
|
2012-02-16 06:57:27 +00:00
|
|
|
IN PLIST_ENTRY WaitList)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PCSR_WAIT_BLOCK WaitBlock;
|
|
|
|
|
|
|
|
/* Acquire the Wait Lock */
|
|
|
|
CsrAcquireWaitLock();
|
|
|
|
|
|
|
|
/* Set the List pointers */
|
|
|
|
NextEntry = WaitList->Flink;
|
|
|
|
|
|
|
|
/* Start looping */
|
|
|
|
while (NextEntry != WaitList)
|
|
|
|
{
|
|
|
|
/* Get the Wait block */
|
|
|
|
WaitBlock = CONTAINING_RECORD(NextEntry, CSR_WAIT_BLOCK, WaitList);
|
|
|
|
|
|
|
|
/* Go to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
2013-01-03 17:18:19 +00:00
|
|
|
/* Check if there's no Wait Routine (satisfied wait) */
|
|
|
|
if (WaitBlock->WaitFunction == NULL)
|
2012-02-16 06:57:27 +00:00
|
|
|
{
|
|
|
|
/* Remove it from the Wait Block Queue */
|
|
|
|
RemoveEntryList(&WaitBlock->WaitList);
|
|
|
|
|
2013-10-05 20:06:47 +00:00
|
|
|
/* Insert the wait into the destination list */
|
|
|
|
InsertTailList(DestinationList, &WaitBlock->WaitList);
|
2012-02-16 06:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the wait lock */
|
|
|
|
CsrReleaseWaitLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrNotifyWait
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
2013-10-05 20:10:06 +00:00
|
|
|
* The CsrNotifyWait routine notifies CSR Wait Blocks.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitList
|
2013-10-05 20:06:47 +00:00
|
|
|
* Pointer to the wait list whose wait blocks will be notified.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
2013-10-05 22:17:34 +00:00
|
|
|
* @param NotifyAll
|
|
|
|
* Whether or not we must notify all the waits.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @param WaitArgument[1-2]
|
|
|
|
* User-defined argument to pass on to the wait function.
|
|
|
|
*
|
2012-10-13 20:32:44 +00:00
|
|
|
* @return TRUE in case of success, FALSE otherwise.
|
2012-02-16 06:57:27 +00:00
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CsrNotifyWait(IN PLIST_ENTRY WaitList,
|
2013-10-05 22:17:34 +00:00
|
|
|
IN BOOLEAN NotifyAll,
|
2012-02-16 06:57:27 +00:00
|
|
|
IN PVOID WaitArgument1,
|
|
|
|
IN PVOID WaitArgument2)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PCSR_WAIT_BLOCK WaitBlock;
|
|
|
|
BOOLEAN NotifySuccess = FALSE;
|
|
|
|
|
|
|
|
/* Acquire the Wait Lock */
|
|
|
|
CsrAcquireWaitLock();
|
|
|
|
|
|
|
|
/* Set the List pointers */
|
|
|
|
NextEntry = WaitList->Flink;
|
|
|
|
|
|
|
|
/* Start looping */
|
|
|
|
while (NextEntry != WaitList)
|
|
|
|
{
|
|
|
|
/* Get the Wait block */
|
|
|
|
WaitBlock = CONTAINING_RECORD(NextEntry, CSR_WAIT_BLOCK, WaitList);
|
|
|
|
|
|
|
|
/* Go to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
2013-01-03 17:18:19 +00:00
|
|
|
/* Check if there is a Wait Routine */
|
|
|
|
if (WaitBlock->WaitFunction != NULL)
|
2012-02-16 06:57:27 +00:00
|
|
|
{
|
|
|
|
/* Notify the Waiter */
|
|
|
|
NotifySuccess |= CsrNotifyWaitBlock(WaitBlock,
|
|
|
|
WaitList,
|
|
|
|
WaitArgument1,
|
|
|
|
WaitArgument2,
|
|
|
|
0,
|
|
|
|
FALSE);
|
|
|
|
|
2013-10-05 22:17:34 +00:00
|
|
|
/*
|
|
|
|
* We've already done a wait, so leave unless
|
|
|
|
* we want to notify all the waits...
|
|
|
|
*/
|
|
|
|
if (!NotifyAll) break;
|
2012-02-16 06:57:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the wait lock and return */
|
|
|
|
CsrReleaseWaitLock();
|
|
|
|
return NotifySuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|