From e07253e0262a11dc96a98598c55c43da16b9678a Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Sun, 4 Nov 2018 20:31:58 +0300 Subject: replace double-checked locking patterns with thread safe local statics Change-Id: I4ed97cc6d9f733292156d71551d5ce3af6071445 Reviewed-on: https://gerrit.libreoffice.org/62858 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- .../source/drivers/postgresql/pq_statics.cxx | 12 ++----- oox/source/drawingml/presetgeometrynames.cxx | 30 +++++------------ package/source/xstor/oseekinstream.cxx | 19 ++--------- svl/source/fsstor/oinputstreamcontainer.cxx | 39 +++++++--------------- 4 files changed, 26 insertions(+), 74 deletions(-) diff --git a/connectivity/source/drivers/postgresql/pq_statics.cxx b/connectivity/source/drivers/postgresql/pq_statics.cxx index 0bec73919687..100c16c070f4 100644 --- a/connectivity/source/drivers/postgresql/pq_statics.cxx +++ b/connectivity/source/drivers/postgresql/pq_statics.cxx @@ -108,12 +108,7 @@ static cppu::IPropertyArrayHelper * createPropertyArrayHelper( Statics & getStatics() { - static Statics * p; - if( ! p ) - { - ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() ); - if( ! p ) - { + static Statics* p = []() { static Statics statics ; statics.SYSTEM_TABLE = "SYSTEM TABLE"; statics.TABLE = "TABLE"; @@ -665,9 +660,8 @@ Statics & getStatics() defTypeInfoMetaData[i].isAutoIncrement ) ); } - p = &statics; - } - } + return &statics; + }(); return *p; } diff --git a/oox/source/drawingml/presetgeometrynames.cxx b/oox/source/drawingml/presetgeometrynames.cxx index 4939cfcf0a3f..272094dd7ef6 100644 --- a/oox/source/drawingml/presetgeometrynames.cxx +++ b/oox/source/drawingml/presetgeometrynames.cxx @@ -20,13 +20,6 @@ namespace typedef std::unordered_map PresetGeometryHashMap; -static PresetGeometryHashMap* pHashMap = nullptr; -::osl::Mutex& getHashMapMutex() -{ - static osl::Mutex s_aHashMapProtection; - return s_aHashMapProtection; -} - struct PresetGeometryName { const char* pMsoName; @@ -79,27 +72,20 @@ static const PresetGeometryName pPresetGeometryNameArray[] OUString PresetGeometryTypeNames::GetFontworkType(const OUString& rMsoType) { - if (!pHashMap) - { // init hash map - ::osl::MutexGuard aGuard(getHashMapMutex()); - if (!pHashMap) - { - PresetGeometryHashMap* pH = new PresetGeometryHashMap; - const PresetGeometryName* pPtr = pPresetGeometryNameArray; - const PresetGeometryName* pEnd = pPtr + SAL_N_ELEMENTS(pPresetGeometryNameArray); - for (; pPtr < pEnd; pPtr++) - (*pH)[pPtr->pMsoName] = pPtr->pFontworkType; - pHashMap = pH; - } - } + static const PresetGeometryHashMap s_HashMap = []() { + PresetGeometryHashMap aH; + for (const auto& item : pPresetGeometryNameArray) + aH[item.pMsoName] = item.pFontworkType; + return aH; + }(); const char* pRetValue = ""; int i, nLen = rMsoType.getLength(); std::unique_ptr pBuf(new char[nLen + 1]); for (i = 0; i < nLen; i++) pBuf[i] = static_cast(rMsoType[i]); pBuf[i] = 0; - PresetGeometryHashMap::const_iterator aHashIter(pHashMap->find(pBuf.get())); - if (aHashIter != pHashMap->end()) + PresetGeometryHashMap::const_iterator aHashIter(s_HashMap.find(pBuf.get())); + if (aHashIter != s_HashMap.end()) pRetValue = (*aHashIter).second; return OUString(pRetValue, strlen(pRetValue), RTL_TEXTENCODING_ASCII_US); diff --git a/package/source/xstor/oseekinstream.cxx b/package/source/xstor/oseekinstream.cxx index 34d6a5d8f480..d19d6a745ab1 100644 --- a/package/source/xstor/oseekinstream.cxx +++ b/package/source/xstor/oseekinstream.cxx @@ -53,23 +53,10 @@ OInputSeekStream::~OInputSeekStream() uno::Sequence< uno::Type > SAL_CALL OInputSeekStream::getTypes() { - static ::cppu::OTypeCollection* pTypeCollection = nullptr ; + static cppu::OTypeCollection aTypeCollection(cppu::UnoType::get(), + OInputCompStream::getTypes()); - if ( pTypeCollection == nullptr ) - { - ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ; - - if ( pTypeCollection == nullptr ) - { - static ::cppu::OTypeCollection aTypeCollection( - cppu::UnoType::get(), - OInputCompStream::getTypes() ); - - pTypeCollection = &aTypeCollection ; - } - } - - return pTypeCollection->getTypes() ; + return aTypeCollection.getTypes(); } uno::Any SAL_CALL OInputSeekStream::queryInterface( const uno::Type& rType ) diff --git a/svl/source/fsstor/oinputstreamcontainer.cxx b/svl/source/fsstor/oinputstreamcontainer.cxx index 9b9899992119..b62aca14c9b2 100644 --- a/svl/source/fsstor/oinputstreamcontainer.cxx +++ b/svl/source/fsstor/oinputstreamcontainer.cxx @@ -39,36 +39,21 @@ OFSInputStreamContainer::~OFSInputStreamContainer() uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes() { - static ::cppu::OTypeCollection* pTypeCollection = nullptr ; - - if ( pTypeCollection == nullptr ) + if (m_bSeekable) { - ::osl::MutexGuard aGuard( m_aMutex ) ; - - if ( pTypeCollection == nullptr ) - { - if ( m_bSeekable ) - { - static ::cppu::OTypeCollection aTypeCollection( - cppu::UnoType::get(), - cppu::UnoType::get(), - cppu::UnoType::get()); - - pTypeCollection = &aTypeCollection ; - } - else - { - static ::cppu::OTypeCollection aTypeCollection( - cppu::UnoType::get(), - cppu::UnoType::get()); - - pTypeCollection = &aTypeCollection ; - } - } - } + static cppu::OTypeCollection aTypeCollection(cppu::UnoType::get(), + cppu::UnoType::get(), + cppu::UnoType::get()); - return pTypeCollection->getTypes() ; + return aTypeCollection.getTypes(); + } + else + { + static cppu::OTypeCollection aTypeCollection(cppu::UnoType::get(), + cppu::UnoType::get()); + return aTypeCollection.getTypes(); + } } uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType ) -- cgit