Skip to content

Commit

Permalink
repeated_timer: finish condition variable should release mutex when w…
Browse files Browse the repository at this point in the history
…aitting for event finished.
  • Loading branch information
ehds committed Jun 11, 2024
1 parent 1b5cbeb commit 47080f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/braft/repeated_timer_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// Ma,Jingwei([email protected])

#include "braft/repeated_timer_task.h"
#include <cstdlib>

#include "braft/util.h"

Expand All @@ -27,7 +28,7 @@ RepeatedTimerTask::RepeatedTimerTask()
_running(false),
_destroyed(true),
_invoking(false),
_finish_event(0) {}
_finish_cv(&_mutex) {}

RepeatedTimerTask::~RepeatedTimerTask() {
CHECK(!_running) << "Is still running";
Expand Down Expand Up @@ -72,7 +73,7 @@ void RepeatedTimerTask::on_timedout() {
lck.unlock();
on_destroy();
}
_finish_event.signal();
_finish_cv.Signal();
return;
}
return schedule(lck);
Expand All @@ -96,7 +97,6 @@ void RepeatedTimerTask::start() {
// is still running, in which case on_timedout would invoke
// schedule as it would not see _stopped
_running = true;
_finish_event.reset(1);
schedule(lck);
}

Expand Down Expand Up @@ -179,8 +179,10 @@ void RepeatedTimerTask::destroy(bool wait_infight_task) {
return;
}

if (wait_infight_task) {
_finish_event.wait();
// `rc` == 1 means timer still running.
// if `wait_infight_task` is true, we should wait until task is finished.
if (rc == 1 && wait_infight_task) {
_finish_cv.Wait();
CHECK(!_running);
return;
}
Expand Down
8 changes: 3 additions & 5 deletions src/braft/repeated_timer_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
// Authors: Zhangyi Chen([email protected])
// Ma,Jingwei([email protected])

#ifndef BRAFT_REPEATED_TIMER_TASK_H
#define BRAFT_REPEATED_TIMER_TASK_H
#pragma once

#include <bthread/unstable.h>

#include "braft/macros.h"
#include "bthread/countdown_event.h"
#include "butil/synchronization/condition_variable.h"

namespace braft {

Expand Down Expand Up @@ -79,9 +79,7 @@ class RepeatedTimerTask {
bool _running;
bool _destroyed;
bool _invoking;
bthread::CountdownEvent _finish_event;
butil::ConditionVariable _finish_cv;
};

} // namespace braft

#endif // BRAFT_REPEATED_TIMER_TASK_H

0 comments on commit 47080f5

Please sign in to comment.