Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete DateTime FCalls and switch to fully managed implementation #46690

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,10 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Compile Include="$(BclSourcesRoot)\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(BclSourcesRoot)\System\DateTime.Unix.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\ClrThreadPoolBoundHandle.Unix.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\LowLevelLifoSemaphore.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="$(BclSourcesRoot)\System\DateTime.Windows.CoreCLR.cs" />
<Compile Include="$(CommonPath)Interop\Windows\OleAut32\Interop.VariantClear.cs">
<Link>Common\Interop\Windows\OleAut32\Interop.VariantClear.cs</Link>
</Compile>
Expand Down

This file was deleted.

This file was deleted.

150 changes: 0 additions & 150 deletions src/coreclr/classlibnative/bcltype/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,157 +30,7 @@
#include "array.h"
#include "eepolicy.h"

#ifndef TARGET_UNIX
typedef void(WINAPI *pfnGetSystemTimeAsFileTime)(LPFILETIME lpSystemTimeAsFileTime);
extern pfnGetSystemTimeAsFileTime g_pfnGetSystemTimeAsFileTime;

void WINAPI InitializeGetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime)
{
pfnGetSystemTimeAsFileTime func = NULL;

HMODULE hKernel32 = WszLoadLibrary(W("kernel32.dll"));
if (hKernel32 != NULL)
{
func = (pfnGetSystemTimeAsFileTime)GetProcAddress(hKernel32, "GetSystemTimePreciseAsFileTime");
if (func != NULL)
{
// GetSystemTimePreciseAsFileTime exists and we'd like to use it. However, on
// misconfigured systems, it's possible for the "precise" time to be inaccurate:
// https://github.com/dotnet/runtime/issues/9014
// If it's inaccurate, though, we expect it to be wildly inaccurate, so as a
// workaround/heuristic, we get both the "normal" and "precise" times, and as
// long as they're close, we use the precise one. This workaround can be removed
// when we better understand what's causing the drift and the issue is no longer
// a problem or can be better worked around on all targeted OSes.

FILETIME systemTimeResult;
::GetSystemTimeAsFileTime(&systemTimeResult);

FILETIME preciseSystemTimeResult;
func(&preciseSystemTimeResult);

LONG64 systemTimeLong100ns = (LONG64)((((ULONG64)systemTimeResult.dwHighDateTime) << 32) | (ULONG64)systemTimeResult.dwLowDateTime);
LONG64 preciseSystemTimeLong100ns = (LONG64)((((ULONG64)preciseSystemTimeResult.dwHighDateTime) << 32) | (ULONG64)preciseSystemTimeResult.dwLowDateTime);

const INT32 THRESHOLD_100NS = 1000000; // 100ms
if (abs(preciseSystemTimeLong100ns - systemTimeLong100ns) > THRESHOLD_100NS)
{
// Too much difference. Don't use GetSystemTimePreciseAsFileTime.
func = NULL;
}
}
}
if (func == NULL)
{
func = &::GetSystemTimeAsFileTime;
}

InterlockedCompareExchangeT(&g_pfnGetSystemTimeAsFileTime, func, &InitializeGetSystemTimeAsFileTime);

g_pfnGetSystemTimeAsFileTime(lpSystemTimeAsFileTime);
}

pfnGetSystemTimeAsFileTime g_pfnGetSystemTimeAsFileTime = &InitializeGetSystemTimeAsFileTime;
#endif // TARGET_UNIX

FCIMPL0(INT64, SystemNative::__GetSystemTimeAsFileTime)
{
FCALL_CONTRACT;

INT64 timestamp;
#ifndef TARGET_UNIX
g_pfnGetSystemTimeAsFileTime((FILETIME*)&timestamp);
#else
GetSystemTimeAsFileTime((FILETIME*)&timestamp);
#endif

#if BIGENDIAN
timestamp = (INT64)(((UINT64)timestamp >> 32) | ((UINT64)timestamp << 32));
#endif

return timestamp;
}
FCIMPLEND;


#ifndef TARGET_UNIX

FCIMPL1(VOID, SystemNative::GetSystemTimeWithLeapSecondsHandling, FullSystemTime *time)
{
FCALL_CONTRACT;
INT64 timestamp;

g_pfnGetSystemTimeAsFileTime((FILETIME*)&timestamp);

if (::FileTimeToSystemTime((FILETIME*)&timestamp, &(time->systemTime)))
{
// to keep the time precision
time->hundredNanoSecond = timestamp % 10000; // 10000 is the number of 100-nano seconds per Millisecond
}
else
{
::GetSystemTime(&(time->systemTime));
time->hundredNanoSecond = 0;
}

if (time->systemTime.wSecond > 59)
{
// we have a leap second, force it to last second in the minute as DateTime doesn't account for leap seconds in its calculation.
// we use the maxvalue from the milliseconds and the 100-nano seconds to avoid reporting two out of order 59 seconds
time->systemTime.wSecond = 59;
time->systemTime.wMilliseconds = 999;
time->hundredNanoSecond = 9999;
}
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::FileTimeToSystemTime, INT64 fileTime, FullSystemTime *time)
{
FCALL_CONTRACT;
if (::FileTimeToSystemTime((FILETIME*)&fileTime, (LPSYSTEMTIME) time))
{
// to keep the time precision
time->hundredNanoSecond = fileTime % 10000; // 10000 is the number of 100-nano seconds per Millisecond
if (time->systemTime.wSecond > 59)
{
// we have a leap second, force it to last second in the minute as DateTime doesn't account for leap seconds in its calculation.
// we use the maxvalue from the milliseconds and the 100-nano seconds to avoid reporting two out of order 59 seconds
time->systemTime.wSecond = 59;
time->systemTime.wMilliseconds = 999;
time->hundredNanoSecond = 9999;
}
FC_RETURN_BOOL(TRUE);
}
FC_RETURN_BOOL(FALSE);
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::ValidateSystemTime, SYSTEMTIME *time, CLR_BOOL localTime)
{
FCALL_CONTRACT;

if (localTime)
{
SYSTEMTIME st;
FC_RETURN_BOOL(::TzSpecificLocalTimeToSystemTime(NULL, time, &st));
}
else
{
FILETIME timestamp;
FC_RETURN_BOOL(::SystemTimeToFileTime(time, &timestamp));
}
}
FCIMPLEND;

FCIMPL2(FC_BOOL_RET, SystemNative::SystemTimeToFileTime, SYSTEMTIME *time, INT64 *pFileTime)
{
FCALL_CONTRACT;

BOOL ret = ::SystemTimeToFileTime(time, (LPFILETIME) pFileTime);
FC_RETURN_BOOL(ret);
}
FCIMPLEND;
#endif // TARGET_UNIX


FCIMPL0(UINT32, SystemNative::GetTickCount)
Expand Down
7 changes: 0 additions & 7 deletions src/coreclr/classlibnative/bcltype/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ class SystemNative

public:
// Functions on the System.Environment class
#ifndef TARGET_UNIX
static FCDECL1(VOID, GetSystemTimeWithLeapSecondsHandling, FullSystemTime *time);
static FCDECL2(FC_BOOL_RET, ValidateSystemTime, SYSTEMTIME *time, CLR_BOOL localTime);
static FCDECL2(FC_BOOL_RET, FileTimeToSystemTime, INT64 fileTime, FullSystemTime *time);
static FCDECL2(FC_BOOL_RET, SystemTimeToFileTime, SYSTEMTIME *time, INT64 *pFileTime);
#endif // TARGET_UNIX
static FCDECL0(INT64, __GetSystemTimeAsFileTime);
static FCDECL0(UINT32, GetTickCount);
static FCDECL0(UINT64, GetTickCount64);

Expand Down
11 changes: 0 additions & 11 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ FCFuncStart(gDiagnosticsStackTrace)
FCFuncElement("GetStackFramesInternal", DebugStackTrace::GetStackFramesInternal)
FCFuncEnd()

FCFuncStart(gDateTimeFuncs)
#if !defined(TARGET_UNIX)
FCFuncElement("GetSystemTimeWithLeapSecondsHandling", SystemNative::GetSystemTimeWithLeapSecondsHandling)
FCFuncElement("ValidateSystemTime", SystemNative::ValidateSystemTime)
FCFuncElement("FileTimeToSystemTime", SystemNative::FileTimeToSystemTime)
FCFuncElement("SystemTimeToFileTime", SystemNative::SystemTimeToFileTime)
#endif // TARGET_UNIX
FCFuncElement("GetSystemTimeAsFileTime", SystemNative::__GetSystemTimeAsFileTime)
FCFuncEnd()

FCFuncStart(gEnvironmentFuncs)
FCFuncElement("get_CurrentManagedThreadId", JIT_GetCurrentManagedThreadId)
FCFuncElement("get_TickCount", SystemNative::GetTickCount)
Expand Down Expand Up @@ -1121,7 +1111,6 @@ FCClassElement("ComWrappers", "System.Runtime.InteropServices", gComWrappersFunc
FCClassElement("CompatibilitySwitch", "System.Runtime.Versioning", gCompatibilitySwitchFuncs)
FCClassElement("CustomAttribute", "System.Reflection", gCOMCustomAttributeFuncs)
FCClassElement("CustomAttributeEncodedArgument", "System.Reflection", gCustomAttributeEncodedArgument)
FCClassElement("DateTime", "System", gDateTimeFuncs)
FCClassElement("Debugger", "System.Diagnostics", gDiagnosticsDebugger)
FCClassElement("Delegate", "System", gDelegateFuncs)
FCClassElement("DependentHandle", "System.Runtime.CompilerServices", gDependentHandleFuncs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static partial class Interop
internal partial class Sys
{
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetSystemTimeAsTicks")]
[SuppressGCTransition]
internal static extern long GetSystemTimeAsTicks();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static partial class Interop
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
[SuppressGCTransition]
internal static extern unsafe Interop.BOOL FileTimeToSystemTime(long* lpFileTime, Interop.Kernel32.SYSTEMTIME* lpSystemTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static partial class Interop
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
[SuppressGCTransition]
internal static extern unsafe void GetSystemTime(Interop.Kernel32.SYSTEMTIME* lpSystemTime);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static partial class Interop
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
[SuppressGCTransition]
internal static extern unsafe Interop.BOOL SystemTimeToFileTime(Interop.Kernel32.SYSTEMTIME* lpSystemTime, long* lpFileTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1401,12 +1401,6 @@
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetSystemTime.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetSystemTime.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetSystemTimeAsFileTime.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetSystemTimeAsFileTime.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetSystemTimePreciseAsFileTime.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetSystemTimePreciseAsFileTime.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetSystemTimes.cs">
<Link>Common\Interop\Windows\Kernel32\Interop.GetSystemTimes.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ public readonly partial struct DateTime
{
internal const bool s_systemSupportsLeapSeconds = false;

#if !CORECLR
public static DateTime UtcNow
{
get
{
return new DateTime(((ulong)(Interop.Sys.GetSystemTimeAsTicks() + UnixEpochTicks)) | KindUtc);
}
}
#endif

private static DateTime FromFileTimeLeapSecondsAware(long fileTime) => default;
private static long ToFileTimeLeapSecondsAware(long ticks) => default;
Expand Down
Loading