diff options
author | Maksym Telychko <maksym.telychko@globallogic.com> | 2020-03-24 19:13:58 +0200 |
---|---|---|
committer | Andrii Pientsov <andrii.pientsov@globallogic.com> | 2020-05-26 19:48:38 +0300 |
commit | 2418037578c64516803d0ac261545c1b8fb69ac2 (patch) | |
tree | f3162a89cf6c8226d58e92b6ba9c09d7a86b9f4b /src | |
parent | 620bd2637b40d718dd5886ce7df0a19b73ea8875 (diff) | |
download | libmts-io-2418037578c64516803d0ac261545c1b8fb69ac2.tar.gz libmts-io-2418037578c64516803d0ac261545c1b8fb69ac2.tar.bz2 libmts-io-2418037578c64516803d0ac261545c1b8fb69ac2.zip |
MTX-3262 mpower lockfile: additional fd check on lock
Diffstat (limited to 'src')
-rw-r--r-- | src/MTS_IO_LockFile.cpp | 22 |
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; } |