diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2018-02-13 13:53:17 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2018-02-15 22:38:10 +0100 |
commit | 4d54e7ad413b0113402a4d9546eb510d21ba2ecd (patch) | |
tree | 4ea7ab23a947fa57b166236405e690583eba1842 /src/util.cpp | |
parent | 32a726846d191796ba76e844a9aa1e7da0a9ed13 (diff) | |
download | bitcoin-4d54e7ad413b0113402a4d9546eb510d21ba2ecd.tar.xz |
test: Add unit test for LockDirectory
Add a unit test for LockDirectory, introduced in #11281.
Github-Pull: #12422
Rebased-From: 1d4cbd26e4220982f7f2f60e447199d6f62ae254
Tree-SHA512: 8186f4b22c65153e30ec1e0b68be20fd59d34e1fe565d99f3b5a302780125953b867fb14c37144de3fd7baf5751df94bf3901eebbb7b820491ca452706d4e205
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/util.cpp b/src/util.cpp index 0769df4b3b..6ec7cba2ad 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -376,19 +376,22 @@ int LogPrintStr(const std::string &str) return ret; } +/** A map that contains all the currently held directory locks. After + * successful locking, these will be held here until the global destructor + * cleans them up and thus automatically unlocks them, or ReleaseDirectoryLocks + * is called. + */ +static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> dir_locks; +/** Mutex to protect dir_locks. */ +static std::mutex cs_dir_locks; + bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only) { - // A map that contains all the currently held directory locks. After - // successful locking, these will be held here until the global - // destructor cleans them up and thus automatically unlocks them. - static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> locks; - // Protect the map with a mutex - static std::mutex cs; - std::lock_guard<std::mutex> ulock(cs); + std::lock_guard<std::mutex> ulock(cs_dir_locks); fs::path pathLockFile = directory / lockfile_name; // If a lock for this directory already exists in the map, don't try to re-lock it - if (locks.count(pathLockFile.string())) { + if (dir_locks.count(pathLockFile.string())) { return true; } @@ -403,7 +406,7 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b } if (!probe_only) { // Lock successful and we're not just probing, put it into the map - locks.emplace(pathLockFile.string(), std::move(lock)); + dir_locks.emplace(pathLockFile.string(), std::move(lock)); } } catch (const boost::interprocess::interprocess_exception& e) { return error("Error while attempting to lock directory %s: %s", directory.string(), e.what()); @@ -411,6 +414,12 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b return true; } +void ReleaseDirectoryLocks() +{ + std::lock_guard<std::mutex> ulock(cs_dir_locks); + dir_locks.clear(); +} + /** Interpret string as boolean, for argument parsing */ static bool InterpretBool(const std::string& strValue) { |