From 36a72228db05897ebbd5509b3c7d8602814302d3 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 6 Sep 2023 16:24:43 +0100 Subject: [PATCH] [*] Cleanup/formatting of SMT yields --- Include/Aurora/Threading/SpinTime.hpp | 10 ++-- .../Threading/Waitables/FutexCondWaitable.hpp | 2 +- .../Waitables/FutexSemaphoreWaitable.hpp | 2 +- .../Threading/Waitables/FutexWaitable.hpp | 2 +- Source/Threading/Primitives/SMTYield.cpp | 18 +++++--- Source/Threading/Primitives/SMTYield.hpp | 46 +++++++++---------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Include/Aurora/Threading/SpinTime.hpp b/Include/Aurora/Threading/SpinTime.hpp index af71826a..d0910922 100644 --- a/Include/Aurora/Threading/SpinTime.hpp +++ b/Include/Aurora/Threading/SpinTime.hpp @@ -14,12 +14,10 @@ namespace Aurora namespace Aurora::Threading { - // Note: the following APIs are in powers of 2! - - AUKN_SYM void SetSpinCountTimeout(AuUInt8 uTimeout); - AUKN_SYM AuUInt8 GetSpinCountTimeout(); - AUKN_SYM void SetThreadLocalAdditionalSpinCountTimeout(AuUInt8 uTimeout); - AUKN_SYM AuUInt32 GetTotalSpinCountTimeout(); + AUKN_SYM void SetSpinCountTimeout(AuUInt32 uTimeout); + AUKN_SYM AuUInt32 GetSpinCountTimeout(); + AUKN_SYM void SetThreadLocalAdditionalSpinCountTimeout(AuUInt32 uTimeout); + AUKN_SYM AuUInt32 GetTotalSpinCountTime(); AUKN_SYM const ThreadingConfig *GetThreadingConfig(); AUKN_SYM void SetThreadingConfig(const ThreadingConfig *pUpdateConfig); diff --git a/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp b/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp index 5ea91e26..a52d2ffb 100644 --- a/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp +++ b/Include/Aurora/Threading/Waitables/FutexCondWaitable.hpp @@ -185,7 +185,7 @@ namespace Aurora::Threading::Waitables } #if (defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)) && !defined(AURORA_RUNTIME_FORCE_ADAPTIVE_FUTEX) - AuUInt uCount(GetTotalSpinCountTimeout()); + AuUInt uCount(GetTotalSpinCountTime()); #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) auto uPerfCounter = __rdtsc() + uCount; while (__rdtsc() < uPerfCounter) diff --git a/Include/Aurora/Threading/Waitables/FutexSemaphoreWaitable.hpp b/Include/Aurora/Threading/Waitables/FutexSemaphoreWaitable.hpp index 35057c3c..dcd43226 100644 --- a/Include/Aurora/Threading/Waitables/FutexSemaphoreWaitable.hpp +++ b/Include/Aurora/Threading/Waitables/FutexSemaphoreWaitable.hpp @@ -33,7 +33,7 @@ namespace Aurora::Threading::Waitables } #if (defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)) && !defined(AURORA_RUNTIME_FORCE_ADAPTIVE_FUTEX) - AuUInt uCount(GetTotalSpinCountTimeout()); + AuUInt uCount(GetTotalSpinCountTime()); #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) auto uPerfCounter = __rdtsc() + uCount; while (__rdtsc() < uPerfCounter) diff --git a/Include/Aurora/Threading/Waitables/FutexWaitable.hpp b/Include/Aurora/Threading/Waitables/FutexWaitable.hpp index f60db790..32d90cfc 100644 --- a/Include/Aurora/Threading/Waitables/FutexWaitable.hpp +++ b/Include/Aurora/Threading/Waitables/FutexWaitable.hpp @@ -32,7 +32,7 @@ namespace Aurora::Threading::Waitables } #if (defined(AURORA_ARCH_X86) || defined(AURORA_ARCH_X64)) && !defined(AURORA_RUNTIME_FORCE_ADAPTIVE_FUTEX) - AuUInt uCount(GetTotalSpinCountTimeout()); + AuUInt uCount(GetTotalSpinCountTime()); #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) auto uPerfCounter = __rdtsc() + uCount; while (__rdtsc() < uPerfCounter) diff --git a/Source/Threading/Primitives/SMTYield.cpp b/Source/Threading/Primitives/SMTYield.cpp index 15d46d33..1ceae97c 100644 --- a/Source/Threading/Primitives/SMTYield.cpp +++ b/Source/Threading/Primitives/SMTYield.cpp @@ -10,37 +10,41 @@ namespace Aurora::Threading { - AUKN_SYM void SetSpinCountTimeout(AuUInt8 uTimeout) + AUKN_SYM void SetSpinCountTimeout(AuUInt32 uTimeout) { gRuntimeConfig.threadingConfig.uSpinLoopPowerA = uTimeout; } - AUKN_SYM AuUInt8 GetSpinCountTimeout() + AUKN_SYM AuUInt32 GetSpinCountTimeout() { return gRuntimeConfig.threadingConfig.uSpinLoopPowerA; } - AUKN_SYM void SetThreadLocalAdditionalSpinCountTimeout(AuUInt8 uTimeout) + AUKN_SYM void SetThreadLocalAdditionalSpinCountTimeout(AuUInt32 uTimeout) { gHasThreadLocalTimeout = 1; tlsSpinCountLocal = uTimeout; } - AUKN_SYM AuUInt32 GetTotalSpinCountTimeout() + AUKN_SYM AuUInt32 GetTotalSpinCountTime() { AuUInt32 uCount {}; - + if (!gRuntimeConfig.threadingConfig.bPlatformIsSMPProcessorOptimized) { - return 16; + return 0; } uCount = AuUInt32(gRuntimeConfig.threadingConfig.uSpinLoopPowerA); if (gHasThreadLocalTimeout) { - uCount += 1u << AuUInt32(tlsSpinCountLocal); + uCount += tlsSpinCountLocal; } + //#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) + // uCount *= 4; + //#endif + // well, i guess not because intel recommends going by rdtsc ticks, and not to assume eventual uop sleep of an unspecified period return uCount; } diff --git a/Source/Threading/Primitives/SMTYield.hpp b/Source/Threading/Primitives/SMTYield.hpp index 211ef63c..3120f225 100644 --- a/Source/Threading/Primitives/SMTYield.hpp +++ b/Source/Threading/Primitives/SMTYield.hpp @@ -10,7 +10,7 @@ namespace Aurora::Threading { inline AuUInt32 gHasThreadLocalTimeout {}; - inline thread_local AuUInt8 tlsSpinCountLocal {}; + inline thread_local AuUInt32 tlsSpinCountLocal {}; } #define SPIN_FOUR 1 @@ -55,12 +55,12 @@ namespace Aurora::Threading::Primitives if (uNow <= gSpinAdaptiveThreshold) { - int loops = spin; + auto uCount = spin; #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) - auto perfCounter = __rdtsc() + spin; + auto perfCounter = __rdtsc() + uCount; while (__rdtsc() < perfCounter) #else - while (loops > 0) + while (uCount > 0) #endif { if (callback()) @@ -75,22 +75,22 @@ namespace Aurora::Threading::Primitives SMPPause(); SMPPause(); SMPPause(); - loops -= 4; + uCount -= 4; #else SMPPause(); - loops -= 1; + uCount -= 1; #endif } } if (gHasThreadLocalTimeout) { - int loops = (1 << tlsSpinCountLocal); + auto uCount = tlsSpinCountLocal; #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) - auto perfCounter = __rdtsc() + loops; + auto perfCounter = __rdtsc() + uCount; while (__rdtsc() < perfCounter) #else - while (loops > 0) + while (uCount > 0) #endif { if (callback()) @@ -101,7 +101,7 @@ namespace Aurora::Threading::Primitives else { SMPPause(); - loops--; + uCount--; } } } @@ -110,12 +110,12 @@ namespace Aurora::Threading::Primitives } else { - int loops = (spin) / 3; + auto uCount = (spin) / 3; #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) - auto perfCounter = __rdtsc() + loops; + auto perfCounter = __rdtsc() + uCount; while (__rdtsc() < perfCounter) #else - while (loops > 0) + while (uCount > 0) #endif { if (callback()) @@ -126,7 +126,7 @@ namespace Aurora::Threading::Primitives else { SMPPause(); - loops --; + uCount--; } } } @@ -135,12 +135,12 @@ namespace Aurora::Threading::Primitives } else { - int loops = spin; + auto uCount = spin; #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) - auto perfCounter = __rdtsc() + spin; + auto perfCounter = __rdtsc() + uCount; while (__rdtsc() < perfCounter) #else - while (loops > 0) + while (uCount > 0) #endif { if (callback()) @@ -154,10 +154,10 @@ namespace Aurora::Threading::Primitives SMPPause(); SMPPause(); SMPPause(); - loops -= 4; + uCount -= 4; #else SMPPause(); - loops -= 1; + uCount -= 1; #endif } } @@ -165,13 +165,11 @@ namespace Aurora::Threading::Primitives if (gHasThreadLocalTimeout) { auto uCount = tlsSpinCountLocal; - - int loops = (1 << uCount); #if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86) - auto perfCounter = __rdtsc() + loops; + auto perfCounter = __rdtsc() + uCount; while (__rdtsc() < perfCounter) #else - while (loops > 0) + while (uCount > 0) #endif { if (callback()) @@ -181,7 +179,7 @@ namespace Aurora::Threading::Primitives else { SMPPause(); - loops --; + uCount--; } } }