1f8e07b45SAndrew Rist /**************************************************************
2*f502cd5dSMatthias Seidel  *
3f8e07b45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f8e07b45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f8e07b45SAndrew Rist  * distributed with this work for additional information
6f8e07b45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f8e07b45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f8e07b45SAndrew Rist  * "License"); you may not use this file except in compliance
9f8e07b45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f502cd5dSMatthias Seidel  *
11f8e07b45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f502cd5dSMatthias Seidel  *
13f8e07b45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f8e07b45SAndrew Rist  * software distributed under the License is distributed on an
15f8e07b45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f8e07b45SAndrew Rist  * KIND, either express or implied.  See the License for the
17f8e07b45SAndrew Rist  * specific language governing permissions and limitations
18f8e07b45SAndrew Rist  * under the License.
19*f502cd5dSMatthias Seidel  *
20f8e07b45SAndrew Rist  *************************************************************/
21f8e07b45SAndrew Rist 
22f8e07b45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
25cdf0e10cSrcweir #define __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_________________________________________________________________________________________________________________
28cdf0e10cSrcweir //	my own includes
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <threadhelp/inoncopyable.h>
32cdf0e10cSrcweir #include <framework/imutex.hxx>
33cdf0e10cSrcweir #include <threadhelp/irwlock.h>
34cdf0e10cSrcweir #include <threadhelp/fairrwlock.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //_________________________________________________________________________________________________________________
37cdf0e10cSrcweir //	interface includes
38cdf0e10cSrcweir //_________________________________________________________________________________________________________________
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //_________________________________________________________________________________________________________________
41cdf0e10cSrcweir //	other includes
42cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43cdf0e10cSrcweir #include <osl/mutex.hxx>
44cdf0e10cSrcweir #include <vos/mutex.hxx>
45cdf0e10cSrcweir #include <fwidllapi.h>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48cdf0e10cSrcweir //	namespace
49cdf0e10cSrcweir //_________________________________________________________________________________________________________________
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace framework{
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //_________________________________________________________________________________________________________________
54cdf0e10cSrcweir //	const
55cdf0e10cSrcweir //_________________________________________________________________________________________________________________
56cdf0e10cSrcweir 
57cdf0e10cSrcweir /*-************************************************************************************************************//**
58*f502cd5dSMatthias Seidel 	@descr			If you use a lock or mutex as a member of your class and wish to use it earlier than other ones
59cdf0e10cSrcweir 					you should have a look on this implementation. You must use it as the first base class
60cdf0e10cSrcweir 					of your implementation - because base classes are initialized by his order and before your
61*f502cd5dSMatthias Seidel 					member! Thats why it is a good place to declare your thread help member so.
62cdf0e10cSrcweir *//*-*************************************************************************************************************/
63cdf0e10cSrcweir enum ELockType
64cdf0e10cSrcweir {
65*f502cd5dSMatthias Seidel 	E_NOTHING		= 0 ,
66*f502cd5dSMatthias Seidel 	E_OWNMUTEX		= 1 ,
67*f502cd5dSMatthias Seidel 	E_SOLARMUTEX	= 2 ,
68*f502cd5dSMatthias Seidel 	E_FAIRRWLOCK	= 3
69cdf0e10cSrcweir };
70cdf0e10cSrcweir 
71*f502cd5dSMatthias Seidel #define ENVVAR_LOCKTYPE		DECLARE_ASCII("LOCKTYPE_FRAMEWORK")
72*f502cd5dSMatthias Seidel #define FALLBACK_LOCKTYPE	E_SOLARMUTEX
73cdf0e10cSrcweir 
74cdf0e10cSrcweir //_________________________________________________________________________________________________________________
75*f502cd5dSMatthias Seidel // declarations
76cdf0e10cSrcweir //_________________________________________________________________________________________________________________
77cdf0e10cSrcweir 
78cdf0e10cSrcweir /*-************************************************************************************************************//**
79cdf0e10cSrcweir     @short          helper to set right lock in right situation
80cdf0e10cSrcweir     @descr          This helper support different types of locking:
81cdf0e10cSrcweir                         a)  no locks - transparent for user!
82*f502cd5dSMatthias Seidel                             This could be useful for simulation or single threaded environments!
83cdf0e10cSrcweir                         b)  own mutex
84*f502cd5dSMatthias Seidel                             An object use his own osl-mutex to be threadsafe. Useful for easy and exclusive locking.
85cdf0e10cSrcweir                         c)  solar mutex
86*f502cd5dSMatthias Seidel                             An object use our solar mutex and will be a part of a greater saved "threadsafe code block".
8707a3d7f1SPedro Giffuni                             Could be useful for simulation and testing of higher modules!
88cdf0e10cSrcweir                         d)  fair rw-lock
89*f502cd5dSMatthias Seidel                             An object use an implementation of a fair rw-lock. This increase granularity of threadsafe mechanism
90cdf0e10cSrcweir                             and should be used for high performance threadsafe code!
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     @attention      We support two interfaces - "IMutex" and "IRWLock". Don't mix using of it!
93cdf0e10cSrcweir                     A guard implementation should use one interface only!
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     @implements     IMutex
96cdf0e10cSrcweir     @implements     IRWLock
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     @base           INonCopyable
99cdf0e10cSrcweir                     IMutex
100cdf0e10cSrcweir                     IRWLock
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     @devstatus      draft
103cdf0e10cSrcweir *//*-*************************************************************************************************************/
104cdf0e10cSrcweir class FWI_DLLPUBLIC LockHelper : public  IMutex
105cdf0e10cSrcweir                  , public  IRWLock
106cdf0e10cSrcweir                  , private INonCopyable
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
109cdf0e10cSrcweir 	//	public methods
110cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
111cdf0e10cSrcweir 	public:
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
114cdf0e10cSrcweir         //  ctor/dtor
115cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
116cdf0e10cSrcweir                  LockHelper( ::vos::IMutex* pSolarMutex = NULL );
117cdf0e10cSrcweir         virtual ~LockHelper(                                   );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
120cdf0e10cSrcweir         //  interface ::framework::IMutex
121cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
122cdf0e10cSrcweir         virtual void acquire();
123cdf0e10cSrcweir         virtual void release();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
126cdf0e10cSrcweir         //  interface ::framework::IRWLock
127cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
128cdf0e10cSrcweir         virtual void acquireReadAccess   ();
129cdf0e10cSrcweir         virtual void releaseReadAccess   ();
130cdf0e10cSrcweir         virtual void acquireWriteAccess  ();
131cdf0e10cSrcweir         virtual void releaseWriteAccess  ();
132cdf0e10cSrcweir         virtual void downgradeWriteAccess();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
135cdf0e10cSrcweir         //  something else
136cdf0e10cSrcweir         //-------------------------------------------------------------------------------------------------------------
137cdf0e10cSrcweir         static LockHelper&  getGlobalLock       ( ::vos::IMutex* pSolarMutex = NULL );
138cdf0e10cSrcweir         ::osl::Mutex&       getShareableOslMutex(                                   );
139cdf0e10cSrcweir 
140*f502cd5dSMatthias Seidel 	//-------------------------------------------------------------------------------------------------------------
141*f502cd5dSMatthias Seidel 	//  private methods
142cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
143cdf0e10cSrcweir     private:
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         static ELockType& implts_getLockType();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	//-------------------------------------------------------------------------------------------------------------
148*f502cd5dSMatthias Seidel 	//  private member
149*f502cd5dSMatthias Seidel 	//  a) Make some member mutable for using in const functions!
150*f502cd5dSMatthias Seidel 	//  b) "m_eLockType" define, which of follow members is used!
151*f502cd5dSMatthias Seidel 	//     You can use "m_pFairRWLock" as a fair rw-lock (multiple reader / one writer / looks for incoming order of threads too) ...
152*f502cd5dSMatthias Seidel 	//     or you can use a normal osl mutex ("m_pOwnMutex") ...
153*f502cd5dSMatthias Seidel 	//     ... or the solarmuex as "m_pSolarMutex" (must be set from outside! because some components must be vcl-free!)
154*f502cd5dSMatthias Seidel 	//     ... but sometimes you need a shareable osl mutex!
155*f502cd5dSMatthias Seidel 	//     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.
156*f502cd5dSMatthias Seidel 	//                                              Creation and use of "m_pShareableOslMutex" isn't necessary!
157*f502cd5dSMatthias Seidel 	//                                         ii ) Otherwise you have no osl mutex ... so you must create "m_pShareableOslMutex" and use it twice!
158*f502cd5dSMatthias Seidel 	//                                              In this case you must lock two members every time - "m_pShareableMutex" AND "m_pFairRWLock" or "m_pSolarMutex" or ...
159*f502cd5dSMatthias Seidel 	//                                              It isn't really fine - but the only possible way.
160*f502cd5dSMatthias Seidel 	//                                         iii) There exists another special case - E_NOTHING is set! Then we should create this shareable mutex ...
161*f502cd5dSMatthias Seidel 	//                                              and you can use it ... but this implementation ignores it.
162*f502cd5dSMatthias Seidel 	//-------------------------------------------------------------------------------------------------------------
163cdf0e10cSrcweir     private:
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         ELockType               m_eLockType             ;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         mutable FairRWLock*     m_pFairRWLock           ;
168cdf0e10cSrcweir         mutable ::osl::Mutex*   m_pOwnMutex             ;
169cdf0e10cSrcweir         mutable ::vos::IMutex*  m_pSolarMutex           ;
170cdf0e10cSrcweir         mutable ::osl::Mutex*   m_pShareableOslMutex    ;
171cdf0e10cSrcweir         mutable sal_Bool        m_bDummySolarMutex      ;
172cdf0e10cSrcweir };
173cdf0e10cSrcweir 
174*f502cd5dSMatthias Seidel }		// namespace framework
175cdf0e10cSrcweir 
176cdf0e10cSrcweir #endif  //  #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_
177