From 5210c3bfae92c57c62a24babc5fcfb1cf89db34e Mon Sep 17 00:00:00 2001 From: psychon Date: Tue, 8 Jan 2008 17:13:10 +0000 Subject: [PATCH] Fix for OpenBSD and don't busy loop for file locks I don't think there is a high change that the file lock becomes available in the short time we did loop and retry the lock, so that code served no real purpose. Also, this includes unistd.h in Utils.h which is needed for OpenBSD. Thanks to fred for testing on his openbsd/sparc box. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@918 726aef4b-f618-498e-8847-2d620e286838 --- Utils.h | 49 +++++++------------------------------------------ znc.cpp | 6 +++--- 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/Utils.h b/Utils.h index 080a6f8e..a94513a5 100644 --- a/Utils.h +++ b/Utils.h @@ -14,6 +14,7 @@ #include #include #include +#include #include using std::map; @@ -109,53 +110,17 @@ public: } //! timeout in milliseconds - bool TryExLock(const CString& sLockFile, unsigned long long iTimeout = 0, bool bRw = false) { + bool TryExLock(const CString& sLockFile, bool bRw = false) { Open(sLockFile, bRw); - return TryExLock(iTimeout); + return TryExLock(); } - bool TryExLock(unsigned long long iTimeout = 0) { - if (iTimeout == 0) { - return Lock(LOCK_EX|LOCK_NB); - } - - unsigned long long iNow = CUtils::GetMillTime(); - - while(true) { - if (Lock(LOCK_EX|LOCK_NB)) { - return true; - } - - if ((CUtils::GetMillTime() - iNow) > iTimeout) { - break; - } - - usleep(100); - } - - return(false); + bool TryExLock() { + return Lock(LOCK_EX|LOCK_NB); } - bool TryShLock(unsigned long long iTimeout = 0) { - if (iTimeout == 0) { - return(Lock(LOCK_SH|LOCK_NB)); - } - - unsigned long long iNow = CUtils::GetMillTime(); - - while(true) { - if (Lock(LOCK_SH|LOCK_NB)) { - return true; - } - - if ((CUtils::GetMillTime() - iNow) > iTimeout) { - break; - } - - usleep(100); - } - - return false; + bool TryShLock() { + return(Lock(LOCK_SH|LOCK_NB)); } bool LockEx() { return Lock(LOCK_EX); } diff --git a/znc.cpp b/znc.cpp index 8f338984..eb327584 100644 --- a/znc.cpp +++ b/znc.cpp @@ -186,7 +186,7 @@ bool CZNC::WriteISpoof(CUser* pUser) { if (!m_sISpoofFile.empty()) { m_pISpoofLockFile = new CLockFile; - if(!m_pISpoofLockFile->TryExLock(m_sISpoofFile, 50, true)) { + if(!m_pISpoofLockFile->TryExLock(m_sISpoofFile, true)) { delete m_pISpoofLockFile; m_pISpoofLockFile = NULL; return false; @@ -457,7 +457,7 @@ bool CZNC::WriteNewConfig(const CString& sConfig) { vector vsLines; if (CFile::Exists(sConfigFile)) { - if (!m_LockFile.TryExLock(sConfigFile, 50)) { + if (!m_LockFile.TryExLock(sConfigFile)) { CUtils::PrintError("ZNC is currently running on this config."); return false; } @@ -760,7 +760,7 @@ bool CZNC::ParseConfig(const CString& sConfig) { return false; } - if (!m_LockFile.TryExLock(m_sConfigFile, 50)) { + if (!m_LockFile.TryExLock(m_sConfigFile)) { CUtils::PrintStatus(false, "ZNC is already running on this config."); return false; }