Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
Show All 11 Lines | |||||
#include <chainparamsbase.h> | #include <chainparamsbase.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <boost/interprocess/sync/file_lock.hpp> | |||||
#include <boost/thread.hpp> | #include <boost/thread.hpp> | ||||
#include <cstdarg> | #include <cstdarg> | ||||
#include <memory> | #include <memory> | ||||
#if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) | #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) | ||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <pthread_np.h> | #include <pthread_np.h> | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
ArgsManager gArgs; | ArgsManager gArgs; | ||||
/** | /** | ||||
* A map that contains all the currently held directory locks. After successful | * 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 | * locking, these will be held here until the global destructor cleans them up | ||||
* and thus automatically unlocks them, or ReleaseDirectoryLocks is called. | * and thus automatically unlocks them, or ReleaseDirectoryLocks is called. | ||||
*/ | */ | ||||
static std::map<std::string, std::unique_ptr<boost::interprocess::file_lock>> | static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks; | ||||
dir_locks; | |||||
/** Mutex to protect dir_locks. */ | /** Mutex to protect dir_locks. */ | ||||
static std::mutex cs_dir_locks; | static std::mutex cs_dir_locks; | ||||
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, | bool LockDirectory(const fs::path &directory, const std::string lockfile_name, | ||||
bool probe_only) { | bool probe_only) { | ||||
std::lock_guard<std::mutex> ulock(cs_dir_locks); | std::lock_guard<std::mutex> ulock(cs_dir_locks); | ||||
fs::path pathLockFile = directory / lockfile_name; | fs::path pathLockFile = directory / lockfile_name; | ||||
// If a lock for this directory already exists in the map, don't try to | // If a lock for this directory already exists in the map, don't try to | ||||
// re-lock it | // re-lock it | ||||
if (dir_locks.count(pathLockFile.string())) { | if (dir_locks.count(pathLockFile.string())) { | ||||
return true; | return true; | ||||
} | } | ||||
// Create empty lock file if it doesn't exist. | // Create empty lock file if it doesn't exist. | ||||
FILE *file = fsbridge::fopen(pathLockFile, "a"); | FILE *file = fsbridge::fopen(pathLockFile, "a"); | ||||
if (file) { | if (file) { | ||||
fclose(file); | fclose(file); | ||||
} | } | ||||
auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile); | |||||
try { | if (!lock->TryLock()) { | ||||
auto lock = std::make_unique<boost::interprocess::file_lock>( | return error("Error while attempting to lock directory %s: %s", | ||||
pathLockFile.string().c_str()); | directory.string(), lock->GetReason()); | ||||
if (!lock->try_lock()) { | |||||
return false; | |||||
} | } | ||||
if (!probe_only) { | if (!probe_only) { | ||||
// Lock successful and we're not just probing, put it into the map | // Lock successful and we're not just probing, put it into the map | ||||
dir_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()); | |||||
} | |||||
return true; | return true; | ||||
} | } | ||||
void ReleaseDirectoryLocks() { | void ReleaseDirectoryLocks() { | ||||
std::lock_guard<std::mutex> ulock(cs_dir_locks); | std::lock_guard<std::mutex> ulock(cs_dir_locks); | ||||
dir_locks.clear(); | dir_locks.clear(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,215 Lines • Show Last 20 Lines |