1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
29*cdf0e10cSrcweir #define __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
32*cdf0e10cSrcweir //	my own includes
33*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <threadhelp/inoncopyable.h>
36*cdf0e10cSrcweir #include <framework/imutex.hxx>
37*cdf0e10cSrcweir #include <threadhelp/irwlock.h>
38*cdf0e10cSrcweir #include <threadhelp/fairrwlock.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
41*cdf0e10cSrcweir //	interface includes
42*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
45*cdf0e10cSrcweir //	other includes
46*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
47*cdf0e10cSrcweir #include <osl/mutex.hxx>
48*cdf0e10cSrcweir #include <vos/mutex.hxx>
49*cdf0e10cSrcweir #include <fwidllapi.h>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
52*cdf0e10cSrcweir //	namespace
53*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace framework{
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
58*cdf0e10cSrcweir //	const
59*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir /*-************************************************************************************************************//**
62*cdf0e10cSrcweir     @descr          If you use a lock or mutex as a member of your class and whish to use it earlier then other ones
63*cdf0e10cSrcweir 					you should have a look on this implementation. You must use it as the first base class
64*cdf0e10cSrcweir 					of your implementation - because base classes are initialized by his order and before your
65*cdf0e10cSrcweir                     member! Thats why ist a good place to declare your thread help member so.
66*cdf0e10cSrcweir *//*-*************************************************************************************************************/
67*cdf0e10cSrcweir enum ELockType
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     E_NOTHING       = 0 ,
70*cdf0e10cSrcweir     E_OWNMUTEX      = 1 ,
71*cdf0e10cSrcweir     E_SOLARMUTEX    = 2 ,
72*cdf0e10cSrcweir     E_FAIRRWLOCK    = 3
73*cdf0e10cSrcweir };
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir #define ENVVAR_LOCKTYPE     DECLARE_ASCII("LOCKTYPE_FRAMEWORK")
76*cdf0e10cSrcweir #define FALLBACK_LOCKTYPE   E_SOLARMUTEX
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
79*cdf0e10cSrcweir //	declarations
80*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir /*-************************************************************************************************************//**
83*cdf0e10cSrcweir     @short          helper to set right lock in right situation
84*cdf0e10cSrcweir     @descr          This helper support different types of locking:
85*cdf0e10cSrcweir                         a)  no locks - transparent for user!
86*cdf0e10cSrcweir                             This could be usefull for simluation or single threaded environments!
87*cdf0e10cSrcweir                         b)  own mutex
88*cdf0e10cSrcweir                             An object use his own osl-mutex to be threadsafe. Usefull for easy and exclusiv locking.
89*cdf0e10cSrcweir                         c)  solar mutex
90*cdf0e10cSrcweir                             An object use our solar mutex and will be a part of a greater safed "threadsafe code block".
91*cdf0e10cSrcweir                             Could be usefull for simulation and testing of higher modules!
92*cdf0e10cSrcweir                         d)  fair rw-lock
93*cdf0e10cSrcweir                             An object use an implementation of a fair rw-lock. This increase granularity of t hreadsafe mechanism
94*cdf0e10cSrcweir                             and should be used for high performance threadsafe code!
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     @attention      We support two interfaces - "IMutex" and "IRWLock". Don't mix using of it!
97*cdf0e10cSrcweir                     A guard implementation should use one interface only!
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     @implements     IMutex
100*cdf0e10cSrcweir     @implements     IRWLock
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     @base           INonCopyable
103*cdf0e10cSrcweir                     IMutex
104*cdf0e10cSrcweir                     IRWLock
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     @devstatus      draft
107*cdf0e10cSrcweir *//*-*************************************************************************************************************/
108*cdf0e10cSrcweir class FWI_DLLPUBLIC LockHelper : public  IMutex
109*cdf0e10cSrcweir                  , public  IRWLock
110*cdf0e10cSrcweir                  , private INonCopyable
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
113*cdf0e10cSrcweir 	//	public methods
114*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
115*cdf0e10cSrcweir 	public:
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
118*cdf0e10cSrcweir         //  ctor/dtor
119*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
120*cdf0e10cSrcweir                  LockHelper( ::vos::IMutex* pSolarMutex = NULL );
121*cdf0e10cSrcweir         virtual ~LockHelper(                                   );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
124*cdf0e10cSrcweir         //  interface ::framework::IMutex
125*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
126*cdf0e10cSrcweir         virtual void acquire();
127*cdf0e10cSrcweir         virtual void release();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
130*cdf0e10cSrcweir         //  interface ::framework::IRWLock
131*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
132*cdf0e10cSrcweir         virtual void acquireReadAccess   ();
133*cdf0e10cSrcweir         virtual void releaseReadAccess   ();
134*cdf0e10cSrcweir         virtual void acquireWriteAccess  ();
135*cdf0e10cSrcweir         virtual void releaseWriteAccess  ();
136*cdf0e10cSrcweir         virtual void downgradeWriteAccess();
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
139*cdf0e10cSrcweir         //  something else
140*cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
141*cdf0e10cSrcweir         static LockHelper&  getGlobalLock       ( ::vos::IMutex* pSolarMutex = NULL );
142*cdf0e10cSrcweir         ::osl::Mutex&       getShareableOslMutex(                                   );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     //-------------------------------------------------------------------------------------------------------------
145*cdf0e10cSrcweir     //  private methods
146*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
147*cdf0e10cSrcweir     private:
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         static ELockType& implts_getLockType();
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
152*cdf0e10cSrcweir     //  private member
153*cdf0e10cSrcweir     //  a) Make some member mutable for using in const functions!
154*cdf0e10cSrcweir     //  b) "m_eLockType" define, which of follow members is used!
155*cdf0e10cSrcweir     //     You can use "m_pFairRWLock" as a fair rw-lock (multiple reader / one writer / looks for incoming order of threads too) ...
156*cdf0e10cSrcweir     //     or you can use a normal osl mutex ("m_pOwnMutex") ...
157*cdf0e10cSrcweir     //     ... or the solarmuex as "m_pSolarMutex" (must be set from outside! because some components must be vcl-free!)
158*cdf0e10cSrcweir     //     ... but sometimes you need a shareable osl mutex!
159*cdf0e10cSrcweir     //     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.
160*cdf0e10cSrcweir     //                                              Creation and using of "m_pShareableOslMutex" isn't neccessary!
161*cdf0e10cSrcweir     //                                         ii ) Otherwise you have no osl mutex ... so you must create "m_pShareableOslMutex" and use it twice!
162*cdf0e10cSrcweir     //                                              In this case you must lock two member everytime - "m_pShareableMutex" AND "m_pFairRWLock" or "m_pSolarMutex" or ...
163*cdf0e10cSrcweir     //                                              It isn't realy fine - but the only possible way.
164*cdf0e10cSrcweir     //                                         iii) There exist another special case - E_NOTHING is set! Then we should create this shareable mutex ...
165*cdf0e10cSrcweir     //                                              nad you can use it ... but this implmentation ignore it.
166*cdf0e10cSrcweir     //-------------------------------------------------------------------------------------------------------------
167*cdf0e10cSrcweir     private:
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         ELockType               m_eLockType             ;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         mutable FairRWLock*     m_pFairRWLock           ;
172*cdf0e10cSrcweir         mutable ::osl::Mutex*   m_pOwnMutex             ;
173*cdf0e10cSrcweir         mutable ::vos::IMutex*  m_pSolarMutex           ;
174*cdf0e10cSrcweir         mutable ::osl::Mutex*   m_pShareableOslMutex    ;
175*cdf0e10cSrcweir         mutable sal_Bool        m_bDummySolarMutex      ;
176*cdf0e10cSrcweir };
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir }		//	namespace framework
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir #endif  //  #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
181