Skip to content
Snippets Groups Projects
Commit 63401691 authored by Eric Cano's avatar Eric Cano
Browse files

Fixed lock leaks by using stack based automatic release (RAII).

parent b84dde36
No related branches found
No related tags found
No related merge requests found
......@@ -77,8 +77,8 @@ castor::common::CastorConfiguration::getConfEnt(const std::string &category,
throw (castor::exception::Exception) {
// check whether we need to reload the configuration
checkAndRenewConfig();
// get read lock
pthread_rwlock_rdlock(&m_lock);
// get read lock. No need to be fancy, just let it auto-release on exit.
smartReadLockTaker srl(&m_lock);
// get the category
Configuration::const_iterator catIt = find(category);
if (end() == catIt) {
......@@ -91,9 +91,6 @@ castor::common::CastorConfiguration::getConfEnt(const std::string &category,
castor::exception::NoEntry e;
throw e;
}
// release read lock
pthread_rwlock_unlock(&m_lock);
// return
return entIt->second;
}
......@@ -118,19 +115,17 @@ void castor::common::CastorConfiguration::checkAndRenewConfig()
// no need to renew
return;
}
// we should probably renew. First take the write lock
pthread_rwlock_wrlock(&m_lock);
// we should probably renew. First take the write lock.
// No need to be fancy, just let it auto-release on exit.
smartWriteLockTaker swl(&m_lock);
// now check that we should really renew, because someone may have done it
// while we waited for the lock
if (currentTime < m_lastUpdateTime + timeout) {
// indeed, someone renew it for us, so give up
pthread_rwlock_unlock(&m_lock);
return;
return;
}
// now we should really renew
renewConfig();
// release the write lock
pthread_rwlock_unlock(&m_lock);
}
//------------------------------------------------------------------------------
......
......@@ -115,6 +115,28 @@ namespace castor {
*/
time_t m_lastUpdateTime;
/**
* Scoped lock allowing safe lock release in all execution pathes
*/
class smartLockTaker {
protected:
smartLockTaker(pthread_rwlock_t *lk): m_reld(false), m_lock(lk) {};
bool m_reld;
pthread_rwlock_t *m_lock;
public:
void release() { if (!m_reld) pthread_rwlock_unlock(m_lock); m_reld=true; }
~smartLockTaker() { release(); }
};
class smartReadLockTaker: public smartLockTaker {
public:
smartReadLockTaker(pthread_rwlock_t *lk): smartLockTaker(lk) { pthread_rwlock_rdlock(m_lock); }
};
class smartWriteLockTaker: public smartLockTaker {
public:
smartWriteLockTaker(pthread_rwlock_t *lk): smartLockTaker(lk) { pthread_rwlock_wrlock(m_lock); }
};
/**
* lock to garantee safe access to the configuration
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment