Add timeout to WaitForAll

This commit is contained in:
Lion Kortlepel 2021-12-05 01:09:41 +01:00
parent 86169ad0fa
commit a289d0e872
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 10 additions and 3 deletions

View File

@ -105,7 +105,8 @@ public:
return LuaEventsCount - GetLuaStateCount(); return LuaEventsCount - GetLuaStateCount();
} }
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results); static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results,
const std::chrono::high_resolution_clock::duration& Max = std::chrono::hours(std::numeric_limits<size_t>().max()));
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results); void ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results);
bool HasState(TLuaStateId StateId); bool HasState(TLuaStateId StateId);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script); [[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);

View File

@ -145,10 +145,16 @@ TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) {
return ""; return "";
} }
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results) { void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::chrono::high_resolution_clock::duration& max) {
size_t ms = 0;
bool Cancelled = false;
for (const auto& Result : Results) { for (const auto& Result : Results) {
while (!Result->Ready) { while (!Result->Ready && !Cancelled) {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
ms += 10;
if (std::chrono::milliseconds(ms) > max) {
Cancelled = true;
}
} }
if (Result->Error) { if (Result->Error) {
if (Result->ErrorMessage != BeamMPFnNotFoundError) { if (Result->ErrorMessage != BeamMPFnNotFoundError) {