Check all futures

This commit is contained in:
Lion Kortlepel
2021-10-01 03:27:24 +02:00
parent 932fbe2b2f
commit 243e96d503
4 changed files with 28 additions and 18 deletions

View File

@@ -45,15 +45,14 @@ void TLuaEngine::operator()() {
beammp_lua_error("Calling \"onInit\" on \"" + Future->StateId + "\" failed: " + Future->ErrorMessage);
}
}
std::queue<std::shared_ptr<TLuaResult>> ResultsToCheck;
std::recursive_mutex ResultsToCheckMutex;
std::thread ResultCheckThread([&] {
auto ResultCheckThread = std::thread([&] {
while (!mShutdown) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::unique_lock Lock(ResultsToCheckMutex);
if (!ResultsToCheck.empty()) {
auto Res = ResultsToCheck.front();
ResultsToCheck.pop();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::unique_lock Lock(mResultsToCheckMutex);
if (!mResultsToCheck.empty()) {
auto Res = mResultsToCheck.front();
mResultsToCheck.pop();
Lock.unlock();
size_t Waited = 0;
@@ -81,10 +80,10 @@ void TLuaEngine::operator()() {
Timer.Reset();
auto Handlers = GetEventHandlersForState(Timer.EventName, Timer.StateId);
std::unique_lock StateLock(mLuaStatesMutex);
//std::unique_lock Lock2(ResultsToCheckMutex);
std::unique_lock Lock2(mResultsToCheckMutex);
for (auto& Handler : Handlers) {
auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCall(Handler, {});
//ResultsToCheck.push(Res);
mResultsToCheck.push(Res);
}
}
}
@@ -98,7 +97,7 @@ void TLuaEngine::operator()() {
}
Before = std::chrono::high_resolution_clock::now();
}
if (ResultCheckThread.joinable()) {
ResultCheckThread.join();
}
@@ -143,6 +142,14 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results) {
}
}
// run this on the error checking thread
void TLuaEngine::IgnoreIfNotError(const std::vector<std::shared_ptr<TLuaResult>>& Results) {
std::unique_lock Lock2(mResultsToCheckMutex);
for (const auto& Result : Results) {
mResultsToCheck.push(Result);
}
}
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script) {
std::unique_lock Lock(mLuaStatesMutex);
return mLuaStates.at(StateID)->EnqueueScript(Script);