aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2018-02-13 13:53:17 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2018-02-15 16:25:13 +0100
commit1d4cbd26e4220982f7f2f60e447199d6f62ae254 (patch)
tree4c8f244dacd9238dd810b5821175bb72b1d49808 /src/util.cpp
parentfc888bfcacb875c45bc8f9d7ca1357ab70a30490 (diff)
test: Add unit test for LockDirectory
Add a unit test for LockDirectory, introduced in #11281.
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 49f40dc942..dcf7ed38b1 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -373,19 +373,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;
}
@@ -400,7 +403,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());
@@ -408,6 +411,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)
{