mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
Use yield() where possible
Replaced calls of this_thread::sleep_* with this_thread::yield(), which yields the thread to the OS' scheduler.
This commit is contained in:
parent
29f8d29e33
commit
754053e73f
@ -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<TLuaResult>& Result);
|
||||
|
||||
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
|
||||
|
||||
|
@ -386,7 +386,7 @@ TConsole::TConsole() {
|
||||
} else {
|
||||
auto Future = mLuaEngine->EnqueueScript(mStateId, { std::make_shared<std::string>(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);
|
||||
|
@ -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<TLuaResult>& Result) {
|
||||
std::unique_lock Lock(mResultsToCheckMutex);
|
||||
mResultsToCheck.push(Result);
|
||||
}
|
||||
|
||||
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& 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");
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user