summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Stahl <mstahl@redhat.com>2014-09-26 22:24:14 +0200
committerMichael Stahl <mstahl@redhat.com>2014-09-26 23:24:32 +0200
commit423142538e81d28229c769e0617c6a00a648709a (patch)
tree811f70e66531068c3978db0f513003885c6bab55
parent93e7ffbd1bd38ae9fad63a0a8abd2deb7fab9543 (diff)
add a RAII class that tries to acquire the SolarMutex and releases it
Motivated by ScCompiler::IsMacro() which has an error return that does not release the SolarMutex. Change-Id: I064219bb3c0d68839a133101491d5f8828a26c7a
-rw-r--r--include/vcl/svapp.hxx47
-rw-r--r--sc/source/core/data/documen3.cxx5
-rw-r--r--sc/source/core/tool/compiler.cxx7
3 files changed, 51 insertions, 8 deletions
diff --git a/include/vcl/svapp.hxx b/include/vcl/svapp.hxx
index e303bc6ec708..64225eccf69c 100644
--- a/include/vcl/svapp.hxx
+++ b/include/vcl/svapp.hxx
@@ -1659,6 +1659,53 @@ protected:
comphelper::SolarMutex& m_solarMutex;
};
+namespace vcl
+{
+
+/** guard class that uses tryToAcquire() and has isAcquired() to check
+ */
+class SolarMutexTryAndBuyGuard
+ : private boost::noncopyable
+{
+ private:
+ bool m_isAcquired;
+#if OSL_DEBUG_LEVEL > 0
+ bool m_isChecked;
+#endif
+ comphelper::SolarMutex& m_rSolarMutex;
+
+ public:
+
+ SolarMutexTryAndBuyGuard()
+ : m_isAcquired(false)
+#if OSL_DEBUG_LEVEL > 0
+ , m_isChecked(false)
+#endif
+ , m_rSolarMutex(Application::GetSolarMutex())
+
+ {
+ m_isAcquired = m_rSolarMutex.tryToAcquire();
+ }
+
+ ~SolarMutexTryAndBuyGuard()
+ {
+#if OSL_DEBUG_LEVEL > 0
+ assert(m_isChecked);
+#endif
+ if (m_isAcquired)
+ m_rSolarMutex.release();
+ }
+
+ bool isAcquired()
+ {
+#if OSL_DEBUG_LEVEL > 0
+ m_isChecked = true;
+#endif
+ return m_isAcquired;
+ }
+};
+
+} // namespace vcl
/**
A helper class that calls Application::ReleaseSolarMutex() in its constructor
diff --git a/sc/source/core/data/documen3.cxx b/sc/source/core/data/documen3.cxx
index 6bf981c07a7c..739b1f34dc20 100644
--- a/sc/source/core/data/documen3.cxx
+++ b/sc/source/core/data/documen3.cxx
@@ -877,13 +877,12 @@ void ScDocument::RemoveUnoObject( SfxListener& rObject )
// This check is done after calling EndListening, so a later BroadcastUno call
// won't touch this object.
- comphelper::SolarMutex& rSolarMutex = Application::GetSolarMutex();
- if ( rSolarMutex.tryToAcquire() )
+ vcl::SolarMutexTryAndBuyGuard g;
+ if (g.isAcquired())
{
// BroadcastUno is always called with the SolarMutex locked, so if it
// can be acquired, this is within the same thread (should not happen)
OSL_FAIL( "RemoveUnoObject called from BroadcastUno" );
- rSolarMutex.release();
}
else
{
diff --git a/sc/source/core/tool/compiler.cxx b/sc/source/core/tool/compiler.cxx
index b4872e2833a2..fa98f83813a1 100644
--- a/sc/source/core/tool/compiler.cxx
+++ b/sc/source/core/tool/compiler.cxx
@@ -2819,8 +2819,8 @@ bool ScCompiler::IsMacro( const OUString& rName )
// formulas are compiled from a threaded import may result in a deadlock.
// Check first if we actually could acquire it and if not bail out.
/* FIXME: yes, but how ... */
- comphelper::SolarMutex& rSolarMutex = Application::GetSolarMutex();
- if (!rSolarMutex.tryToAcquire())
+ vcl::SolarMutexTryAndBuyGuard g;
+ if (!g.isAcquired())
{
SAL_WARN( "sc.core", "ScCompiler::IsMacro - SolarMutex would deadlock, not obtaining Basic");
return false; // bad luck
@@ -2854,7 +2854,6 @@ bool ScCompiler::IsMacro( const OUString& rName )
SbxMethod* pMeth = (SbxMethod*) pObj->Find( aName, SbxCLASS_METHOD );
if( !pMeth )
{
- rSolarMutex.release();
return false;
}
// It really should be a BASIC function!
@@ -2862,12 +2861,10 @@ bool ScCompiler::IsMacro( const OUString& rName )
|| ( pMeth->IsFixed() && pMeth->GetType() == SbxEMPTY )
|| !pMeth->ISA(SbMethod) )
{
- rSolarMutex.release();
return false;
}
maRawToken.SetExternal( aName.getStr() );
maRawToken.eOp = ocMacro;
- rSolarMutex.release();
return true;
#endif
}