Skip to content

Commit

Permalink
client: fix potential dead lock in close file
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hanqing authored and xu-chaojie committed Feb 2, 2021
1 parent 12c23c0 commit 9a39ffa
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/client/lease_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define SRC_CLIENT_LEASE_EXECUTOR_H_

#include <brpc/periodic_task.h>
#include <bthread/condition_variable.h>
#include <bthread/mutex.h>

#include <memory>
#include <string>
Expand Down Expand Up @@ -198,7 +200,7 @@ class RefreshSessionTask : public brpc::PeriodicTask {
* false 停止执行当前任务
*/
bool OnTriggeringTask(timespec* next_abstime) override {
std::lock_guard<std::mutex> lk(stopMtx_);
std::lock_guard<bthread::Mutex> lk(stopMtx_);
if (stopped_) {
return false;
}
Expand All @@ -211,15 +213,15 @@ class RefreshSessionTask : public brpc::PeriodicTask {
* @brief 停止再次执行当前任务
*/
void Stop() {
std::lock_guard<std::mutex> lk(stopMtx_);
std::lock_guard<bthread::Mutex> lk(stopMtx_);
stopped_ = true;
}

/**
* @brief 任务停止后调用
*/
void OnDestroyingTask() override {
std::unique_lock<std::mutex> lk(terminatedMtx_);
std::unique_lock<bthread::Mutex> lk(terminatedMtx_);
terminated_ = true;
terminatedCv_.notify_one();
}
Expand All @@ -228,8 +230,10 @@ class RefreshSessionTask : public brpc::PeriodicTask {
* @brief 等待任务退出
*/
void WaitTaskExit() {
std::unique_lock<std::mutex> lk(terminatedMtx_);
terminatedCv_.wait(lk, [this]() { return terminated_ == true; });
std::unique_lock<bthread::Mutex> lk(terminatedMtx_);
while (terminated_ != true) {
terminatedCv_.wait(lk);
}
}

/**
Expand All @@ -245,11 +249,11 @@ class RefreshSessionTask : public brpc::PeriodicTask {
uint64_t refreshIntervalUs_;

bool stopped_;
std::mutex stopMtx_;
bthread::Mutex stopMtx_;

bool terminated_;
std::mutex terminatedMtx_;
std::condition_variable terminatedCv_;
bthread::Mutex terminatedMtx_;
bthread::ConditionVariable terminatedCv_;
};

} // namespace client
Expand Down

0 comments on commit 9a39ffa

Please sign in to comment.