summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2021-07-16 15:01:53 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-07-17 09:11:32 +0200
commit3545caa5aac59022d5c00d4a0c2a74657cbcae8d (patch)
tree8403e52bf6263ab5bb0714468b4d643b913951e4
parent4a096b9c787fa2079ef1c8f00fed91d432d46fae (diff)
osl::Mutex->std::mutex in OWeakConnectionPoint
it is at least a little bit cheaper than osl::Mutex (since it's not recursive) Change-Id: I4ecbbf33045888b9f5111c4224346341e71d4b05 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119066 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r--cppuhelper/source/weak.cxx47
1 files changed, 23 insertions, 24 deletions
diff --git a/cppuhelper/source/weak.cxx b/cppuhelper/source/weak.cxx
index e52dc607a0a3..657bf5d0c7da 100644
--- a/cppuhelper/source/weak.cxx
+++ b/cppuhelper/source/weak.cxx
@@ -21,7 +21,6 @@
#include <sal/log.hxx>
#include <osl/diagnose.h>
-#include <osl/mutex.hxx>
#include <cppuhelper/weakagg.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <cppuhelper/queryinterface.hxx>
@@ -30,6 +29,7 @@
#include <algorithm>
#include <vector>
+#include <mutex>
using namespace osl;
using namespace com::sun::star::uno;
@@ -40,11 +40,7 @@ namespace cppu
// due to static Reflection destruction from usr, there must be a mutex leak (#73272#)
// this is used to lock all instances of OWeakConnectionPoint and OWeakRefListener as well as OWeakObject::m_pWeakConnectionPoint
-static Mutex & getWeakMutex()
-{
- static Mutex * s_pMutex = new Mutex();
- return *s_pMutex;
-}
+static std::mutex * gpWeakMutex = new std::mutex;
//-- OWeakConnectionPoint ----------------------------------------------------
@@ -123,7 +119,7 @@ void OWeakConnectionPoint::dispose()
{
std::vector<Reference<XReference>> aCopy;
{ // only hold the mutex while we access the field
- MutexGuard aGuard(getWeakMutex());
+ std::scoped_lock aGuard(*cppu::gpWeakMutex);
// OWeakObject is not the only owner of this, so clear m_pObject
// so that queryAdapted() won't use it now that it's dead
m_pObject = nullptr;
@@ -155,40 +151,43 @@ Reference< XInterface > SAL_CALL OWeakConnectionPoint::queryAdapted()
{
Reference< XInterface > ret;
- ClearableMutexGuard guard(getWeakMutex());
-
- if (m_pObject)
{
+ std::scoped_lock guard(*gpWeakMutex);
+
+ if (!m_pObject)
+ return ret;
+
oslInterlockedCount n = osl_atomic_increment( &m_pObject->m_refCount );
- if (n > 1)
+ if (n <= 1)
{
- // The reference is incremented. The object cannot be destroyed.
- // Release the guard at the earliest point.
- guard.clear();
- // WeakObject has a (XInterface *) cast operator
- ret = *m_pObject;
- osl_atomic_decrement( &m_pObject->m_refCount );
- }
- else
// Another thread wait in the dispose method at the guard
osl_atomic_decrement( &m_pObject->m_refCount );
+ return ret;
+ }
}
+ // n is now > 1
+ // The reference is incremented. The object cannot be destroyed.
+ // Release the guard at the earliest point.
+ // WeakObject has a (XInterface *) cast operator
+ ret = *m_pObject;
+ osl_atomic_decrement( &m_pObject->m_refCount );
+
return ret;
}
// XInterface
void SAL_CALL OWeakConnectionPoint::addReference(const Reference< XReference >& rRef)
{
- MutexGuard aGuard(getWeakMutex());
+ std::scoped_lock aGuard(*gpWeakMutex);
m_aReferences.push_back( rRef );
}
// XInterface
void SAL_CALL OWeakConnectionPoint::removeReference(const Reference< XReference >& rRef)
{
- MutexGuard aGuard(getWeakMutex());
+ std::scoped_lock aGuard(*gpWeakMutex);
// Search from end because the thing that last added a ref is most likely to be the
// first to remove a ref.
// It's not really valid to compare the pointer directly, but it's faster.
@@ -269,7 +268,7 @@ Reference< XAdapter > SAL_CALL OWeakObject::queryAdapter()
if (!m_pWeakConnectionPoint)
{
// only acquire mutex if member is not created
- MutexGuard aGuard( getWeakMutex() );
+ std::scoped_lock aGuard( *gpWeakMutex );
if( !m_pWeakConnectionPoint )
{
OWeakConnectionPoint * p = new OWeakConnectionPoint(this);
@@ -422,7 +421,7 @@ void SAL_CALL OWeakRefListener::dispose()
{
Reference< XAdapter > xAdp;
{
- MutexGuard guard(cppu::getWeakMutex());
+ std::scoped_lock guard(*cppu::gpWeakMutex);
if( m_XWeakConnectionPoint.is() )
{
xAdp = m_XWeakConnectionPoint;
@@ -518,7 +517,7 @@ Reference< XInterface > WeakReferenceHelper::get() const
Reference< XAdapter > xAdp;
{
// must lock to access m_XWeakConnectionPoint
- MutexGuard guard(cppu::getWeakMutex());
+ std::scoped_lock guard(*cppu::gpWeakMutex);
if( m_pImpl && m_pImpl->m_XWeakConnectionPoint.is() )
xAdp = m_pImpl->m_XWeakConnectionPoint;
}