2023-09-09 16:37:14 +00:00
|
|
|
|
/***
|
2023-03-12 15:27:28 +00:00
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
2023-03-21 03:18:09 +00:00
|
|
|
|
File: SMTYield.hpp
|
2023-03-12 15:27:28 +00:00
|
|
|
|
Date: 2023-3-12
|
|
|
|
|
Author: Reece
|
|
|
|
|
***/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-08-19 17:14:28 +00:00
|
|
|
|
namespace Aurora::Threading
|
|
|
|
|
{
|
|
|
|
|
inline AuUInt32 gHasThreadLocalTimeout {};
|
2023-09-06 15:24:43 +00:00
|
|
|
|
inline thread_local AuUInt32 tlsSpinCountLocal {};
|
2023-08-19 17:14:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#define SPIN_FOUR 1
|
|
|
|
|
|
2023-03-12 15:27:28 +00:00
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
|
{
|
2023-09-09 16:37:14 +00:00
|
|
|
|
namespace ThrdCfg
|
|
|
|
|
{
|
|
|
|
|
inline bool gPlatformIsSMPProcessorOptimized {}; // to include or not to include 🤔
|
|
|
|
|
inline bool gEnableAggressiveScheduling {};
|
|
|
|
|
inline bool gEnableAgrSchedulingRatelimit {};
|
|
|
|
|
inline bool gPreferNtCondvarModernWinSpin {};
|
|
|
|
|
inline bool gPreferNtCondvarOlderWinSpin {};
|
|
|
|
|
inline bool gPreferNtSemaphoreSpinTryLock {};
|
|
|
|
|
inline bool gPreferNtMutexSpinTryLock {};
|
|
|
|
|
inline bool gPreferNtCondMutexSpinTryLock {};
|
|
|
|
|
inline bool gPreferLinuxSemaphoreSpinTryLock {};
|
|
|
|
|
inline bool gPreferLinuxMutexSpinTryLock {};
|
|
|
|
|
inline bool gPreferLinuxCondMutexSpinTryLock {};
|
|
|
|
|
inline bool gPreferEmulatedWakeOnAddress {};
|
|
|
|
|
inline bool gPreferWaitOnAddressAlwaysSpin {};
|
|
|
|
|
inline bool gPreferRWLockReadLockSpin {};
|
|
|
|
|
inline bool gUWPNanosecondEmulationCheckFirst {};
|
|
|
|
|
inline AuUInt32 gUWPNanosecondEmulationMaxYields {};
|
|
|
|
|
inline bool gForceEnableAdaptiveSpin {};
|
|
|
|
|
inline bool gPreferEnableAdaptiveSpin {};
|
|
|
|
|
inline bool gPreferLinuxAdaptiveSpin {};
|
|
|
|
|
inline bool gPreferOldWin32AdaptiveSpin {};
|
|
|
|
|
inline bool gPreferNewWin32AdaptiveSpin {};
|
|
|
|
|
inline AuUInt32 gAdaptiveSpinCUCnt0 {};
|
|
|
|
|
inline AuUInt32 gAdaptiveSpinCUCnt4 {};
|
|
|
|
|
inline AuUInt32 gAdaptiveSpinCUCnt8 {};
|
|
|
|
|
inline AuUInt32 gAdaptiveSpinCUCnt16 {};
|
|
|
|
|
inline bool gPreferFutexRWLock {};
|
2023-09-10 13:50:59 +00:00
|
|
|
|
inline bool gPreferFutexEvent {};
|
2023-09-09 16:37:14 +00:00
|
|
|
|
inline bool gWinXpThrough7BlazeOptimizerPower {};
|
|
|
|
|
inline bool gPreferLinuxPrimitivesFutexNoSpin {};
|
|
|
|
|
inline bool gPreferUnixPrimitivesNoSpin {};
|
|
|
|
|
inline bool gAlwaysRWLockWriteBiasOnReadLock {};
|
|
|
|
|
inline bool gEnableRWLockWriteBiasOnReadLock {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
inline AuUInt32 gSpinAdaptiveThreshold {};
|
|
|
|
|
inline AuUInt32 gSpinAdaptiveCurrentCount {};
|
2023-09-09 17:09:22 +00:00
|
|
|
|
inline AuUInt32 gSpinAdaptiveThreadCount {};
|
2023-08-22 11:57:47 +00:00
|
|
|
|
|
2023-08-23 13:52:47 +00:00
|
|
|
|
inline AuUInt32 gUseFutexRWLock {};
|
2023-09-10 13:50:59 +00:00
|
|
|
|
inline AuUInt32 gPreferFutexEvent {};
|
2023-08-23 13:52:47 +00:00
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
void InitAdaptiveThreshold();
|
|
|
|
|
void InitAdaptiveThresholdFirstTime();
|
2023-09-09 16:37:14 +00:00
|
|
|
|
void InitCfg();
|
2023-08-22 11:57:47 +00:00
|
|
|
|
|
2023-03-12 15:27:28 +00:00
|
|
|
|
static auline void SMPPause()
|
|
|
|
|
{
|
|
|
|
|
#if (defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86))
|
|
|
|
|
_mm_pause();
|
|
|
|
|
#elif defined(AURORA_ARCH_ARM)
|
|
|
|
|
#if defined(AURORA_COMPILER_GCC)
|
|
|
|
|
asm volatile("yield");
|
|
|
|
|
#else
|
|
|
|
|
__yield();
|
|
|
|
|
#endif
|
|
|
|
|
#else
|
|
|
|
|
// TODO: your platform here
|
|
|
|
|
AuThreading::ContextYield();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool auline YieldToSharedCore(long spin, T callback)
|
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
if (gSpinAdaptiveThreshold)
|
2023-03-12 15:27:28 +00:00
|
|
|
|
{
|
2023-08-22 11:57:47 +00:00
|
|
|
|
auto uNow = AuAtomicAdd(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
|
|
|
|
|
if (uNow <= gSpinAdaptiveThreshold)
|
|
|
|
|
{
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto uCount = spin;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto perfCounter = __rdtsc() + uCount;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
while (__rdtsc() < perfCounter)
|
|
|
|
|
#else
|
2023-09-06 15:24:43 +00:00
|
|
|
|
while (uCount > 0)
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#endif
|
2023-08-22 11:57:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
AuAtomicSub(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-27 20:27:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#if defined(SPIN_FOUR) && SPIN_FOUR == 1
|
2023-08-27 20:27:49 +00:00
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount -= 4;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#else
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount -= 1;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#endif
|
2023-08-27 20:27:49 +00:00
|
|
|
|
}
|
2023-08-22 11:57:47 +00:00
|
|
|
|
}
|
2023-03-12 15:27:28 +00:00
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
if (gHasThreadLocalTimeout)
|
|
|
|
|
{
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto uCount = tlsSpinCountLocal;
|
2023-09-04 22:03:08 +00:00
|
|
|
|
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto perfCounter = __rdtsc() + uCount;
|
2023-09-04 22:03:08 +00:00
|
|
|
|
while (__rdtsc() < perfCounter)
|
|
|
|
|
#else
|
2023-09-06 15:24:43 +00:00
|
|
|
|
while (uCount > 0)
|
2023-09-04 22:03:08 +00:00
|
|
|
|
#endif
|
2023-08-22 11:57:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
AuAtomicSub(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-27 20:27:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount--;
|
2023-08-27 20:27:49 +00:00
|
|
|
|
}
|
2023-08-22 11:57:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-12 15:27:28 +00:00
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
AuAtomicSub(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
}
|
2023-09-09 17:09:22 +00:00
|
|
|
|
else if (uNow <= gSpinAdaptiveThreadCount)
|
2023-03-12 15:27:28 +00:00
|
|
|
|
{
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto uCount = (spin) / 3;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto perfCounter = __rdtsc() + uCount;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
while (__rdtsc() < perfCounter)
|
|
|
|
|
#else
|
2023-09-06 15:24:43 +00:00
|
|
|
|
while (uCount > 0)
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#endif
|
2023-08-22 11:57:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
AuAtomicSub(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-27 20:27:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount--;
|
2023-08-27 20:27:49 +00:00
|
|
|
|
}
|
2023-08-22 11:57:47 +00:00
|
|
|
|
}
|
2023-03-12 15:27:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 11:57:47 +00:00
|
|
|
|
AuAtomicSub(&gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-08-19 17:14:28 +00:00
|
|
|
|
{
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto uCount = spin;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto perfCounter = __rdtsc() + uCount;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
while (__rdtsc() < perfCounter)
|
|
|
|
|
#else
|
2023-09-06 15:24:43 +00:00
|
|
|
|
while (uCount > 0)
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#endif
|
2023-08-19 17:14:28 +00:00
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-27 20:27:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#if defined(SPIN_FOUR) && SPIN_FOUR == 1
|
2023-08-27 20:27:49 +00:00
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount -= 4;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#else
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount -= 1;
|
2023-09-02 13:29:55 +00:00
|
|
|
|
#endif
|
2023-08-27 20:27:49 +00:00
|
|
|
|
}
|
2023-08-19 17:14:28 +00:00
|
|
|
|
}
|
2023-08-22 11:57:47 +00:00
|
|
|
|
|
|
|
|
|
if (gHasThreadLocalTimeout)
|
|
|
|
|
{
|
|
|
|
|
auto uCount = tlsSpinCountLocal;
|
2023-09-04 22:03:08 +00:00
|
|
|
|
#if defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86)
|
2023-09-06 15:24:43 +00:00
|
|
|
|
auto perfCounter = __rdtsc() + uCount;
|
2023-09-04 22:03:08 +00:00
|
|
|
|
while (__rdtsc() < perfCounter)
|
|
|
|
|
#else
|
2023-09-06 15:24:43 +00:00
|
|
|
|
while (uCount > 0)
|
2023-09-04 22:03:08 +00:00
|
|
|
|
#endif
|
2023-08-22 11:57:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (callback())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-27 20:27:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SMPPause();
|
2023-09-06 15:24:43 +00:00
|
|
|
|
uCount--;
|
2023-08-27 20:27:49 +00:00
|
|
|
|
}
|
2023-08-22 11:57:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-19 17:14:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 15:27:28 +00:00
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
bool auline DoTryIf(T callback)
|
|
|
|
|
{
|
2023-09-09 16:37:14 +00:00
|
|
|
|
if (ThrdCfg::gPlatformIsSMPProcessorOptimized)
|
2023-03-12 15:27:28 +00:00
|
|
|
|
{
|
|
|
|
|
return YieldToSharedCore(gRuntimeConfig.threadingConfig.uSpinLoopPowerA, callback);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|