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: readguard.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_READGUARD_HXX_
32 #define __FRAMEWORK_THREADHELP_READGUARD_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 //_________________________________________________________________________________________________________________
52 #include <sal/types.h>
54 //_________________________________________________________________________________________________________________
56 //_________________________________________________________________________________________________________________
60 //_________________________________________________________________________________________________________________
62 //_________________________________________________________________________________________________________________
64 //_________________________________________________________________________________________________________________
66 //_________________________________________________________________________________________________________________
68 /*-************************************************************************************************************//**
69 @short implement a guard to set read locks
70 @descr This guard should be used to set a lock for reading object internal member.
71 Nobody can control it but don't use member after successfuly locking for writing!
72 We never need a own mutex to safe our internal member access - because
73 a guard is used as function-local member only. There exist no multithreaded access to it realy ...
75 @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
76 b) Use interface "IRWLock" of set LockHelper only - because we must support a finer granularity of locking.
77 Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"!
82 @devstatus ready to use
83 *//*-*************************************************************************************************************/
84 class ReadGuard
: private INonCopyable
86 //-------------------------------------------------------------------------------------------------------------
88 //-------------------------------------------------------------------------------------------------------------
91 /*-****************************************************************************************************//**
93 @descr These ctors initialize the guard with a reference to used lock member of object to protect.
94 Null isn't allowed as value!
98 @param "pLock" ,reference to used lock member of object to protect
99 @param "rLock" ,reference to used lock member of object to protect
103 *//*-*****************************************************************************************************/
104 inline ReadGuard( IRWLock
* pLock
)
106 , m_bLocked ( sal_False
)
111 //*********************************************************************************************************
112 inline ReadGuard( IRWLock
& rLock
)
114 , m_bLocked ( sal_False
)
119 /*-****************************************************************************************************//**
121 @descr We unlock the used lock member automaticly if user forget it.
129 *//*-*****************************************************************************************************/
135 /*-****************************************************************************************************//**
137 @descr Call this method to set the read lock. The call will block till all current threads are synchronized!
139 @seealso method unlock()
145 *//*-*****************************************************************************************************/
148 if( m_bLocked
== sal_False
)
150 m_pLock
->acquireReadAccess();
151 m_bLocked
= sal_True
;
155 /*-****************************************************************************************************//**
156 @short unset read lock
157 @descr Call this method to unlock the rw-lock temp.!
158 Normaly we do it at dtor automaticly for you ...
160 @seealso method lock()
166 *//*-*****************************************************************************************************/
169 if( m_bLocked
== sal_True
)
171 m_pLock
->releaseReadAccess();
172 m_bLocked
= sal_False
;
176 //-------------------------------------------------------------------------------------------------------------
178 //-------------------------------------------------------------------------------------------------------------
181 /*-****************************************************************************************************//**
182 @short disable using of these functions!
183 @descr It's not allowed to use this methods. Different problem can occure otherwise.
184 Thats why we disable it by make it private.
192 *//*-*****************************************************************************************************/
195 //-------------------------------------------------------------------------------------------------------------
197 //-------------------------------------------------------------------------------------------------------------
200 IRWLock
* m_pLock
; /// reference to lock-member of protected object
201 sal_Bool m_bLocked
; /// protection against multiple lock calls without unlock!
203 }; // class ReadGuard
205 } // namespace framework
207 #endif // #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_