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: resetableguard.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_RESETABLEGUARD_HXX_
32 #define __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
38 #include <threadhelp/inoncopyable.h>
39 #include <threadhelp/imutex.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 for implementing save thread access
70 @descr These guard has an additional feature to well known one ::osl::Guard.
71 You can lock() and unlock() it very often!
72 A set bool flag inside protect this implementation against multiple lock() calls
73 without any unlock()! So the increasing of guarded mutex couldn't be greater then 1 ...
75 @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
76 b) Use interface "IMutex" of set LockHelper only - because we must support an exclusiv locking.
77 Interface "IRWLock" should be used by special guard implementations ... like "ReadGuard" or "WriteGuard"!
82 @devstatus ready to use
83 *//*-*************************************************************************************************************/
84 class ResetableGuard
: private INonCopyable
86 //-------------------------------------------------------------------------------------------------------------
88 //-------------------------------------------------------------------------------------------------------------
91 /*-****************************************************************************************************//**
93 @descr Use these ctor methods to initialize the guard right.
94 Given lock reference must be valid - otherwise crashes could occure!
98 @param "pLock", pointer to lock helper of user
99 @param "rLock", reference to lock helper of user
103 *//*-*****************************************************************************************************/
104 inline ResetableGuard( IMutex
* pLock
)
106 , m_bLocked ( sal_False
)
111 //*********************************************************************************************************
112 inline ResetableGuard( IMutex
& rLock
)
114 , m_bLocked ( sal_False
)
119 /*-****************************************************************************************************//**
121 @descr We must release set mutex if programmer forget it ...
129 *//*-*****************************************************************************************************/
130 inline ~ResetableGuard()
135 /*-****************************************************************************************************//**
136 @short enable/disable the lock
137 @descr Use this methods to lock or unlock the mutex.
138 You can do it so often you wish to do that ...
140 @attention We use another member to prevent us against multiple acquire calls of the same guard
141 without suitable release calls!
142 You don't must protect access at these bool member by using an own mutex ....
143 because nobody use the same guard instance from different threads!
144 It will be a function-local object every time.
152 *//*-*****************************************************************************************************/
155 if( m_bLocked
== sal_False
)
158 m_bLocked
= sal_True
;
162 //*********************************************************************************************************
165 if( m_bLocked
== sal_True
)
168 m_bLocked
= sal_False
;
172 //-------------------------------------------------------------------------------------------------------------
174 //-------------------------------------------------------------------------------------------------------------
177 /*-****************************************************************************************************//**
178 @short disable using of these functions!
179 @descr It's not allowed to use this methods. Different problem can occure otherwise.
180 Thats why we disable it by make it private.
188 *//*-*****************************************************************************************************/
191 //-------------------------------------------------------------------------------------------------------------
193 //-------------------------------------------------------------------------------------------------------------
196 IMutex
* m_pLock
; /// pointer to safed lock member of user
197 sal_Bool m_bLocked
; /// protection against multiple lock() calls without unlock()
199 }; // class ResetableGuard
201 } // namespace framework
203 #endif // #ifndef __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_