cid#1555986 Check of thread-shared field evades lock acquisition

and

cid#1556190 Check of thread-shared field evades lock acquisition
cid#1555295 Check of thread-shared field evades lock acquisition
cid#1608596 Check of thread-shared field evades lock acquisition
cid#1555236 Check of thread-shared field evades lock acquisition

Double-checked locking pattern

Change-Id: Id5cba26750bbd9c7e0248181f636ff653ada5654
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176739
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
This commit is contained in:
Caolán McNamara 2024-11-17 19:48:42 +00:00 committed by Stephan Bergmann
parent b9464eadfc
commit a1a3fd98dc
5 changed files with 8 additions and 33 deletions

View File

@ -97,7 +97,6 @@ static bool td_equals(typelib_TypeDescriptionReference const* pTDR1,
static cppu::type_entry* getTypeEntries(cppu::class_data* cd) static cppu::type_entry* getTypeEntries(cppu::class_data* cd)
{ {
cppu::type_entry* pEntries = cd->m_typeEntries; cppu::type_entry* pEntries = cd->m_typeEntries;
if (!cd->m_storedTypeRefs) // not inited?
{ {
static std::mutex aMutex; static std::mutex aMutex;
std::scoped_lock guard(aMutex); std::scoped_lock guard(aMutex);
@ -122,14 +121,9 @@ static cppu::type_entry* getTypeEntries(cppu::class_data* cd)
// ref is statically held by getCppuType() // ref is statically held by getCppuType()
pEntry->m_type.typeRef = rType.getTypeLibType(); pEntry->m_type.typeRef = rType.getTypeLibType();
} }
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
cd->m_storedTypeRefs = true; cd->m_storedTypeRefs = true;
} }
} }
else
{
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
}
return pEntries; return pEntries;
} }

View File

@ -81,7 +81,6 @@ static bool td_equals(typelib_TypeDescriptionReference const* pTDR1,
static cppu::type_entry* getTypeEntries(cppu::class_data* cd) static cppu::type_entry* getTypeEntries(cppu::class_data* cd)
{ {
cppu::type_entry* pEntries = cd->m_typeEntries; cppu::type_entry* pEntries = cd->m_typeEntries;
if (!cd->m_storedTypeRefs) // not inited?
{ {
static std::mutex aMutex; static std::mutex aMutex;
std::scoped_lock guard(aMutex); std::scoped_lock guard(aMutex);
@ -106,14 +105,9 @@ static cppu::type_entry* getTypeEntries(cppu::class_data* cd)
// ref is statically held by getCppuType() // ref is statically held by getCppuType()
pEntry->m_type.typeRef = rType.getTypeLibType(); pEntry->m_type.typeRef = rType.getTypeLibType();
} }
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
cd->m_storedTypeRefs = true; cd->m_storedTypeRefs = true;
} }
} }
else
{
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
}
return pEntries; return pEntries;
} }

View File

@ -64,7 +64,6 @@ static bool td_equals(
static type_entry * getTypeEntries( class_data * cd ) static type_entry * getTypeEntries( class_data * cd )
{ {
type_entry * pEntries = cd->m_typeEntries; type_entry * pEntries = cd->m_typeEntries;
if (! cd->m_storedTypeRefs) // not inited?
{ {
static std::mutex aMutex; static std::mutex aMutex;
std::scoped_lock guard( aMutex ); std::scoped_lock guard( aMutex );
@ -86,14 +85,9 @@ static type_entry * getTypeEntries( class_data * cd )
// ref is statically held by getCppuType() // ref is statically held by getCppuType()
pEntry->m_type.typeRef = rType.getTypeLibType(); pEntry->m_type.typeRef = rType.getTypeLibType();
} }
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
cd->m_storedTypeRefs = true; cd->m_storedTypeRefs = true;
} }
} }
else
{
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
}
return pEntries; return pEntries;
} }

View File

@ -254,16 +254,12 @@ OWeakObject::~OWeakObject() COVERITY_NOEXCEPT_FALSE
// XWeak // XWeak
Reference< XAdapter > SAL_CALL OWeakObject::queryAdapter() Reference< XAdapter > SAL_CALL OWeakObject::queryAdapter()
{ {
if (!m_pWeakConnectionPoint) std::scoped_lock aGuard( *gpWeakMutex );
if( !m_pWeakConnectionPoint )
{ {
// only acquire mutex if member is not created OWeakConnectionPoint * p = new OWeakConnectionPoint(this);
std::scoped_lock aGuard( *gpWeakMutex ); p->acquire();
if( !m_pWeakConnectionPoint ) m_pWeakConnectionPoint = p;
{
OWeakConnectionPoint * p = new OWeakConnectionPoint(this);
p->acquire();
m_pWeakConnectionPoint = p;
}
} }
return m_pWeakConnectionPoint; return m_pWeakConnectionPoint;

View File

@ -112,14 +112,11 @@ template <class TYPE>
::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper() ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
{ {
OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !"); OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
std::unique_lock aGuard(theMutex());
if (!s_pProps) if (!s_pProps)
{ {
std::unique_lock aGuard(theMutex()); s_pProps = createArrayHelper();
if (!s_pProps) OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
{
s_pProps = createArrayHelper();
OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
}
} }
return s_pProps; return s_pProps;
} }