diff --git a/emper/Actor.hpp b/emper/Actor.hpp index f2b1d57baeb154369acf2a324edb4accf5d848eb..3b95c0d673c0b0c5075eb5478eda41eee10030f0 100644 --- a/emper/Actor.hpp +++ b/emper/Actor.hpp @@ -8,6 +8,7 @@ #include "CallerEnvironment.hpp" #include "Fiber.hpp" #include "UnboundedBlockingMpscQueue.hpp" +#include "io/Future.hpp" template <typename T> class Actor { @@ -77,17 +78,30 @@ class Actor { auto pendingMailboxItems() -> size_t { return queue.size(); } auto waitUntilIdle(long timeout) -> bool { - const auto start = std::chrono::steady_clock::now(); - const auto deadline = start + std::chrono::milliseconds(timeout); - while (!(queue.size() == 0 && state.load(std::memory_order_acquire) == Retrieving)) { - // TODO: The suppressed linter error below may be a false positive - // reported by clang-tidy. - // NOLINTNEXTLINE(modernize-use-nullptr) - if (std::chrono::steady_clock::now() > deadline) { - return false; + if constexpr (emper::IO) { + for (; timeout > 0; --timeout) { + emper::io::AlarmFuture::Timespec ts = {.tv_sec = 1, .tv_nsec = 0}; + emper::io::AlarmFuture alarm(ts); + alarm.submitAndWait(); + if (queue.size() == 0 && state.load(std::memory_order_acquire) == Retrieving) { + return true; + } } - } - return true; + return false; + } else { + const auto start = std::chrono::steady_clock::now(); + const auto deadline = start + std::chrono::seconds(timeout); + while (!(queue.size() == 0 && state.load(std::memory_order_acquire) == Retrieving)) { + // TODO: The suppressed linter error below may be a false positive + // reported by clang-tidy. + // NOLINTNEXTLINE(modernize-use-nullptr) + if (std::chrono::steady_clock::now() > deadline) { + return false; + } + } + + return true; + } } }; diff --git a/tests/AlarmActorTest.cpp b/tests/AlarmActorTest.cpp index a0f962b60f994a2699a842b385ef24639a18c903..31c9c46a4c922817acb595a13d40598e704ca40f 100644 --- a/tests/AlarmActorTest.cpp +++ b/tests/AlarmActorTest.cpp @@ -61,7 +61,7 @@ auto main(int argc, char* argv[]) -> int { cps.wait(); // Wait for the actor to become idle. - bool actorIdle = alarmActor.waitUntilIdle(60 * 1000); + bool actorIdle = alarmActor.waitUntilIdle(60); if (!actorIdle) { std::cerr << "FAILURE: Actor did not went idle"; exit(EXIT_FAILURE); diff --git a/tests/SimpleActorTest.cpp b/tests/SimpleActorTest.cpp index 3415863c08a9332fbe78fc7eda651f96ecbfaa27..76b4cfe3d08b717531c400ef348366367378c39f 100644 --- a/tests/SimpleActorTest.cpp +++ b/tests/SimpleActorTest.cpp @@ -63,7 +63,7 @@ static void mainFiber(void* runtime_ptr) { cps.wait(); // Wait for the actor to become idle. - bool actorIdle = sumActor.waitUntilIdle(60 * 1000); + bool actorIdle = sumActor.waitUntilIdle(60); if (!actorIdle) { std::cerr << "FAILURE: Actor did not went idle"; exit(EXIT_FAILURE); diff --git a/tests/UnblockOnMainActorTest.cpp b/tests/UnblockOnMainActorTest.cpp index 020db28f5dc8242054a729fb65b1613676f14a5d..6ae3883f27974b786f8df82b848c03096b375e5d 100644 --- a/tests/UnblockOnMainActorTest.cpp +++ b/tests/UnblockOnMainActorTest.cpp @@ -76,7 +76,7 @@ auto main(int argc, char* argv[]) -> int { cps.wait(); // Wait for the actor to become idle. - bool actorIdle = alarmActor.waitUntilIdle(60 * 1000); + bool actorIdle = alarmActor.waitUntilIdle(60); if (!actorIdle) { std::cerr << "FAILURE: Actor did not went idle"; quit = true;