Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Oct 12, 2023
1 parent 6413f07 commit a9d161f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
24 changes: 11 additions & 13 deletions src/game/scheduling/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
#include "game/scheduling/dispatcher.hpp"
#include "lib/thread/thread_pool.hpp"
#include "lib/di/container.hpp"
#include "utils/tools.hpp"

constexpr static auto ASYNC_TIME_OUT = std::chrono::seconds(15);
constexpr static auto SLEEP_TIME_MS = 15;

Dispatcher &Dispatcher::getInstance() {
return inject<Dispatcher>();
}

void Dispatcher::init() {
Task::TIME_NOW = std::chrono::system_clock::now();
updateClock();

threadPool.addLoad([this] {
std::unique_lock asyncLock(mutex);

while (!threadPool.getIoContext().stopped()) {
// Current Time Cache
Task::TIME_NOW = std::chrono::system_clock::now();
updateClock();

// Execute all asynchronous events separately by context
for (uint_fast8_t i = 0; i < static_cast<uint8_t>(AsyncEventContext::Last); ++i) {
Expand All @@ -43,12 +44,12 @@ void Dispatcher::init() {
// Merge all events that were created by events and scheduled events
mergeEvents();

std::this_thread::sleep_for(std::chrono::milliseconds(15));
sleep_for(SLEEP_TIME_MS);
}
});
}

void Dispatcher::addEvent(std::function<void(void)> &&f, std::string &&context, uint32_t expiresAfterMs) {
void Dispatcher::addEvent(std::function<void(void)> &&f, const std::string_view context, uint32_t expiresAfterMs) {
threads[getThreadId()].tasks.emplace_back(expiresAfterMs, f, context);
}

Expand All @@ -61,8 +62,8 @@ uint64_t Dispatcher::scheduleEvent(const std::shared_ptr<Task> &task) {
return scheduledtasksRef.emplace(task->generateId(), task).first->first;
}

uint64_t Dispatcher::scheduleEvent(uint32_t delay, std::function<void(void)> &&f, std::string &&context, bool cycle) {
const auto &task = std::make_shared<Task>(std::move(f), std::move(context), delay, cycle);
uint64_t Dispatcher::scheduleEvent(uint32_t delay, std::function<void(void)> &&f, const std::string_view context, bool cycle) {
const auto &task = std::make_shared<Task>(std::move(f), context, delay, cycle);
return scheduleEvent(task);
}

Expand Down Expand Up @@ -91,16 +92,15 @@ void Dispatcher::executeAsyncEvents(const uint8_t contextId, std::unique_lock<st
return;
}

const size_t sizeEventAsync = asyncTasks.size();
std::atomic_uint_fast64_t executedTasks = 0;

// Execute Async Task
for (const auto &task : asyncTasks) {
threadPool.addLoad([&] {
threadPool.addLoad([this, &task, &executedTasks, totalTaskSize = asyncTasks.size()] {
task.execute();

executedTasks.fetch_add(1);
if (executedTasks.load() == sizeEventAsync) {
if (executedTasks.load() == totalTaskSize) {
asyncTasks_cv.notify_one();
}
});
Expand All @@ -122,9 +122,7 @@ void Dispatcher::executeScheduledEvents() {
break;
}

task->execute();

if (!task->isCanceled() && task->isCycle()) {
if (task->execute() && task->isCycle()) {
task->updateTime();
scheduledtasks.emplace(task);
} else {
Expand Down
17 changes: 11 additions & 6 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class Dispatcher {
asyncTasks_cv.notify_all();
}

void addEvent(std::function<void(void)> &&f, std::string &&context, uint32_t expiresAfterMs = 0);
void addEvent(std::function<void(void)> &&f, const std::string_view context, uint32_t expiresAfterMs = 0);

Check warning on line 46 in src/game/scheduling/dispatcher.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/dispatcher.hpp#L46

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/dispatcher.hpp:46:Function parameter 'context' should be passed by const reference.
void addEvent_async(std::function<void(void)> &&f, AsyncEventContext context = AsyncEventContext::First);

uint64_t scheduleEvent(const std::shared_ptr<Task> &task);
uint64_t scheduleEvent(uint32_t delay, std::function<void(void)> &&f, std::string &&context) {
return scheduleEvent(delay, std::move(f), std::move(context), false);
uint64_t scheduleEvent(uint32_t delay, std::function<void(void)> &&f, const std::string_view context) {

Check warning on line 50 in src/game/scheduling/dispatcher.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/dispatcher.hpp#L50

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/dispatcher.hpp:50:Function parameter 'context' should be passed by const reference.
return scheduleEvent(delay, std::move(f), context, false);
}
uint64_t cycleEvent(uint32_t delay, std::function<void(void)> &&f, std::string &&context) {
return scheduleEvent(delay, std::move(f), std::move(context), true);
uint64_t cycleEvent(uint32_t delay, std::function<void(void)> &&f, const std::string_view context) {

Check warning on line 53 in src/game/scheduling/dispatcher.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/dispatcher.hpp#L53

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/dispatcher.hpp:53:Function parameter 'context' should be passed by const reference.
return scheduleEvent(delay, std::move(f), context, true);
}

[[nodiscard]] uint64_t getDispatcherCycle() const {
Expand All @@ -61,6 +61,11 @@ class Dispatcher {
void stopEvent(uint64_t eventId);

private:
// Update Time Cache
static void updateClock() {
Task::TIME_NOW = std::chrono::system_clock::now();
}

static int16_t getThreadId() {
static std::atomic_int16_t lastId = -1;
thread_local static int16_t id = -1;
Expand All @@ -73,7 +78,7 @@ class Dispatcher {
return id;
};

uint64_t scheduleEvent(uint32_t delay, std::function<void(void)> &&f, std::string &&context, bool cycle);
uint64_t scheduleEvent(uint32_t delay, std::function<void(void)> &&f, const std::string_view context, bool cycle);

Check warning on line 81 in src/game/scheduling/dispatcher.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/dispatcher.hpp#L81

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/dispatcher.hpp:81:Function parameter 'context' should be passed by const reference.

inline void mergeEvents();
inline void executeEvents();
Expand Down
8 changes: 4 additions & 4 deletions src/game/scheduling/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class Task {
public:
static std::chrono::system_clock::time_point TIME_NOW;

Task(uint32_t expiresAfterMs, std::function<void(void)> &f, std::string context) :
Task(uint32_t expiresAfterMs, std::function<void(void)> &f, const std::string_view context) :

Check warning on line 19 in src/game/scheduling/task.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/task.hpp#L19

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/task.hpp:19:Function parameter 'context' should be passed by const reference.
expiration(expiresAfterMs > 0 ? TIME_NOW + std::chrono::milliseconds(expiresAfterMs) : SYSTEM_TIME_ZERO),
context(std::move(context)), func(std::move(f)) {
context(std::string(context)), func(std::move(f)) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Task(std::function<void(void)> &&f, std::string context, uint32_t delay, bool cycle = false) :
delay(delay), utime(TIME_NOW + std::chrono::milliseconds(delay)), cycle(cycle), context(std::move(context)), func(std::move(f)) {
Task(std::function<void(void)> &&f, const std::string_view context, uint32_t delay, bool cycle = false) :

Check warning on line 25 in src/game/scheduling/task.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/game/scheduling/task.hpp#L25

Function parameter 'context' should be passed by const reference.
Raw output
src/game/scheduling/task.hpp:25:Function parameter 'context' should be passed by const reference.
cycle(cycle), delay(delay), utime(TIME_NOW + std::chrono::milliseconds(delay)), context(std::string(context)), func(std::move(f)) {
assert(!this->context.empty() && "Context cannot be empty!");
}

Expand Down
5 changes: 4 additions & 1 deletion src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,6 @@ AmmoTypeNames ammoTypeNames = {
{ "throwingknife", AMMO_THROWINGKNIFE },
{ "diamondarrow", AMMO_ARROW },
{ "spectralbolt", AMMO_BOLT },

};

WeaponActionNames weaponActionNames = {
Expand Down Expand Up @@ -1795,3 +1794,7 @@ std::string formatNumber(uint64_t number) {
}
return formattedNumber;
}

void sleep_for(uint64_t ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
1 change: 1 addition & 0 deletions src/utils/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ ItemAttribute_t stringToItemAttribute(const std::string &str);

const char* getReturnMessage(ReturnValue value);

void sleep_for(uint64_t ms);
void capitalizeWords(std::string &source);
void consoleHandlerExit();
std::string validateNameHouse(const std::string &name);
Expand Down

0 comments on commit a9d161f

Please sign in to comment.