mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:38:38 +00:00
Every GDK title calls XGameRuntimeInitialize first and builds its async work on XTaskQueue/XAsync. Add an xgameruntime.dll shim table for those, ahead of the executable: XGameRuntimeInitialize/Uninitialize (reference counted), XSystemGetDeviceType (Series X; LEONIDA_DEVICE_TYPE overrides) and XSystemGetConsoleId (placeholder), plus the 23 XTaskQueue/XAsync functions. XTaskQueue/XAsync come from microsoft/libHttpClient (MIT), copied unmodified into third_party/xtaskqueue (task queue only, no HTTP stack); a small pch.h stand-in lets them build alone. xgameruntime_test drives the table through the shim registry the way a title boots. Nothing goes online.
72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#pragma once
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <thread>
|
|
|
|
#if defined(_WIN32) || defined(__WINDOWS__)
|
|
#include <intrin.h>
|
|
#elif (defined(_M_IX86) || defined(_M_X64))
|
|
#include <x86intrin.h>
|
|
#endif
|
|
|
|
//
|
|
// SpinLock: A spinlock implementation based on std::atomic_flag that
|
|
// prevents CPU starvation. SpinLock can be used as a RAII wrapper around
|
|
// an external flag, or its static Lock API may be used to lock an
|
|
// external flag.
|
|
//
|
|
class SpinLock
|
|
{
|
|
public:
|
|
SpinLock(_In_ std::atomic_flag& flag) : m_lock(flag)
|
|
{
|
|
Lock(m_lock);
|
|
}
|
|
|
|
~SpinLock()
|
|
{
|
|
m_lock.clear(std::memory_order_release);
|
|
}
|
|
|
|
static void Lock(_In_ std::atomic_flag& flag)
|
|
{
|
|
unsigned int backoff = 1;
|
|
constexpr unsigned int maxBackoff = 1024;
|
|
|
|
while (flag.test_and_set(std::memory_order_acquire)) {
|
|
for (unsigned int i = 0; i < backoff; ++i) {
|
|
cpu_pause();
|
|
}
|
|
|
|
// Exponential backoff with cap. If we are over the cap yield
|
|
// this thread.
|
|
|
|
backoff = backoff << 1;
|
|
if (backoff >= maxBackoff)
|
|
{
|
|
backoff = maxBackoff;
|
|
std::this_thread::yield();
|
|
}
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::atomic_flag& m_lock;
|
|
|
|
static inline void cpu_pause()
|
|
{
|
|
#if defined(_M_IX86) || defined(_M_X64)
|
|
// x86/x64: Tells CPU we're spinning
|
|
// Reduces energy consumption
|
|
// Helps avoid memory order violations
|
|
// Maps to PAUSE instruction
|
|
_mm_pause();
|
|
#elif defined(_M_ARM) || defined(_M_ARM64)
|
|
// ARM: Yields to other hardware threads
|
|
__yield();
|
|
#else
|
|
// Other platforms: No specific CPU hint
|
|
// Still helps due to compiler barrier
|
|
#endif
|
|
}
|
|
}; |