Skip to content

Commit

Permalink
mutexing of timer list
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Nov 19, 2024
1 parent 23cf123 commit a2a21c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ class DPP_EXPORT cluster {
*/
std::unique_ptr<std::thread> engine_thread{nullptr};

/**
* @brief Protection mutex for timers
*/
std::mutex timer_guard;

public:
/**
* @brief Current bot token for all shards on this cluster and all commands sent via HTTP
Expand Down
14 changes: 9 additions & 5 deletions src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,16 @@ void cluster::start(bool return_after) {
void cluster::shutdown() {
/* Signal termination */
terminating = true;
/* Free memory for active timers */
for (auto & t : timer_list) {
delete t.second;
{
std::lock_guard<std::mutex> l(timer_guard);
log(ll_info, "Terminating " + std::to_string(timer_list.size()) + "timers");
/* Free memory for active timers */
for (auto &t: timer_list) {
delete t.second;
}
timer_list.clear();
next_timer.clear();
}
timer_list.clear();
next_timer.clear();
/* Terminate shards */
for (const auto& sh : shards) {
log(ll_info, "Terminating shard id " + std::to_string(sh.second->shard_id));
Expand Down
1 change: 0 additions & 1 deletion src/dpp/cluster/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace dpp {

timer lasthandle = 1;
std::mutex timer_guard;

timer cluster::start_timer(timer_callback_t on_tick, uint64_t frequency, timer_callback_t on_stop) {
std::lock_guard<std::mutex> l(timer_guard);
Expand Down

0 comments on commit a2a21c9

Please sign in to comment.