2010-10-27 13:11:31 +01:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-11-12 17:21:24 +00:00
/*
* This file is part of the LibreOffice project .
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice :
*
* Licensed to the Apache Software Foundation ( ASF ) under one or more
* contributor license agreements . See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership . The ASF licenses this file to you under the Apache
* License , Version 2.0 ( the " License " ) ; you may not use this file
* except in compliance with the License . You may obtain a copy of
* the License at http : //www.apache.org/licenses/LICENSE-2.0 .
*/
2001-06-11 09:12:01 +00:00
# ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
# define __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
# include <threadhelp/inoncopyable.h>
2010-11-10 12:12:05 +01:00
# include <framework/imutex.hxx>
2001-06-11 09:12:01 +00:00
# include <threadhelp/irwlock.h>
# include <threadhelp/fairrwlock.hxx>
2013-05-14 13:15:38 +02:00
# include <comphelper/solarmutex.hxx>
2010-04-16 23:01:28 +02:00
# include <fwidllapi.h>
2001-06-11 09:12:01 +00:00
namespace framework {
/*-************************************************************************************************************/ /**
@ descr If you use a lock or mutex as a member of your class and whish to use it earlier then other ones
you should have a look on this implementation . You must use it as the first base class
of your implementation - because base classes are initialized by his order and before your
member ! Thats why ist a good place to declare your thread help member so .
*/ /*-*************************************************************************************************************/
enum ELockType
{
E_NOTHING = 0 ,
E_OWNMUTEX = 1 ,
E_SOLARMUTEX = 2 ,
E_FAIRRWLOCK = 3
} ;
# define ENVVAR_LOCKTYPE DECLARE_ASCII("LOCKTYPE_FRAMEWORK")
2001-06-26 04:28:51 +00:00
# define FALLBACK_LOCKTYPE E_SOLARMUTEX
2001-06-11 09:12:01 +00:00
/*-************************************************************************************************************/ /**
@ short helper to set right lock in right situation
@ descr This helper support different types of locking :
a ) no locks - transparent for user !
2013-02-22 11:12:12 +01:00
This could be useful for simluation or single threaded environments !
2001-06-11 09:12:01 +00:00
b ) own mutex
2013-02-22 11:12:12 +01:00
An object use his own osl - mutex to be threadsafe . Useful for easy and exclusiv locking .
2001-06-11 09:12:01 +00:00
c ) solar mutex
An object use our solar mutex and will be a part of a greater safed " threadsafe code block " .
2013-02-22 11:12:12 +01:00
Could be useful for simulation and testing of higher modules !
2001-06-11 09:12:01 +00:00
d ) fair rw - lock
An object use an implementation of a fair rw - lock . This increase granularity of t hreadsafe mechanism
and should be used for high performance threadsafe code !
@ attention We support two interfaces - " IMutex " and " IRWLock " . Don ' t mix using of it !
A guard implementation should use one interface only !
@ implements IMutex
@ implements IRWLock
@ base INonCopyable
IMutex
IRWLock
@ devstatus draft
*/ /*-*************************************************************************************************************/
2010-04-16 23:01:28 +02:00
class FWI_DLLPUBLIC LockHelper : public IMutex
2001-06-11 09:12:01 +00:00
, public IRWLock
, private INonCopyable
{
//-------------------------------------------------------------------------------------------------------------
// public methods
//-------------------------------------------------------------------------------------------------------------
public :
//-------------------------------------------------------------------------------------------------------------
// ctor/dtor
//-------------------------------------------------------------------------------------------------------------
2013-05-14 13:15:38 +02:00
LockHelper ( comphelper : : SolarMutex * pSolarMutex = NULL ) ;
2001-06-11 09:12:01 +00:00
virtual ~ LockHelper ( ) ;
//-------------------------------------------------------------------------------------------------------------
// interface ::framework::IMutex
//-------------------------------------------------------------------------------------------------------------
virtual void acquire ( ) ;
virtual void release ( ) ;
//-------------------------------------------------------------------------------------------------------------
// interface ::framework::IRWLock
//-------------------------------------------------------------------------------------------------------------
virtual void acquireReadAccess ( ) ;
virtual void releaseReadAccess ( ) ;
virtual void acquireWriteAccess ( ) ;
virtual void releaseWriteAccess ( ) ;
virtual void downgradeWriteAccess ( ) ;
//-------------------------------------------------------------------------------------------------------------
// something else
//-------------------------------------------------------------------------------------------------------------
2013-05-14 13:15:38 +02:00
static LockHelper & getGlobalLock ( comphelper : : SolarMutex * pSolarMutex = NULL ) ;
2001-06-11 09:12:01 +00:00
: : osl : : Mutex & getShareableOslMutex ( ) ;
//-------------------------------------------------------------------------------------------------------------
// private methods
//-------------------------------------------------------------------------------------------------------------
private :
static ELockType & implts_getLockType ( ) ;
//-------------------------------------------------------------------------------------------------------------
// private member
// a) Make some member mutable for using in const functions!
// b) "m_eLockType" define, which of follow members is used!
// You can use "m_pFairRWLock" as a fair rw-lock (multiple reader / one writer / looks for incoming order of threads too) ...
// or you can use a normal osl mutex ("m_pOwnMutex") ...
// ... or the solarmuex as "m_pSolarMutex" (must be set from outside! because some components must be vcl-free!)
// ... but sometimes you need a shareable osl mutex!
// In this case you has some problems: i ) If your lock type is set to E_OWNMUTEX => it's easy; you can use your member "m_pOwnMutex" - it's a osl mutex.
2013-03-03 17:11:39 +01:00
// Creation and using of "m_pShareableOslMutex" isn't necessary!
2001-06-11 09:12:01 +00:00
// ii ) Otherwise you have no osl mutex ... so you must create "m_pShareableOslMutex" and use it twice!
// In this case you must lock two member everytime - "m_pShareableMutex" AND "m_pFairRWLock" or "m_pSolarMutex" or ...
// It isn't realy fine - but the only possible way.
// iii) There exist another special case - E_NOTHING is set! Then we should create this shareable mutex ...
// nad you can use it ... but this implmentation ignore it.
//-------------------------------------------------------------------------------------------------------------
private :
ELockType m_eLockType ;
mutable FairRWLock * m_pFairRWLock ;
mutable : : osl : : Mutex * m_pOwnMutex ;
2013-05-14 13:15:38 +02:00
mutable comphelper : : SolarMutex * m_pSolarMutex ;
2001-06-11 09:12:01 +00:00
mutable : : osl : : Mutex * m_pShareableOslMutex ;
2004-02-20 07:46:23 +00:00
mutable sal_Bool m_bDummySolarMutex ;
2001-06-11 09:12:01 +00:00
} ;
} // namespace framework
# endif // #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
2010-10-27 13:11:31 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */