[KERNEL32]: Implement a BaseFormatTimeOut helper function to take care of dwMillisecond->LARGE_INTEGER timeout conversion instead of duplicating 3 different versions of the code required to do so.

svn path=/trunk/; revision=52798
This commit is contained in:
Alex Ionescu
2011-07-23 11:05:00 +00:00
parent 293d54ec7e
commit 72e42d63b1
5 changed files with 61 additions and 81 deletions
+2 -12
View File
@@ -654,18 +654,8 @@ WaitForDebugEvent(IN LPDEBUG_EVENT lpDebugEvent,
DBGUI_WAIT_STATE_CHANGE WaitStateChange;
NTSTATUS Status;
/* Check if this is an infinite wait */
if (dwMilliseconds == INFINITE)
{
/* Under NT, this means no timer argument */
Timeout = NULL;
}
else
{
/* Otherwise, convert the time to NT Format */
WaitTime.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
Timeout = &WaitTime;
}
/* Convert to NT Timeout */
Timeout = BaseFormatTimeOut(&WaitTime, dwMilliseconds);
/* Loop while we keep getting interrupted */
do
@@ -103,24 +103,23 @@ GetQueuedCompletionStatus(
NTSTATUS errCode;
IO_STATUS_BLOCK IoStatus;
ULONG_PTR CompletionKey;
LARGE_INTEGER Interval;
LARGE_INTEGER Time;
PLARGE_INTEGER TimePtr;
if (!lpNumberOfBytesTransferred || !lpCompletionKey || !lpOverlapped)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if (dwMilliseconds != INFINITE)
{
Interval.QuadPart = (-(MILLIS_TO_100NS(dwMilliseconds)));
}
/* Convert the timeout */
TimePtr = BaseFormatTimeOut(&Time, dwMilliseconds);
errCode = NtRemoveIoCompletion(CompletionHandle,
(PVOID*)&CompletionKey,
(PVOID*)lpOverlapped,
&IoStatus,
dwMilliseconds == INFINITE ? NULL : &Interval);
TimePtr);
if (!NT_SUCCESS(errCode) || errCode == STATUS_TIMEOUT) {
*lpOverlapped = NULL;
+35 -62
View File
@@ -49,18 +49,8 @@ WaitForSingleObjectEx(IN HANDLE hHandle,
hHandle = GetConsoleInputWaitHandle();
}
/* Check if this is an infinite wait */
if (dwMilliseconds == INFINITE)
{
/* Under NT, this means no timer argument */
TimePtr = NULL;
}
else
{
/* Otherwise, convert the time to NT Format */
Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
TimePtr = &Time;
}
/* Convert the timeout */
TimePtr = BaseFormatTimeOut(&Time, dwMilliseconds);
/* Start wait loop */
do
@@ -151,18 +141,8 @@ WaitForMultipleObjectsEx(IN DWORD nCount,
}
}
/* Check if this is an infinite wait */
if (dwMilliseconds == INFINITE)
{
/* Under NT, this means no timer argument */
TimePtr = NULL;
}
else
{
/* Otherwise, convert the time to NT Format */
Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
TimePtr = &Time;
}
/* Convert the timeout */
TimePtr = BaseFormatTimeOut(&Time, dwMilliseconds);
/* Start wait loop */
do
@@ -217,18 +197,8 @@ SignalObjectAndWait(IN HANDLE hObjectToSignal,
hObjectToWaitOn = GetConsoleInputWaitHandle();
}
/* Check if this is an infinite wait */
if (dwMilliseconds == INFINITE)
{
/* Under NT, this means no timer argument */
TimePtr = NULL;
}
else
{
/* Otherwise, convert the time to NT Format */
Time.QuadPart = UInt32x32To64(-10000, dwMilliseconds);
TimePtr = &Time;
}
/* Convert the timeout */
TimePtr = BaseFormatTimeOut(&Time, dwMilliseconds);
/* Start wait loop */
do
@@ -637,49 +607,52 @@ InitializeCriticalSectionAndSpinCount(OUT LPCRITICAL_SECTION lpCriticalSection,
return TRUE;
}
/*
* @implemented
*/
VOID WINAPI
Sleep(DWORD dwMilliseconds)
VOID
WINAPI
Sleep(IN DWORD dwMilliseconds)
{
SleepEx(dwMilliseconds, FALSE);
return;
/* Call the new API */
SleepEx(dwMilliseconds, FALSE);
}
/*
* @implemented
*/
DWORD WINAPI
SleepEx(DWORD dwMilliseconds,
BOOL bAlertable)
DWORD
WINAPI
SleepEx(IN DWORD dwMilliseconds,
IN BOOL bAlertable)
{
LARGE_INTEGER Interval;
NTSTATUS errCode;
LARGE_INTEGER Time;
PLARGE_INTEGER TimePtr;
NTSTATUS errCode;
if (dwMilliseconds != INFINITE)
/* Convert the timeout */
TimePtr = BaseFormatTimeOut(&Time, dwMilliseconds);
if (!TimePtr)
{
/*
* System time units are 100 nanoseconds (a nanosecond is a billionth of
* a second).
*/
Interval.QuadPart = -((LONGLONG)dwMilliseconds * 10000);
}
else
{
/* Approximately 292000 years hence */
Interval.QuadPart = -0x7FFFFFFFFFFFFFFFLL;
/* Turn an infinite wait into a really long wait */
Time.LowPart = 0;
Time.HighPart = 0x80000000;
TimePtr = &Time;
}
dowait:
errCode = NtDelayExecution ((BOOLEAN)bAlertable, &Interval);
if ((bAlertable) && (errCode == STATUS_ALERTED)) goto dowait;
return (errCode == STATUS_USER_APC) ? WAIT_IO_COMPLETION : 0;
/* Loop the delay while APCs are alerting us */
do
{
/* Do the delay */
errCode = NtDelayExecution((BOOLEAN)bAlertable, TimePtr);
}
while ((bAlertable) && (errCode == STATUS_ALERTED));
/* Return the correct code */
return (errCode == STATUS_USER_APC) ? WAIT_IO_COMPLETION : 0;
}
/*
* @implemented
*/
+13
View File
@@ -117,6 +117,19 @@ BasepAnsiStringToHeapUnicodeString(IN LPCSTR AnsiString,
}
}
PLARGE_INTEGER
WINAPI
BaseFormatTimeOut(OUT PLARGE_INTEGER Timeout,
IN DWORD dwMilliseconds)
{
/* Check if this is an infinite wait, which means no timeout argument */
if (dwMilliseconds == INFINITE) return NULL;
/* Otherwise, convert the time to NT Format */
Timeout->QuadPart = UInt32x32To64(dwMilliseconds, -10000);
return Timeout;
}
/*
* Converts lpSecurityAttributes + Object Name into ObjectAttributes.
*/
@@ -137,6 +137,11 @@ DWORD FilenameU2A_FitOrFail(LPSTR DestA, INT destLen, PUNICODE_STRING SourceU);
#define HeapFree RtlFreeHeap
#define _lread (_readfun)_hread
PLARGE_INTEGER
WINAPI
BaseFormatTimeOut(OUT PLARGE_INTEGER Timeout,
IN DWORD dwMilliseconds);
POBJECT_ATTRIBUTES
WINAPI
BasepConvertObjectAttributes(OUT POBJECT_ATTRIBUTES ObjectAttributes,