From 84d87ae75f5d70bfcaa3e2b124c74a8f733980a3 Mon Sep 17 00:00:00 2001
From: Noel Grandin <noelgrandin@gmail.com>
Date: Sun, 22 Aug 2021 18:20:58 +0200
Subject: osl::Mutex->std::mutex in OInstanceLocker

Change-Id: Ia92632beccac90e60bceb305a736e93ffb37d356
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120902
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
---
 comphelper/source/misc/instancelocker.cxx | 28 +++++++++++++---------------
 comphelper/source/misc/instancelocker.hxx |  8 ++++----
 2 files changed, 17 insertions(+), 19 deletions(-)

(limited to 'comphelper')

diff --git a/comphelper/source/misc/instancelocker.cxx b/comphelper/source/misc/instancelocker.cxx
index b942d7ec8329..ffb325bc40b2 100644
--- a/comphelper/source/misc/instancelocker.cxx
+++ b/comphelper/source/misc/instancelocker.cxx
@@ -66,19 +66,20 @@ OInstanceLocker::~OInstanceLocker()
 
 void SAL_CALL OInstanceLocker::dispose()
 {
-    ::osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( m_bDisposed )
         throw lang::DisposedException();
 
     lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
-    if ( m_pListenersContainer )
-        m_pListenersContainer->disposeAndClear( aSource );
-
+    m_aListenersContainer.disposeAndClear( aGuard, aSource );
+    aGuard.lock();
     if ( m_xLockListener.is() )
     {
-        m_xLockListener->Dispose();
-        m_xLockListener.clear();
+        auto tmp = std::move(m_xLockListener);
+        aGuard.unlock();
+        tmp->Dispose();
+        aGuard.lock();
     }
 
     m_bDisposed = true;
@@ -87,29 +88,25 @@ void SAL_CALL OInstanceLocker::dispose()
 
 void SAL_CALL OInstanceLocker::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
 {
-    ::osl::MutexGuard aGuard( m_aMutex );
+    std::scoped_lock aGuard( m_aMutex );
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
-    if ( !m_pListenersContainer )
-        m_pListenersContainer.reset( new ::comphelper::OInterfaceContainerHelper2( m_aMutex ) );
-
-    m_pListenersContainer->addInterface( xListener );
+    m_aListenersContainer.addInterface( xListener );
 }
 
 
 void SAL_CALL OInstanceLocker::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
 {
-    ::osl::MutexGuard aGuard( m_aMutex );
-    if ( m_pListenersContainer )
-        m_pListenersContainer->removeInterface( xListener );
+    std::scoped_lock aGuard( m_aMutex );
+    m_aListenersContainer.removeInterface( xListener );
 }
 
 // XInitialization
 
 void SAL_CALL OInstanceLocker::initialize( const uno::Sequence< uno::Any >& aArguments )
 {
-    ::osl::MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
     if ( m_bInitialized )
         throw frame::DoubleInitializationException();
 
@@ -166,6 +163,7 @@ void SAL_CALL OInstanceLocker::initialize( const uno::Sequence< uno::Any >& aArg
     }
     catch( uno::Exception& )
     {
+        aGuard.unlock();
         dispose();
         throw;
     }
diff --git a/comphelper/source/misc/instancelocker.hxx b/comphelper/source/misc/instancelocker.hxx
index 02180e511c0f..a188e6e4614e 100644
--- a/comphelper/source/misc/instancelocker.hxx
+++ b/comphelper/source/misc/instancelocker.hxx
@@ -24,14 +24,14 @@
 #include <com/sun/star/frame/XTerminateListener.hpp>
 #include <com/sun/star/lang/XInitialization.hpp>
 #include <com/sun/star/lang/XServiceInfo.hpp>
+#include <comphelper/interfacecontainer4.hxx>
 #include <cppuhelper/weakref.hxx>
-#include <osl/mutex.hxx>
 #include <cppuhelper/implbase.hxx>
 #include <rtl/ref.hxx>
 #include <memory>
+#include <mutex>
 
 namespace com::sun::star::embed { class XActionsApproval; }
-namespace comphelper { class OInterfaceContainerHelper2; }
 
 
 class OLockListener;
@@ -42,11 +42,11 @@ class OInstanceLocker : public ::cppu::WeakImplHelper< css::lang::XComponent,
                                                        css::lang::XInitialization,
                                                        css::lang::XServiceInfo >
 {
-    ::osl::Mutex m_aMutex;
+    std::mutex m_aMutex;
 
     rtl::Reference< OLockListener > m_xLockListener;
 
-    std::unique_ptr<::comphelper::OInterfaceContainerHelper2> m_pListenersContainer; // list of listeners
+    comphelper::OInterfaceContainerHelper4<css::lang::XEventListener> m_aListenersContainer; // list of listeners
 
     bool m_bDisposed;
     bool m_bInitialized;
-- 
cgit