1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
25 #define __FRAMEWORK_THREADHELP_READGUARD_HXX_
26 
27 //_________________________________________________________________________________________________________________
28 //	my own includes
29 //_________________________________________________________________________________________________________________
30 
31 #include <threadhelp/inoncopyable.h>
32 #include <threadhelp/irwlock.h>
33 
34 //#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_
35 //#include <threadhelp/threadhelpbase.hxx>
36 //#endif
37 
38 //_________________________________________________________________________________________________________________
39 //	interface includes
40 //_________________________________________________________________________________________________________________
41 
42 //_________________________________________________________________________________________________________________
43 //	other includes
44 //_________________________________________________________________________________________________________________
45 #include <sal/types.h>
46 
47 //_________________________________________________________________________________________________________________
48 //	namespace
49 //_________________________________________________________________________________________________________________
50 
51 namespace framework{
52 
53 //_________________________________________________________________________________________________________________
54 //	const
55 //_________________________________________________________________________________________________________________
56 
57 //_________________________________________________________________________________________________________________
58 //	declarations
59 //_________________________________________________________________________________________________________________
60 
61 /*-************************************************************************************************************//**
62 	@short          implement a guard to set read locks
63 	@descr			This guard should be used to set a lock for reading object internal member.
64 					Nobody can control it but don't use member after successfuly locking for writing!
65 					We never need a own mutex to safe our internal member access - because
66 					a guard is used as function-local member only. There exist no multithreaded access to it realy ...
67 
68     @attention      a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
69                     b) Use interface "IRWLock" of set LockHelper only - because we must support a finer granularity of locking.
70                        Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"!
71 
72 	@implements		-
73     @base           INonCopyable
74 
75 	@devstatus		ready to use
76 *//*-*************************************************************************************************************/
77 class ReadGuard : private INonCopyable
78 {
79 	//-------------------------------------------------------------------------------------------------------------
80 	//	public methods
81 	//-------------------------------------------------------------------------------------------------------------
82 	public:
83 
84         /*-****************************************************************************************************//**
85             @short      ctor
86             @descr      These ctors initialize the guard with a reference to used lock member of object to protect.
87                         Null isn't allowed as value!
88 
89             @seealso    -
90 
91             @param      "pLock" ,reference to used lock member of object to protect
92             @param      "rLock" ,reference to used lock member of object to protect
93             @return     -
94 
95             @onerror    -
96         *//*-*****************************************************************************************************/
ReadGuard(IRWLock * pLock)97         inline ReadGuard( IRWLock* pLock )
98             :   m_pLock     ( pLock     )
99             ,   m_bLocked   ( sal_False )
100         {
101             lock();
102         }
103 
104         //*********************************************************************************************************
ReadGuard(IRWLock & rLock)105         inline ReadGuard( IRWLock& rLock )
106             :   m_pLock     ( &rLock    )
107             ,   m_bLocked   ( sal_False )
108         {
109             lock();
110         }
111 
112         /*-****************************************************************************************************//**
113             @short      dtor
114             @descr      We unlock the used lock member automaticly if user forget it.
115 
116             @seealso    -
117 
118             @param      -
119             @return     -
120 
121             @onerror    -
122         *//*-*****************************************************************************************************/
~ReadGuard()123         inline ~ReadGuard()
124         {
125             unlock();
126         }
127 
128         /*-****************************************************************************************************//**
129             @short      set read lock
130             @descr      Call this method to set the read lock. The call will block till all current threads are synchronized!
131 
132             @seealso    method unlock()
133 
134             @param      -
135             @return     -
136 
137             @onerror    -
138         *//*-*****************************************************************************************************/
lock()139         inline void lock()
140         {
141             if( m_bLocked == sal_False )
142             {
143                 m_pLock->acquireReadAccess();
144                 m_bLocked = sal_True;
145             }
146         }
147 
148         /*-****************************************************************************************************//**
149             @short      unset read lock
150             @descr      Call this method to unlock the rw-lock temp.!
151                         Normaly we do it at dtor automaticly for you ...
152 
153             @seealso    method lock()
154 
155             @param      -
156             @return     -
157 
158             @onerror    -
159         *//*-*****************************************************************************************************/
unlock()160         inline void unlock()
161         {
162             if( m_bLocked == sal_True )
163             {
164                 m_pLock->releaseReadAccess();
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 		ReadGuard();
187 
188 	//-------------------------------------------------------------------------------------------------------------
189 	//	private member
190 	//-------------------------------------------------------------------------------------------------------------
191 	private:
192 
193         IRWLock*    m_pLock     ;   /// reference to lock-member of protected object
194 		sal_Bool	m_bLocked	;	/// protection against multiple lock calls without unlock!
195 
196 };		//	class ReadGuard
197 
198 }		//	namespace framework
199 
200 #endif	//	#ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
201