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_READGUARD_HXX_ 29 #define __FRAMEWORK_THREADHELP_READGUARD_HXX_ 30 31 //_________________________________________________________________________________________________________________ 32 // my own includes 33 //_________________________________________________________________________________________________________________ 34 35 #include <threadhelp/inoncopyable.h> 36 #include <threadhelp/irwlock.h> 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 to set read locks 67 @descr This guard should be used to set a lock for reading object internal member. 68 Nobody can control it but don't use member after successfuly locking for writing! 69 We never need a own mutex to safe our internal member access - because 70 a guard is used as function-local member only. There exist no multithreaded access to it realy ... 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 "IRWLock" of set LockHelper only - because we must support a finer granularity of locking. 74 Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"! 75 76 @implements - 77 @base INonCopyable 78 79 @devstatus ready to use 80 *//*-*************************************************************************************************************/ 81 class ReadGuard : private INonCopyable 82 { 83 //------------------------------------------------------------------------------------------------------------- 84 // public methods 85 //------------------------------------------------------------------------------------------------------------- 86 public: 87 88 /*-****************************************************************************************************//** 89 @short ctor 90 @descr These ctors initialize the guard with a reference to used lock member of object to protect. 91 Null isn't allowed as value! 92 93 @seealso - 94 95 @param "pLock" ,reference to used lock member of object to protect 96 @param "rLock" ,reference to used lock member of object to protect 97 @return - 98 99 @onerror - 100 *//*-*****************************************************************************************************/ 101 inline ReadGuard( IRWLock* pLock ) 102 : m_pLock ( pLock ) 103 , m_bLocked ( sal_False ) 104 { 105 lock(); 106 } 107 108 //********************************************************************************************************* 109 inline ReadGuard( IRWLock& rLock ) 110 : m_pLock ( &rLock ) 111 , m_bLocked ( sal_False ) 112 { 113 lock(); 114 } 115 116 /*-****************************************************************************************************//** 117 @short dtor 118 @descr We unlock the used lock member automaticly if user forget it. 119 120 @seealso - 121 122 @param - 123 @return - 124 125 @onerror - 126 *//*-*****************************************************************************************************/ 127 inline ~ReadGuard() 128 { 129 unlock(); 130 } 131 132 /*-****************************************************************************************************//** 133 @short set read lock 134 @descr Call this method to set the read lock. The call will block till all current threads are synchronized! 135 136 @seealso method unlock() 137 138 @param - 139 @return - 140 141 @onerror - 142 *//*-*****************************************************************************************************/ 143 inline void lock() 144 { 145 if( m_bLocked == sal_False ) 146 { 147 m_pLock->acquireReadAccess(); 148 m_bLocked = sal_True; 149 } 150 } 151 152 /*-****************************************************************************************************//** 153 @short unset read lock 154 @descr Call this method to unlock the rw-lock temp.! 155 Normaly we do it at dtor automaticly for you ... 156 157 @seealso method lock() 158 159 @param - 160 @return - 161 162 @onerror - 163 *//*-*****************************************************************************************************/ 164 inline void unlock() 165 { 166 if( m_bLocked == sal_True ) 167 { 168 m_pLock->releaseReadAccess(); 169 m_bLocked = sal_False; 170 } 171 } 172 173 //------------------------------------------------------------------------------------------------------------- 174 // private methods 175 //------------------------------------------------------------------------------------------------------------- 176 private: 177 178 /*-****************************************************************************************************//** 179 @short disable using of these functions! 180 @descr It's not allowed to use this methods. Different problem can occure otherwise. 181 Thats why we disable it by make it private. 182 183 @seealso other ctor 184 185 @param - 186 @return - 187 188 @onerror - 189 *//*-*****************************************************************************************************/ 190 ReadGuard(); 191 192 //------------------------------------------------------------------------------------------------------------- 193 // private member 194 //------------------------------------------------------------------------------------------------------------- 195 private: 196 197 IRWLock* m_pLock ; /// reference to lock-member of protected object 198 sal_Bool m_bLocked ; /// protection against multiple lock calls without unlock! 199 200 }; // class ReadGuard 201 202 } // namespace framework 203 204 #endif // #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_ 205