1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: writeguard.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
32 #define __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
38 #include <threadhelp/inoncopyable.h>
39 #include <threadhelp/irwlock.h>
41 //#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_
42 //#include <threadhelp/threadhelpbase.hxx>
45 //_________________________________________________________________________________________________________________
47 //_________________________________________________________________________________________________________________
49 //_________________________________________________________________________________________________________________
51 //_________________________________________________________________________________________________________________
53 //_________________________________________________________________________________________________________________
55 //_________________________________________________________________________________________________________________
59 //_________________________________________________________________________________________________________________
61 //_________________________________________________________________________________________________________________
63 //_________________________________________________________________________________________________________________
65 //_________________________________________________________________________________________________________________
67 /*-************************************************************************************************************//**
68 @short implement a guard to set write locks
69 @descr This guard should be used to set a lock for reading AND writing object internal member.
70 We never need a own mutex to safe our internal member access - because
71 a guard is used as function-local member only. There exist no multithreaded access to it realy ...
73 @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
74 b) Use interface "IRWLock" of set LockHelper only - because we must support a finer granularity of locking.
75 Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"!
80 @devstatus ready to use
81 *//*-*************************************************************************************************************/
82 class WriteGuard
: private INonCopyable
84 //-------------------------------------------------------------------------------------------------------------
86 //-------------------------------------------------------------------------------------------------------------
89 /*-****************************************************************************************************//**
91 @descr These ctors initialize the guard with a reference to used lock member of object to protect.
92 Null isn't allowed as value!
96 @param "pLock" ,reference to used lock member of object to protect
97 @param "rLock" ,reference to used lock member of object to protect
101 *//*-*****************************************************************************************************/
102 inline WriteGuard( IRWLock
* pLock
)
104 , m_eMode ( E_NOLOCK
)
109 //*********************************************************************************************************
110 inline WriteGuard( IRWLock
& rLock
)
112 , m_eMode ( E_NOLOCK
)
117 /*-****************************************************************************************************//**
119 @descr We unlock the used lock member automaticly if user forget it.
127 *//*-*****************************************************************************************************/
133 /*-****************************************************************************************************//**
134 @short set write lock
135 @descr Call this method to set the write lock. The call will block till all current threads are synchronized!
137 @seealso method unlock()
143 *//*-*****************************************************************************************************/
149 // Acquire write access and set return state.
150 // Mode is set later if it was successful!
151 m_pLock
->acquireWriteAccess();
152 m_eMode
= E_WRITELOCK
;
156 // User has downgrade to read access before!
157 // We must release it before we can set a new write access!
158 m_pLock
->releaseReadAccess();
159 m_pLock
->acquireWriteAccess();
160 m_eMode
= E_WRITELOCK
;
163 default: break; // nothing to do
167 /*-****************************************************************************************************//**
168 @short unset write lock
169 @descr Call this method to unlock the rw-lock temp.!
170 Normaly we do it at dtor automaticly for you ...
172 @seealso method lock()
178 *//*-*****************************************************************************************************/
184 // User has downgraded to a read lock before!
185 // => There isn't realy a write lock ...
186 m_pLock
->releaseReadAccess();
191 m_pLock
->releaseWriteAccess();
195 default: break; // nothing to do
199 /*-****************************************************************************************************//**
200 @short downgrade write access to read access without new blocking!
201 @descr If this write lock is set you can change it to a "read lock".
202 An "upgrade" is the same like new calling "lock()"!
210 *//*-*****************************************************************************************************/
211 inline void downgrade()
213 if( m_eMode
== E_WRITELOCK
)
215 m_pLock
->downgradeWriteAccess();
216 m_eMode
= E_READLOCK
;
220 /*-****************************************************************************************************//**
221 @short return internal states
222 @descr For user they dont know what they are doing ...
227 @return Current set lock mode.
229 @onerror No error should occure.
230 *//*-*****************************************************************************************************/
231 inline ELockMode
getMode() const
236 //-------------------------------------------------------------------------------------------------------------
238 //-------------------------------------------------------------------------------------------------------------
241 /*-****************************************************************************************************//**
242 @short disable using of these functions!
243 @descr It's not allowed to use this methods. Different problem can occure otherwise.
244 Thats why we disable it by make it private.
252 *//*-*****************************************************************************************************/
255 //-------------------------------------------------------------------------------------------------------------
257 //-------------------------------------------------------------------------------------------------------------
260 IRWLock
* m_pLock
; /// reference to lock-member of protected object
261 ELockMode m_eMode
; /// protection against multiple lock calls without unlock and difference between supported lock modi
263 }; // class WriteGuard
265 } // namespace framework
267 #endif // #ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_