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