summaryrefslogtreecommitdiff
path: root/comphelper/source
diff options
context:
space:
mode:
authorJan-Marek Glogowski <glogow@fbihome.de>2017-08-15 08:05:51 +0200
committerJan-Marek Glogowski <glogow@fbihome.de>2017-09-19 09:25:14 +0200
commit3840aede596e6fc24f7ed7df9100fb028134aac6 (patch)
tree74d1d5efa3b2ad5448181fc185faf226aed192e4 /comphelper/source
parent10b49dfb3996f99dec8dd0d2ffae2aef4022f395 (diff)
Unify SolarMutex implementations
All backends implement the SolarMutex in mostly the same way. So this consolidates this code into a GenericSolarMutex. We still need the abstract SolarMutex class for the fake AKA fascade implementation in dbaccess. The patch also replaces various places of direct mutex usage with either SolarMutexGuard or SolarMutexReleaser objects. Change-Id: Ia0146dd6c51a3b9a513cc6af34a66def58aad831 Reviewed-on: https://gerrit.libreoffice.org/42325 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Michael Meeks <michael.meeks@collabora.com> Reviewed-by: Jan-Marek Glogowski <glogow@fbihome.de>
Diffstat (limited to 'comphelper/source')
-rw-r--r--comphelper/source/misc/solarmutex.cxx66
1 files changed, 65 insertions, 1 deletions
diff --git a/comphelper/source/misc/solarmutex.cxx b/comphelper/source/misc/solarmutex.cxx
index 4b573078dffd..1501228b1bc6 100644
--- a/comphelper/source/misc/solarmutex.cxx
+++ b/comphelper/source/misc/solarmutex.cxx
@@ -18,9 +18,12 @@
*/
#include <sal/config.h>
-#include <assert.h>
+
#include <comphelper/solarmutex.hxx>
+#include <assert.h>
+#include <cstdlib>
+
namespace comphelper {
SolarMutex::SolarMutex() {}
@@ -42,6 +45,67 @@ SolarMutex *SolarMutex::get()
return pSolarMutex;
}
+GenericSolarMutex::GenericSolarMutex()
+ : m_nCount( 0 )
+ , m_nThreadId( 0 )
+ , m_aBeforeReleaseHandler( nullptr )
+{
+ setSolarMutex( this );
+}
+
+GenericSolarMutex::~GenericSolarMutex()
+{
+ setSolarMutex( nullptr );
+}
+
+void GenericSolarMutex::doAcquire( const sal_uInt32 nLockCount )
+{
+ for ( sal_uInt32 n = nLockCount; n ; --n )
+ m_aMutex.acquire();
+ m_nThreadId = osl::Thread::getCurrentIdentifier();
+ m_nCount += nLockCount;
+}
+
+sal_uInt32 GenericSolarMutex::doRelease( bool bUnlockAll )
+{
+ if ( m_nCount == 0 )
+ std::abort();
+ if ( m_nThreadId != osl::Thread::getCurrentIdentifier() )
+ std::abort();
+
+ const sal_uInt32 nCount = bUnlockAll ? m_nCount : 1;
+ m_nCount -= nCount;
+
+ if ( 0 == m_nCount )
+ {
+ if ( m_aBeforeReleaseHandler )
+ m_aBeforeReleaseHandler();
+ m_nThreadId = 0;
+ }
+
+ for ( sal_uInt32 n = nCount ; n ; --n )
+ m_aMutex.release();
+
+ return nCount;
+}
+
+bool GenericSolarMutex::IsCurrentThread() const
+{
+ return m_nThreadId == osl::Thread::getCurrentIdentifier();
+}
+
+bool GenericSolarMutex::tryToAcquire()
+{
+ if ( m_aMutex.tryToAcquire() )
+ {
+ m_nThreadId = osl::Thread::getCurrentIdentifier();
+ m_nCount++;
+ return true;
+ }
+ else
+ return false;
+}
+
} // namespace comphelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */