TLuaEngine: Make WaitForAll timeout optional

This commit is contained in:
Lion Kortlepel
2021-12-05 01:32:15 +01:00
parent 672c7d02d1
commit 479bb9f931
3 changed files with 11 additions and 8 deletions
+1 -1
View File
@@ -106,7 +106,7 @@ public:
} }
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())); const std::optional<std::chrono::high_resolution_clock::duration>& Max = std::nullopt);
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);
+2 -2
View File
@@ -265,7 +265,7 @@ void TConsole::Command_Status(const std::string&) {
void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) { void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) {
auto FutureIsNonNil = auto FutureIsNonNil =
[](const std::shared_ptr<TLuaResult>& Future) { [](const std::shared_ptr<TLuaResult>& Future) {
if (!Future->Error) { if (!Future->Error && Future->Result.valid()) {
auto Type = Future->Result.get_type(); auto Type = Future->Result.get_type();
return Type != sol::type::lua_nil && Type != sol::type::none; return Type != sol::type::lua_nil && Type != sol::type::none;
} }
@@ -274,7 +274,7 @@ void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) {
std::vector<std::shared_ptr<TLuaResult>> NonNilFutures; std::vector<std::shared_ptr<TLuaResult>> NonNilFutures;
{ // Futures scope { // Futures scope
auto Futures = mLuaEngine->TriggerEvent("onConsoleInput", "", cmd); auto Futures = mLuaEngine->TriggerEvent("onConsoleInput", "", cmd);
TLuaEngine::WaitForAll(Futures); TLuaEngine::WaitForAll(Futures, std::chrono::seconds(5));
size_t Count = 0; size_t Count = 0;
for (auto& Future : Futures) { for (auto& Future : Futures) {
if (!Future->Error) { if (!Future->Error) {
+8 -5
View File
@@ -145,18 +145,21 @@ TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) {
return ""; return "";
} }
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::chrono::high_resolution_clock::duration& max) { void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& Max) {
size_t ms = 0;
bool Cancelled = false;
for (const auto& Result : Results) { for (const auto& Result : Results) {
bool Cancelled = false;
size_t ms = 0;
while (!Result->Ready && !Cancelled) { while (!Result->Ready && !Cancelled) {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
ms += 10; ms += 10;
if (std::chrono::milliseconds(ms) > max) { if (Max.has_value() && std::chrono::milliseconds(ms) > Max.value()) {
beammp_trace("'" + Result->Function + "' in '" + Result->StateId + "' did not finish executing in time (took: " + std::to_string(ms) + "ms)");
Cancelled = true; Cancelled = true;
} }
} }
if (Result->Error) { if (Cancelled) {
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time.");
} else if (Result->Error) {
if (Result->ErrorMessage != BeamMPFnNotFoundError) { if (Result->ErrorMessage != BeamMPFnNotFoundError) {
beammp_lua_error(Result->Function + ": " + Result->ErrorMessage); beammp_lua_error(Result->Function + ": " + Result->ErrorMessage);
} }