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