From: Scott Cantor Date: Sat, 24 Dec 2011 21:45:21 +0000 (+0000) Subject: Adjust Lock API X-Git-Tag: 1.5.0~44 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fcpp-xmltooling.git;a=commitdiff_plain;h=f239625ccb33bd1648f119ca4e93ad374aba958a Adjust Lock API --- diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 6760d9e..bd7fbcb 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -294,7 +294,6 @@ XMLToolingInternalConfig::XMLToolingInternalConfig() : XMLToolingInternalConfig::~XMLToolingInternalConfig() { - delete m_lock; } bool XMLToolingInternalConfig::log_config(const char* config) diff --git a/xmltooling/internal.h b/xmltooling/internal.h index c486b10..bf879f3 100644 --- a/xmltooling/internal.h +++ b/xmltooling/internal.h @@ -132,7 +132,7 @@ namespace xmltooling { private: int m_initCount; - Mutex* m_lock; + std::auto_ptr m_lock; std::map m_namedLocks; std::vector m_libhandles; ParserPool* m_parserPool; diff --git a/xmltooling/util/ParserPool.cpp b/xmltooling/util/ParserPool.cpp index ca155ca..42fc535 100644 --- a/xmltooling/util/ParserPool.cpp +++ b/xmltooling/util/ParserPool.cpp @@ -229,7 +229,7 @@ bool ParserPool::loadSchema(const XMLCh* nsURI, const XMLCh* pathname) return false; } - Lock lock(m_lock.get()); + Lock lock(m_lock); m_schemaLocMap[nsURI]=pathname; m_schemaLocations.erase(); for_each(m_schemaLocMap.begin(), m_schemaLocMap.end(), doubleit(m_schemaLocations,chSpace)); @@ -302,7 +302,7 @@ bool ParserPool::loadCatalog(const XMLCh* pathname) // Fetch all the elements. DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,system); - Lock lock(m_lock.get()); + Lock lock(m_lock); for (XMLSize_t i=0; igetLength(); i++) { root=static_cast(mappings->item(i)); const XMLCh* from=root->getAttributeNS(nullptr,systemId); @@ -400,7 +400,7 @@ DOMLSParser* ParserPool::createBuilder() DOMLSParser* ParserPool::checkoutBuilder() { - Lock lock(m_lock.get()); + Lock lock(m_lock); if (m_pool.empty()) { DOMLSParser* builder=createBuilder(); return builder; @@ -415,7 +415,7 @@ DOMLSParser* ParserPool::checkoutBuilder() void ParserPool::checkinBuilder(DOMLSParser* builder) { if (builder) { - Lock lock(m_lock.get()); + Lock lock(m_lock); m_pool.push(builder); } } @@ -447,7 +447,7 @@ DOMBuilder* ParserPool::createBuilder() DOMBuilder* ParserPool::checkoutBuilder() { - Lock lock(m_lock.get()); + Lock lock(m_lock); if (m_pool.empty()) { DOMBuilder* builder=createBuilder(); return builder; @@ -462,7 +462,7 @@ DOMBuilder* ParserPool::checkoutBuilder() void ParserPool::checkinBuilder(DOMBuilder* builder) { if (builder) { - Lock lock(m_lock.get()); + Lock lock(m_lock); m_pool.push(builder); } } diff --git a/xmltooling/util/Threads.h b/xmltooling/util/Threads.h index 959031d..5269763 100644 --- a/xmltooling/util/Threads.h +++ b/xmltooling/util/Threads.h @@ -29,6 +29,7 @@ #include +#include #include namespace xmltooling @@ -305,6 +306,16 @@ namespace xmltooling } /** + * Locks and wraps the designated mutex. + * + * @param mtx mutex to lock + */ + Lock(std::auto_ptr& mtx) : mutex(mtx.get()) { + if (mutex) + mutex->lock(); + } + + /** * Unlocks the wrapped mutex. */ ~Lock() { @@ -334,6 +345,17 @@ namespace xmltooling } /** + * Locks and wraps the designated shared lock. + * + * @param lock lock to acquire + * @param lockit true if the lock should be acquired here, false if already acquired + */ + SharedLock(std::auto_ptr& lock, bool lockit=true) : rwlock(lock.get()) { + if (rwlock && lockit) + rwlock->rdlock(); + } + + /** * Unlocks the wrapped shared lock. */ ~SharedLock() {