summaryrefslogtreecommitdiff
path: root/include/comphelper
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 /include/comphelper
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 'include/comphelper')
-rw-r--r--include/comphelper/solarmutex.hxx72
1 files changed, 64 insertions, 8 deletions
diff --git a/include/comphelper/solarmutex.hxx b/include/comphelper/solarmutex.hxx
index 48453de915e4..76aea3fa336f 100644
--- a/include/comphelper/solarmutex.hxx
+++ b/include/comphelper/solarmutex.hxx
@@ -22,10 +22,13 @@
#include <sal/config.h>
+#include <osl/thread.hxx>
+#include <osl/mutex.hxx>
#include <comphelper/comphelperdllapi.h>
namespace comphelper {
+
/**
* Abstract SolarMutex interface, needed for VCL's
* Application::GetSolarMutex().
@@ -37,29 +40,82 @@ namespace comphelper {
*/
class COMPHELPER_DLLPUBLIC SolarMutex {
public:
- virtual void acquire() = 0;
-
- virtual void release() = 0;
+ void acquire( sal_uInt32 nLockCount = 1 );
+ sal_uInt32 release( bool bUnlockAll = false );
virtual bool tryToAcquire() = 0;
+ // returns true, if the mutex is owned by the current thread
+ virtual bool IsCurrentThread() const = 0;
+
/// Help components to get the SolarMutex easily.
static SolarMutex *get();
- /// semi-private: allow VCL to push its one-big-lock down here.
- static void setSolarMutex( SolarMutex *pMutex );
-
protected:
SolarMutex();
-
virtual ~SolarMutex();
+
+ /// allow VCL to push its one-big-lock down here.
+ static void setSolarMutex( SolarMutex *pMutex );
+
+ virtual sal_uInt32 doRelease( bool bUnlockAll ) = 0;
+ virtual void doAcquire( sal_uInt32 nLockCount ) = 0;
+
private:
SolarMutex(const SolarMutex&) = delete;
SolarMutex& operator=(const SolarMutex&) = delete;
};
+inline void SolarMutex::acquire( sal_uInt32 nLockCount )
+{
+ assert( nLockCount > 0 );
+ doAcquire( nLockCount );
+}
+
+inline sal_uInt32 SolarMutex::release( bool bUnlockAll )
+{
+ return doRelease( bUnlockAll );
+}
+
+
+/**
+ * Generic implementation of the abstract SolarMutex interface.
+ *
+ * Treat this as a singleton, as its constructor calls
+ * setSolarMutex( this )!
+ *
+ * Kept seperately from SolarMutex, so others can implement fascades.
+ */
+class COMPHELPER_DLLPUBLIC GenericSolarMutex
+ : public SolarMutex
+{
+public:
+ typedef void (*BeforeReleaseHandler) ();
+
+ void SetBeforeReleaseHandler( const BeforeReleaseHandler& rLink )
+ { m_aBeforeReleaseHandler = rLink; }
+
+ virtual bool tryToAcquire() override;
+ virtual bool IsCurrentThread() const override;
+
+protected:
+ osl::Mutex m_aMutex;
+ sal_uInt32 m_nCount;
+ oslThreadIdentifier m_nThreadId;
+
+ virtual void doAcquire( sal_uInt32 nLockCount ) override;
+ virtual sal_uInt32 doRelease( bool bUnlockAll ) override;
+
+protected:
+ GenericSolarMutex();
+ virtual ~GenericSolarMutex() override;
+
+private:
+ BeforeReleaseHandler m_aBeforeReleaseHandler;
+};
+
}
-#endif
+#endif // INCLUDED_COMPHELPER_SOLARMUTEX_HXX
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */