summaryrefslogtreecommitdiff
path: root/src/MTS_IO_LockFile.cpp
diff options
context:
space:
mode:
authorMaksym Telychko <maksym.telychko@globallogic.com>2020-03-24 19:13:58 +0200
committerMaksym Telychko <maksym.telychko@globallogic.com>2020-03-24 19:13:58 +0200
commitefd5a28d96b91f5f9597a1e6ceef41cb22885cfd (patch)
tree571a080e95074cc8ddf7286f5a6c0d31ad0b576e /src/MTS_IO_LockFile.cpp
parentbab7e498b6dc31b6967a59fd728c5e5fe59d485c (diff)
downloadlibmts-io-efd5a28d96b91f5f9597a1e6ceef41cb22885cfd.tar.gz
libmts-io-efd5a28d96b91f5f9597a1e6ceef41cb22885cfd.tar.bz2
libmts-io-efd5a28d96b91f5f9597a1e6ceef41cb22885cfd.zip
MTX-3262 mpower lockfile: additional fd check on lock
Diffstat (limited to 'src/MTS_IO_LockFile.cpp')
-rw-r--r--src/MTS_IO_LockFile.cpp22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/MTS_IO_LockFile.cpp b/src/MTS_IO_LockFile.cpp
index 4312ce4..842ffb7 100644
--- a/src/MTS_IO_LockFile.cpp
+++ b/src/MTS_IO_LockFile.cpp
@@ -48,19 +48,23 @@ LockFile::~LockFile()
bool LockFile::lock(uint32_t attemptMillis)
{
- if (isLocked())
+ if (isLocked()) {
return true;
+ }
- MTS::Timer timer;
- timer.start();
m_iLockFd = ::open(m_sFile.c_str(), O_CREAT | O_RDWR, 0666);
- while (timer.getMillis() < attemptMillis) {
- m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB);
- if (m_iLockErr == 0)
- return true;
- ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second
+ if (m_iLockFd >= 0) {
+ MTS::Timer timer;
+ timer.start();
+ while (timer.getMillis() < attemptMillis) {
+ m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB);
+ if (m_iLockErr == 0) {
+ return true;
+ }
+ ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second
+ }
+ ::close(m_iLockFd);
}
- ::close(m_iLockFd);
m_iLockFd = -1;
return false;
}