basic/source/classes/sbunoobj.cxx                                          |   
 4 
 chart2/source/tools/ModifyListenerHelper.cxx                               |   
10 -
 chart2/source/tools/RangeHighlighter.cxx                                   |   
20 +-
 comphelper/source/misc/accessibleeventnotifier.cxx                         |   
16 -
 comphelper/source/misc/compbase.cxx                                        |   
 4 
 comphelper/source/misc/instancelocker.cxx                                  |   
 8 
 editeng/source/uno/unotext2.cxx                                            |   
 4 
 framework/source/uiconfiguration/imagemanagerimpl.cxx                      |   
12 -
 framework/source/uiconfiguration/moduleuiconfigurationmanager.cxx          |   
12 -
 framework/source/uiconfiguration/uiconfigurationmanager.cxx                |   
12 -
 include/comphelper/interfacecontainer4.hxx                                 |   
87 +++++++---
 include/comphelper/multiinterfacecontainer4.hxx                            |   
17 +
 include/vcl/weldutils.hxx                                                  |   
24 +-
 sc/source/ui/Accessibility/DrawModelBroadcaster.cxx                        |   
24 --
 scripting/source/stringresource/stringresource.cxx                         |   
61 +++----
 scripting/source/stringresource/stringresource.hxx                         |   
12 -
 sd/source/ui/slideshow/slideshowviewimpl.cxx                               |   
40 ++--
 sd/source/ui/slideshow/slideshowviewimpl.hxx                               |   
 4 
 sfx2/inc/unoctitm.hxx                                                      |   
 2 
 sfx2/source/control/unoctitm.cxx                                           |   
21 --
 sfx2/source/doc/printhelper.cxx                                            |   
 8 
 sfx2/source/notify/globalevents.cxx                                        |   
52 +----
 sfx2/source/view/viewsh.cxx                                                |   
28 ++-
 svl/source/fsstor/oinputstreamcontainer.cxx                                |   
 8 
 svl/source/fsstor/ostreamcontainer.cxx                                     |   
 8 
 svx/source/table/tabledesign.cxx                                           |   
12 -
 svx/source/unodialogs/textconversiondlgs/chinese_translation_unodialog.cxx |   
 4 
 sw/source/core/unocore/unobkm.cxx                                          |   
 4 
 sw/source/core/unocore/unochart.cxx                                        |   
23 +-
 sw/source/core/unocore/unofield.cxx                                        |   
37 ++--
 sw/source/core/unocore/unoframe.cxx                                        |   
 8 
 sw/source/core/unocore/unoftn.cxx                                          |   
 4 
 sw/source/core/unocore/unoidx.cxx                                          |   
 4 
 sw/source/core/unocore/unoparagraph.cxx                                    |   
 4 
 sw/source/core/unocore/unorefmk.cxx                                        |   
 8 
 sw/source/core/unocore/unosect.cxx                                         |   
 4 
 sw/source/core/unocore/unotbl.cxx                                          |   
32 +--
 sw/source/uibase/uno/unotxdoc.cxx                                          |   
 8 
 toolkit/source/controls/grid/gridcolumn.cxx                                |   
 7 
 toolkit/source/hatchwindow/documentcloser.cxx                              |   
 4 
 ucbhelper/source/provider/resultset.cxx                                    |   
16 +
 ucbhelper/source/provider/resultsethelper.cxx                              |   
 6 
 42 files changed, 349 insertions(+), 334 deletions(-)

New commits:
commit a2eaf99e46f370ffb3b73828c2bdc53dc193b9a4
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Fri Jan 14 10:56:50 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Fri Jan 14 21:04:10 2022 +0100

    make comphelper::OInterfaceContainerHelper4 more threadsafe
    
    (*) make all the methods that require an external mutex take a
    std::unique_lock as a parameter, so that call sites cannot forget
    
    (*) make the forEach method drop the lock when firing listener methods,
    to reduce the odds of deadlock
    
    Change-Id: I0a80e3b3d1c1c03b7de4a658d31fcc2847690903
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128415
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/basic/source/classes/sbunoobj.cxx 
b/basic/source/classes/sbunoobj.cxx
index 97e217ca8ba5..999972c94744 100644
--- a/basic/source/classes/sbunoobj.cxx
+++ b/basic/source/classes/sbunoobj.cxx
@@ -4364,13 +4364,13 @@ void SAL_CALL ModuleInvocationProxy::dispose()
 void SAL_CALL ModuleInvocationProxy::addEventListener( const Reference< 
XEventListener >& xListener )
 {
     std::unique_lock aGuard( m_aMutex );
-    m_aListeners.addInterface( xListener );
+    m_aListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL ModuleInvocationProxy::removeEventListener( const Reference< 
XEventListener >& xListener )
 {
     std::unique_lock aGuard( m_aMutex );
-    m_aListeners.removeInterface( xListener );
+    m_aListeners.removeInterface( aGuard, xListener );
 }
 
 
diff --git a/chart2/source/tools/ModifyListenerHelper.cxx 
b/chart2/source/tools/ModifyListenerHelper.cxx
index b05d6210aba7..0213e97d9720 100644
--- a/chart2/source/tools/ModifyListenerHelper.cxx
+++ b/chart2/source/tools/ModifyListenerHelper.cxx
@@ -38,23 +38,25 @@ void SAL_CALL ModifyEventForwarder::addModifyListener( 
const Reference< util::XM
 {
     std::unique_lock aGuard(m_aMutex);
 
-    m_aModifyListeners.addInterface( aListener );
+    m_aModifyListeners.addInterface( aGuard, aListener );
 }
 
 void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< 
util::XModifyListener >& aListener )
 {
     std::unique_lock aGuard(m_aMutex);
 
-    m_aModifyListeners.removeInterface( aListener );
+    m_aModifyListeners.removeInterface( aGuard, aListener );
 }
 
 // ____ XModifyListener ____
 void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
 {
-    if( m_aModifyListeners.getLength() == 0 )
+    std::unique_lock aGuard(m_aMutex);
+
+    if( m_aModifyListeners.getLength(aGuard) == 0 )
         return;
 
-    m_aModifyListeners.notifyEach( &util::XModifyListener::modified, aEvent );
+    m_aModifyListeners.notifyEach( aGuard, &util::XModifyListener::modified, 
aEvent );
 }
 
 // ____ XEventListener (base of XModifyListener) ____
diff --git a/chart2/source/tools/RangeHighlighter.cxx 
b/chart2/source/tools/RangeHighlighter.cxx
index 247daaaae7e0..4a7e4be66ca0 100644
--- a/chart2/source/tools/RangeHighlighter.cxx
+++ b/chart2/source/tools/RangeHighlighter.cxx
@@ -306,7 +306,8 @@ void SAL_CALL RangeHighlighter::addSelectionChangeListener( 
const Reference< vie
 
     if( m_nAddedListenerCount == 0 )
         startListening();
-    maSelectionChangeListeners.addInterface( xListener);
+    std::unique_lock g(m_aMutex);
+    maSelectionChangeListeners.addInterface( g, xListener);
     ++m_nAddedListenerCount;
 
     //bring the new listener up to the current state
@@ -316,7 +317,8 @@ void SAL_CALL RangeHighlighter::addSelectionChangeListener( 
const Reference< vie
 
 void SAL_CALL RangeHighlighter::removeSelectionChangeListener( const 
Reference< view::XSelectionChangeListener >& xListener )
 {
-    maSelectionChangeListeners.removeInterface( xListener );
+    std::unique_lock g(m_aMutex);
+    maSelectionChangeListeners.removeInterface( g, xListener );
     --m_nAddedListenerCount;
     if( m_nAddedListenerCount == 0 )
         stopListening();
@@ -334,14 +336,16 @@ void SAL_CALL RangeHighlighter::selectionChanged( const 
lang::EventObject& /*aEv
 
 void RangeHighlighter::fireSelectionEvent()
 {
-    if( maSelectionChangeListeners.getLength() )
+    std::unique_lock g(m_aMutex);
+    if( maSelectionChangeListeners.getLength(g) )
     {
         lang::EventObject aEvent( static_cast< lang::XComponent* >( this ) );
-        comphelper::OInterfaceIteratorHelper4 aIt( maSelectionChangeListeners 
);
-        while( aIt.hasMoreElements() )
-        {
-            aIt.next()->selectionChanged( aEvent );
-        }
+        maSelectionChangeListeners.forEach(g,
+            [&aEvent](const 
css::uno::Reference<view::XSelectionChangeListener>& xListener)
+            {
+                xListener->selectionChanged(aEvent);
+            }
+        );
     }
 }
 
diff --git a/comphelper/source/misc/accessibleeventnotifier.cxx 
b/comphelper/source/misc/accessibleeventnotifier.cxx
index c1e26c35bdd9..f8bd5a1170e8 100644
--- a/comphelper/source/misc/accessibleeventnotifier.cxx
+++ b/comphelper/source/misc/accessibleeventnotifier.cxx
@@ -209,7 +209,7 @@ void AccessibleEventNotifier::revokeClientNotifyDisposing(
 sal_Int32 AccessibleEventNotifier::addEventListener(
     const TClientId _nClient, const Reference< XAccessibleEventListener >& 
_rxListener )
 {
-    std::scoped_lock aGuard( GetLocalMutex() );
+    std::unique_lock aGuard( GetLocalMutex() );
 
     ClientMap::iterator aClientPos;
     if ( !implLookupClient( _nClient, aClientPos ) )
@@ -217,15 +217,15 @@ sal_Int32 AccessibleEventNotifier::addEventListener(
         return 0;
 
     if ( _rxListener.is() )
-        aClientPos->second->addInterface( _rxListener );
+        aClientPos->second->addInterface( aGuard, _rxListener );
 
-    return aClientPos->second->getLength();
+    return aClientPos->second->getLength(aGuard);
 }
 
 sal_Int32 AccessibleEventNotifier::removeEventListener(
     const TClientId _nClient, const Reference< XAccessibleEventListener >& 
_rxListener )
 {
-    std::scoped_lock aGuard( GetLocalMutex() );
+    std::unique_lock aGuard( GetLocalMutex() );
 
     ClientMap::iterator aClientPos;
     if ( !implLookupClient( _nClient, aClientPos ) )
@@ -233,9 +233,9 @@ sal_Int32 AccessibleEventNotifier::removeEventListener(
         return 0;
 
     if ( _rxListener.is() )
-        aClientPos->second->removeInterface( _rxListener );
+        aClientPos->second->removeInterface( aGuard,  _rxListener );
 
-    return aClientPos->second->getLength();
+    return aClientPos->second->getLength(aGuard);
 }
 
 void AccessibleEventNotifier::addEvent( const TClientId _nClient, const 
AccessibleEventObject& _rEvent )
@@ -243,7 +243,7 @@ void AccessibleEventNotifier::addEvent( const TClientId 
_nClient, const Accessib
     std::vector< Reference< XAccessibleEventListener > > aListeners;
 
     {
-        std::scoped_lock aGuard( GetLocalMutex() );
+        std::unique_lock aGuard( GetLocalMutex() );
 
         ClientMap::iterator aClientPos;
         if ( !implLookupClient( _nClient, aClientPos ) )
@@ -251,7 +251,7 @@ void AccessibleEventNotifier::addEvent( const TClientId 
_nClient, const Accessib
             return;
 
         // since we're synchronous, again, we want to notify immediately
-        aListeners = aClientPos->second->getElements();
+        aListeners = aClientPos->second->getElements(aGuard);
     }
 
     // default handling: loop through all listeners, and notify them
diff --git a/comphelper/source/misc/compbase.cxx 
b/comphelper/source/misc/compbase.cxx
index ff3c4778b2ba..f8a8897b7bb7 100644
--- a/comphelper/source/misc/compbase.cxx
+++ b/comphelper/source/misc/compbase.cxx
@@ -37,14 +37,14 @@ void SAL_CALL WeakComponentImplHelperBase::addEventListener(
     std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         return;
-    maEventListeners.addInterface(rxListener);
+    maEventListeners.addInterface(aGuard, rxListener);
 }
 
 void SAL_CALL WeakComponentImplHelperBase::removeEventListener(
     css::uno::Reference<css::lang::XEventListener> const& rxListener)
 {
     std::unique_lock aGuard(m_aMutex);
-    maEventListeners.removeInterface(rxListener);
+    maEventListeners.removeInterface(aGuard, rxListener);
 }
 
 css::uno::Any SAL_CALL 
WeakComponentImplHelperBase::queryInterface(css::uno::Type const& rType)
diff --git a/comphelper/source/misc/instancelocker.cxx 
b/comphelper/source/misc/instancelocker.cxx
index a24572c6c865..347ff513cb6e 100644
--- a/comphelper/source/misc/instancelocker.cxx
+++ b/comphelper/source/misc/instancelocker.cxx
@@ -87,18 +87,18 @@ void SAL_CALL OInstanceLocker::dispose()
 
 void SAL_CALL OInstanceLocker::addEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
-    m_aListenersContainer.addInterface( xListener );
+    m_aListenersContainer.addInterface( aGuard, xListener );
 }
 
 
 void SAL_CALL OInstanceLocker::removeEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
-    m_aListenersContainer.removeInterface( xListener );
+    std::unique_lock aGuard( m_aMutex );
+    m_aListenersContainer.removeInterface( aGuard, xListener );
 }
 
 // XInitialization
diff --git a/editeng/source/uno/unotext2.cxx b/editeng/source/uno/unotext2.cxx
index 275319f3dfe7..66f094a00e6a 100644
--- a/editeng/source/uno/unotext2.cxx
+++ b/editeng/source/uno/unotext2.cxx
@@ -242,13 +242,13 @@ void SAL_CALL SvxUnoTextContent::dispose()
 void SAL_CALL SvxUnoTextContent::addEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
     std::unique_lock aGuard(maDisposeContainerMutex);
-    maDisposeListeners.addInterface(xListener);
+    maDisposeListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SvxUnoTextContent::removeEventListener( const uno::Reference< 
lang::XEventListener >& aListener )
 {
    std::unique_lock aGuard(maDisposeContainerMutex);
-   maDisposeListeners.removeInterface(aListener);
+   maDisposeListeners.removeInterface(aGuard, aListener);
 }
 
 // XEnumerationAccess
diff --git a/framework/source/uiconfiguration/imagemanagerimpl.cxx 
b/framework/source/uiconfiguration/imagemanagerimpl.cxx
index 1dd41fd38bdf..fd18bb9b7c06 100644
--- a/framework/source/uiconfiguration/imagemanagerimpl.cxx
+++ b/framework/source/uiconfiguration/imagemanagerimpl.cxx
@@ -537,14 +537,14 @@ void ImageManagerImpl::addEventListener( const 
uno::Reference< XEventListener >&
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.addInterface( xListener );
+    m_aEventListeners.addInterface( aGuard, xListener );
 }
 
 void ImageManagerImpl::removeEventListener( const uno::Reference< 
XEventListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.removeInterface( xListener );
+    m_aEventListeners.removeInterface( aGuard, xListener );
 }
 
 // XInitialization
@@ -1154,20 +1154,20 @@ void ImageManagerImpl::addConfigurationListener( const 
uno::Reference< css::ui::
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.addInterface( xListener );
+    m_aConfigListeners.addInterface( aGuard, xListener );
 }
 
 void ImageManagerImpl::removeConfigurationListener( const uno::Reference< 
css::ui::XUIConfigurationListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.removeInterface( xListener );
+    m_aConfigListeners.removeInterface( aGuard, xListener );
 }
 
 void ImageManagerImpl::implts_notifyContainerListener( const 
ConfigurationEvent& aEvent, NotifyOp eOp )
 {
     std::unique_lock aGuard(m_mutex);
-    comphelper::OInterfaceIteratorHelper4 pIterator( m_aConfigListeners );
+    comphelper::OInterfaceIteratorHelper4 pIterator( aGuard, 
m_aConfigListeners );
     while ( pIterator.hasMoreElements() )
     {
         try
@@ -1187,7 +1187,7 @@ void ImageManagerImpl::implts_notifyContainerListener( 
const ConfigurationEvent&
         }
         catch( const css::uno::RuntimeException& )
         {
-            pIterator.remove();
+            pIterator.remove(aGuard);
         }
     }
 }
diff --git a/framework/source/uiconfiguration/moduleuiconfigurationmanager.cxx 
b/framework/source/uiconfiguration/moduleuiconfigurationmanager.cxx
index 28c2b150f9bb..65ff144913f9 100644
--- a/framework/source/uiconfiguration/moduleuiconfigurationmanager.cxx
+++ b/framework/source/uiconfiguration/moduleuiconfigurationmanager.cxx
@@ -943,14 +943,14 @@ void SAL_CALL 
ModuleUIConfigurationManager::addEventListener( const Reference< X
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.addInterface( xListener );
+    m_aEventListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL ModuleUIConfigurationManager::removeEventListener( const 
Reference< XEventListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.removeInterface( xListener );
+    m_aEventListeners.removeInterface( aGuard, xListener );
 }
 
 // XUIConfiguration
@@ -965,14 +965,14 @@ void SAL_CALL 
ModuleUIConfigurationManager::addConfigurationListener( const Refe
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.addInterface( xListener );
+    m_aConfigListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL ModuleUIConfigurationManager::removeConfigurationListener( const 
Reference< css::ui::XUIConfigurationListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.removeInterface( xListener );
+    m_aConfigListeners.removeInterface( aGuard, xListener );
 }
 
 // XUIConfigurationManager
@@ -1623,7 +1623,7 @@ sal_Bool SAL_CALL 
ModuleUIConfigurationManager::isReadOnly()
 void ModuleUIConfigurationManager::implts_notifyContainerListener( const 
ui::ConfigurationEvent& aEvent, NotifyOp eOp )
 {
     std::unique_lock aGuard(m_mutex);
-    comphelper::OInterfaceIteratorHelper4 pIterator( m_aConfigListeners );
+    comphelper::OInterfaceIteratorHelper4 pIterator( aGuard, 
m_aConfigListeners );
     while ( pIterator.hasMoreElements() )
     {
         try
@@ -1643,7 +1643,7 @@ void 
ModuleUIConfigurationManager::implts_notifyContainerListener( const ui::Con
         }
         catch( const css::uno::RuntimeException& )
         {
-            pIterator.remove();
+            pIterator.remove(aGuard);
         }
     }
 }
diff --git a/framework/source/uiconfiguration/uiconfigurationmanager.cxx 
b/framework/source/uiconfiguration/uiconfigurationmanager.cxx
index 002fde84daea..d44f4a45d418 100644
--- a/framework/source/uiconfiguration/uiconfigurationmanager.cxx
+++ b/framework/source/uiconfiguration/uiconfigurationmanager.cxx
@@ -727,14 +727,14 @@ void SAL_CALL UIConfigurationManager::addEventListener( 
const Reference< XEventL
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.addInterface( xListener );
+    m_aEventListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL UIConfigurationManager::removeEventListener( const Reference< 
XEventListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aEventListeners.removeInterface( xListener );
+    m_aEventListeners.removeInterface( aGuard, xListener );
 }
 
 // XUIConfigurationManager
@@ -749,14 +749,14 @@ void SAL_CALL 
UIConfigurationManager::addConfigurationListener( const Reference<
     }
 
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.addInterface( xListener );
+    m_aConfigListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL UIConfigurationManager::removeConfigurationListener( const 
Reference< css::ui::XUIConfigurationListener >& xListener )
 {
     /* SAFE AREA 
-----------------------------------------------------------------------------------------------
 */
     std::unique_lock aGuard(m_mutex);
-    m_aConfigListeners.removeInterface( xListener );
+    m_aConfigListeners.removeInterface( aGuard, xListener );
 }
 
 void SAL_CALL UIConfigurationManager::reset()
@@ -1347,7 +1347,7 @@ sal_Bool SAL_CALL UIConfigurationManager::isReadOnly()
 void UIConfigurationManager::implts_notifyContainerListener( const 
ConfigurationEvent& aEvent, NotifyOp eOp )
 {
     std::unique_lock aGuard(m_mutex);
-    comphelper::OInterfaceIteratorHelper4 pIterator( m_aConfigListeners );
+    comphelper::OInterfaceIteratorHelper4 pIterator( aGuard, 
m_aConfigListeners );
     while ( pIterator.hasMoreElements() )
     {
         try
@@ -1367,7 +1367,7 @@ void 
UIConfigurationManager::implts_notifyContainerListener( const Configuration
         }
         catch( const css::uno::RuntimeException& )
         {
-            pIterator.remove();
+            pIterator.remove(aGuard);
         }
     }
 }
diff --git a/include/comphelper/interfacecontainer4.hxx 
b/include/comphelper/interfacecontainer4.hxx
index e58b958629b9..b8e2079a9201 100644
--- a/include/comphelper/interfacecontainer4.hxx
+++ b/include/comphelper/interfacecontainer4.hxx
@@ -56,8 +56,11 @@ public:
        change the contents...
 
        @param rCont the container of the elements.
+       @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
      */
-    OInterfaceIteratorHelper4(OInterfaceContainerHelper4<ListenerT>& rCont_)
+    OInterfaceIteratorHelper4(std::unique_lock<std::mutex>& /*rGuard*/,
+                              OInterfaceContainerHelper4<ListenerT>& rCont_)
         : rCont(rCont_)
         , maData(rCont.maData)
         , nRemain(maData->size())
@@ -75,8 +78,10 @@ public:
         from the underlying container. Calling this method before
         next() has been called or calling it twice with no next()
         in between is an error.
+        @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
     */
-    void remove();
+    void remove(::std::unique_lock<::std::mutex>& rGuard);
 
 private:
     OInterfaceContainerHelper4<ListenerT>& rCont;
@@ -94,9 +99,10 @@ const css::uno::Reference<ListenerT>& 
OInterfaceIteratorHelper4<ListenerT>::next
     return (*maData)[nRemain];
 }
 
-template <class ListenerT> void OInterfaceIteratorHelper4<ListenerT>::remove()
+template <class ListenerT>
+void 
OInterfaceIteratorHelper4<ListenerT>::remove(::std::unique_lock<::std::mutex>& 
rGuard)
 {
-    rCont.removeInterface((*maData)[nRemain]);
+    rCont.removeInterface(rGuard, (*maData)[nRemain]);
 }
 
 /**
@@ -119,13 +125,18 @@ public:
     /**
       Return the number of Elements in the container. Only useful if you have 
acquired
       the mutex.
+      @param rGuard
+        this parameter only here to make that this container is accessed while 
locked
      */
-    sal_Int32 getLength() const;
+    sal_Int32 getLength(std::unique_lock<std::mutex>& rGuard) const;
 
     /**
       Return all interfaces added to this container.
+      @param rGuard
+          this parameter only here to make that this container is accessed 
while locked
      **/
-    std::vector<css::uno::Reference<ListenerT>> getElements() const;
+    std::vector<css::uno::Reference<ListenerT>>
+    getElements(std::unique_lock<std::mutex>& rGuard) const;
 
     /** Inserts an element into the container.  The position is not specified, 
thus it is not
         specified in which order events are fired.
@@ -140,18 +151,24 @@ public:
         @param rxIFace
                interface to be added; it is allowed to insert
                the same interface more than once
+        @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
         @return
                 the new count of elements in the container
     */
-    sal_Int32 addInterface(const css::uno::Reference<ListenerT>& rxIFace);
+    sal_Int32 addInterface(std::unique_lock<std::mutex>& rGuard,
+                           const css::uno::Reference<ListenerT>& rxIFace);
     /** Removes an element from the container.  It uses interface equality to 
remove the interface.
 
         @param rxIFace
                interface to be removed
+        @param rGuard
+               this parameter only here to make that this container is 
accessed while locked
         @return
                 the new count of elements in the container
     */
-    sal_Int32 removeInterface(const css::uno::Reference<ListenerT>& rxIFace);
+    sal_Int32 removeInterface(std::unique_lock<std::mutex>& rGuard,
+                              const css::uno::Reference<ListenerT>& rxIFace);
     /**
       Call disposing on all object in the container that
       support XEventListener. Then clear the container.
@@ -161,8 +178,10 @@ public:
                          const css::lang::EventObject& rEvt);
     /**
       Clears the container without calling disposing().
+        @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
      */
-    void clear();
+    void clear(::std::unique_lock<::std::mutex>& rGuard);
 
     /** Executes a functor for each contained listener of specified type, e.g.
         <code>forEach<awt::XPaintListener>(...</code>.
@@ -173,8 +192,11 @@ public:
         @tparam FuncT unary functor type, let your compiler deduce this for you
         @param func unary functor object expecting an argument of type
                     css::uno::Reference<ListenerT>
+        @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
     */
-    template <typename FuncT> inline void forEach(FuncT const& func);
+    template <typename FuncT>
+    inline void forEach(std::unique_lock<std::mutex>& rGuard, FuncT const& 
func);
 
     /** Calls a UNO listener method for each contained listener.
 
@@ -189,6 +211,8 @@ public:
             Pointer to a method of a ListenerT interface.
         @param Event
             Event to notify to all contained listeners
+        @param rGuard
+            this parameter only here to make that this container is accessed 
while locked
 
         Example:
 @code
@@ -197,7 +221,8 @@ public:
 @endcode
     */
     template <typename EventT>
-    inline void notifyEach(void (SAL_CALL 
ListenerT::*NotificationMethod)(const EventT&),
+    inline void notifyEach(std::unique_lock<std::mutex>& rGuard,
+                           void (SAL_CALL 
ListenerT::*NotificationMethod)(const EventT&),
                            const EventT& Event);
 
 private:
@@ -242,9 +267,14 @@ inline 
OInterfaceContainerHelper4<T>::OInterfaceContainerHelper4()
 
 template <class T>
 template <typename FuncT>
-inline void OInterfaceContainerHelper4<T>::forEach(FuncT const& func)
+inline void 
OInterfaceContainerHelper4<T>::forEach(std::unique_lock<std::mutex>& rGuard,
+                                                   FuncT const& func)
 {
-    OInterfaceIteratorHelper4<T> iter(*this);
+    if (std::as_const(maData)->size() == 0)
+        return;
+    maData.make_unique(); // so we can iterate over the data without holding 
the lock
+    OInterfaceIteratorHelper4<T> iter(rGuard, *this);
+    rGuard.unlock();
     while (iter.hasMoreElements())
     {
         auto xListener = iter.next();
@@ -255,34 +285,44 @@ inline void OInterfaceContainerHelper4<T>::forEach(FuncT 
const& func)
         catch (css::lang::DisposedException const& exc)
         {
             if (exc.Context == xListener)
-                iter.remove();
+            {
+                rGuard.lock();
+                iter.remove(rGuard);
+                rGuard.unlock();
+            }
         }
     }
+    rGuard.lock();
 }
 
 template <class ListenerT>
 template <typename EventT>
 inline void OInterfaceContainerHelper4<ListenerT>::notifyEach(
+    std::unique_lock<std::mutex>& rGuard,
     void (SAL_CALL ListenerT::*NotificationMethod)(const EventT&), const 
EventT& Event)
 {
-    
forEach<NotifySingleListener<EventT>>(NotifySingleListener<EventT>(NotificationMethod,
 Event));
+    forEach<NotifySingleListener<EventT>>(rGuard,
+                                          
NotifySingleListener<EventT>(NotificationMethod, Event));
 }
 
-template <class ListenerT> sal_Int32 
OInterfaceContainerHelper4<ListenerT>::getLength() const
+template <class ListenerT>
+sal_Int32
+OInterfaceContainerHelper4<ListenerT>::getLength(std::unique_lock<std::mutex>& 
/*rGuard*/) const
 {
     return maData->size();
 }
 
 template <class ListenerT>
 std::vector<css::uno::Reference<ListenerT>>
-OInterfaceContainerHelper4<ListenerT>::getElements() const
+OInterfaceContainerHelper4<ListenerT>::getElements(std::unique_lock<std::mutex>&
 /*rGuard*/) const
 {
     return *maData;
 }
 
 template <class ListenerT>
 sal_Int32
-OInterfaceContainerHelper4<ListenerT>::addInterface(const 
css::uno::Reference<ListenerT>& rListener)
+OInterfaceContainerHelper4<ListenerT>::addInterface(std::unique_lock<std::mutex>&
 /*rGuard*/,
+                                                    const 
css::uno::Reference<ListenerT>& rListener)
 {
     assert(rListener.is());
     maData->push_back(rListener);
@@ -291,7 +331,7 @@ OInterfaceContainerHelper4<ListenerT>::addInterface(const 
css::uno::Reference<Li
 
 template <class ListenerT>
 sal_Int32 OInterfaceContainerHelper4<ListenerT>::removeInterface(
-    const css::uno::Reference<ListenerT>& rListener)
+    std::unique_lock<std::mutex>& /*rGuard*/, const 
css::uno::Reference<ListenerT>& rListener)
 {
     assert(rListener.is());
 
@@ -315,9 +355,10 @@ template <class ListenerT>
 void 
OInterfaceContainerHelper4<ListenerT>::disposeAndClear(std::unique_lock<std::mutex>&
 rGuard,
                                                             const 
css::lang::EventObject& rEvt)
 {
-    OInterfaceIteratorHelper4<ListenerT> aIt(*this);
+    OInterfaceIteratorHelper4<ListenerT> aIt(rGuard, *this);
     maData->clear();
     rGuard.unlock();
+    // unlock followed by iterating is only safe because we are not going to 
call remove() on the iterator
     while (aIt.hasMoreElements())
     {
         try
@@ -332,7 +373,11 @@ void 
OInterfaceContainerHelper4<ListenerT>::disposeAndClear(std::unique_lock<std
     }
 }
 
-template <class ListenerT> void OInterfaceContainerHelper4<ListenerT>::clear() 
{ maData->clear(); }
+template <class ListenerT>
+void 
OInterfaceContainerHelper4<ListenerT>::clear(::std::unique_lock<::std::mutex>& 
/*rGuard*/)
+{
+    maData->clear();
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/comphelper/multiinterfacecontainer4.hxx 
b/include/comphelper/multiinterfacecontainer4.hxx
index 5d2457669f0d..eeccd20920ca 100644
--- a/include/comphelper/multiinterfacecontainer4.hxx
+++ b/include/comphelper/multiinterfacecontainer4.hxx
@@ -43,13 +43,13 @@ public:
     /**
       Return all id's under which at least one interface is added.
      */
-    inline std::vector<key> getContainedTypes() const
+    inline std::vector<key> getContainedTypes(std::unique_lock<std::mutex>& 
rGuard) const
     {
         std::vector<key> aInterfaceTypes;
         aInterfaceTypes.reserve(m_aMap.size());
         for (const auto& rPair : m_aMap)
             // are interfaces added to this container?
-            if (rPair.second->getLength())
+            if (rPair.second->getLength(rGuard))
                 // yes, put the type in the array
                 aInterfaceTypes.push_back(rPair.first);
         return aInterfaceTypes;
@@ -91,17 +91,18 @@ public:
         @return
                 the new count of elements in the container
     */
-    inline sal_Int32 addInterface(const key& rKey, const 
css::uno::Reference<listener>& rListener)
+    inline sal_Int32 addInterface(::std::unique_lock<::std::mutex>& rGuard, 
const key& rKey,
+                                  const css::uno::Reference<listener>& 
rListener)
     {
         auto iter = find(rKey);
         if (iter == m_aMap.end())
         {
             auto pLC = new OInterfaceContainerHelper4<listener>();
             m_aMap.emplace_back(rKey, pLC);
-            return pLC->addInterface(rListener);
+            return pLC->addInterface(rGuard, rListener);
         }
         else
-            return (*iter).second->addInterface(rListener);
+            return (*iter).second->addInterface(rGuard, rListener);
     }
     /** Removes an element from the container with the specified key.
         It uses interface equality to remove the interface.
@@ -112,14 +113,14 @@ public:
         @return
                 the new count of elements in the container
     */
-    inline sal_Int32 removeInterface(const key& rKey,
+    inline sal_Int32 removeInterface(::std::unique_lock<::std::mutex>& rGuard, 
const key& rKey,
                                      const css::uno::Reference<listener>& 
rListener)
     {
         // search container with id nUik
         auto iter = find(rKey);
         // container found?
         if (iter != m_aMap.end())
-            return (*iter).second->removeInterface(rListener);
+            return (*iter).second->removeInterface(rGuard, rListener);
         // no container with this id. Always return 0
         return 0;
     }
@@ -139,7 +140,7 @@ public:
         rGuard.unlock();
         for (auto& rPair : tempMap)
         {
-            OInterfaceIteratorHelper4<listener> aIt(*rPair.second);
+            OInterfaceIteratorHelper4<listener> aIt(rGuard, *rPair.second);
             while (aIt.hasMoreElements())
             {
                 try
diff --git a/include/vcl/weldutils.hxx b/include/vcl/weldutils.hxx
index a8c2e1eac11d..07fc5eb2d684 100644
--- a/include/vcl/weldutils.hxx
+++ b/include/vcl/weldutils.hxx
@@ -89,84 +89,84 @@ public:
     addWindowListener(const css::uno::Reference<css::awt::XWindowListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aWindowListeners.addInterface(rListener);
+        m_aWindowListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL
     removeWindowListener(const css::uno::Reference<css::awt::XWindowListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aWindowListeners.removeInterface(rListener);
+        m_aWindowListeners.removeInterface(g, rListener);
     }
 
     void SAL_CALL
     addFocusListener(const css::uno::Reference<css::awt::XFocusListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aFocusListeners.addInterface(rListener);
+        m_aFocusListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL
     removeFocusListener(const css::uno::Reference<css::awt::XFocusListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aFocusListeners.removeInterface(rListener);
+        m_aFocusListeners.removeInterface(g, rListener);
     }
 
     void SAL_CALL
     addKeyListener(const css::uno::Reference<css::awt::XKeyListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aKeyListeners.addInterface(rListener);
+        m_aKeyListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL
     removeKeyListener(const css::uno::Reference<css::awt::XKeyListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aKeyListeners.removeInterface(rListener);
+        m_aKeyListeners.removeInterface(g, rListener);
     }
 
     void SAL_CALL
     addMouseListener(const css::uno::Reference<css::awt::XMouseListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aMouseListeners.addInterface(rListener);
+        m_aMouseListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL
     removeMouseListener(const css::uno::Reference<css::awt::XMouseListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aMouseListeners.removeInterface(rListener);
+        m_aMouseListeners.removeInterface(g, rListener);
     }
 
     void SAL_CALL addMouseMotionListener(
         const css::uno::Reference<css::awt::XMouseMotionListener>& rListener) 
override
     {
         std::unique_lock g(m_aMutex);
-        m_aMotionListeners.addInterface(rListener);
+        m_aMotionListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL removeMouseMotionListener(
         const css::uno::Reference<css::awt::XMouseMotionListener>& rListener) 
override
     {
         std::unique_lock g(m_aMutex);
-        m_aMotionListeners.removeInterface(rListener);
+        m_aMotionListeners.removeInterface(g, rListener);
     }
 
     void SAL_CALL
     addPaintListener(const css::uno::Reference<css::awt::XPaintListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aPaintListeners.addInterface(rListener);
+        m_aPaintListeners.addInterface(g, rListener);
     }
 
     void SAL_CALL
     removePaintListener(const css::uno::Reference<css::awt::XPaintListener>& 
rListener) override
     {
         std::unique_lock g(m_aMutex);
-        m_aPaintListeners.removeInterface(rListener);
+        m_aPaintListeners.removeInterface(g, rListener);
     }
 };
 
diff --git a/sc/source/ui/Accessibility/DrawModelBroadcaster.cxx 
b/sc/source/ui/Accessibility/DrawModelBroadcaster.cxx
index 8a4a8d437440..043aa479fdf1 100644
--- a/sc/source/ui/Accessibility/DrawModelBroadcaster.cxx
+++ b/sc/source/ui/Accessibility/DrawModelBroadcaster.cxx
@@ -40,14 +40,14 @@ ScDrawModelBroadcaster::~ScDrawModelBroadcaster()
 
 void SAL_CALL ScDrawModelBroadcaster::addEventListener( const uno::Reference< 
document::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard(maListenerMutex);
-    maEventListeners.addInterface( xListener );
+    std::unique_lock aGuard(maListenerMutex);
+    maEventListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL ScDrawModelBroadcaster::removeEventListener( const 
uno::Reference< document::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard(maListenerMutex);
-    maEventListeners.removeInterface( xListener );
+    std::unique_lock aGuard(maListenerMutex);
+    maEventListeners.removeInterface( aGuard, xListener );
 }
 
 void SAL_CALL ScDrawModelBroadcaster::addShapeEventListener(
@@ -87,20 +87,12 @@ void ScDrawModelBroadcaster::Notify( SfxBroadcaster&,
         return;
 
     std::unique_lock aGuard(maListenerMutex);
-    ::comphelper::OInterfaceIteratorHelper4 aIter( maEventListeners );
-    aGuard.unlock();
-    while( aIter.hasMoreElements() )
-    {
-        const uno::Reference < document::XEventListener >& xListener = 
aIter.next();
-        try
-        {
-            xListener->notifyEvent( aEvent );
-        }
-        catch( const uno::RuntimeException& )
+    maEventListeners.forEach(aGuard,
+        [&aEvent](const css::uno::Reference<document::XEventListener>& 
xListener)
         {
-            TOOLS_WARN_EXCEPTION("sc.ui", "Runtime exception caught while 
notifying shape");
+            xListener->notifyEvent(aEvent);
         }
-    }
+    );
 
     // right now, we're only handling the specific event necessary to fix this 
performance problem
     if (pSdrHint->GetKind() == SdrHintKind::ObjectChange)
diff --git a/scripting/source/stringresource/stringresource.cxx 
b/scripting/source/stringresource/stringresource.cxx
index 3b3c356fa784..c6cdf9ba9225 100644
--- a/scripting/source/stringresource/stringresource.cxx
+++ b/scripting/source/stringresource/stringresource.cxx
@@ -103,7 +103,7 @@ void StringResourceImpl::addModifyListener( const 
Reference< XModifyListener >&
         throw RuntimeException();
 
     std::unique_lock aGuard( m_aMutex );
-    m_aListenerContainer.addInterface( aListener );
+    m_aListenerContainer.addInterface( aGuard, aListener );
 }
 
 void StringResourceImpl::removeModifyListener( const Reference< 
XModifyListener >& aListener )
@@ -112,7 +112,7 @@ void StringResourceImpl::removeModifyListener( const 
Reference< XModifyListener
         throw RuntimeException();
 
     std::unique_lock aGuard( m_aMutex );
-    m_aListenerContainer.removeInterface( aListener );
+    m_aListenerContainer.removeInterface( aGuard, aListener );
 }
 
 
@@ -266,7 +266,7 @@ sal_Bool StringResourceImpl::isReadOnly()
     return m_bReadOnly;
 }
 
-void StringResourceImpl::implSetCurrentLocale( const Locale& locale,
+void StringResourceImpl::implSetCurrentLocale( std::unique_lock<std::mutex>& 
rGuard, const Locale& locale,
     bool FindClosestMatch, bool bUseDefaultIfNoMatch )
 {
     LocaleItem* pLocaleItem = nullptr;
@@ -284,14 +284,14 @@ void StringResourceImpl::implSetCurrentLocale( const 
Locale& locale,
         m_pCurrentLocaleItem = pLocaleItem;
 
         // Only notify without modifying
-        implNotifyListeners();
+        implNotifyListeners(rGuard);
     }
 }
 
 void StringResourceImpl::setCurrentLocale( const Locale& locale, sal_Bool 
FindClosestMatch )
 {
     std::unique_lock aGuard( m_aMutex );
-    implSetCurrentLocale( locale, FindClosestMatch, 
false/*bUseDefaultIfNoMatch*/ );
+    implSetCurrentLocale( aGuard, locale, FindClosestMatch, 
false/*bUseDefaultIfNoMatch*/ );
 }
 
 void StringResourceImpl::setDefaultLocale( const Locale& locale )
@@ -310,11 +310,11 @@ void StringResourceImpl::setDefaultLocale( const Locale& 
locale )
 
         m_pDefaultLocaleItem = pLocaleItem;
         m_bDefaultModified = true;
-        implModified();
+        implModified(aGuard);
     }
 }
 
-void StringResourceImpl::implSetString( const OUString& ResourceID,
+void StringResourceImpl::implSetString( std::unique_lock<std::mutex>& rGuard, 
const OUString& ResourceID,
     const OUString& Str, LocaleItem* pLocaleItem )
 {
     if( !(pLocaleItem != nullptr && loadLocale( pLocaleItem )) )
@@ -332,14 +332,14 @@ void StringResourceImpl::implSetString( const OUString& 
ResourceID,
     }
     rHashMap[ ResourceID ] = Str;
     pLocaleItem->m_bModified = true;
-    implModified();
+    implModified(rGuard);
 }
 
 void StringResourceImpl::setString( const OUString& ResourceID, const 
OUString& Str )
 {
     std::unique_lock aGuard( m_aMutex );
     implCheckReadOnly( "StringResourceImpl::setString(): Read only" );
-    implSetString( ResourceID, Str, m_pCurrentLocaleItem );
+    implSetString( aGuard, ResourceID, Str, m_pCurrentLocaleItem );
 }
 
 void StringResourceImpl::setStringForLocale
@@ -348,10 +348,10 @@ void StringResourceImpl::setStringForLocale
     std::unique_lock aGuard( m_aMutex );
     implCheckReadOnly( "StringResourceImpl::setStringForLocale(): Read only" );
     LocaleItem* pLocaleItem = getItemForLocale( locale, false );
-    implSetString( ResourceID, Str, pLocaleItem );
+    implSetString( aGuard, ResourceID, Str, pLocaleItem );
 }
 
-void StringResourceImpl::implRemoveId( const OUString& ResourceID, LocaleItem* 
pLocaleItem )
+void StringResourceImpl::implRemoveId( std::unique_lock<std::mutex>& rGuard, 
const OUString& ResourceID, LocaleItem* pLocaleItem )
 {
     if( pLocaleItem != nullptr && loadLocale( pLocaleItem ) )
     {
@@ -363,7 +363,7 @@ void StringResourceImpl::implRemoveId( const OUString& 
ResourceID, LocaleItem* p
         }
         rHashMap.erase( it );
         pLocaleItem->m_bModified = true;
-        implModified();
+        implModified(rGuard);
     }
 }
 
@@ -371,7 +371,7 @@ void StringResourceImpl::removeId( const OUString& 
ResourceID )
 {
     std::unique_lock aGuard( m_aMutex );
     implCheckReadOnly( "StringResourceImpl::removeId(): Read only" );
-    implRemoveId( ResourceID, m_pCurrentLocaleItem );
+    implRemoveId( aGuard, ResourceID, m_pCurrentLocaleItem );
 }
 
 void StringResourceImpl::removeIdForLocale( const OUString& ResourceID, const 
Locale& locale )
@@ -379,7 +379,7 @@ void StringResourceImpl::removeIdForLocale( const OUString& 
ResourceID, const Lo
     std::unique_lock aGuard( m_aMutex );
     implCheckReadOnly( "StringResourceImpl::removeIdForLocale(): Read only" );
     LocaleItem* pLocaleItem = getItemForLocale( locale, false );
-    implRemoveId( ResourceID, pLocaleItem );
+    implRemoveId( aGuard, ResourceID, pLocaleItem );
 }
 
 void StringResourceImpl::newLocale( const Locale& locale )
@@ -438,7 +438,7 @@ void StringResourceImpl::newLocale( const Locale& locale )
         m_bDefaultModified = true;
     }
 
-    implModified();
+    implModified(aGuard);
 }
 
 void StringResourceImpl::removeLocale( const Locale& locale )
@@ -499,7 +499,7 @@ void StringResourceImpl::removeLocale( const Locale& locale 
)
 
     m_aLocaleItemVector.erase( it );
 
-    implModified();
+    implModified(aGuard);
 }
 
 void StringResourceImpl::implScanIdForNumber( const OUString& ResourceID )
@@ -590,29 +590,22 @@ LocaleItem* 
StringResourceImpl::getClosestMatchItemForLocale( const Locale& loca
     return pRetItem;
 }
 
-void StringResourceImpl::implModified()
+void StringResourceImpl::implModified(std::unique_lock<std::mutex>& rGuard)
 {
     m_bModified = true;
-    implNotifyListeners();
+    implNotifyListeners(rGuard);
 }
 
-void StringResourceImpl::implNotifyListeners()
+void StringResourceImpl::implNotifyListeners(std::unique_lock<std::mutex>& 
rGuard)
 {
     EventObject aEvent;
     aEvent.Source = static_cast< XInterface* >( 
static_cast<OWeakObject*>(this) );
-
-    ::comphelper::OInterfaceIteratorHelper4 it( m_aListenerContainer );
-    while( it.hasMoreElements() )
-    {
-        try
+    m_aListenerContainer.forEach(rGuard,
+        [&aEvent](const css::uno::Reference<XModifyListener>& xListener)
         {
-            it.next()->modified( aEvent );
+            xListener->modified(aEvent);
         }
-        catch(RuntimeException&)
-        {
-            it.remove();
-        }
-    }
+    );
 }
 
 
@@ -671,7 +664,7 @@ Sequence< OUString > 
StringResourcePersistenceImpl::getSupportedServiceNames(  )
 constexpr OUStringLiteral aNameBaseDefaultStr = u"strings";
 
 void StringResourcePersistenceImpl::implInitializeCommonParameters
-    ( const Sequence< Any >& aArguments )
+    ( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
 {
     bool bReadOnlyOk = (aArguments[1] >>= m_bReadOnly);
     if( !bReadOnlyOk )
@@ -702,7 +695,7 @@ void 
StringResourcePersistenceImpl::implInitializeCommonParameters
 
     implScanLocales();
 
-    implSetCurrentLocale( aCurrentLocale, true/*FindClosestMatch*/, 
true/*bUseDefaultIfNoMatch*/ );
+    implSetCurrentLocale( rGuard, aCurrentLocale, true/*FindClosestMatch*/, 
true/*bUseDefaultIfNoMatch*/ );
 }
 
 
@@ -2078,7 +2071,7 @@ void StringResourceWithStorageImpl::initialize( const 
Sequence< Any >& aArgument
         throw IllegalArgumentException( 
"StringResourceWithStorageImpl::initialize: invalid storage", Reference< 
XInterface >(), 0 );
     }
 
-    implInitializeCommonParameters( aArguments );
+    implInitializeCommonParameters( aGuard, aArguments );
 }
 
 
@@ -2368,7 +2361,7 @@ void StringResourceWithLocationImpl::initialize( const 
Sequence< Any >& aArgumen
         throw IllegalArgumentException( 
"StringResourceWithStorageImpl::initialize: invalid type", Reference< 
XInterface >(), 5 );
     }
 
-    implInitializeCommonParameters( aArguments );
+    implInitializeCommonParameters( aGuard, aArguments );
 }
 
 
diff --git a/scripting/source/stringresource/stringresource.hxx 
b/scripting/source/stringresource/stringresource.hxx
index 1cce2c93782c..a2494cda8a6b 100644
--- a/scripting/source/stringresource/stringresource.hxx
+++ b/scripting/source/stringresource/stringresource.hxx
@@ -117,21 +117,21 @@ protected:
     LocaleItem* getClosestMatchItemForLocale( const css::lang::Locale& locale 
);
     /// @throws css::lang::IllegalArgumentException
     /// @throws css::uno::RuntimeException
-    void implSetCurrentLocale( const css::lang::Locale& locale,
+    void implSetCurrentLocale( std::unique_lock<std::mutex>& rGuard, const 
css::lang::Locale& locale,
         bool FindClosestMatch, bool bUseDefaultIfNoMatch );
 
-    void implModified();
-    void implNotifyListeners();
+    void implModified(std::unique_lock<std::mutex>&);
+    void implNotifyListeners(std::unique_lock<std::mutex>&);
 
     //=== Impl methods for ...ForLocale methods ===
     /// @throws css::resource::MissingResourceException
     OUString implResolveString( const OUString& ResourceID, LocaleItem* 
pLocaleItem );
     bool implHasEntryForId( const OUString& ResourceID, LocaleItem* 
pLocaleItem );
     css::uno::Sequence< OUString > implGetResourceIDs( LocaleItem* pLocaleItem 
);
-    void implSetString( const OUString& ResourceID,
+    void implSetString( std::unique_lock<std::mutex>& rGuard, const OUString& 
ResourceID,
         const OUString& Str, LocaleItem* pLocaleItem );
     /// @throws css::resource::MissingResourceException
-    void implRemoveId( const OUString& ResourceID, LocaleItem* pLocaleItem );
+    void implRemoveId( std::unique_lock<std::mutex>& rGuard, const OUString& 
ResourceID, LocaleItem* pLocaleItem );
 
     // Method to load a locale if necessary, returns true if loading was
     // successful. Default implementation in base class just returns true.
@@ -196,7 +196,7 @@ protected:
 
     /// @throws css::uno::Exception
     /// @throws css::uno::RuntimeException
-    void implInitializeCommonParameters( const css::uno::Sequence< 
css::uno::Any >& aArguments );
+    void implInitializeCommonParameters( std::unique_lock<std::mutex>& rGuard, 
const css::uno::Sequence< css::uno::Any >& aArguments );
 
     // Scan locale properties files
     virtual void implScanLocales();
diff --git a/sd/source/ui/slideshow/slideshowviewimpl.cxx 
b/sd/source/ui/slideshow/slideshowviewimpl.cxx
index 29eca42ea45f..6d2b1613d029 100644
--- a/sd/source/ui/slideshow/slideshowviewimpl.cxx
+++ b/sd/source/ui/slideshow/slideshowviewimpl.cxx
@@ -50,9 +50,9 @@ using namespace ::com::sun::star;
 namespace sd
 {
 
-void SlideShowViewMouseListeners::notify( const WrappedMouseEvent& rEvent )
+void SlideShowViewMouseListeners::notify( std::unique_lock<std::mutex>& 
rGuard, const WrappedMouseEvent& rEvent )
 {
-    forEach(
+    forEach(rGuard,
         [&rEvent] (const Reference<css::awt::XMouseListener>& rListener)
         {
             switch( rEvent.meType )
@@ -77,9 +77,9 @@ void SlideShowViewMouseListeners::notify( const 
WrappedMouseEvent& rEvent )
 }
 
 
-void SlideShowViewMouseMotionListeners::notify( const WrappedMouseMotionEvent& 
rEvent )
+void SlideShowViewMouseMotionListeners::notify( std::unique_lock<std::mutex>& 
rGuard,const WrappedMouseMotionEvent& rEvent )
 {
-    forEach(
+    forEach(rGuard,
         [&rEvent] (const Reference< awt::XMouseMotionListener >&  rListener)
         {
             switch( rEvent.meType )
@@ -188,17 +188,17 @@ void 
SlideShowView::disposingImpl(std::unique_lock<std::mutex>& rGuard)
         }
         rGuard.lock();
     }
-    if (maPaintListeners.getLength())
+    if (maPaintListeners.getLength(rGuard))
     {
         maPaintListeners.disposeAndClear( rGuard, evt );
         rGuard.lock();
     }
-    if (maMouseListeners.getLength())
+    if (maMouseListeners.getLength(rGuard))
     {
         maMouseListeners.disposeAndClear( rGuard, evt );
         rGuard.lock();
     }
-    if (maMouseMotionListeners.getLength())
+    if (maMouseMotionListeners.getLength(rGuard))
     {
         maMouseMotionListeners.disposeAndClear( rGuard, evt );
         rGuard.lock();
@@ -223,7 +223,7 @@ void SlideShowView::paint( const awt::PaintEvent& e )
         // with view
         awt::PaintEvent aEvent( e );
         aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
-        maPaintListeners.notifyEach( &css::awt::XPaintListener::windowPaint, 
aEvent );
+        maPaintListeners.notifyEach( aGuard, 
&css::awt::XPaintListener::windowPaint, aEvent );
         updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
     }
 }
@@ -351,7 +351,7 @@ void SAL_CALL SlideShowView::addPaintListener( const 
Reference< awt::XPaintListe
     std::unique_lock aGuard( m_aMutex );
 
     if (!m_bDisposed)
-        maPaintListeners.addInterface( xListener );
+        maPaintListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL SlideShowView::removePaintListener( const Reference< 
awt::XPaintListener >& xListener )
@@ -359,7 +359,7 @@ void SAL_CALL SlideShowView::removePaintListener( const 
Reference< awt::XPaintLi
     std::unique_lock aGuard( m_aMutex );
 
     if (!m_bDisposed)
-        maPaintListeners.removeInterface( xListener );
+        maPaintListeners.removeInterface( aGuard, xListener );
 }
 
 void SAL_CALL SlideShowView::addMouseListener( const Reference< 
awt::XMouseListener >& xListener )
@@ -367,7 +367,7 @@ void SAL_CALL SlideShowView::addMouseListener( const 
Reference< awt::XMouseListe
     std::unique_lock aGuard( m_aMutex );
 
     if (!m_bDisposed)
-        maMouseListeners.addInterface( xListener );
+        maMouseListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL SlideShowView::removeMouseListener( const Reference< 
awt::XMouseListener >& xListener )
@@ -375,7 +375,7 @@ void SAL_CALL SlideShowView::removeMouseListener( const 
Reference< awt::XMouseLi
     std::unique_lock aGuard( m_aMutex );
 
     if (!m_bDisposed)
-        maMouseListeners.removeInterface( xListener );
+        maMouseListeners.removeInterface( aGuard, xListener );
 }
 
 void SAL_CALL SlideShowView::addMouseMotionListener( const Reference< 
awt::XMouseMotionListener >& xListener )
@@ -393,7 +393,7 @@ void SAL_CALL SlideShowView::addMouseMotionListener( const 
Reference< awt::XMous
         mxWindow->addMouseMotionListener( this );
     }
 
-    maMouseMotionListeners.addInterface( xListener );
+    maMouseMotionListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL SlideShowView::removeMouseMotionListener( const Reference< 
awt::XMouseMotionListener >& xListener )
@@ -401,7 +401,7 @@ void SAL_CALL SlideShowView::removeMouseMotionListener( 
const Reference< awt::XM
     std::unique_lock aGuard( m_aMutex );
 
     if (!m_bDisposed)
-        maMouseMotionListeners.removeInterface( xListener );
+        maMouseMotionListeners.removeInterface( aGuard, xListener );
 
     // TODO(P1): Might be nice to deregister for mouse motion
     // events, when the last listener is gone.
@@ -523,7 +523,7 @@ void SAL_CALL SlideShowView::mousePressed( const 
awt::MouseEvent& e )
         aEvent.maEvent = e;
         aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-        maMouseListeners.notify( aEvent );
+        maMouseListeners.notify( aGuard, aEvent );
         updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
     }
 }
@@ -548,7 +548,7 @@ void SAL_CALL SlideShowView::mouseReleased( const 
awt::MouseEvent& e )
         aEvent.maEvent = e;
         aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-        maMouseListeners.notify( aEvent );
+        maMouseListeners.notify( aGuard, aEvent );
         updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
     }
 }
@@ -566,7 +566,7 @@ void SAL_CALL SlideShowView::mouseEntered( const 
awt::MouseEvent& e )
     aEvent.maEvent = e;
     aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-    maMouseListeners.notify( aEvent );
+    maMouseListeners.notify( aGuard, aEvent );
     updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
 }
 
@@ -583,7 +583,7 @@ void SAL_CALL SlideShowView::mouseExited( const 
awt::MouseEvent& e )
     aEvent.maEvent = e;
     aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-    maMouseListeners.notify( aEvent );
+    maMouseListeners.notify( aGuard, aEvent );
     updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
 }
 
@@ -601,7 +601,7 @@ void SAL_CALL SlideShowView::mouseDragged( const 
awt::MouseEvent& e )
     aEvent.maEvent = e;
     aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-    maMouseMotionListeners.notify( aEvent );
+    maMouseMotionListeners.notify( aGuard, aEvent );
     updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
 }
 
@@ -618,7 +618,7 @@ void SAL_CALL SlideShowView::mouseMoved( const 
awt::MouseEvent& e )
     aEvent.maEvent = e;
     aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
 
-    maMouseMotionListeners.notify( aEvent );
+    maMouseMotionListeners.notify( aGuard, aEvent );
     updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
 }
 
diff --git a/sd/source/ui/slideshow/slideshowviewimpl.hxx 
b/sd/source/ui/slideshow/slideshowviewimpl.hxx
index b88fd2cdeff3..3a5018be41b7 100644
--- a/sd/source/ui/slideshow/slideshowviewimpl.hxx
+++ b/sd/source/ui/slideshow/slideshowviewimpl.hxx
@@ -76,7 +76,7 @@ typedef ::comphelper::OInterfaceContainerHelper4< 
css::awt::XMouseListener > Sli
 class SlideShowViewMouseListeners : public SlideShowViewMouseListeners_Base
 {
 public:
-    void notify(const WrappedMouseEvent& rEvent);
+    void notify(std::unique_lock<std::mutex>& rGuard, const WrappedMouseEvent& 
rEvent);
 };
 
 
@@ -86,7 +86,7 @@ typedef ::comphelper::OInterfaceContainerHelper4< 
css::awt::XMouseMotionListener
 class SlideShowViewMouseMotionListeners : public 
SlideShowViewMouseMotionListeners_Base
 {
 public:
-    void notify( const WrappedMouseMotionEvent& rEvent );
+    void notify( std::unique_lock<std::mutex>& rGuard, const 
WrappedMouseMotionEvent& rEvent );
 };
 
 // SlideShowView
diff --git a/sfx2/inc/unoctitm.hxx b/sfx2/inc/unoctitm.hxx
index 1a53091b2b74..a021c5379840 100644
--- a/sfx2/inc/unoctitm.hxx
+++ b/sfx2/inc/unoctitm.hxx
@@ -63,7 +63,7 @@ public:
     // Something else
     void                ReleaseAll();
     void                sendStatusChanged(const OUString& rURL, const 
css::frame::FeatureStateEvent& rEvent);
-    std::vector<OUString> getContainedTypes() { return 
maListeners.getContainedTypes(); };
+    std::vector<OUString> getContainedTypes() { std::unique_lock g(maMutex); 
return maListeners.getContainedTypes(g); };
 };
 
 class SfxSlotServer;
diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx
index 5ae4e87f77e4..87d04d3524a8 100644
--- a/sfx2/source/control/unoctitm.cxx
+++ b/sfx2/source/control/unoctitm.cxx
@@ -134,19 +134,12 @@ void SfxStatusDispatcher::sendStatusChanged(const 
OUString& rURL, const css::fra
     ::comphelper::OInterfaceContainerHelper4<css::frame::XStatusListener>* 
pContnr = maListeners.getContainer(rURL);
     if (!pContnr)
         return;
-    ::comphelper::OInterfaceIteratorHelper4 aIt(*pContnr);
-    aGuard.unlock();
-    while (aIt.hasMoreElements())
-    {
-        try
-        {
-            aIt.next()->statusChanged(rEvent);
-        }
-        catch (const css::uno::RuntimeException&)
+    pContnr->forEach(aGuard,
+        [&rEvent](const css::uno::Reference<css::frame::XStatusListener>& 
xListener)
         {
-            aIt.remove();
+            xListener->statusChanged(rEvent);
         }
-    }
+    );
 }
 
 void SAL_CALL SfxStatusDispatcher::dispatch( const css::util::URL&, const 
css::uno::Sequence< css::beans::PropertyValue >& )
@@ -168,7 +161,7 @@ void SAL_CALL SfxStatusDispatcher::addStatusListener(const 
css::uno::Reference<
 {
     {
         std::unique_lock aGuard(maMutex);
-        maListeners.addInterface( aURL.Complete, aListener );
+        maListeners.addInterface( aGuard, aURL.Complete, aListener );
     }
     if ( aURL.Complete == ".uno:LifeTime" )
     {
@@ -184,7 +177,7 @@ void SAL_CALL SfxStatusDispatcher::addStatusListener(const 
css::uno::Reference<
 void SAL_CALL SfxStatusDispatcher::removeStatusListener( const 
css::uno::Reference< css::frame::XStatusListener > & aListener, const 
css::util::URL& aURL )
 {
     std::unique_lock aGuard(maMutex);
-    maListeners.removeInterface( aURL.Complete, aListener );
+    maListeners.removeInterface( aGuard, aURL.Complete, aListener );
 }
 
 
@@ -300,7 +293,7 @@ void SAL_CALL SfxOfficeDispatch::addStatusListener(const 
css::uno::Reference< cs
 {
     {
         std::unique_lock aGuard(maMutex);
-        maListeners.addInterface( aURL.Complete, aListener );
+        maListeners.addInterface( aGuard, aURL.Complete, aListener );
     }
     if ( pImpl )
     {
diff --git a/sfx2/source/doc/printhelper.cxx b/sfx2/source/doc/printhelper.cxx
index 5cbd8e258c72..0ccdb1b1f2b0 100644
--- a/sfx2/source/doc/printhelper.cxx
+++ b/sfx2/source/doc/printhelper.cxx
@@ -782,13 +782,13 @@ void IMPL_PrintListener_DataContainer::Notify( 
SfxBroadcaster& rBC, const SfxHin
     }
 
     std::unique_lock aGuard(m_aMutex);
-    if (!m_aJobListeners.getLength())
+    if (!m_aJobListeners.getLength(aGuard))
         return;
     view::PrintJobEvent aEvent;
     aEvent.Source = m_xPrintJob;
     aEvent.State = pPrintHint->GetWhich();
 
-    comphelper::OInterfaceIteratorHelper4 pIterator(m_aJobListeners);
+    comphelper::OInterfaceIteratorHelper4 pIterator(aGuard, m_aJobListeners);
     aGuard.unlock();
     while (pIterator.hasMoreElements())
         pIterator.next()->printJobEvent( aEvent );
@@ -797,13 +797,13 @@ void IMPL_PrintListener_DataContainer::Notify( 
SfxBroadcaster& rBC, const SfxHin
 void SAL_CALL SfxPrintHelper::addPrintJobListener( const css::uno::Reference< 
css::view::XPrintJobListener >& xListener )
 {
     std::unique_lock aGuard(m_pData->m_aMutex);
-    m_pData->m_aJobListeners.addInterface( xListener );
+    m_pData->m_aJobListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL SfxPrintHelper::removePrintJobListener( const 
css::uno::Reference< css::view::XPrintJobListener >& xListener )
 {
     std::unique_lock aGuard(m_pData->m_aMutex);
-    m_pData->m_aJobListeners.removeInterface( xListener );
+    m_pData->m_aJobListeners.removeInterface( aGuard, xListener );
 }
 
 
diff --git a/sfx2/source/notify/globalevents.cxx 
b/sfx2/source/notify/globalevents.cxx
index 0208a5f412a5..817e8a38448b 100644
--- a/sfx2/source/notify/globalevents.cxx
+++ b/sfx2/source/notify/globalevents.cxx
@@ -168,35 +168,35 @@ uno::Reference< container::XNameReplace > SAL_CALL 
SfxGlobalEvents_Impl::getEven
 
 void SAL_CALL SfxGlobalEvents_Impl::addEventListener(const uno::Reference< 
document::XEventListener >& xListener)
 {
-    std::scoped_lock g(m_aLock);
+    std::unique_lock g(m_aLock);
     if (m_disposed)
         throw css::lang::DisposedException();
-    m_aLegacyListeners.addInterface(xListener);
+    m_aLegacyListeners.addInterface(g, xListener);
 }
 
 
 void SAL_CALL SfxGlobalEvents_Impl::removeEventListener(const uno::Reference< 
document::XEventListener >& xListener)
 {
-    std::scoped_lock g(m_aLock);
+    std::unique_lock g(m_aLock);
     // The below removeInterface will silently do nothing when m_disposed
-    m_aLegacyListeners.removeInterface(xListener);
+    m_aLegacyListeners.removeInterface(g, xListener);
 }
 
 
 void SAL_CALL SfxGlobalEvents_Impl::addDocumentEventListener( const 
uno::Reference< document::XDocumentEventListener >& Listener )
 {
-    std::scoped_lock g(m_aLock);
+    std::unique_lock g(m_aLock);
     if (m_disposed)
         throw css::lang::DisposedException();
-    m_aDocumentListeners.addInterface( Listener );
+    m_aDocumentListeners.addInterface( g, Listener );
 }
 
 
 void SAL_CALL SfxGlobalEvents_Impl::removeDocumentEventListener( const 
uno::Reference< document::XDocumentEventListener >& Listener )
 {
-    std::scoped_lock g(m_aLock);
+    std::unique_lock g(m_aLock);
     // The below removeInterface will silently do nothing when m_disposed:
-    m_aDocumentListeners.removeInterface( Listener );
+    m_aDocumentListeners.removeInterface( g, Listener );
 }
 
 
@@ -478,43 +478,21 @@ void 
SfxGlobalEvents_Impl::implts_checkAndExecuteEventBindings(const document::D
 
 void SfxGlobalEvents_Impl::implts_notifyListener(const 
document::DocumentEvent& aEvent)
 {
-    
std::optional<comphelper::OInterfaceIteratorHelper4<document::XEventListener>> 
aIt1;
-    
std::optional<comphelper::OInterfaceIteratorHelper4<document::XDocumentEventListener>>
 aIt2;
-
-    {
-        std::scoped_lock g(m_aLock);
-        aIt1.emplace(m_aLegacyListeners);
-        aIt2.emplace(m_aDocumentListeners);
-    }
+    std::unique_lock g(m_aLock);
 
     document::EventObject aLegacyEvent(aEvent.Source, aEvent.EventName);
-    while (aIt1->hasMoreElements())
-    {
-        auto xListener = aIt1->next();
-        try
+    m_aLegacyListeners.forEach(g,
+        [&aLegacyEvent](const css::uno::Reference<document::XEventListener>& 
xListener)
         {
             xListener->notifyEvent(aLegacyEvent);
         }
-        catch (css::lang::DisposedException const& exc)
-        {
-            if (exc.Context == xListener)
-                aIt1->remove();
-        }
-    }
-
-    while (aIt2->hasMoreElements())
-    {
-        auto xListener = aIt2->next();
-        try
+    );
+    m_aDocumentListeners.forEach(g,
+        [&aEvent](const css::uno::Reference<document::XDocumentEventListener>& 
xListener)
         {
             xListener->documentEventOccured(aEvent);
         }
-        catch (css::lang::DisposedException const& exc)
-        {
-            if (exc.Context == xListener)
-                aIt2->remove();
-        }
-    }
+    );
 }
 
 
diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx
index 8fdf33487a56..298f7a722ed2 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -1841,12 +1841,14 @@ SfxBaseController* 
SfxViewShell::GetBaseController_Impl() const
 
 void SfxViewShell::AddContextMenuInterceptor_Impl( const uno::Reference< 
ui::XContextMenuInterceptor >& xInterceptor )
 {
-    pImpl->aInterceptorContainer.addInterface( xInterceptor );
+    std::unique_lock g(pImpl->aMutex);
+    pImpl->aInterceptorContainer.addInterface( g, xInterceptor );
 }
 
 void SfxViewShell::RemoveContextMenuInterceptor_Impl( const uno::Reference< 
ui::XContextMenuInterceptor >& xInterceptor )
 {
-    pImpl->aInterceptorContainer.removeInterface( xInterceptor );
+    std::unique_lock g(pImpl->aMutex);
+    pImpl->aInterceptorContainer.removeInterface( g, xInterceptor );
 }
 
 bool SfxViewShell::TryContextMenuInterception(const 
css::uno::Reference<css::awt::XPopupMenu>& rIn,
@@ -1866,16 +1868,17 @@ bool SfxViewShell::TryContextMenuInterception(const 
css::uno::Reference<css::awt
 
     // call interceptors
     std::unique_lock g(pImpl->aMutex);
-    ::comphelper::OInterfaceIteratorHelper4 aIt( pImpl->aInterceptorContainer 
);
+    std::vector<uno::Reference< ui::XContextMenuInterceptor>> aInterceptors =
+        pImpl->aInterceptorContainer.getElements(g);
     g.unlock();
-    while( aIt.hasMoreElements() )
+    for (const auto & rListener : aInterceptors )
     {
         try
         {
             ui::ContextMenuInterceptorAction eAction;
             {
                 SolarMutexReleaser rel;
-                eAction = aIt.next()->notifyContextMenuExecute( aEvent );
+                eAction = rListener->notifyContextMenuExecute( aEvent );
             }
             switch ( eAction )
             {
@@ -1900,7 +1903,9 @@ bool SfxViewShell::TryContextMenuInterception(const 
css::uno::Reference<css::awt
         }
         catch (...)
         {
-            aIt.remove();
+            g.lock();
+            pImpl->aInterceptorContainer.removeInterface(g, rListener);
+            g.unlock();
         }
 
         break;
@@ -1931,16 +1936,17 @@ bool SfxViewShell::TryContextMenuInterception(const 
css::uno::Reference<css::awt
 
     // call interceptors
     std::unique_lock g(pImpl->aMutex);
-    ::comphelper::OInterfaceIteratorHelper4 aIt( pImpl->aInterceptorContainer 
);
+    std::vector<uno::Reference< ui::XContextMenuInterceptor>> aInterceptors =
+        pImpl->aInterceptorContainer.getElements(g);
     g.unlock();
-    while( aIt.hasMoreElements() )
+    for (const auto & rListener : aInterceptors )
     {
         try
         {
             css::ui::ContextMenuInterceptorAction eAction;
             {
                 SolarMutexReleaser rel;
-                eAction = aIt.next()->notifyContextMenuExecute( aEvent );
+                eAction = rListener->notifyContextMenuExecute( aEvent );
             }
             switch ( eAction )
             {
@@ -1965,7 +1971,9 @@ bool SfxViewShell::TryContextMenuInterception(const 
css::uno::Reference<css::awt
         }
         catch (...)
         {
-            aIt.remove();
+            g.lock();
+            pImpl->aInterceptorContainer.removeInterface(g, rListener);
+            g.unlock();
         }
 
         break;
diff --git a/svl/source/fsstor/oinputstreamcontainer.cxx 
b/svl/source/fsstor/oinputstreamcontainer.cxx
index 1339096dce9f..a2dc0456a84c 100644
--- a/svl/source/fsstor/oinputstreamcontainer.cxx
+++ b/svl/source/fsstor/oinputstreamcontainer.cxx
@@ -239,22 +239,22 @@ void SAL_CALL OFSInputStreamContainer::dispose(  )
 
 void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( m_bDisposed )
         throw lang::DisposedException();
 
-    m_aListenersContainer.addInterface( xListener );
+    m_aListenersContainer.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL OFSInputStreamContainer::removeEventListener( const 
uno::Reference< lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( m_bDisposed )
         throw lang::DisposedException();
 
-    m_aListenersContainer.removeInterface( xListener );
+    m_aListenersContainer.removeInterface( aGuard, xListener );
 }
 
 
diff --git a/svl/source/fsstor/ostreamcontainer.cxx 
b/svl/source/fsstor/ostreamcontainer.cxx
index bfa6962ee9c4..d8ef930f7e98 100644
--- a/svl/source/fsstor/ostreamcontainer.cxx
+++ b/svl/source/fsstor/ostreamcontainer.cxx
@@ -233,22 +233,22 @@ void SAL_CALL OFSStreamContainer::dispose()
 
 void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( m_bDisposed )
         throw lang::DisposedException();
 
-    m_aListenersContainer.addInterface( xListener );
+    m_aListenersContainer.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL OFSStreamContainer::removeEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
-    std::scoped_lock aGuard( m_aMutex );
+    std::unique_lock aGuard( m_aMutex );
 
     if ( m_bDisposed )
         throw lang::DisposedException();
 
-    m_aListenersContainer.removeInterface( xListener );
+    m_aListenersContainer.removeInterface( aGuard, xListener );
 }
 
 
diff --git a/svx/source/table/tabledesign.cxx b/svx/source/table/tabledesign.cxx
index d383898adae1..935cebefe4bd 100644
--- a/svx/source/table/tabledesign.cxx
+++ b/svx/source/table/tabledesign.cxx
@@ -228,9 +228,9 @@ sal_Bool SAL_CALL TableDesignStyle::isUserDefined()
 sal_Bool SAL_CALL TableDesignStyle::isInUse()
 {
     std::unique_lock aGuard( m_aMutex );
-    if (maModifyListeners.getLength())
+    if (maModifyListeners.getLength(aGuard))
     {
-        comphelper::OInterfaceIteratorHelper4 it(maModifyListeners);
+        comphelper::OInterfaceIteratorHelper4 it(aGuard, maModifyListeners);
         while ( it.hasMoreElements() )
         {
             TableDesignUser* pUser = dynamic_cast< TableDesignUser* >( 
it.next().get() );
@@ -396,7 +396,7 @@ void SAL_CALL TableDesignStyle::addModifyListener( const 
Reference< XModifyListe
     }
     else
     {
-        maModifyListeners.addInterface( xListener );
+        maModifyListeners.addInterface( aGuard, xListener );
     }
 }
 
@@ -404,7 +404,7 @@ void SAL_CALL TableDesignStyle::addModifyListener( const 
Reference< XModifyListe
 void SAL_CALL TableDesignStyle::removeModifyListener( const Reference< 
XModifyListener >& xListener )
 {
     std::unique_lock aGuard( m_aMutex );
-    maModifyListeners.removeInterface( xListener );
+    maModifyListeners.removeInterface( aGuard, xListener );
 }
 
 
@@ -412,10 +412,10 @@ void TableDesignStyle::notifyModifyListener()
 {
     std::unique_lock aGuard( m_aMutex );
 
-    if( maModifyListeners.getLength() )
+    if( maModifyListeners.getLength(aGuard) )
     {
         EventObject aEvt( static_cast< OWeakObject * >( this ) );
-        maModifyListeners.forEach(
+        maModifyListeners.forEach(aGuard,
             [&] (Reference<XModifyListener> const& xListener)
                 { return xListener->modified(aEvt); });
     }
diff --git 
a/svx/source/unodialogs/textconversiondlgs/chinese_translation_unodialog.cxx 
b/svx/source/unodialogs/textconversiondlgs/chinese_translation_unodialog.cxx
index 80cda9bb6555..466a4efca29e 100644
--- a/svx/source/unodialogs/textconversiondlgs/chinese_translation_unodialog.cxx
+++ b/svx/source/unodialogs/textconversiondlgs/chinese_translation_unodialog.cxx
@@ -134,7 +134,7 @@ void SAL_CALL 
ChineseTranslation_UnoDialog::addEventListener( const uno::Referen
     if( m_bDisposed || m_bInDispose )
         return;
     std::unique_lock aGuard(m_aContainerMutex);
-    m_aDisposeEventListeners.addInterface( xListener );
+    m_aDisposeEventListeners.addInterface( aGuard, xListener );
 }
 
 void SAL_CALL ChineseTranslation_UnoDialog::removeEventListener( const 
uno::Reference< lang::XEventListener > & xListener )
@@ -143,7 +143,7 @@ void SAL_CALL 
ChineseTranslation_UnoDialog::removeEventListener( const uno::Refe
     if( m_bDisposed || m_bInDispose )
         return;
     std::unique_lock aGuard(m_aContainerMutex);
-    m_aDisposeEventListeners.removeInterface( xListener );
+    m_aDisposeEventListeners.removeInterface( aGuard, xListener );
 }
 
 
diff --git a/sw/source/core/unocore/unobkm.cxx 
b/sw/source/core/unocore/unobkm.cxx
index 347038a09ba0..b5577df6ecde 100644
--- a/sw/source/core/unocore/unobkm.cxx
+++ b/sw/source/core/unocore/unobkm.cxx
@@ -301,7 +301,7 @@ void SAL_CALL SwXBookmark::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXBookmark::removeEventListener(
@@ -309,7 +309,7 @@ void SAL_CALL SwXBookmark::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 OUString SAL_CALL SwXBookmark::getName()
diff --git a/sw/source/core/unocore/unochart.cxx 
b/sw/source/core/unocore/unochart.cxx
index d1bc07765758..b32a0dc35b80 100644
--- a/sw/source/core/unocore/unochart.cxx
+++ b/sw/source/core/unocore/unochart.cxx
@@ -160,7 +160,8 @@ static void LaunchModifiedEvent(
         const uno::Reference< uno::XInterface > &rxI )
 {
     lang::EventObject aEvtObj( rxI );
-    rICH.notifyEach( &util::XModifyListener::modified, aEvtObj );
+    std::unique_lock aGuard(GetChartMutex());
+    rICH.notifyEach( aGuard, &util::XModifyListener::modified, aEvtObj );
 }
 
 /**
@@ -1383,7 +1384,7 @@ void SAL_CALL SwChartDataProvider::addEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEventListeners.addInterface( rxListener );
+        m_aEventListeners.addInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartDataProvider::removeEventListener(
@@ -1391,7 +1392,7 @@ void SAL_CALL SwChartDataProvider::removeEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEventListeners.removeInterface( rxListener );
+        m_aEventListeners.removeInterface( aGuard, rxListener );
 }
 
 OUString SAL_CALL SwChartDataProvider::getImplementationName(  )
@@ -2191,7 +2192,7 @@ void SAL_CALL SwChartDataSequence::addModifyListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aModifyListeners.addInterface( rxListener );
+        m_aModifyListeners.addInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartDataSequence::removeModifyListener(
@@ -2199,7 +2200,7 @@ void SAL_CALL SwChartDataSequence::removeModifyListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aModifyListeners.removeInterface( rxListener );
+        m_aModifyListeners.removeInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartDataSequence::disposing( const lang::EventObject& rSource 
)
@@ -2274,7 +2275,7 @@ void SAL_CALL SwChartDataSequence::addEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEvtListeners.addInterface( rxListener );
+        m_aEvtListeners.addInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartDataSequence::removeEventListener(
@@ -2282,7 +2283,7 @@ void SAL_CALL SwChartDataSequence::removeEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEvtListeners.removeInterface( rxListener );
+        m_aEvtListeners.removeInterface( aGuard, rxListener );
 }
 
 bool SwChartDataSequence::DeleteBox( const SwTableBox &rBox )
@@ -2652,7 +2653,7 @@ void SAL_CALL 
SwChartLabeledDataSequence::addModifyListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aModifyListeners.addInterface( rxListener );
+        m_aModifyListeners.addInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartLabeledDataSequence::removeModifyListener(
@@ -2660,7 +2661,7 @@ void SAL_CALL 
SwChartLabeledDataSequence::removeModifyListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aModifyListeners.removeInterface( rxListener );
+        m_aModifyListeners.removeInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartLabeledDataSequence::dispose(  )
@@ -2690,7 +2691,7 @@ void SAL_CALL 
SwChartLabeledDataSequence::addEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEventListeners.addInterface( rxListener );
+        m_aEventListeners.addInterface( aGuard, rxListener );
 }
 
 void SAL_CALL SwChartLabeledDataSequence::removeEventListener(
@@ -2698,7 +2699,7 @@ void SAL_CALL 
SwChartLabeledDataSequence::removeEventListener(
 {
     std::unique_lock aGuard( GetChartMutex() );
     if (!m_bDisposed && rxListener.is())
-        m_aEventListeners.removeInterface( rxListener );
+        m_aEventListeners.removeInterface( aGuard, rxListener );
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/unocore/unofield.cxx 
b/sw/source/core/unocore/unofield.cxx
index 717776dbc6f0..703eb3e1f2a5 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -404,10 +404,9 @@ static sal_uInt16 lcl_GetPropertyMapOfService( 
SwServiceType nServiceId )
 class SwXFieldMaster::Impl
     : public SvtListener
 {
-private:
-    std::mutex m_Mutex; // just for OInterfaceContainerHelper3
-
 public:
+    std::mutex m_Mutex; // just for OInterfaceContainerHelper4
+
     uno::WeakReference<uno::XInterface> m_wThis;
     ::comphelper::OInterfaceContainerHelper4<css::lang::XEventListener> 
m_EventListeners;
 
@@ -970,15 +969,15 @@ void SAL_CALL SwXFieldMaster::dispose()
 void SAL_CALL SwXFieldMaster::addEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXFieldMaster::removeEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 void SwXFieldMaster::Impl::Notify(const SfxHint& rHint)
@@ -1083,12 +1082,11 @@ struct SwFieldProperties_Impl
 class SwXTextField::Impl
     : public SvtListener
 {
-private:
-    std::mutex m_Mutex; // just for OInterfaceContainerHelper3
+public:
+    std::mutex m_Mutex; // just for OInterfaceContainerHelper4
     SwFieldType* m_pFieldType;
     SwFormatField* m_pFormatField;
 
-public:
     uno::WeakReference<uno::XInterface> m_wThis;
     ::comphelper::OInterfaceContainerHelper4<css::lang::XEventListener> 
m_EventListeners;
 
@@ -2112,15 +2110,15 @@ void SAL_CALL SwXTextField::dispose()
 void SAL_CALL SwXTextField::addEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXTextField::removeEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 uno::Reference< beans::XPropertySetInfo > SAL_CALL
@@ -2898,22 +2896,23 @@ void SAL_CALL SwXTextFieldTypes::refresh()
     }
     // call refresh listeners (without SolarMutex locked)
     lang::EventObject const event(static_cast< ::cppu::OWeakObject*>(this));
-    m_pImpl->m_RefreshListeners.notifyEach(
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_RefreshListeners.notifyEach(aGuard,
             & util::XRefreshListener::refreshed, event);
 }
 
 void SAL_CALL SwXTextFieldTypes::addRefreshListener(
         const uno::Reference<util::XRefreshListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_RefreshListeners.addInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_RefreshListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXTextFieldTypes::removeRefreshListener(
         const uno::Reference<util::XRefreshListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_RefreshListeners.removeInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_RefreshListeners.removeInterface(aGuard, xListener);
 }
 
 class SwXFieldEnumeration::Impl
diff --git a/sw/source/core/unocore/unoframe.cxx 
b/sw/source/core/unocore/unoframe.cxx
index 73f713b438aa..5ba3ecf98f8e 100644
--- a/sw/source/core/unocore/unoframe.cxx
+++ b/sw/source/core/unocore/unoframe.cxx
@@ -2598,15 +2598,15 @@ uno::Any SwXFrame::getPropertyDefault( const OUString& 
rPropertyName )
 void SAL_CALL SwXFrame::addEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXFrame::removeEventListener(
         const uno::Reference<lang::XEventListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    std::unique_lock aGuard(m_pImpl->m_Mutex);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 void SwXFrame::DisposeInternal()
diff --git a/sw/source/core/unocore/unoftn.cxx 
b/sw/source/core/unocore/unoftn.cxx
index 82c7f2555243..ca012bbb03e5 100644
--- a/sw/source/core/unocore/unoftn.cxx
+++ b/sw/source/core/unocore/unoftn.cxx
@@ -378,7 +378,7 @@ SwXFootnote::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL
@@ -387,7 +387,7 @@ SwXFootnote::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 const SwStartNode *SwXFootnote::GetStartNode() const
diff --git a/sw/source/core/unocore/unoidx.cxx 
b/sw/source/core/unocore/unoidx.cxx
index e4276a618410..98eb107977be 100644
--- a/sw/source/core/unocore/unoidx.cxx
+++ b/sw/source/core/unocore/unoidx.cxx
@@ -1990,7 +1990,7 @@ SwXDocumentIndexMark::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL
@@ -1999,7 +1999,7 @@ SwXDocumentIndexMark::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 uno::Reference< beans::XPropertySetInfo > SAL_CALL
diff --git a/sw/source/core/unocore/unoparagraph.cxx 
b/sw/source/core/unocore/unoparagraph.cxx
index 658fa7aac3eb..208ebad07fd7 100644
--- a/sw/source/core/unocore/unoparagraph.cxx
+++ b/sw/source/core/unocore/unoparagraph.cxx
@@ -1234,7 +1234,7 @@ void SAL_CALL SwXParagraph::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXParagraph::removeEventListener(
@@ -1242,7 +1242,7 @@ void SAL_CALL SwXParagraph::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 uno::Reference< container::XEnumeration >  SAL_CALL
diff --git a/sw/source/core/unocore/unorefmk.cxx 
b/sw/source/core/unocore/unorefmk.cxx
index 925f40b370f9..53c12ab255a5 100644
--- a/sw/source/core/unocore/unorefmk.cxx
+++ b/sw/source/core/unocore/unorefmk.cxx
@@ -360,7 +360,7 @@ void SAL_CALL SwXReferenceMark::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXReferenceMark::removeEventListener(
@@ -368,7 +368,7 @@ void SAL_CALL SwXReferenceMark::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 OUString SAL_CALL SwXReferenceMark::getName()
@@ -876,7 +876,7 @@ SwXMeta::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL
@@ -885,7 +885,7 @@ SwXMeta::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 void SAL_CALL
diff --git a/sw/source/core/unocore/unosect.cxx 
b/sw/source/core/unocore/unosect.cxx
index 2fc5ac582ab7..54cc983c57c8 100644
--- a/sw/source/core/unocore/unosect.cxx
+++ b/sw/source/core/unocore/unosect.cxx
@@ -494,7 +494,7 @@ void SAL_CALL SwXTextSection::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXTextSection::removeEventListener(
@@ -502,7 +502,7 @@ void SAL_CALL SwXTextSection::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 uno::Reference< beans::XPropertySetInfo > SAL_CALL
diff --git a/sw/source/core/unocore/unotbl.cxx 
b/sw/source/core/unocore/unotbl.cxx
index 7853d7ccbe2b..7680b458176d 100644
--- a/sw/source/core/unocore/unotbl.cxx
+++ b/sw/source/core/unocore/unotbl.cxx
@@ -146,12 +146,12 @@ namespace
         return event;
     }
 
-    void lcl_SendChartEvent(
+    void lcl_SendChartEvent(std::unique_lock<std::mutex>& rGuard,
             uno::Reference<uno::XInterface> const& xSource,
             
::comphelper::OInterfaceContainerHelper4<chart::XChartDataChangeEventListener> 
& rListeners)
     {
-        if (rListeners.getLength())
-            rListeners.notifyEach(
+        if (rListeners.getLength(rGuard))
+            rListeners.notifyEach(rGuard,
                     &chart::XChartDataChangeEventListener::chartDataChanged,
                     createChartEvent(xSource));
     }
@@ -2167,7 +2167,7 @@ void SAL_CALL SwXTextTable::addEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.addInterface(xListener);
+    m_pImpl->m_EventListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXTextTable::removeEventListener(
@@ -2175,7 +2175,7 @@ void SAL_CALL SwXTextTable::removeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_EventListeners.removeInterface(xListener);
+    m_pImpl->m_EventListeners.removeInterface(aGuard, xListener);
 }
 
 uno::Reference<table::XCell>  SwXTextTable::getCellByPosition(sal_Int32 
nColumn, sal_Int32 nRow)
@@ -2317,7 +2317,7 @@ void SwXTextTable::setData(const uno::Sequence< 
uno::Sequence< double > >& rData
     xAllRange->setData(rData);
     // this is rather inconsistent: setData on XTextTable sends events, but 
e.g. CellRanges do not
     std::unique_lock aGuard2(m_pImpl->m_Mutex);
-    lcl_SendChartEvent(*this, m_pImpl->m_ChartListeners);
+    lcl_SendChartEvent(aGuard2, *this, m_pImpl->m_ChartListeners);
 }
 
 uno::Sequence<OUString> SwXTextTable::getRowDescriptions()
@@ -2373,7 +2373,7 @@ void SAL_CALL 
SwXTextTable::addChartDataChangeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_ChartListeners.addInterface(xListener);
+    m_pImpl->m_ChartListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXTextTable::removeChartDataChangeEventListener(
@@ -2381,7 +2381,7 @@ void SAL_CALL 
SwXTextTable::removeChartDataChangeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_ChartListeners.removeInterface(xListener);
+    m_pImpl->m_ChartListeners.removeInterface(aGuard, xListener);
 }
 
 sal_Bool SwXTextTable::isNotANumber(double nNumber)
@@ -2492,7 +2492,7 @@ void SwXTextTable::setPropertyValue(const OUString& 
rPropertyName, const uno::An
                     if (m_pImpl->m_bFirstRowAsLabel != bTmp)
                     {
                         std::unique_lock aGuard2(m_pImpl->m_Mutex);
-                        lcl_SendChartEvent(*this, m_pImpl->m_ChartListeners);
+                        lcl_SendChartEvent(aGuard2, *this, 
m_pImpl->m_ChartListeners);
                         m_pImpl->m_bFirstRowAsLabel = bTmp;
                     }
                 }
@@ -2504,7 +2504,7 @@ void SwXTextTable::setPropertyValue(const OUString& 
rPropertyName, const uno::An
                     if (m_pImpl->m_bFirstColumnAsLabel != bTmp)
                     {
                         std::unique_lock aGuard2(m_pImpl->m_Mutex);
-                        lcl_SendChartEvent(*this, m_pImpl->m_ChartListeners);
+                        lcl_SendChartEvent(aGuard2, *this, 
m_pImpl->m_ChartListeners);
                         m_pImpl->m_bFirstColumnAsLabel = bTmp;
                     }
                 }
@@ -3086,7 +3086,7 @@ void SwXTextTable::Impl::Notify(const SfxHint& rHint)
         else
         {
             std::unique_lock aGuard(m_Mutex);
-            lcl_SendChartEvent(xThis, m_ChartListeners);
+            lcl_SendChartEvent(aGuard, xThis, m_ChartListeners);
         }
     }
 }
@@ -3413,7 +3413,7 @@ SwXCellRange::setPropertyValue(const OUString& 
rPropertyName, const uno::Any& aV
             if (m_pImpl->m_bFirstRowAsLabel != bTmp)
             {
                 std::unique_lock aGuard2(m_pImpl->m_Mutex);
-                lcl_SendChartEvent(*this, m_pImpl->m_ChartListeners);
+                lcl_SendChartEvent(aGuard2, *this, m_pImpl->m_ChartListeners);
                 m_pImpl->m_bFirstRowAsLabel = bTmp;
             }
         }
@@ -3424,7 +3424,7 @@ SwXCellRange::setPropertyValue(const OUString& 
rPropertyName, const uno::Any& aV
             if (m_pImpl->m_bFirstColumnAsLabel != bTmp)
             {
                 std::unique_lock aGuard2(m_pImpl->m_Mutex);
-                lcl_SendChartEvent(*this, m_pImpl->m_ChartListeners);
+                lcl_SendChartEvent(aGuard2, *this, m_pImpl->m_ChartListeners);
                 m_pImpl->m_bFirstColumnAsLabel = bTmp;
             }
         }
@@ -3762,7 +3762,7 @@ void SAL_CALL 
SwXCellRange::addChartDataChangeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_ChartListeners.addInterface(xListener);
+    m_pImpl->m_ChartListeners.addInterface(aGuard, xListener);
 }
 
 void SAL_CALL SwXCellRange::removeChartDataChangeEventListener(
@@ -3770,7 +3770,7 @@ void SAL_CALL 
SwXCellRange::removeChartDataChangeEventListener(
 {
     // no need to lock here as m_pImpl is const and container threadsafe
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_ChartListeners.removeInterface(xListener);
+    m_pImpl->m_ChartListeners.removeInterface(aGuard, xListener);
 }
 
 sal_Bool SwXCellRange::isNotANumber(double /*fNumber*/)
@@ -3828,7 +3828,7 @@ void SwXCellRange::Impl::Notify( const SfxHint& rHint )
         if(m_pFrameFormat)
         {
             std::unique_lock aGuard(m_Mutex);
-            lcl_SendChartEvent(xThis, m_ChartListeners);
+            lcl_SendChartEvent(aGuard, xThis, m_ChartListeners);
         }
         else
         {
diff --git a/sw/source/uibase/uno/unotxdoc.cxx 
b/sw/source/uibase/uno/unotxdoc.cxx
index 7c2b71079635..959f7abd7527 100644
--- a/sw/source/uibase/uno/unotxdoc.cxx
+++ b/sw/source/uibase/uno/unotxdoc.cxx
@@ -2144,7 +2144,7 @@ void SwXTextDocument::NotifyRefreshListeners()
     // (sometimes) a different shell than GetWrtShell()?
     lang::EventObject const ev(static_cast<SwXTextDocumentBaseClass &>(*this));
     std::unique_lock aGuard(m_pImpl->m_Mutex);
-    m_pImpl->m_RefreshListeners.notifyEach(
+    m_pImpl->m_RefreshListeners.notifyEach(aGuard,
             & util::XRefreshListener::refreshed, ev);
 }
 
@@ -2163,22 +2163,20 @@ void SwXTextDocument::refresh()
 void SAL_CALL SwXTextDocument::addRefreshListener(
         const Reference<util::XRefreshListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
     if (xListener)
     {
         std::unique_lock aGuard(m_pImpl->m_Mutex);
-        m_pImpl->m_RefreshListeners.addInterface(xListener);
+        m_pImpl->m_RefreshListeners.addInterface(aGuard, xListener);
     }
 }
 
 void SAL_CALL SwXTextDocument::removeRefreshListener(
         const Reference<util::XRefreshListener> & xListener)
 {
-    // no need to lock here as m_pImpl is const and container threadsafe
     if (xListener)
     {
         std::unique_lock aGuard(m_pImpl->m_Mutex);
-        m_pImpl->m_RefreshListeners.removeInterface(xListener);
+        m_pImpl->m_RefreshListeners.removeInterface(aGuard, xListener);
     }
 }
 
diff --git a/toolkit/source/controls/grid/gridcolumn.cxx 
b/toolkit/source/controls/grid/gridcolumn.cxx
index 6d94ecb9a72b..0db9b50a5d75 100644
--- a/toolkit/source/controls/grid/gridcolumn.cxx
+++ b/toolkit/source/controls/grid/gridcolumn.cxx
@@ -81,8 +81,7 @@ namespace toolkit
             i_oldValue, i_newValue, m_nIndex
         );
 
-        i_Guard.unlock();
-        maGridColumnListeners.notifyEach( &XGridColumnListener::columnChanged, 
aEvent );
+        maGridColumnListeners.notifyEach( i_Guard, 
&XGridColumnListener::columnChanged, aEvent );
     }
 
 
@@ -209,14 +208,14 @@ namespace toolkit
     void SAL_CALL GridColumn::addGridColumnListener( const Reference< 
XGridColumnListener >& xListener )
     {
         std::unique_lock aGuard( m_aMutex );
-        maGridColumnListeners.addInterface( xListener );
+        maGridColumnListeners.addInterface( aGuard, xListener );
     }
 
 
     void SAL_CALL GridColumn::removeGridColumnListener( const Reference< 
XGridColumnListener >& xListener )
     {
         std::unique_lock aGuard( m_aMutex );
-        maGridColumnListeners.removeInterface( xListener );
+        maGridColumnListeners.removeInterface( aGuard, xListener );
     }
 
 
diff --git a/toolkit/source/hatchwindow/documentcloser.cxx 
b/toolkit/source/hatchwindow/documentcloser.cxx
index 16df8536f5f1..2eceee0d9c4c 100644
--- a/toolkit/source/hatchwindow/documentcloser.cxx
+++ b/toolkit/source/hatchwindow/documentcloser.cxx
@@ -190,14 +190,14 @@ void SAL_CALL ODocumentCloser::addEventListener( const 
uno::Reference< lang::XEv
     if ( m_bDisposed )
         throw lang::DisposedException(); // TODO
 
-    m_aListenersContainer.addInterface( xListener );
+    m_aListenersContainer.addInterface( aGuard, xListener );
 }
 
 
 void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< 
lang::XEventListener >& xListener )
 {
     std::unique_lock aGuard( m_aMutex );
-    m_aListenersContainer.removeInterface( xListener );
+    m_aListenersContainer.removeInterface( aGuard, xListener );
 }
 
 // XServiceInfo
diff --git a/ucbhelper/source/provider/resultset.cxx 
b/ucbhelper/source/provider/resultset.cxx
index 162f32cffabf..c3565b1179f0 100644
--- a/ucbhelper/source/provider/resultset.cxx
+++ b/ucbhelper/source/provider/resultset.cxx
@@ -236,7 +236,7 @@ void SAL_CALL ResultSet::dispose()
 {
     std::unique_lock aGuard( m_pImpl->m_aMutex );
 
-    if ( m_pImpl->m_aDisposeEventListeners.getLength() )
+    if ( m_pImpl->m_aDisposeEventListeners.getLength(aGuard) )
     {
         lang::EventObject aEvt;
         aEvt.Source = static_cast< lang::XComponent * >( this );
@@ -262,7 +262,7 @@ void SAL_CALL ResultSet::addEventListener(
 {
     std::unique_lock aGuard( m_pImpl->m_aMutex );
 
-    m_pImpl->m_aDisposeEventListeners.addInterface( Listener );
+    m_pImpl->m_aDisposeEventListeners.addInterface( aGuard, Listener );
 }
 
 
@@ -272,7 +272,7 @@ void SAL_CALL ResultSet::removeEventListener(
 {
     std::unique_lock aGuard( m_pImpl->m_aMutex );
 
-    m_pImpl->m_aDisposeEventListeners.removeInterface( Listener );
+    m_pImpl->m_aDisposeEventListeners.removeInterface( aGuard, Listener );
 }
 
 
@@ -1256,7 +1256,7 @@ void SAL_CALL ResultSet::addPropertyChangeListener(
         m_pImpl->m_pPropertyChangeListeners.reset(
              new PropertyChangeListeners());
 
-    m_pImpl->m_pPropertyChangeListeners->addInterface(
+    m_pImpl->m_pPropertyChangeListeners->addInterface(aGuard,
                                                 aPropertyName, xListener );
 }
 
@@ -1274,7 +1274,7 @@ void SAL_CALL ResultSet::removePropertyChangeListener(
         throw beans::UnknownPropertyException(aPropertyName);
 
     if ( m_pImpl->m_pPropertyChangeListeners )
-        m_pImpl->m_pPropertyChangeListeners->removeInterface(
+        m_pImpl->m_pPropertyChangeListeners->removeInterface(aGuard,
                                                     aPropertyName, xListener );
 
 }
@@ -1303,6 +1303,8 @@ void SAL_CALL ResultSet::removeVetoableChangeListener(
 
 void ResultSet::propertyChanged( const beans::PropertyChangeEvent& rEvt ) const
 {
+    std::unique_lock aGuard( m_pImpl->m_aMutex );
+
     if ( !m_pImpl->m_pPropertyChangeListeners )
         return;
 
@@ -1312,7 +1314,7 @@ void ResultSet::propertyChanged( const 
beans::PropertyChangeEvent& rEvt ) const
                                                         rEvt.PropertyName );
     if ( pPropsContainer )
     {
-        
pPropsContainer->notifyEach(&beans::XPropertyChangeListener::propertyChange, 
rEvt);
+        pPropsContainer->notifyEach(aGuard, 
&beans::XPropertyChangeListener::propertyChange, rEvt);
     }
 
     // Notify listeners interested in all properties.
@@ -1320,7 +1322,7 @@ void ResultSet::propertyChanged( const 
beans::PropertyChangeEvent& rEvt ) const
         = m_pImpl->m_pPropertyChangeListeners->getContainer( OUString() );
     if ( pPropsContainer )
     {
-        comphelper::OInterfaceIteratorHelper4 aIter( *pPropsContainer );
+        comphelper::OInterfaceIteratorHelper4 aIter( aGuard, *pPropsContainer 
);
         while ( aIter.hasMoreElements() )
         {
             aIter.next()->propertyChange( rEvt );
diff --git a/ucbhelper/source/provider/resultsethelper.cxx 
b/ucbhelper/source/provider/resultsethelper.cxx
index 25ead4bd8a22..6d750c82dbba 100644
--- a/ucbhelper/source/provider/resultsethelper.cxx
+++ b/ucbhelper/source/provider/resultsethelper.cxx
@@ -90,7 +90,7 @@ void SAL_CALL ResultSetImplHelper::dispose()
 {
     std::unique_lock aGuard( m_aMutex );
 
-    if ( m_aDisposeEventListeners.getLength() )
+    if ( m_aDisposeEventListeners.getLength(aGuard) )
     {
         lang::EventObject aEvt;
         aEvt.Source = static_cast< lang::XComponent * >( this );
@@ -105,7 +105,7 @@ void SAL_CALL ResultSetImplHelper::addEventListener(
 {
     std::unique_lock aGuard( m_aMutex );
 
-    m_aDisposeEventListeners.addInterface( Listener );
+    m_aDisposeEventListeners.addInterface( aGuard, Listener );
 }
 
 
@@ -115,7 +115,7 @@ void SAL_CALL ResultSetImplHelper::removeEventListener(
 {
     std::unique_lock aGuard( m_aMutex );
 
-    m_aDisposeEventListeners.removeInterface( Listener );
+    m_aDisposeEventListeners.removeInterface( aGuard, Listener );
 }
 
 

Reply via email to