From 7c6c6d3df72ff04fd2f88552847fcc67370ead71 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:22:02 -0500 Subject: [PATCH 1/9] refactor: replace winapi TLS functions in Vec3 and AABB --- Minecraft.World/Util/AABB.cpp | 23 ++++++++++------------- Minecraft.World/Util/AABB.h | 4 ++-- Minecraft.World/Util/Vec3.cpp | 22 ++++++++++------------ Minecraft.World/Util/Vec3.h | 5 +++-- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Minecraft.World/Util/AABB.cpp b/Minecraft.World/Util/AABB.cpp index c4a8bff2e..3dff9c23d 100644 --- a/Minecraft.World/Util/AABB.cpp +++ b/Minecraft.World/Util/AABB.cpp @@ -7,8 +7,8 @@ #include "AABB.h" #include "HitResult.h" -unsigned int AABB::tlsIdx = 0; -AABB::ThreadStorage* AABB::tlsDefault = NULL; +thread_local AABB::ThreadStorage* AABB::m_tlsPool = nullptr; +AABB::ThreadStorage* AABB::m_tlsPoolDefault = nullptr; AABB::ThreadStorage::ThreadStorage() { pool = new AABB[POOL_SIZE]; // 4jcraft, needs to be deleted with delete[] @@ -21,21 +21,18 @@ AABB::ThreadStorage::~ThreadStorage() { void AABB::CreateNewThreadStorage() { ThreadStorage* tls = new ThreadStorage(); - if (tlsDefault == NULL) { - tlsIdx = TlsAlloc(); - tlsDefault = tls; + if (m_tlsPoolDefault == nullptr) { + m_tlsPoolDefault = tls; } - - TlsSetValue(tlsIdx, tls); + m_tlsPool = m_tlsPoolDefault; } -void AABB::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); } +void AABB::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } void AABB::ReleaseThreadStorage() { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); - if (tls == tlsDefault) return; - - delete tls; + if (m_tlsPool != m_tlsPoolDefault) { + delete m_tlsPool; + } } AABB* AABB::newPermanent(double x0, double y0, double z0, double x1, double y1, @@ -49,7 +46,7 @@ void AABB::resetPool() {} AABB* AABB::newTemp(double x0, double y0, double z0, double x1, double y1, double z1) { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); + ThreadStorage* tls = m_tlsPool; AABB* thisAABB = &tls->pool[tls->poolPointer]; thisAABB->set(x0, y0, z0, x1, y1, z1); tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE; diff --git a/Minecraft.World/Util/AABB.h b/Minecraft.World/Util/AABB.h index bcd941f39..205d5ac98 100644 --- a/Minecraft.World/Util/AABB.h +++ b/Minecraft.World/Util/AABB.h @@ -15,8 +15,8 @@ class AABB { ThreadStorage(); ~ThreadStorage(); }; - static unsigned int tlsIdx; - static ThreadStorage* tlsDefault; + static thread_local ThreadStorage* m_tlsPool; + static ThreadStorage* m_tlsPoolDefault; public: // Each new thread that needs to use Vec3 pools will need to call one of the diff --git a/Minecraft.World/Util/Vec3.cpp b/Minecraft.World/Util/Vec3.cpp index 4e42b99a3..7796aba16 100644 --- a/Minecraft.World/Util/Vec3.cpp +++ b/Minecraft.World/Util/Vec3.cpp @@ -2,8 +2,8 @@ #include "Vec3.h" #include "AABB.h" -unsigned int Vec3::tlsIdx = 0; -Vec3::ThreadStorage* Vec3::tlsDefault = NULL; +thread_local Vec3::ThreadStorage* Vec3::m_tlsPool = nullptr; +Vec3::ThreadStorage* Vec3::m_tlsPoolDefault = nullptr; Vec3::ThreadStorage::ThreadStorage() { pool = new Vec3[POOL_SIZE]; @@ -14,20 +14,18 @@ Vec3::ThreadStorage::~ThreadStorage() { delete[] pool; } void Vec3::CreateNewThreadStorage() { ThreadStorage* tls = new ThreadStorage(); - if (tlsDefault == NULL) { - tlsIdx = TlsAlloc(); - tlsDefault = tls; + if (m_tlsPoolDefault == nullptr) { + m_tlsPoolDefault = tls; } - TlsSetValue(tlsIdx, tls); + m_tlsPool = m_tlsPoolDefault; } -void Vec3::UseDefaultThreadStorage() { TlsSetValue(tlsIdx, tlsDefault); } +void Vec3::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } void Vec3::ReleaseThreadStorage() { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); - if (tls == tlsDefault) return; - - delete tls; + if (m_tlsPool != m_tlsPoolDefault) { + delete m_tlsPool; + } } Vec3* Vec3::newPermanent(double x, double y, double z) { @@ -39,7 +37,7 @@ void Vec3::clearPool() {} void Vec3::resetPool() {} Vec3* Vec3::newTemp(double x, double y, double z) { - ThreadStorage* tls = (ThreadStorage*)TlsGetValue(tlsIdx); + ThreadStorage* tls = m_tlsPool; Vec3* thisVec = &tls->pool[tls->poolPointer]; thisVec->set(x, y, z); tls->poolPointer = (tls->poolPointer + 1) % ThreadStorage::POOL_SIZE; diff --git a/Minecraft.World/Util/Vec3.h b/Minecraft.World/Util/Vec3.h index bc1239d81..da9fcc73b 100644 --- a/Minecraft.World/Util/Vec3.h +++ b/Minecraft.World/Util/Vec3.h @@ -12,8 +12,9 @@ class Vec3 { ThreadStorage(); ~ThreadStorage(); }; - static unsigned int tlsIdx; - static ThreadStorage* tlsDefault; + + static thread_local ThreadStorage* m_tlsPool; + static ThreadStorage* m_tlsPoolDefault; public: // Each new thread that needs to use Vec3 pools will need to call one of the From 7b18641f7006984ccf26e406f36862a501ab1e0f Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:24:45 -0500 Subject: [PATCH 2/9] chore: clean up unused code from Linux_Minecraft.cpp --- Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp index 852dbb58c..4daca7e16 100644 --- a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp +++ b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp @@ -5,7 +5,6 @@ #include // #include -#include #if defined(__linux__) && defined(__GLIBC__) #include #include @@ -710,16 +709,6 @@ void CleanupDevice() { } #endif -int StartMinecraftThreadProc(void* lpParameter) { - Vec3::UseDefaultThreadStorage(); - AABB::UseDefaultThreadStorage(); - Tesselator::CreateNewThreadStorage(1024 * 1024); - RenderManager.InitialiseContext(); - Minecraft::start(std::wstring(), std::wstring()); - delete Tesselator::getInstance(); - return 0; -} - int main(int argc, const char* argv[]) { #if defined(__linux__) && defined(__GLIBC__) struct sigaction sa; @@ -1202,7 +1191,7 @@ void FreeRichPresenceStrings() { vRichPresenceStrings.clear(); } -#ifdef MEMORY_TRACKING +#if 0 // #ifdef MEMORY_TRACKING int totalAllocGen = 0; std::unordered_map allocCounts; From 9fec342554f3c8d7dc9d418071d9b0951352c81b Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:32:06 -0500 Subject: [PATCH 3/9] remove TlsAlloc, TlsFree, TlsGetValue, TlsSetValue --- .../Platform/Linux/Stubs/winapi_stubs.h | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h index 452562fad..78fe936b2 100644 --- a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h +++ b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h @@ -306,28 +306,6 @@ static inline ULONG TryEnterCriticalSection( return pthread_mutex_trylock(CriticalSection) == 0; } -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc -static inline DWORD TlsAlloc(VOID) { - pthread_key_t key; - if (pthread_key_create(&key, NULL) == 0) return key; - return TLS_OUT_OF_INDEXES; -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsfree -static inline BOOL TlsFree(DWORD dwTlsIndex) { - return pthread_key_delete(dwTlsIndex) == 0; -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsgetvalue -static inline LPVOID TlsGetValue(DWORD dwTlsIndex) { - return pthread_getspecific(dwTlsIndex); -} - -// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlssetvalue -static inline BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue) { - return pthread_setspecific(dwTlsIndex, lpTlsValue) == 0; -} - // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalmemorystatus static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) { // TODO: Parse /proc/meminfo and set lpBuffer based on that. Probably will From 06a4096cf6a904f822ee0456cbe813b33f570422 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 18:43:10 -0500 Subject: [PATCH 4/9] fix: properly assign new pool to m_tlsPool --- Minecraft.World/Util/AABB.cpp | 2 +- Minecraft.World/Util/Vec3.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Minecraft.World/Util/AABB.cpp b/Minecraft.World/Util/AABB.cpp index 3dff9c23d..f09dbc6f0 100644 --- a/Minecraft.World/Util/AABB.cpp +++ b/Minecraft.World/Util/AABB.cpp @@ -24,7 +24,7 @@ void AABB::CreateNewThreadStorage() { if (m_tlsPoolDefault == nullptr) { m_tlsPoolDefault = tls; } - m_tlsPool = m_tlsPoolDefault; + m_tlsPool = tls; } void AABB::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } diff --git a/Minecraft.World/Util/Vec3.cpp b/Minecraft.World/Util/Vec3.cpp index 7796aba16..fcc922af2 100644 --- a/Minecraft.World/Util/Vec3.cpp +++ b/Minecraft.World/Util/Vec3.cpp @@ -17,7 +17,7 @@ void Vec3::CreateNewThreadStorage() { if (m_tlsPoolDefault == nullptr) { m_tlsPoolDefault = tls; } - m_tlsPool = m_tlsPoolDefault; + m_tlsPool = tls; } void Vec3::UseDefaultThreadStorage() { m_tlsPool = m_tlsPoolDefault; } From 8e94b763a718379fccaae5c9640ee9c0d0c44455 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:12:11 -0500 Subject: [PATCH 5/9] refactor: remove usage of win32 `Sleep` function for `this_thread::sleep_for` --- Minecraft.Client/Level/ServerLevel.cpp | 5 ++- Minecraft.Client/Minecraft.cpp | 12 ++++--- Minecraft.Client/MinecraftServer.cpp | 14 ++++---- Minecraft.Client/Network/ServerChunkCache.cpp | 5 ++- .../Platform/Common/Consoles_App.cpp | 16 +++++---- .../Leaderboards/SonyLeaderboardManager.cpp | 7 ++-- .../Common/Network/GameNetworkManager.cpp | 33 ++++++++++--------- .../Common/Network/Sony/SonyCommerce.cpp | 8 +++-- .../Common/Network/Sony/SonyRemoteStorage.cpp | 5 +-- .../Platform/Common/UI/IUIScene_PauseMenu.cpp | 9 +++-- .../Common/UI/UIScene_LoadOrJoinMenu.cpp | 23 +++++++------ .../Platform/Common/XUI/XUI_Death.cpp | 7 ++-- .../Platform/Common/XUI/XUI_Leaderboards.cpp | 5 ++- .../Common/XUI/XUI_MultiGameJoinLoad.cpp | 4 ++- .../Platform/Common/XUI/XUI_PauseMenu.cpp | 8 +++-- .../Common/XUI/XUI_TransferToXboxOne.cpp | 10 +++--- Minecraft.Client/Rendering/LevelRenderer.cpp | 5 ++- .../IO/Files/ConsoleSaveFileOriginal.cpp | 5 ++- .../IO/Files/ConsoleSaveFileSplit.cpp | 5 ++- .../Level/Storage/McRegionChunkStorage.cpp | 14 +++++--- Minecraft.World/Network/Connection.cpp | 7 ++-- Minecraft.World/Network/Socket.cpp | 11 ++++--- Minecraft.World/Util/C4JThread.cpp | 4 ++- 23 files changed, 144 insertions(+), 78 deletions(-) diff --git a/Minecraft.Client/Level/ServerLevel.cpp b/Minecraft.Client/Level/ServerLevel.cpp index 025190b7f..ece03b701 100644 --- a/Minecraft.Client/Level/ServerLevel.cpp +++ b/Minecraft.Client/Level/ServerLevel.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../Platform/stdafx.h" #include "ServerLevel.h" #include "../MinecraftServer.h" @@ -1570,7 +1573,7 @@ int ServerLevel::runUpdate(void* lpParam) { } PIXEndNamedEvent(); #ifdef __PS3__ - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); #endif //__PS3__ } diff --git a/Minecraft.Client/Minecraft.cpp b/Minecraft.Client/Minecraft.cpp index 1e9a1c954..76aab6c96 100644 --- a/Minecraft.Client/Minecraft.cpp +++ b/Minecraft.Client/Minecraft.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "Platform/stdafx.h" #include "Minecraft.h" #include "GameState/GameMode.h" @@ -781,7 +784,7 @@ void Minecraft::run() { this->toggleFullScreen(); } - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } */ @@ -796,7 +799,7 @@ void Minecraft::run() achievementPopup->render(); - Sleep(0); // 4J - was Thread.yield() + std::this_thread::sleep_for(std::chrono::milliseconds(0)); // 4J - was Thread.yield()) // if (Keyboard::isKeyDown(Keyboard::KEY_F7)) Display.update(); // 4J - removed condition Display::update(); @@ -2112,7 +2115,7 @@ void Minecraft::run_middle() { { this->toggleFullScreen(); } - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } */ @@ -2141,7 +2144,8 @@ void Minecraft::run_middle() { achievementPopup->render(); PIXBeginNamedEvent(0, "Sleeping"); - Sleep(0); // 4J - was Thread.yield() + std::this_thread::sleep_for( + std::chrono::milliseconds(0)); // 4J - was Thread.yield()) PIXEndNamedEvent(); // if (Keyboard::isKeyDown(Keyboard::KEY_F7)) diff --git a/Minecraft.Client/MinecraftServer.cpp b/Minecraft.Client/MinecraftServer.cpp index a08f48ac9..d92e75bd0 100644 --- a/Minecraft.Client/MinecraftServer.cpp +++ b/Minecraft.Client/MinecraftServer.cpp @@ -2,6 +2,8 @@ // #include "Minecraft.h" #include +#include +#include #include "Input/ConsoleInput.h" #include "Level/DerivedServerLevel.h" @@ -229,7 +231,7 @@ bool MinecraftServer::initServer(__int64 seed, NetworkGameInitData* initData, // 4J-JEV: Need to wait for levelGenerationOptions to load. while (app.getLevelGenerationOptions() != NULL && !app.getLevelGenerationOptions()->hasLoadedData()) - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); if (app.getLevelGenerationOptions() != NULL && !app.getLevelGenerationOptions()->ready()) { @@ -336,7 +338,7 @@ int MinecraftServer::runPostUpdate(void* lpParam) { } else { LeaveCriticalSection(&server->m_postProcessCS); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } while (!server->m_postUpdateTerminate && ShutdownManager::ShouldRun(ShutdownManager::ePostProcessThread)); // #ifndef __PS3__ @@ -357,7 +359,7 @@ int MinecraftServer::runPostUpdate(void* lpParam) { printf("processing request %00d\n", server->m_postProcessRequests.size()); #endif - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); #endif EnterCriticalSection(&server->m_postProcessCS); } @@ -1080,7 +1082,7 @@ void MinecraftServer::stopServer(bool didInit) { // files have completed saving. #if defined(_DURANGO) || defined(__ORBIS__) || defined(__PSVITA__) while (StorageManager.GetSaveState() != C4JStorage::ESaveGame_Idle) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } #endif @@ -1582,7 +1584,7 @@ void MinecraftServer::run(int64_t seed, void* lpParameter) { app.SetXuiServerAction(i, eXuiServerAction_Idle); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } // else @@ -1590,7 +1592,7 @@ void MinecraftServer::run(int64_t seed, void* lpParameter) { // while (running) // { // handleConsoleInputs(); - // Sleep(10); + // std::this_thread::sleep_for(std::chrono::milliseconds(10)); // } // } #if 0 diff --git a/Minecraft.Client/Network/ServerChunkCache.cpp b/Minecraft.Client/Network/ServerChunkCache.cpp index a8b5b78c0..68c701155 100644 --- a/Minecraft.Client/Network/ServerChunkCache.cpp +++ b/Minecraft.Client/Network/ServerChunkCache.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../Platform/stdafx.h" #include "ServerChunkCache.h" #include "../Level/ServerLevel.h" @@ -277,7 +280,7 @@ LevelChunk* ServerChunkCache::create( } #ifdef __PS3__ - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); #endif // __PS3__ return chunk; } diff --git a/Minecraft.Client/Platform/Common/Consoles_App.cpp b/Minecraft.Client/Platform/Common/Consoles_App.cpp index f85d56632..830578af0 100644 --- a/Minecraft.Client/Platform/Common/Consoles_App.cpp +++ b/Minecraft.Client/Platform/Common/Consoles_App.cpp @@ -1,5 +1,4 @@ - -#include "../Minecraft.World/Platform/stdafx.h" +#include "../Minecraft.World/Platform/stdafx.h" #include "../Minecraft.World/Recipes/Recipy.h" #include "../Minecraft.Client/GameState/Options.h" @@ -69,6 +68,9 @@ #include #endif +#include +#include + #include "Leaderboards/LeaderboardManager.h" // CMinecraftApp app; @@ -4832,7 +4834,7 @@ int CMinecraftApp::SignoutExitWorldThreadProc(void* lpParameter) { // We can't start/join a new game until the session is destroyed, so wait // for it to be idle again while (g_NetworkManager.IsInSession()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } return S_OK; @@ -7726,7 +7728,7 @@ int CMinecraftApp::RemoteSaveThreadProc(void* lpParameter) { eAppAction_WaitRemoteServerSaveComplete) { // Tick all the games connections pMinecraft->tickAllConnections(); - Sleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } if (app.GetXuiAction(ProfileManager.GetPrimaryPad()) != @@ -9229,7 +9231,8 @@ bool CMinecraftApp::RetrieveNextTMSPPContent() { C4JStorage::ETMSStatus_Fail_ReadInProgress) { app.DebugPrintf( "TMSPP_ReadFile failed - read in progress\n"); - Sleep(50); + std::this_thread::sleep_for( + std::chrono::milliseconds(50)); LeaveCriticalSection(&csTMSPPDownloadQueue); return false; } @@ -9268,7 +9271,8 @@ bool CMinecraftApp::RetrieveNextTMSPPContent() { app.DebugPrintf( "@@@@@@@@@@@@@@@@@ TMSPP_ReadFile failed - busy " "(probably reading already)\n"); - Sleep(50); + std::this_thread::sleep_for( + std::chrono::milliseconds(50)); LeaveCriticalSection(&csTMSPPDownloadQueue); return false; } diff --git a/Minecraft.Client/Platform/Common/Leaderboards/SonyLeaderboardManager.cpp b/Minecraft.Client/Platform/Common/Leaderboards/SonyLeaderboardManager.cpp index 88a4077d9..8153ac650 100644 --- a/Minecraft.Client/Platform/Common/Leaderboards/SonyLeaderboardManager.cpp +++ b/Minecraft.Client/Platform/Common/Leaderboards/SonyLeaderboardManager.cpp @@ -1,5 +1,7 @@ #include "../../stdafx.h" +#include +#include #include #include // #include @@ -54,7 +56,7 @@ SonyLeaderboardManager::~SonyLeaderboardManager() { // 4J-JEV: Wait for thread to stop and hope it doesn't take too long. long long startShutdown = System::currentTimeMillis(); while (m_threadScoreboard->isRunning()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); assert((System::currentTimeMillis() - startShutdown) < 16); } @@ -87,7 +89,8 @@ int SonyLeaderboardManager::scoreboardThreadEntry(LPVOID lpParam) { } if ((!needsWriting) && (self->m_eStatsState != eStatsState_Getting)) { - Sleep(50); // 4J-JEV: When we're not reading or writing. + std::this_thread::sleep_for(std::chrono::milliseconds( + 50)); // 4J-JEV: When we're not reading or writing. } } while ((self->m_running || self->m_eStatsState == eStatsState_Getting || diff --git a/Minecraft.Client/Platform/Common/Network/GameNetworkManager.cpp b/Minecraft.Client/Platform/Common/Network/GameNetworkManager.cpp index a26addc80..863e864f9 100644 --- a/Minecraft.Client/Platform/Common/Network/GameNetworkManager.cpp +++ b/Minecraft.Client/Platform/Common/Network/GameNetworkManager.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Minecraft.World/Platform/stdafx.h" #include "../../Minecraft.World/Util/StringHelpers.h" #include "../../Minecraft.World/Util/AABB.h" @@ -344,7 +347,7 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft, // message I could find pMinecraft->progressRenderer->progressStagePercentage( g_NetworkManager.GetJoiningReadyPercentage()); - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if (changedMessage) { pMinecraft->progressRenderer->progressStagePercentage(100); @@ -367,7 +370,7 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft, // this while (!app.DLCInstallProcessCompleted() && app.DLCInstallPending() && !g_NetworkManager.IsLeavingGame()) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if (g_NetworkManager.IsLeavingGame()) { MinecraftServer::HaltServer(); @@ -448,7 +451,7 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft, // 4J Stu - We were ticking this way too fast which could cause the // connection to time out The connections should tick at 20 per second - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } while ((IsInSession() && !connection->isStarted() && !connection->isClosed() && !g_NetworkManager.IsLeavingGame()) || tPack->isLoadingData() || @@ -548,7 +551,7 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft* minecraft, // 4J Stu - We were ticking this way too fast which could cause // the connection to time out The connections should tick at 20 // per second - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); app.DebugPrintf("<***> %d %d %d %d %d\n", IsInSession(), !connection->isStarted(), !connection->isClosed(), @@ -960,7 +963,7 @@ int CGameNetworkManager::RunNetworkGameThreadProc(void* lpParameter) { while (tPack->isLoadingData() || (Minecraft::GetInstance()->skins->needsUIUpdate() || ui.IsReloadingSkin())) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } ui.CleanUpSkinReload(); if (app.GetDisconnectReason() == DisconnectPacket::eDisconnect_None) { @@ -978,7 +981,7 @@ int CGameNetworkManager::RunNetworkGameThreadProc(void* lpParameter) { #ifdef __PSVITA__ // 4J-JEV: Wait for the loading/saving to finish. while (StorageManager.GetSaveState() != C4JStorage::ESaveGame_Idle) - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); #endif Tile::ReleaseThreadStorage(); @@ -1000,7 +1003,7 @@ int CGameNetworkManager::ServerThreadProc(void* lpParameter) { param->texturePackId)) { while ((Minecraft::GetInstance()->skins->needsUIUpdate() || ui.IsReloadingSkin())) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } param->levelGen->loadBaseSaveData(); } @@ -1041,7 +1044,7 @@ int CGameNetworkManager::ExitAndJoinFromInviteThreadProc(void* lpParam) { UIScene_PauseMenu::_ExitWorld(NULL); while (g_NetworkManager.IsInSession()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } // Xbox should always be online when receiving invites - on PS3 we need to @@ -1247,7 +1250,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { while (app.GetXuiServerAction(ProfileManager.GetPrimaryPad()) != eXuiServerAction_Idle && !MinecraftServer::serverHalted()) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } app.SetXuiServerAction(ProfileManager.GetPrimaryPad(), eXuiServerAction_PauseServer, (void*)TRUE); @@ -1287,7 +1290,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { // if we don't do this then there's an async thread running doing this, // which could then finish at any inappropriate time later while (s_pPlatformNetworkManager->IsAddingPlayer()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } #endif @@ -1318,7 +1321,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { // wait for the current session to end while (g_NetworkManager.IsInSession()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } // Reset this flag as the we don't need to know that we only lost the room @@ -1348,7 +1351,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { // Wait for all the local players to rejoin the session while (g_NetworkManager.GetPlayerCount() < numLocalPlayers) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } // Restore the network player of all the server players that are local @@ -1398,7 +1401,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { // Make sure that we have transitioned through any joining/creating stages // so we're actually ready to set to play while (!s_pPlatformNetworkManager->IsReadyToPlayOrIdle()) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } #endif @@ -1407,7 +1410,7 @@ int CGameNetworkManager::ChangeSessionTypeThreadProc(void* lpParam) { #ifndef _XBOX // Wait until the message box has been closed while (ui.IsSceneInStack(XUSER_INDEX_ANY, eUIScene_MessageBox)) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } #endif @@ -1957,7 +1960,7 @@ void CGameNetworkManager::HandleInviteWhenInMenus( #if 0 while( waitHere ) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } #endif diff --git a/Minecraft.Client/Platform/Common/Network/Sony/SonyCommerce.cpp b/Minecraft.Client/Platform/Common/Network/Sony/SonyCommerce.cpp index 3ce5ec6e0..d6bcd9b2f 100644 --- a/Minecraft.Client/Platform/Common/Network/Sony/SonyCommerce.cpp +++ b/Minecraft.Client/Platform/Common/Network/Sony/SonyCommerce.cpp @@ -1,8 +1,11 @@ +#include +#include +#include + #include "../../../Minecraft.World/Platform/stdafx.h" #include "SonyCommerce.h" #include "../../../../Platform/PS3/PS3Extras/ShutdownManager.h" -#include bool SonyCommerce::m_bCommerceInitialised = false; SceNpCommerce2SessionInfo SonyCommerce::m_sessionInfo; @@ -122,7 +125,8 @@ int SonyCommerce::TickLoop(void* lpParam) { ShutdownManager::ShouldRun(ShutdownManager::eCommerceThread)) { processEvent(); processMessage(); - Sleep(16); // sleep for a frame + std::this_thread::sleep_for( + std::chrono::milliseconds(16)); // sleep for a frame } ShutdownManager::HasFinished(ShutdownManager::eCommerceThread); diff --git a/Minecraft.Client/Platform/Common/Network/Sony/SonyRemoteStorage.cpp b/Minecraft.Client/Platform/Common/Network/Sony/SonyRemoteStorage.cpp index 132c17034..817763b78 100644 --- a/Minecraft.Client/Platform/Common/Network/Sony/SonyRemoteStorage.cpp +++ b/Minecraft.Client/Platform/Common/Network/Sony/SonyRemoteStorage.cpp @@ -1,4 +1,5 @@ - +#include +#include #include "../../../Minecraft.World/Platform/stdafx.h" #include "SonyRemoteStorage.h" @@ -286,7 +287,7 @@ bool SonyRemoteStorage::shutdown() { void SonyRemoteStorage::waitForStorageManagerIdle() { C4JStorage::ESaveGameState storageState = StorageManager.GetSaveState(); while (storageState != C4JStorage::ESaveGame_Idle) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); // app.DebugPrintf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // >>>>> storageState = %d\n", storageState); storageState = StorageManager.GetSaveState(); diff --git a/Minecraft.Client/Platform/Common/UI/IUIScene_PauseMenu.cpp b/Minecraft.Client/Platform/Common/UI/IUIScene_PauseMenu.cpp index 65d52d39a..e387901c7 100644 --- a/Minecraft.Client/Platform/Common/UI/IUIScene_PauseMenu.cpp +++ b/Minecraft.Client/Platform/Common/UI/IUIScene_PauseMenu.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Minecraft.World/Platform/stdafx.h" #include "IUIScene_PauseMenu.h" #include "UIScene.h" @@ -394,7 +397,7 @@ int IUIScene_PauseMenu::SaveWorldThreadProc(void* lpParameter) { while (app.GetXuiServerAction(ProfileManager.GetPrimaryPad()) != eXuiServerAction_Idle && !MinecraftServer::serverHalted()) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if (!MinecraftServer::serverHalted() && !app.GetChangingSessionType()) @@ -663,7 +666,7 @@ void IUIScene_PauseMenu::_ExitWorld(void* lpParameter) { // multiplayer client if host of the game will exit during the clients // loading to created world. while (g_NetworkManager.IsNetworkThreadRunning()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } pMinecraft->setLevel(NULL, exitReasonStringId, nullptr, saveStats); @@ -683,7 +686,7 @@ void IUIScene_PauseMenu::_ExitWorld(void* lpParameter) { // loads saved data We can't start/join a new game until the session is // destroyed, so wait for it to be idle again while (g_NetworkManager.IsInSession()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } app.SetChangingSessionType(false); diff --git a/Minecraft.Client/Platform/Common/UI/UIScene_LoadOrJoinMenu.cpp b/Minecraft.Client/Platform/Common/UI/UIScene_LoadOrJoinMenu.cpp index 563124002..31e09f0fb 100644 --- a/Minecraft.Client/Platform/Common/UI/UIScene_LoadOrJoinMenu.cpp +++ b/Minecraft.Client/Platform/Common/UI/UIScene_LoadOrJoinMenu.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Minecraft.World/Platform/stdafx.h" #include "UI.h" #include "UIScene_LoadOrJoinMenu.h" @@ -2229,7 +2232,7 @@ int UIScene_LoadOrJoinMenu::MustSignInReturnedTexturePack(void* pParam, while (commerceState != CConsoleMinecraftApp::eCommerce_State_Offline && commerceState != CConsoleMinecraftApp::eCommerce_State_Online && commerceState != CConsoleMinecraftApp::eCommerce_State_Error) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); commerceState = app.GetCommerceState(); } @@ -2852,7 +2855,7 @@ int UIScene_LoadOrJoinMenu::DownloadSonyCrossSaveThreadProc(void* lpParameter) { #if defined(_DURANGO) || defined(__ORBIS__) while (StorageManager.GetSaveState() != C4JStorage::ESaveGame_Idle) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); StorageManager.Tick(); } #endif @@ -2999,7 +3002,7 @@ int UIScene_LoadOrJoinMenu::DownloadSonyCrossSaveThreadProc(void* lpParameter) { // waiting to dismiss the dialog break; } - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } m_bSaveTransferRunning = false; #ifdef __PS3__ @@ -3151,7 +3154,7 @@ int UIScene_LoadOrJoinMenu::UploadSonyCrossSaveThreadProc(void* lpParameter) { // waiting for dialog to be dismissed break; } - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } return 0; @@ -3247,7 +3250,7 @@ int UIScene_LoadOrJoinMenu::DownloadXbox360SaveThreadProc(void* lpParameter) { while (StorageManager.SaveTransferClearState() != C4JStorage::eSaveTransfer_Idle) { - Sleep(5); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); } pStateContainer->m_bSaveTransferInProgress = true; @@ -3432,7 +3435,7 @@ int UIScene_LoadOrJoinMenu::DownloadXbox360SaveThreadProc(void* lpParameter) { while (StorageManager.GetSaveState() != C4JStorage::ESaveGame_Idle) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 4J Stu - DO NOT tick this here. The main thread should be // the only place ticking the StorageManager. You WILL get @@ -3470,7 +3473,7 @@ int UIScene_LoadOrJoinMenu::DownloadXbox360SaveThreadProc(void* lpParameter) { break; } - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } if (pStateContainer->m_bSaveTransferCancelled) { @@ -3517,7 +3520,7 @@ void UIScene_LoadOrJoinMenu::RequestFileSize(SaveTransferStateContainer* pClass, IDS_SAVETRANSFER_TITLE_GET); pMinecraft->progressRenderer->progressStage( IDS_SAVETRANSFER_STAGE_GET_DETAILS); - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); pClass->m_eSaveTransferState = StorageManager.SaveTransferGetDetails( pClass->m_iPad, C4JStorage::eGlobalStorage_TitleUser, @@ -3573,7 +3576,7 @@ void UIScene_LoadOrJoinMenu::RequestFileData(SaveTransferStateContainer* pClass, pMinecraft->progressRenderer->progressStart( IDS_SAVETRANSFER_TITLE_GET); pMinecraft->progressRenderer->progressStage(-1); - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); pClass->m_eSaveTransferState = StorageManager.SaveTransferGetData( pClass->m_iPad, C4JStorage::eGlobalStorage_TitleUser, filename, &UIScene_LoadOrJoinMenu::SaveTransferReturned, @@ -3740,7 +3743,7 @@ int UIScene_LoadOrJoinMenu::CopySaveThreadProc(void* lpParameter) { bool bContinue = true; do { - Sleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); ui.EnterCallbackIdCriticalSection(); pClass = (UIScene_LoadOrJoinMenu*)ui.GetSceneFromCallbackId( (size_t)lpParameter); diff --git a/Minecraft.Client/Platform/Common/XUI/XUI_Death.cpp b/Minecraft.Client/Platform/Common/XUI/XUI_Death.cpp index 7902b427a..45d6f58a7 100644 --- a/Minecraft.Client/Platform/Common/XUI/XUI_Death.cpp +++ b/Minecraft.Client/Platform/Common/XUI/XUI_Death.cpp @@ -1,9 +1,12 @@ // Minecraft.cpp : Defines the entry point for the application. // +#include +#include +#include + #include "../../Minecraft.World/Platform/stdafx.h" #include "XUI_Death.h" -#include #include "../../Minecraft.World/Util/AABB.h" #include "../../Minecraft.World/Util/Vec3.h" #include "../../Minecraft.World/Headers/net.minecraft.stats.h" @@ -235,7 +238,7 @@ int CScene_Death::RespawnThreadProc(void* lpParameter) { // If we are offline, this should release straight away // WaitForSingleObject( pMinecraft->m_hPlayerRespawned, INFINITE ); while (pMinecraft->localplayers[iPad]->GetPlayerRespawned() == false) { - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } return S_OK; diff --git a/Minecraft.Client/Platform/Common/XUI/XUI_Leaderboards.cpp b/Minecraft.Client/Platform/Common/XUI/XUI_Leaderboards.cpp index 193ea0a01..978ca2335 100644 --- a/Minecraft.Client/Platform/Common/XUI/XUI_Leaderboards.cpp +++ b/Minecraft.Client/Platform/Common/XUI/XUI_Leaderboards.cpp @@ -7,6 +7,9 @@ #include "XUI_Ctrl_CraftIngredientSlot.h" #include "XUI_XZP_Icons.h" +#include +#include + LPCWSTR CScene_Leaderboards::m_TitleIconNameA[7] = { L"XuiHSlot1", L"XuiHSlot2", L"XuiHSlot3", L"XuiHSlot4", L"XuiHSlot5", L"XuiHSlot6", L"XuiHSlot7", @@ -283,7 +286,7 @@ HRESULT CScene_Leaderboards::OnDestroy() { app.SetLiveLinkRequired(false); while (m_isProcessingStatsRead) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if (m_friends != NULL) delete[] m_friends; diff --git a/Minecraft.Client/Platform/Common/XUI/XUI_MultiGameJoinLoad.cpp b/Minecraft.Client/Platform/Common/XUI/XUI_MultiGameJoinLoad.cpp index 2e07152e6..d9fb16f46 100644 --- a/Minecraft.Client/Platform/Common/XUI/XUI_MultiGameJoinLoad.cpp +++ b/Minecraft.Client/Platform/Common/XUI/XUI_MultiGameJoinLoad.cpp @@ -1,6 +1,8 @@ #include "../../Minecraft.World/Platform/stdafx.h" #include #include +#include +#include #include #include "../../Minecraft.World/Util/StringHelpers.h" #include "../Tutorial/TutorialMode.h" @@ -2174,7 +2176,7 @@ bool CScene_MultiGameJoinLoad::WaitForTransferComplete( // cancelled return false; } - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); // update the progress pMinecraft->progressRenderer->progressStagePercentage( (unsigned int)(pClass->m_fProgress * 100.0f)); diff --git a/Minecraft.Client/Platform/Common/XUI/XUI_PauseMenu.cpp b/Minecraft.Client/Platform/Common/XUI/XUI_PauseMenu.cpp index d77644991..edfb1ce3c 100644 --- a/Minecraft.Client/Platform/Common/XUI/XUI_PauseMenu.cpp +++ b/Minecraft.Client/Platform/Common/XUI/XUI_PauseMenu.cpp @@ -4,6 +4,8 @@ #include "../../Minecraft.World/Platform/stdafx.h" #include +#include +#include #include "../../Minecraft.World/Util/AABB.h" #include "../../Minecraft.World/Util/Vec3.h" #include "../../Minecraft.World/Headers/net.minecraft.stats.h" @@ -1087,7 +1089,7 @@ int UIScene_PauseMenu::SaveWorldThreadProc(LPVOID lpParameter) { while (app.GetXuiServerAction(ProfileManager.GetPrimaryPad()) != eXuiServerAction_Idle && !MinecraftServer::serverHalted()) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } if (!MinecraftServer::serverHalted() && !app.GetChangingSessionType()) @@ -1297,7 +1299,7 @@ void UIScene_PauseMenu::_ExitWorld(LPVOID lpParameter) { // multiplayer client if host of the game will exit during the clients // loading to created world. while (g_NetworkManager.IsNetworkThreadRunning()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } pMinecraft->setLevel(NULL, exitReasonStringId, nullptr, saveStats); @@ -1317,7 +1319,7 @@ void UIScene_PauseMenu::_ExitWorld(LPVOID lpParameter) { // loads saved data We can't start/join a new game until the session is // destroyed, so wait for it to be idle again while (g_NetworkManager.IsInSession()) { - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } app.SetChangingSessionType(false); diff --git a/Minecraft.Client/Platform/Common/XUI/XUI_TransferToXboxOne.cpp b/Minecraft.Client/Platform/Common/XUI/XUI_TransferToXboxOne.cpp index 178c3607f..1dd0cc64d 100644 --- a/Minecraft.Client/Platform/Common/XUI/XUI_TransferToXboxOne.cpp +++ b/Minecraft.Client/Platform/Common/XUI/XUI_TransferToXboxOne.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "XUI_Ctrl_4JList.h" #include "XUI_Ctrl_4JIcon.h" @@ -363,7 +365,7 @@ int CScene_TransferToXboxOne::UploadSaveForXboxOneThreadProc( // loop waiting for the write to complete uiComplete = 0; while (pClass->m_bWaitingForWrite && (hr == S_OK)) { - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); uiComplete++; if (uiComplete > 100) uiComplete = 100; @@ -380,7 +382,7 @@ int CScene_TransferToXboxOne::UploadSaveForXboxOneThreadProc( // finish the bar for (int i = uiComplete; i < 100; i++) { - Sleep(5); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); pMinecraft->progressRenderer->progressStagePercentage(i); } @@ -396,7 +398,7 @@ int CScene_TransferToXboxOne::UploadSaveForXboxOneThreadProc( // sleep until we have the data while (pClass->m_bSaveDataReceived == false) { - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } // write the save to user TMS @@ -443,7 +445,7 @@ int CScene_TransferToXboxOne::UploadSaveForXboxOneThreadProc( } else { // loop waiting for the write to complete while (pClass->m_bWaitingForWrite && (hr == S_OK)) { - Sleep(50); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } uiComplete += uiPercentageChunk; if (uiComplete > 100) uiComplete = 100; diff --git a/Minecraft.Client/Rendering/LevelRenderer.cpp b/Minecraft.Client/Rendering/LevelRenderer.cpp index 313922573..eeb04049e 100644 --- a/Minecraft.Client/Rendering/LevelRenderer.cpp +++ b/Minecraft.Client/Rendering/LevelRenderer.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../Platform/stdafx.h" #include "LevelRenderer.h" #include "../Textures/Textures.h" @@ -2321,7 +2324,7 @@ bool LevelRenderer::updateDirtyChunks() { } LeaveCriticalSection(&m_csDirtyChunks); #ifdef __PS3__ - Sleep(5); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); #endif // __PS3__ return false; } diff --git a/Minecraft.World/IO/Files/ConsoleSaveFileOriginal.cpp b/Minecraft.World/IO/Files/ConsoleSaveFileOriginal.cpp index bc54c3e07..a24829c80 100644 --- a/Minecraft.World/IO/Files/ConsoleSaveFileOriginal.cpp +++ b/Minecraft.World/IO/Files/ConsoleSaveFileOriginal.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Platform/stdafx.h" #include "../../Util/StringHelpers.h" #include "../../Util/PortableFileIO.h" @@ -664,7 +667,7 @@ void ConsoleSaveFileOriginal::Flush(bool autosave, bool updateThumbnail) { // save/save-exiting so seems prudent to wait for idle while (StorageManager.GetSaveState() != C4JStorage::ESaveGame_Idle) { app.DebugPrintf("Flush wait\n"); - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } #endif diff --git a/Minecraft.World/IO/Files/ConsoleSaveFileSplit.cpp b/Minecraft.World/IO/Files/ConsoleSaveFileSplit.cpp index 00690c559..9ee5cc552 100644 --- a/Minecraft.World/IO/Files/ConsoleSaveFileSplit.cpp +++ b/Minecraft.World/IO/Files/ConsoleSaveFileSplit.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Platform/stdafx.h" #include "../../Util/StringHelpers.h" #include "../../Util/PortableFileIO.h" @@ -1298,7 +1301,7 @@ void ConsoleSaveFileSplit::Flush(bool autosave, bool updateThumbnail) { #endif app.DebugPrintf("Flush wait\n"); - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } finalizeWrite(); diff --git a/Minecraft.World/Level/Storage/McRegionChunkStorage.cpp b/Minecraft.World/Level/Storage/McRegionChunkStorage.cpp index cef015625..a0bc150e5 100644 --- a/Minecraft.World/Level/Storage/McRegionChunkStorage.cpp +++ b/Minecraft.World/Level/Storage/McRegionChunkStorage.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../../Platform/stdafx.h" #include "../../Headers/net.minecraft.world.level.h" #include "../../IO/Files/ConsoleSaveFileIO.h" @@ -388,9 +391,10 @@ int McRegionChunkStorage::runSaveThreadProc(void* lpParam) { // If there was more than one thing in the queue last time we checked, // then we want to spin round again soon Otherwise wait a bit longer if ((lastQueueSize - 1) > 0) - Sleep(1); // Sleep 1 to yield + std::this_thread::sleep_for( + std::chrono::milliseconds(1)); // Sleep 1 to yield else - Sleep(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } Compression::ReleaseThreadStorage(); @@ -410,7 +414,7 @@ void McRegionChunkStorage::WaitForAllSaves() { LeaveCriticalSection(&cs_memory); while (queueSize > 0) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); EnterCriticalSection(&cs_memory); queueSize = s_chunkDataQueue.size(); @@ -424,7 +428,7 @@ void McRegionChunkStorage::WaitForAllSaves() { LeaveCriticalSection(&cs_memory); while (runningThreadCount > 0) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); EnterCriticalSection(&cs_memory); runningThreadCount = s_runningThreadCount; @@ -444,7 +448,7 @@ void McRegionChunkStorage::WaitForSaves() { if (queueSize > MAX_QUEUE_SIZE) { while (queueSize > DESIRED_QUEUE_SIZE) { - Sleep(10); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); EnterCriticalSection(&cs_memory); queueSize = s_chunkDataQueue.size(); diff --git a/Minecraft.World/Network/Connection.cpp b/Minecraft.World/Network/Connection.cpp index 942634ed9..66ba0998f 100644 --- a/Minecraft.World/Network/Connection.cpp +++ b/Minecraft.World/Network/Connection.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../Platform/stdafx.h" #include "../IO/Streams/InputOutputStream.h" #include "Socket.h" @@ -577,7 +580,7 @@ int Connection::runRead(void* lpParam) { while (con->readTick()); // try { - // Sleep(100L); + // std::this_thread::sleep_for(std::chrono::milliseconds(100L)); // TODO - 4J Stu - 1.8.2 changes these sleeps to 2L, but not sure // whether we should do that as well con->m_hWakeReadThread->WaitForSignal(100L); @@ -626,7 +629,7 @@ int Connection::runWrite(void* lpParam) { ShutdownManager::ShouldRun(ShutdownManager::eConnectionWriteThreads)) { while (con->writeTick()); - // Sleep(100L); + // std::this_thread::sleep_for(std::chrono::milliseconds(100L)); // TODO - 4J Stu - 1.8.2 changes these sleeps to 2L, but not sure // whether we should do that as well waitResult = con->m_hWakeWriteThread->WaitForSignal(100L); diff --git a/Minecraft.World/Network/Socket.cpp b/Minecraft.World/Network/Socket.cpp index 0d032886c..6606dda77 100644 --- a/Minecraft.World/Network/Socket.cpp +++ b/Minecraft.World/Network/Socket.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "../Platform/stdafx.h" #include "../IO/Streams/InputOutputStream.h" #include "SocketAddress.h" @@ -250,7 +253,7 @@ int Socket::SocketInputStreamLocal::read() { } LeaveCriticalSection(&s_hostQueueLock[m_queueIdx]); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } return -1; } @@ -277,7 +280,7 @@ int Socket::SocketInputStreamLocal::read(byteArray b, unsigned int offset, } LeaveCriticalSection(&s_hostQueueLock[m_queueIdx]); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } return -1; } @@ -356,7 +359,7 @@ int Socket::SocketInputStreamNetwork::read() { } LeaveCriticalSection(&m_socket->m_queueLockNetwork[m_queueIdx]); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } return -1; } @@ -385,7 +388,7 @@ int Socket::SocketInputStreamNetwork::read(byteArray b, unsigned int offset, } LeaveCriticalSection(&m_socket->m_queueLockNetwork[m_queueIdx]); } - Sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } return -1; } diff --git a/Minecraft.World/Util/C4JThread.cpp b/Minecraft.World/Util/C4JThread.cpp index ace672d19..eccfdfcaa 100644 --- a/Minecraft.World/Util/C4JThread.cpp +++ b/Minecraft.World/Util/C4JThread.cpp @@ -1,6 +1,8 @@ #include "../Platform/stdafx.h" #include "C4JThread.h" +#include +#include #ifdef __PSVITA__ #include "../../Minecraft.Client/Platform/PSVita/PSVitaExtras/ShutdownManager.h" #include "../../Minecraft.Client/Platform/PSVita/PSVitaExtras/PSVitaTLSStorage.h" @@ -443,7 +445,7 @@ void C4JThread::Sleep(int millisecs) { // 4J Stu - 0 is an error, so add a tiny sleep when we just want to yield sceKernelDelayThread(millisecs * 1000 + 1); #else - ::Sleep(millisecs); + std::this_thread::sleep_for(std::chrono::milliseconds(millisecs)); #endif // __PS3__ } From d9f03fdddf5cdf426f6d1ca80177177313462b9e Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:13:12 -0500 Subject: [PATCH 6/9] refactor: replace 0ms sleep with a proper yield --- Minecraft.Client/Minecraft.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Minecraft.Client/Minecraft.cpp b/Minecraft.Client/Minecraft.cpp index 76aab6c96..dfd39cd8e 100644 --- a/Minecraft.Client/Minecraft.cpp +++ b/Minecraft.Client/Minecraft.cpp @@ -2144,8 +2144,10 @@ void Minecraft::run_middle() { achievementPopup->render(); PIXBeginNamedEvent(0, "Sleeping"); - std::this_thread::sleep_for( - std::chrono::milliseconds(0)); // 4J - was Thread.yield()) + std::this_thread::yield_now(); // 4jcraft added now that we have + // portable thread yield. + // std::this_thread::sleep_for( + // std::chrono::milliseconds(0)); // 4J - was Thread.yield()) PIXEndNamedEvent(); // if (Keyboard::isKeyDown(Keyboard::KEY_F7)) From caf7128d5889999e75cbe314f66b314afd0939e9 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:14:53 -0500 Subject: [PATCH 7/9] refactor: remove `Sleep` from winapi_stubs --- Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h index 452562fad..a2f76c701 100644 --- a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h +++ b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h @@ -336,17 +336,6 @@ static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) { static inline DWORD GetLastError(VOID) { return errno; } -static inline VOID Sleep(DWORD dwMilliseconds) { - struct timespec ts; - ts.tv_nsec = (dwMilliseconds * 1000000) % 1000000000; - ts.tv_sec = dwMilliseconds / 1000; - - int ret; - do { - ret = nanosleep(&ts, &ts); - } while (ret == -1 && errno == EINTR); -} - static inline LONG64 InterlockedCompareExchangeRelease64( LONG64 volatile* Destination, LONG64 Exchange, LONG64 Comperand) { LONG64 expected = Comperand; From b598aa9232c11939e2a5b18681976be0b9385015 Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:20:18 -0500 Subject: [PATCH 8/9] fix: only use `InterlockedCompareExchangeRelease64` on LP64 systems --- Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h index a2f76c701..5aea5dc23 100644 --- a/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h +++ b/Minecraft.Client/Platform/Linux/Stubs/winapi_stubs.h @@ -336,6 +336,7 @@ static inline VOID GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer) { static inline DWORD GetLastError(VOID) { return errno; } +#ifdef __LP64__ static inline LONG64 InterlockedCompareExchangeRelease64( LONG64 volatile* Destination, LONG64 Exchange, LONG64 Comperand) { LONG64 expected = Comperand; @@ -343,6 +344,15 @@ static inline LONG64 InterlockedCompareExchangeRelease64( __ATOMIC_RELEASE, __ATOMIC_RELAXED); return expected; } +#else +static inline LONG64 InterlockedCompareExchangeRelease( + LONG volatile* Destination, LONG Exchange, LONG Comperand) { + LONG expected = Comperand; + __atomic_compare_exchange_n(Destination, &expected, Exchange, false, + __ATOMIC_RELEASE, __ATOMIC_RELAXED); + return expected; +} +#endif // internal helper: convert time_t to FILETIME (100ns intervals since // 1601-01-01) From cbca0280b4baf31e8b48ebb51f8911e218097b3b Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Wed, 25 Mar 2026 19:21:36 -0500 Subject: [PATCH 9/9] rust brainrot --- Minecraft.Client/Minecraft.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Minecraft.Client/Minecraft.cpp b/Minecraft.Client/Minecraft.cpp index dfd39cd8e..c1dcecf01 100644 --- a/Minecraft.Client/Minecraft.cpp +++ b/Minecraft.Client/Minecraft.cpp @@ -2144,8 +2144,8 @@ void Minecraft::run_middle() { achievementPopup->render(); PIXBeginNamedEvent(0, "Sleeping"); - std::this_thread::yield_now(); // 4jcraft added now that we have - // portable thread yield. + std::this_thread::yield(); // 4jcraft added now that we have + // portable thread yield. // std::this_thread::sleep_for( // std::chrono::milliseconds(0)); // 4J - was Thread.yield()) PIXEndNamedEvent();