diff --git a/Minecraft-Community-Edition/Core/CoroutineSchedular.cpp b/Minecraft-Community-Edition/Core/CoroutineSchedular.cpp deleted file mode 100644 index 54df4b3..0000000 --- a/Minecraft-Community-Edition/Core/CoroutineSchedular.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "CoroutineSchedular.hpp" - -namespace mce{ - CoroutineSchedular::~CoroutineSchedular() { - CoroutineSchedular::vecTasks.clear(); - CoroutineSchedular::vecTasks.shrink_to_fit(); - } - void CoroutineSchedular::processCoroutineSchedular() { - for (auto& task : CoroutineSchedular::vecTasks) { - if (task.clock.getElapsedTime() >= task.interval) { - task.interval = task.callback(); - task.clock.restart(); - } - } - } -} diff --git a/Minecraft-Community-Edition/Core/CoroutineScheduler.cpp b/Minecraft-Community-Edition/Core/CoroutineScheduler.cpp new file mode 100644 index 0000000..da63c74 --- /dev/null +++ b/Minecraft-Community-Edition/Core/CoroutineScheduler.cpp @@ -0,0 +1,29 @@ +#include "CoroutineScheduler.hpp" + +namespace mce{ + CoroutineScheduler::~CoroutineScheduler() { + std::unique_lock lock(tasksMutex); + CoroutineScheduler::vecTasks.clear(); + CoroutineScheduler::vecTasks.shrink_to_fit(); + } + + void CoroutineScheduler::addTask(Callback cb) { + Task task; + task.callback = cb; + task.interval = cb(); + task.clock.restart(); + + std::unique_lock lock(tasksMutex); + CoroutineScheduler::vecTasks.push_back(task); + } + + void CoroutineScheduler::processTasks() { + std::unique_lock lock(tasksMutex); + for (auto&[callback, interval, taskClock] : CoroutineScheduler::vecTasks) { + if (taskClock.getElapsedTime() >= interval) { + interval = callback(); + taskClock.restart(); + } + } + } +} diff --git a/Minecraft-Community-Edition/Core/CoroutineSchedular.hpp b/Minecraft-Community-Edition/Core/CoroutineScheduler.hpp similarity index 82% rename from Minecraft-Community-Edition/Core/CoroutineSchedular.hpp rename to Minecraft-Community-Edition/Core/CoroutineScheduler.hpp index e5a03a5..1447853 100644 --- a/Minecraft-Community-Edition/Core/CoroutineSchedular.hpp +++ b/Minecraft-Community-Edition/Core/CoroutineScheduler.hpp @@ -1,88 +1,84 @@ -#pragma once -#include -#include -#include -#include - -/** - * @file CoroutineSchedular.hpp - * @brief Simple coroutine scheduler that runs periodic callbacks using SFML time utilities. - */ - -namespace mce { -/** - * @brief Coroutine scheduler that manages periodic tasks. - * - * The scheduler stores tasks that each have their own clock and interval. Each - * task's callback should return an sf::Time representing the time to wait - * until the next invocation. When a task's clock has elapsed at least its - * interval the callback is invoked and the returned sf::Time is used as the - * new interval. - * - * example usage: - * - * addTask([]() -> sf::Time { - * - * return sf::seconds(1); - * - * } - */ - class CoroutineSchedular { - public: - /** - * @brief Callback type invoked by scheduled tasks. - * - * The callback should perform the task work and return an sf::Time that - * indicates how long to wait until the next invocation of this callback. - */ - using Callback = eastl::function; - - /** - * @brief Default constructor. - */ - CoroutineSchedular() = default; - - ~CoroutineSchedular(); - - /** - * @brief Represents a single scheduled task. - * - * Holds the callback to invoke, the next execution interval, and a clock - * used to measure elapsed time since the last invocation. - */ - struct Task { - Callback callback; /**< Task callback invoked when interval elapses. */ - sf::Time interval; /**< Time interval until the next invocation. */ - sf::Clock clock; /**< Clock used to measure elapsed time for this task. */ - }; - - /** - * @brief Add a task to the scheduler. - * @param cb The callback to schedule. The callback is invoked immediately to obtain the initial interval. - * - * The callback should return an sf::Time specifying how long to wait until - * the next invocation. The task's clock is restarted when added. - */ - void addTask(Callback cb) { - Task task; - task.callback = cb; - task.interval = cb(); - task.clock.restart(); - CoroutineSchedular::vecTasks.push_back(task); - } - - /** - * @brief Process all scheduled tasks. - * - * This should be called periodically (for example once per frame). For - * each task if its clock has elapsed at least its interval, the task's - * callback is invoked, the returned sf::Time is used as the new interval, - * and the task clock is restarted. - */ - void processCoroutineSchedular(); - private: - eastl::vector vecTasks; /**< Container of scheduled tasks. */ - sf::Clock clock; /**< Global clock (reserved for future use). */ - }; -} - +#pragma once +#include +#include +#include +#include +#include + +/** + * @file CoroutineScheduler.hpp + * @brief Simple coroutine scheduler that runs periodic callbacks using SFML time utilities. + */ + +namespace mce { +/** + * @brief Coroutine scheduler that manages periodic tasks. + * + * The scheduler stores tasks that each have their own clock and interval. Each + * task's callback should return an sf::Time representing the time to wait + * until the next invocation. When a task's clock has elapsed at least its + * interval the callback is invoked and the returned sf::Time is used as the + * new interval. + * + * example usage: + * + * addTask([]() -> sf::Time { + * + * return sf::seconds(1); + * + * } + */ + class CoroutineScheduler { + public: + /** + * @brief Callback type invoked by scheduled tasks. + * + * @returns An sf::Time that + * indicates how long to wait until the next invocation of this callback. + */ + using Callback = eastl::function; + + /** + * @brief Default constructor. + */ + CoroutineScheduler() = default; + ~CoroutineScheduler(); + + /** + * @brief Represents a single scheduled task. + * + * Holds the callback to invoke, the next execution interval, and a clock + * used to measure elapsed time since the last invocation. + */ + struct Task { + Callback callback; /**< Task callback invoked when interval elapses. */ + sf::Time interval; /**< Time interval until the next invocation. */ + sf::Clock clock; /**< Clock used to measure elapsed time for this task. */ + }; + + /** + * @brief Add a task to the scheduler. + * @param cb The callback to schedule. The callback is invoked immediately to obtain the initial interval. + * + * The callback should return an sf::Time specifying how long to wait until + * the next invocation. The task's clock is restarted when added. + */ + void addTask(Callback cb); + + /** + * @brief Process all scheduled tasks. + * + * This should be called periodically (for example once per frame). For + * each task if its clock has elapsed at least its interval, the task's + * callback is invoked, the returned sf::Time is used as the new interval, + * and the task clock is restarted. + */ + void processTasks(); + private: + std::mutex tasksMutex; + eastl::vector vecTasks; /**< Container of scheduled tasks. */ + + sf::Clock clock; /**< Global clock (reserved for future use). */ + }; +} +