Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repeated_timer: finish condition variable should release mutex when waiting for still running task finished. #14

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/braft/configuration_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ void ConfigurationManager::set_snapshot(ConfigurationEntry&& entry) {

void ConfigurationManager::get(int64_t last_included_index,
ConfigurationEntry* conf) {
if (_configurations.empty()) {
if (_configurations.empty() ||
_configurations.begin()->id.index > last_included_index) {
// configurations is empty or
// the smallest index configuration entry is greater than
// last_included_index.
CHECK_GE(last_included_index, _snapshot.id.index);
*conf = _snapshot;
return;
Expand All @@ -80,8 +84,9 @@ void ConfigurationManager::get(int64_t last_included_index,
return rhs.id.index > index;
});

CHECK(it != _configurations.rend());
CHECK(it != _configurations.rend()); // must found.
*conf = *it;
return;
}

const ConfigurationEntry& ConfigurationManager::last_configuration() const {
Expand Down
2 changes: 2 additions & 0 deletions src/braft/configuration_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ConfigurationManager {

void set_snapshot(ConfigurationEntry&& snapshot);

// find the latest ConfigurationEntry which `last_included_index`
// log belongs to.
void get(int64_t last_included_index, ConfigurationEntry* entry);

const ConfigurationEntry& last_configuration() const;
Expand Down
11 changes: 6 additions & 5 deletions src/braft/repeated_timer_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 +72,7 @@ void RepeatedTimerTask::on_timedout() {
lck.unlock();
on_destroy();
}
_finish_event.signal();
_finish_cv.Signal();
return;
}
return schedule(lck);
Expand All @@ -96,7 +96,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 +178,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
Loading