Skip to content

Commit

Permalink
ThreadPool::getThreadId
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Oct 23, 2023
1 parent bb584a0 commit eab27bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/game/scheduling/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void Dispatcher::executeEvents(std::unique_lock<std::mutex> &asyncLock) {
}

void Dispatcher::executeScheduledEvents() {
auto &threadScheduledTasks = threads[getThreadId()]->scheduledTasks;
auto &threadScheduledTasks = getThreadTask()->scheduledTasks;

auto it = scheduledTasks.begin();
while (it != scheduledTasks.end()) {
Expand Down Expand Up @@ -163,14 +163,14 @@ std::chrono::nanoseconds Dispatcher::timeUntilNextScheduledTask() const {
}

void Dispatcher::addEvent(std::function<void(void)> &&f, std::string_view context, uint32_t expiresAfterMs) {
const auto &thread = threads[getThreadId()];
const auto &thread = getThreadTask();
std::scoped_lock lock(thread->mutex);
thread->tasks[static_cast<uint8_t>(TaskGroup::Serial)].emplace_back(expiresAfterMs, std::move(f), context);
notify();
}

uint64_t Dispatcher::scheduleEvent(const std::shared_ptr<Task> &task) {
const auto &thread = threads[getThreadId()];
const auto &thread = getThreadTask();
std::scoped_lock lock(thread->mutex);

auto eventId = scheduledTasksRef
Expand All @@ -182,7 +182,7 @@ uint64_t Dispatcher::scheduleEvent(const std::shared_ptr<Task> &task) {
}

void Dispatcher::asyncEvent(std::function<void(void)> &&f, TaskGroup group) {
const auto &thread = threads[getThreadId()];
const auto &thread = getThreadTask();
std::scoped_lock lock(thread->mutex);
thread->tasks[static_cast<uint8_t>(group)].emplace_back(0, std::move(f), dispacherContext.taskName);
notify();
Expand Down
14 changes: 3 additions & 11 deletions src/game/scheduling/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,9 @@ class Dispatcher {
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;

if (id == -1) {
lastId.fetch_add(1);
id = lastId.load();
}

return id;
};
const auto& getThreadTask() const {
return threads[ThreadPool::getThreadId()];
}

uint64_t scheduleEvent(uint32_t delay, std::function<void(void)> &&f, std::string_view context, bool cycle, bool log = true) {
return scheduleEvent(std::make_shared<Task>(std::move(f), context, delay, cycle, log));
Expand Down
12 changes: 12 additions & 0 deletions src/lib/thread/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class ThreadPool {
return nThreads;
}

static int16_t getThreadId() {
static std::atomic_int16_t lastId = -1;
thread_local static int16_t id = -1;

if (id == -1) {
lastId.fetch_add(1);
id = lastId.load();
}

return id;
};

private:
Logger &logger;
asio::io_context ioService;
Expand Down

0 comments on commit eab27bb

Please sign in to comment.