From 8630b586591723abe028b304342fa875cce743e9 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 20 Nov 2023 13:34:48 -0300 Subject: [PATCH 1/2] init --- src/game/scheduling/dispatcher.cpp | 16 ++++++++-------- src/game/scheduling/dispatcher.hpp | 9 ++------- src/game/scheduling/task.cpp | 2 -- src/game/scheduling/task.hpp | 16 ++++++---------- src/utils/tools.cpp | 9 +++++++-- src/utils/tools.hpp | 3 ++- 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/game/scheduling/dispatcher.cpp b/src/game/scheduling/dispatcher.cpp index a24ef770466..fff897def89 100644 --- a/src/game/scheduling/dispatcher.cpp +++ b/src/game/scheduling/dispatcher.cpp @@ -21,13 +21,13 @@ Dispatcher &Dispatcher::getInstance() { } void Dispatcher::init() { - updateClock(); + UPDATE_OTSYS_TIME(); threadPool.addLoad([this] { std::unique_lock asyncLock(dummyMutex); while (!threadPool.getIoContext().stopped()) { - updateClock(); + UPDATE_OTSYS_TIME(); executeEvents(); executeScheduledEvents(); @@ -104,7 +104,7 @@ void Dispatcher::executeScheduledEvents() { auto it = scheduledTasks.begin(); while (it != scheduledTasks.end()) { const auto &task = *it; - if (task->getTime() > Task::TIME_NOW) { + if (task->getTime() > OTSYS_TIME()) { break; } @@ -168,17 +168,17 @@ void Dispatcher::mergeEvents() { checkPendingTasks(); } -std::chrono::nanoseconds Dispatcher::timeUntilNextScheduledTask() const { - static constexpr auto CHRONO_NANO_0 = std::chrono::nanoseconds(0); - static constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max(); +std::chrono::milliseconds Dispatcher::timeUntilNextScheduledTask() const { + constexpr auto CHRONO_0 = std::chrono::milliseconds(0); + constexpr auto CHRONO_MILI_MAX = std::chrono::milliseconds::max(); if (scheduledTasks.empty()) { return CHRONO_MILI_MAX; } const auto &task = *scheduledTasks.begin(); - const auto timeRemaining = task->getTime() - Task::TIME_NOW; - return std::max(timeRemaining, CHRONO_NANO_0); + const auto timeRemaining = std::chrono::milliseconds(task->getTime() - OTSYS_TIME()); + return std::max(timeRemaining, CHRONO_0); } void Dispatcher::addEvent(std::function &&f, std::string_view context, uint32_t expiresAfterMs) { diff --git a/src/game/scheduling/dispatcher.hpp b/src/game/scheduling/dispatcher.hpp index 9d0a5c51750..1c5f72658c1 100644 --- a/src/game/scheduling/dispatcher.hpp +++ b/src/game/scheduling/dispatcher.hpp @@ -32,7 +32,7 @@ enum class DispatcherType : uint8_t { struct DispatcherContext { bool isOn() const { - return Task::TIME_NOW != SYSTEM_TIME_ZERO; + return OTSYS_TIME() != 0; } bool isGroup(const TaskGroup _group) const { @@ -134,11 +134,6 @@ class Dispatcher { private: thread_local static DispatcherContext dispacherContext; - // Update Time Cache - static void updateClock() { - Task::TIME_NOW = std::chrono::system_clock::now(); - } - const auto &getThreadTask() const { return threads[ThreadPool::getThreadId()]; } @@ -159,7 +154,7 @@ class Dispatcher { inline void executeSerialEvents(std::vector &tasks); inline void executeParallelEvents(std::vector &tasks, const uint8_t groupId); - inline std::chrono::nanoseconds timeUntilNextScheduledTask() const; + inline std::chrono::milliseconds timeUntilNextScheduledTask() const; inline void checkPendingTasks() { hasPendingTasks = false; diff --git a/src/game/scheduling/task.cpp b/src/game/scheduling/task.cpp index c9e6157ab9e..28418a4a169 100644 --- a/src/game/scheduling/task.cpp +++ b/src/game/scheduling/task.cpp @@ -10,8 +10,6 @@ #include "pch.hpp" #include "task.hpp" #include "lib/logging/log_with_spd_log.hpp" - -std::chrono::system_clock::time_point Task::TIME_NOW = SYSTEM_TIME_ZERO; std::atomic_uint_fast64_t Task::LAST_EVENT_ID = 0; bool Task::execute() const { diff --git a/src/game/scheduling/task.hpp b/src/game/scheduling/task.hpp index f42602242c5..18dfd5d39de 100644 --- a/src/game/scheduling/task.hpp +++ b/src/game/scheduling/task.hpp @@ -11,19 +11,15 @@ #include "utils/tools.hpp" #include -static constexpr auto SYSTEM_TIME_ZERO = std::chrono::system_clock::time_point(std::chrono::milliseconds(0)); - class Task { public: - static std::chrono::system_clock::time_point TIME_NOW; - Task(uint32_t expiresAfterMs, std::function &&f, std::string_view context) : - func(std::move(f)), context(context), utime(TIME_NOW), expiration(expiresAfterMs > 0 ? TIME_NOW + std::chrono::milliseconds(expiresAfterMs) : SYSTEM_TIME_ZERO) { + func(std::move(f)), context(context), utime(OTSYS_TIME()), expiration(expiresAfterMs > 0 ? OTSYS_TIME() + expiresAfterMs : 0) { assert(!this->context.empty() && "Context cannot be empty!"); } Task(std::function &&f, std::string_view context, uint32_t delay, bool cycle = false, bool log = true) : - func(std::move(f)), context(context), utime(TIME_NOW + std::chrono::milliseconds(delay)), delay(delay), cycle(cycle), log(log) { + func(std::move(f)), context(context), utime(OTSYS_TIME() + delay), delay(delay), cycle(cycle), log(log) { assert(!this->context.empty() && "Context cannot be empty!"); } @@ -54,7 +50,7 @@ class Task { } bool hasExpired() const { - return expiration != SYSTEM_TIME_ZERO && expiration < TIME_NOW; + return expiration != 0 && expiration < OTSYS_TIME(); } bool isCycle() const { @@ -75,7 +71,7 @@ class Task { static std::atomic_uint_fast64_t LAST_EVENT_ID; void updateTime() { - utime = TIME_NOW + std::chrono::milliseconds(delay); + utime = OTSYS_TIME() + delay; } bool hasTraceableContext() const { @@ -117,8 +113,8 @@ class Task { std::function func = nullptr; std::string_view context; - std::chrono::system_clock::time_point utime = SYSTEM_TIME_ZERO; - std::chrono::system_clock::time_point expiration = SYSTEM_TIME_ZERO; + int64_t utime = 0; + int64_t expiration = 0; uint64_t id = 0; uint32_t delay = 0; diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index a4b9f8d31db..708dfe19510 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -1454,8 +1454,13 @@ const char* getReturnMessage(ReturnValue value) { } } -int64_t OTSYS_TIME() { - return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); +int64_t OTSYSTIME = 0; +void UPDATE_OTSYS_TIME() { + OTSYSTIME = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); +} + +constexpr int64_t OTSYS_TIME() { + return OTSYSTIME; } SpellGroup_t stringToSpellGroup(const std::string &value) { diff --git a/src/utils/tools.hpp b/src/utils/tools.hpp index 099f77afc67..076c1d1ef99 100644 --- a/src/utils/tools.hpp +++ b/src/utils/tools.hpp @@ -134,7 +134,8 @@ bool isCaskItem(uint16_t itemId); std::string getObjectCategoryName(ObjectCategory_t category); -int64_t OTSYS_TIME(); +constexpr int64_t OTSYS_TIME(); +void UPDATE_OTSYS_TIME(); SpellGroup_t stringToSpellGroup(const std::string &value); From 35ffad9597c72dd6ae0eb8cd51613d6b35f61582 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 20 Nov 2023 13:48:21 -0300 Subject: [PATCH 2/2] removed constexpr --- src/utils/tools.cpp | 2 +- src/utils/tools.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index 708dfe19510..ce42c04ad50 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -1459,7 +1459,7 @@ void UPDATE_OTSYS_TIME() { OTSYSTIME = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); } -constexpr int64_t OTSYS_TIME() { +int64_t OTSYS_TIME() { return OTSYSTIME; } diff --git a/src/utils/tools.hpp b/src/utils/tools.hpp index 076c1d1ef99..580a962c9e4 100644 --- a/src/utils/tools.hpp +++ b/src/utils/tools.hpp @@ -134,7 +134,7 @@ bool isCaskItem(uint16_t itemId); std::string getObjectCategoryName(ObjectCategory_t category); -constexpr int64_t OTSYS_TIME(); +int64_t OTSYS_TIME(); void UPDATE_OTSYS_TIME(); SpellGroup_t stringToSpellGroup(const std::string &value);