Add Platform::Yield

This commit is contained in:
Wojtek Figat
2025-07-28 18:33:05 +02:00
parent a7ffd9e57f
commit a00ffe6ec3
12 changed files with 34 additions and 4 deletions

View File

@@ -748,6 +748,11 @@ void AndroidPlatform::Sleep(int32 milliseconds)
usleep(milliseconds * 1000);
}
void AndroidPlatform::Yield()
{
pthread_yield();
}
double AndroidPlatform::GetTimeSeconds()
{
struct timespec ts;

View File

@@ -91,6 +91,7 @@ public:
static void SetThreadPriority(ThreadPriority priority);
static void SetThreadAffinityMask(uint64 affinityMask);
static void Sleep(int32 milliseconds);
static void Yield();
static double GetTimeSeconds();
static uint64 GetTimeCycles();
FORCE_INLINE static uint64 GetClockFrequency()

View File

@@ -293,6 +293,11 @@ void ApplePlatform::Sleep(int32 milliseconds)
usleep(milliseconds * 1000);
}
void ApplePlatform::Yield()
{
pthread_yield();
}
double ApplePlatform::GetTimeSeconds()
{
return SecondsPerCycle * mach_absolute_time();

View File

@@ -75,6 +75,7 @@ public:
static void SetThreadPriority(ThreadPriority priority);
static void SetThreadAffinityMask(uint64 affinityMask);
static void Sleep(int32 milliseconds);
static void Yield();
static double GetTimeSeconds();
static uint64 GetTimeCycles();
static uint64 GetClockFrequency();

View File

@@ -436,11 +436,16 @@ public:
static void SetThreadAffinityMask(uint64 affinityMask) = delete;
/// <summary>
/// Suspends the execution of the current thread until the time-out interval elapses
/// Suspends the execution of the current thread until the time-out interval elapses.
/// </summary>
/// <param name="milliseconds">The time interval for which execution is to be suspended, in milliseconds.</param>
static void Sleep(int32 milliseconds) = delete;
/// <summary>
/// Yields the execution of the current thread to another thread that is ready to run on the current processor.
/// </summary>
static void Yield() = delete;
public:
/// <summary>
/// Gets the current time in seconds.

View File

@@ -1836,6 +1836,11 @@ void LinuxPlatform::Sleep(int32 milliseconds)
usleep(milliseconds * 1000);
}
void LinuxPlatform::Yield()
{
pthread_yield();
}
double LinuxPlatform::GetTimeSeconds()
{
struct timespec ts;

View File

@@ -106,6 +106,7 @@ public:
static void SetThreadPriority(ThreadPriority priority);
static void SetThreadAffinityMask(uint64 affinityMask);
static void Sleep(int32 milliseconds);
static void Yield();
static double GetTimeSeconds();
static uint64 GetTimeCycles();
FORCE_INLINE static uint64 GetClockFrequency()

View File

@@ -68,6 +68,7 @@
#undef CreateMutex
#undef DrawState
#undef LoadLibrary
#undef Yield
#undef GetEnvironmentVariable
#undef SetEnvironmentVariable

View File

@@ -400,6 +400,11 @@ void Win32Platform::Sleep(int32 milliseconds)
WaitForSingleObject(timer, INFINITE);
}
void Win32Platform::Yield()
{
SwitchToThread();
}
double Win32Platform::GetTimeSeconds()
{
LARGE_INTEGER counter;

View File

@@ -100,6 +100,7 @@ public:
static void SetThreadPriority(ThreadPriority priority);
static void SetThreadAffinityMask(uint64 affinityMask);
static void Sleep(int32 milliseconds);
static void Yield();
static double GetTimeSeconds();
static uint64 GetTimeCycles();
static uint64 GetClockFrequency();

View File

@@ -324,7 +324,7 @@ private:
// Wait for all threads to stop adding items before resizing can happen
RETRY:
while (Platform::AtomicRead(&_threadsAdding))
Platform::Sleep(0);
Platform::Yield();
// Thread-safe resizing
_locker.Lock();

View File

@@ -39,7 +39,7 @@ RETRY:
{
// Someone else is doing opposite operation so wait for it's end
// TODO: use ConditionVariable+CriticalSection to prevent active-waiting
Platform::Sleep(0);
Platform::Yield();
goto RETRY;
}
@@ -47,7 +47,7 @@ RETRY:
if (exclusively && Platform::AtomicRead(thisCounter) != 0)
{
// Someone else is doing opposite operation so wait for it's end
Platform::Sleep(0);
Platform::Yield();
goto RETRY;
}