diff options
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/source/misc/solarmutex.cxx | 66 |
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: */ |