From 4e03ee129389d85147ca5ff66678800a38a6d00b Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Tue, 30 Oct 2018 11:21:16 +0300 Subject: [PATCH] FileFd: fix sleeping while mutex is locked GitOrigin-RevId: fb6b5bb1967a4c144e9393b2ab82ccf6dfe2f15a --- tdutils/td/utils/port/FileFd.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp index 2ca7c6c93..912885759 100644 --- a/tdutils/td/utils/port/FileFd.cpp +++ b/tdutils/td/utils/port/FileFd.cpp @@ -301,21 +301,23 @@ Status FileFd::lock(const LockFlags flags, const string &path, int32 max_tries) CHECK(flags == LockFlags::Write); VLOG(fd) << "Trying to lock file \"" << path << '"'; while (true) { - std::unique_lock lock(in_process_lock_mutex); - if (locked_files.find(path) != locked_files.end()) { - if (--max_tries > 0) { - usleep_for(100000); - continue; + { // mutex lock scope + std::lock_guard lock(in_process_lock_mutex); + if (locked_files.find(path) == locked_files.end()) { + VLOG(fd) << "Lock file \"" << path << '"'; + need_local_unlock = true; + locked_files.insert(path); + break; } - - return Status::Error( - 0, PSLICE() << "Can't lock file \"" << path << "\", because it is already in use by current program"); } - VLOG(fd) << "Lock file \"" << path << '"'; - need_local_unlock = true; - locked_files.insert(path); - break; + if (--max_tries > 0) { + usleep_for(100000); + continue; + } + + return Status::Error( + 0, PSLICE() << "Can't lock file \"" << path << "\", because it is already in use by current program"); } } }