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