From 754053e73fc39573d17a82e17b59ca25f23f171e Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 3 Feb 2022 18:57:52 +0100 Subject: [PATCH] Use yield() where possible Replaced calls of this_thread::sleep_* with this_thread::yield(), which yields the thread to the OS' scheduler. --- include/TLuaEngine.h | 1 + src/TConsole.cpp | 2 +- src/TLuaEngine.cpp | 34 ++++++++++++++-------------------- src/TPPSMonitor.cpp | 4 ++-- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 1673c61..9b0a884 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -149,6 +149,7 @@ public: void CancelEventTimers(const std::string& EventName, TLuaStateId StateId); sol::state_view GetStateForPlugin(const fs::path& PluginPath); TLuaStateId GetStateIDForPlugin(const fs::path& PluginPath); + void AddResultToCheck(const std::shared_ptr& Result); static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND"; diff --git a/src/TConsole.cpp b/src/TConsole.cpp index 79df8d2..827881c 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -386,7 +386,7 @@ TConsole::TConsole() { } else { auto Future = mLuaEngine->EnqueueScript(mStateId, { std::make_shared(cmd), "", "" }); while (!Future->Ready) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); // TODO: Add a timeout + std::this_thread::yield(); // TODO: Add a timeout } if (Future->Error) { beammp_lua_error(Future->ErrorMessage); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 89742e2..41ffb96 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -54,25 +54,17 @@ void TLuaEngine::operator()() { auto ResultCheckThread = std::thread([&] { RegisterThread("ResultCheckThread"); while (!mShutdown) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::unique_lock Lock(mResultsToCheckMutex); if (!mResultsToCheck.empty()) { auto Res = mResultsToCheck.front(); mResultsToCheck.pop(); Lock.unlock(); - size_t Waited = 0; - while (!Res->Ready) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - Waited++; - if (Waited > 250) { - // FIXME: This should *eventually* timeout. - // beammp_lua_error(Res->Function + " in " + Res->StateId + " took >1s to respond, not printing possible errors"); - Lock.lock(); - mResultsToCheck.push(Res); - Lock.unlock(); - break; - } + if (!Res->Ready) { + Lock.lock(); + mResultsToCheck.push(Res); + Lock.unlock(); } if (Res->Error) { if (Res->ErrorMessage != BeamMPFnNotFoundError) { @@ -80,13 +72,14 @@ void TLuaEngine::operator()() { } } } + std::this_thread::yield(); } }); // event loop auto Before = std::chrono::high_resolution_clock::now(); while (!mShutdown) { if (mLuaStates.size() == 0) { - std::this_thread::sleep_for(std::chrono::seconds(500)); + std::this_thread::sleep_for(std::chrono::seconds(100)); } { // Timed Events Scope std::unique_lock Lock(mTimedEventsMutex); @@ -149,6 +142,11 @@ TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) { return ""; } +void TLuaEngine::AddResultToCheck(const std::shared_ptr& Result) { + std::unique_lock Lock(mResultsToCheckMutex); + mResultsToCheck.push(Result); +} + void TLuaEngine::WaitForAll(std::vector>& Results, const std::optional& Max) { for (const auto& Result : Results) { bool Cancelled = false; @@ -705,6 +703,7 @@ void TLuaEngine::StateThreadData::AddPath(const fs::path& Path) { void TLuaResult::WaitUntilReady() { while (!Ready) { + std::this_thread::yield(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } @@ -762,12 +761,7 @@ void TPluginMonitor::operator()() { auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path()); auto Res = mEngine.EnqueueScript(StateID, Chunk); // TODO: call onInit - while (!Res->Ready) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } - if (Res->Error) { - beammp_lua_error(Res->ErrorMessage); - } + mEngine.AddResultToCheck(Res); } else { // TODO: trigger onFileChanged event beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet"); diff --git a/src/TPPSMonitor.cpp b/src/TPPSMonitor.cpp index 8d58d84..b5b4951 100644 --- a/src/TPPSMonitor.cpp +++ b/src/TPPSMonitor.cpp @@ -21,8 +21,8 @@ TPPSMonitor::TPPSMonitor(TServer& Server) void TPPSMonitor::operator()() { RegisterThread("PPSMonitor"); while (!mNetwork) { - // hard spi - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + // hard(-ish) spin + std::this_thread::yield(); } beammp_debug("PPSMonitor starting"); Application::SetSubsystemStatus("PPSMonitor", Application::Status::Good);