1f8e07b45SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 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 10f8e07b45SAndrew Rist * 11f8e07b45SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12f8e07b45SAndrew Rist * 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. 19f8e07b45SAndrew Rist * 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 /*-************************************************************************************************************//** 58cdf0e10cSrcweir @descr If you use a lock or mutex as a member of your class and whish to use it earlier then 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 61cdf0e10cSrcweir member! Thats why ist a good place to declare your thread help member so. 62cdf0e10cSrcweir *//*-*************************************************************************************************************/ 63cdf0e10cSrcweir enum ELockType 64cdf0e10cSrcweir { 65cdf0e10cSrcweir E_NOTHING = 0 , 66cdf0e10cSrcweir E_OWNMUTEX = 1 , 67cdf0e10cSrcweir E_SOLARMUTEX = 2 , 68cdf0e10cSrcweir E_FAIRRWLOCK = 3 69cdf0e10cSrcweir }; 70cdf0e10cSrcweir 71cdf0e10cSrcweir #define ENVVAR_LOCKTYPE DECLARE_ASCII("LOCKTYPE_FRAMEWORK") 72cdf0e10cSrcweir #define FALLBACK_LOCKTYPE E_SOLARMUTEX 73cdf0e10cSrcweir 74cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 75cdf0e10cSrcweir // 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*07a3d7f1SPedro Giffuni This could be useful for simluation or single threaded environments! 83cdf0e10cSrcweir b) own mutex 84*07a3d7f1SPedro Giffuni An object use his own osl-mutex to be threadsafe. Useful for easy and exclusiv locking. 85cdf0e10cSrcweir c) solar mutex 86cdf0e10cSrcweir An object use our solar mutex and will be a part of a greater safed "threadsafe code block". 87*07a3d7f1SPedro Giffuni Could be useful for simulation and testing of higher modules! 88cdf0e10cSrcweir d) fair rw-lock 89cdf0e10cSrcweir An object use an implementation of a fair rw-lock. This increase granularity of t hreadsafe 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 140cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 141cdf0e10cSrcweir // private methods 142cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 143cdf0e10cSrcweir private: 144cdf0e10cSrcweir 145cdf0e10cSrcweir static ELockType& implts_getLockType(); 146cdf0e10cSrcweir 147cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 148cdf0e10cSrcweir // private member 149cdf0e10cSrcweir // a) Make some member mutable for using in const functions! 150cdf0e10cSrcweir // b) "m_eLockType" define, which of follow members is used! 151cdf0e10cSrcweir // You can use "m_pFairRWLock" as a fair rw-lock (multiple reader / one writer / looks for incoming order of threads too) ... 152cdf0e10cSrcweir // or you can use a normal osl mutex ("m_pOwnMutex") ... 153cdf0e10cSrcweir // ... or the solarmuex as "m_pSolarMutex" (must be set from outside! because some components must be vcl-free!) 154cdf0e10cSrcweir // ... but sometimes you need a shareable osl mutex! 155cdf0e10cSrcweir // 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*07a3d7f1SPedro Giffuni // Creation and use of "m_pShareableOslMutex" isn't necessary! 157cdf0e10cSrcweir // ii ) Otherwise you have no osl mutex ... so you must create "m_pShareableOslMutex" and use it twice! 158*07a3d7f1SPedro Giffuni // In this case you must lock two members every time - "m_pShareableMutex" AND "m_pFairRWLock" or "m_pSolarMutex" or ... 159*07a3d7f1SPedro Giffuni // It isn't really fine - but the only possible way. 160cdf0e10cSrcweir // iii) There exist another special case - E_NOTHING is set! Then we should create this shareable mutex ... 161cdf0e10cSrcweir // nad you can use it ... but this implmentation ignore it. 162cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 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 174cdf0e10cSrcweir } // namespace framework 175cdf0e10cSrcweir 176cdf0e10cSrcweir #endif // #ifndef __FRAMEWORK_THREADHELP_LOCKHELPER_HXX_ 177