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_RESETABLEGUARD_HXX_ 29*cdf0e10cSrcweir #define __FRAMEWORK_THREADHELP_RESETABLEGUARD_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 38*cdf0e10cSrcweir //#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_ 39*cdf0e10cSrcweir //#include <threadhelp/threadhelpbase.hxx> 40*cdf0e10cSrcweir //#endif 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 43*cdf0e10cSrcweir // interface includes 44*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 47*cdf0e10cSrcweir // other includes 48*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 49*cdf0e10cSrcweir #include <sal/types.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 // declarations 63*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir /*-************************************************************************************************************//** 66*cdf0e10cSrcweir @short implement a guard for implementing save thread access 67*cdf0e10cSrcweir @descr These guard has an additional feature to well known one ::osl::Guard. 68*cdf0e10cSrcweir You can lock() and unlock() it very often! 69*cdf0e10cSrcweir A set bool flag inside protect this implementation against multiple lock() calls 70*cdf0e10cSrcweir without any unlock()! So the increasing of guarded mutex couldn't be greater then 1 ... 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private! 73*cdf0e10cSrcweir b) Use interface "IMutex" of set LockHelper only - because we must support an exclusiv locking. 74*cdf0e10cSrcweir Interface "IRWLock" should be used by special guard implementations ... like "ReadGuard" or "WriteGuard"! 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir @implements - 77*cdf0e10cSrcweir @base INonCopyable 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir @devstatus ready to use 80*cdf0e10cSrcweir *//*-*************************************************************************************************************/ 81*cdf0e10cSrcweir class ResetableGuard : private INonCopyable 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 84*cdf0e10cSrcweir // public methods 85*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 86*cdf0e10cSrcweir public: 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /*-****************************************************************************************************//** 89*cdf0e10cSrcweir @short ctors 90*cdf0e10cSrcweir @descr Use these ctor methods to initialize the guard right. 91*cdf0e10cSrcweir Given lock reference must be valid - otherwise crashes could occure! 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir @seealso - 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir @param "pLock", pointer to lock helper of user 96*cdf0e10cSrcweir @param "rLock", reference to lock helper of user 97*cdf0e10cSrcweir @return - 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir @onerror - 100*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 101*cdf0e10cSrcweir inline ResetableGuard( IMutex* pLock ) 102*cdf0e10cSrcweir : m_pLock ( pLock ) 103*cdf0e10cSrcweir , m_bLocked ( sal_False ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir lock(); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir //********************************************************************************************************* 109*cdf0e10cSrcweir inline ResetableGuard( IMutex& rLock ) 110*cdf0e10cSrcweir : m_pLock ( &rLock ) 111*cdf0e10cSrcweir , m_bLocked ( sal_False ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir lock(); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /*-****************************************************************************************************//** 117*cdf0e10cSrcweir @short dtor 118*cdf0e10cSrcweir @descr We must release set mutex if programmer forget it ... 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir @seealso - 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir @param - 123*cdf0e10cSrcweir @return - 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir @onerror - 126*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 127*cdf0e10cSrcweir inline ~ResetableGuard() 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir unlock(); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir /*-****************************************************************************************************//** 133*cdf0e10cSrcweir @short enable/disable the lock 134*cdf0e10cSrcweir @descr Use this methods to lock or unlock the mutex. 135*cdf0e10cSrcweir You can do it so often you wish to do that ... 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir @attention We use another member to prevent us against multiple acquire calls of the same guard 138*cdf0e10cSrcweir without suitable release calls! 139*cdf0e10cSrcweir You don't must protect access at these bool member by using an own mutex .... 140*cdf0e10cSrcweir because nobody use the same guard instance from different threads! 141*cdf0e10cSrcweir It will be a function-local object every time. 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir @seealso - 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir @param - 146*cdf0e10cSrcweir @return - 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir @onerror - 149*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 150*cdf0e10cSrcweir inline void lock() 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir if( m_bLocked == sal_False ) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir m_pLock->acquire(); 155*cdf0e10cSrcweir m_bLocked = sal_True; 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir //********************************************************************************************************* 160*cdf0e10cSrcweir inline void unlock() 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir if( m_bLocked == sal_True ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir m_pLock->release(); 165*cdf0e10cSrcweir m_bLocked = sal_False; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 170*cdf0e10cSrcweir // private methods 171*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 172*cdf0e10cSrcweir private: 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir /*-****************************************************************************************************//** 175*cdf0e10cSrcweir @short disable using of these functions! 176*cdf0e10cSrcweir @descr It's not allowed to use this methods. Different problem can occure otherwise. 177*cdf0e10cSrcweir Thats why we disable it by make it private. 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir @seealso other ctor 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir @param - 182*cdf0e10cSrcweir @return - 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir @onerror - 185*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 186*cdf0e10cSrcweir ResetableGuard(); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 189*cdf0e10cSrcweir // private member 190*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 191*cdf0e10cSrcweir private: 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir IMutex* m_pLock ; /// pointer to safed lock member of user 194*cdf0e10cSrcweir sal_Bool m_bLocked ; /// protection against multiple lock() calls without unlock() 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir }; // class ResetableGuard 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir } // namespace framework 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir #endif // #ifndef __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_ 201