[*] x86_64 Use RDSC for more deterministic back-off durations

Well, sort of. It's more likely to be referenced against the exact frequency stated in the hard-coded CPUID vendor string.
This commit is contained in:
Reece Wilson 2023-09-02 14:29:55 +01:00
parent 85102ec7ce
commit 9fbdafea74

View File

@ -13,6 +13,8 @@ namespace Aurora::Threading
inline thread_local AuUInt8 tlsSpinCountLocal {};
}
#define SPIN_FOUR 1
namespace Aurora::Threading::Primitives
{
inline AuUInt32 gSpinAdaptiveThreshold {};
@ -54,7 +56,12 @@ namespace Aurora::Threading::Primitives
if (uNow <= gSpinAdaptiveThreshold)
{
int loops = spin;
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
auto perfCounter = __rdtsc() + spin;
while (__rdtsc() < perfCounter)
#else
while (loops > 0)
#endif
{
if (callback())
{
@ -63,11 +70,16 @@ namespace Aurora::Threading::Primitives
}
else
{
#if defined(SPIN_FOUR) && SPIN_FOUR == 1
SMPPause();
SMPPause();
SMPPause();
SMPPause();
loops -= 4;
#else
SMPPause();
loops -= 1;
#endif
}
}
@ -94,7 +106,12 @@ namespace Aurora::Threading::Primitives
else
{
int loops = (spin) / 3;
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
auto perfCounter = __rdtsc() + loops;
while (__rdtsc() < perfCounter)
#else
while (loops > 0)
#endif
{
if (callback())
{
@ -114,7 +131,12 @@ namespace Aurora::Threading::Primitives
else
{
int loops = spin;
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
auto perfCounter = __rdtsc() + spin;
while (__rdtsc() < perfCounter)
#else
while (loops > 0)
#endif
{
if (callback())
{
@ -122,11 +144,16 @@ namespace Aurora::Threading::Primitives
}
else
{
#if defined(SPIN_FOUR) && SPIN_FOUR == 1
SMPPause();
SMPPause();
SMPPause();
SMPPause();
loops -= 4;
#else
SMPPause();
loops -= 1;
#endif
}
}