From 72e42d63b1eda4141be004438bbb4f21dd49111e Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Sat, 23 Jul 2011 11:05:00 +0000 Subject: [PATCH] [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 --- reactos/dll/win32/kernel32/client/debugger.c | 14 +-- .../dll/win32/kernel32/client/file/iocompl.c | 13 ++- reactos/dll/win32/kernel32/client/synch.c | 97 +++++++------------ reactos/dll/win32/kernel32/client/utils.c | 13 +++ reactos/dll/win32/kernel32/include/kernel32.h | 5 + 5 files changed, 61 insertions(+), 81 deletions(-) diff --git a/reactos/dll/win32/kernel32/client/debugger.c b/reactos/dll/win32/kernel32/client/debugger.c index a979486b777..edfb1c262d7 100644 --- a/reactos/dll/win32/kernel32/client/debugger.c +++ b/reactos/dll/win32/kernel32/client/debugger.c @@ -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 diff --git a/reactos/dll/win32/kernel32/client/file/iocompl.c b/reactos/dll/win32/kernel32/client/file/iocompl.c index c1f5f2a3963..472ec1b6359 100644 --- a/reactos/dll/win32/kernel32/client/file/iocompl.c +++ b/reactos/dll/win32/kernel32/client/file/iocompl.c @@ -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; diff --git a/reactos/dll/win32/kernel32/client/synch.c b/reactos/dll/win32/kernel32/client/synch.c index ca90f5d645c..20da3cb94f1 100644 --- a/reactos/dll/win32/kernel32/client/synch.c +++ b/reactos/dll/win32/kernel32/client/synch.c @@ -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 */ diff --git a/reactos/dll/win32/kernel32/client/utils.c b/reactos/dll/win32/kernel32/client/utils.c index 55afa9032b2..55669143237 100644 --- a/reactos/dll/win32/kernel32/client/utils.c +++ b/reactos/dll/win32/kernel32/client/utils.c @@ -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. */ diff --git a/reactos/dll/win32/kernel32/include/kernel32.h b/reactos/dll/win32/kernel32/include/kernel32.h index 2a50f01ccc3..e81edef68e7 100644 --- a/reactos/dll/win32/kernel32/include/kernel32.h +++ b/reactos/dll/win32/kernel32/include/kernel32.h @@ -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,