Skip to content

Commit

Permalink
fix: added condition argument to wait calls in threadMain functions (o…
Browse files Browse the repository at this point in the history
…pentibiabr#790)

This addresses the issues reported by SonarCloud regarding the use of wait() without a condition argument in several threadMain functions. The changes made include the addition of a condition argument to the wait() calls in the threadMain functions in the DatabaseTasks, Scheduler and other classes. These changes ensure that the threads will not wait indefinitely and will only wake up when there is a task to be executed, thus avoiding any potential race conditions and deadlocks.
  • Loading branch information
dudantas authored Jan 18, 2023
1 parent bdd4b09 commit a870e01
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/database/databasetasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ void DatabaseTasks::threadMain()
if (flushTasks) {
flushSignal.notify_one();
}
taskSignal.wait(taskLockUnique);
taskSignal.wait(taskLockUnique, [this] {
return !tasks.empty();
});
}

if (!tasks.empty()) {
Expand Down Expand Up @@ -102,7 +104,9 @@ void DatabaseTasks::flush()
std::unique_lock<std::mutex> guard{ taskLock };
if (!tasks.empty()) {
flushTasks = true;
flushSignal.wait(guard);
flushSignal.wait(guard, [this] {
return !flushTasks;
});
flushTasks = false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/game/scheduling/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ void Scheduler::threadMain()

eventLockUnique.lock();
if (eventList.empty()) {
eventSignal.wait(eventLockUnique);
eventSignal.wait(eventLockUnique, [this] {
return eventList.empty();
});
} else {
ret = eventSignal.wait_until(eventLockUnique, eventList.top()->getCycle());
}
Expand Down
4 changes: 3 additions & 1 deletion src/game/scheduling/tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ void Dispatcher::threadMain()

if (taskList.empty()) {
//if the list is empty wait for signal
taskSignal.wait(taskLockUnique);
taskSignal.wait(taskLockUnique, [this] {
return taskList.empty();
});
}

if (!taskList.empty()) {
Expand Down
7 changes: 6 additions & 1 deletion src/otserv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
std::mutex g_loaderLock;
std::condition_variable g_loaderSignal;
std::unique_lock<std::mutex> g_loaderUniqueLock(g_loaderLock);
bool g_loaderDone = false;

/**
*It is preferable to keep the close button off as it closes the server without saving (this can cause the player to lose items from houses and others informations, since windows automatically closes the process in five seconds, when forcing the close)
Expand Down Expand Up @@ -217,7 +218,9 @@ int main(int argc, char* argv[]) {
g_dispatcher().addTask(createTask(std::bind(mainLoader, argc, argv,
&serviceManager)));

g_loaderSignal.wait(g_loaderUniqueLock);
g_loaderSignal.wait(g_loaderUniqueLock, [] {
return g_loaderDone;
});

if (serviceManager.is_running()) {
SPDLOG_INFO("{} {}", g_configManager().getString(SERVER_NAME),
Expand Down Expand Up @@ -383,5 +386,7 @@ void mainLoader(int, char*[], ServiceManager* services) {
std::string url = g_configManager().getString(DISCORD_WEBHOOK_URL);
webhook_send_message("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE, url);

g_loaderDone = true;

g_loaderSignal.notify_all();
}

0 comments on commit a870e01

Please sign in to comment.