Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #ifndef __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_
21 : : #define __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_
22 : :
23 : : #include <threadhelp/inoncopyable.h>
24 : : #include <threadhelp/irwlock.h>
25 : : #include <macros/debug.hxx>
26 : :
27 : : #include <com/sun/star/uno/XInterface.hpp>
28 : :
29 : : #include <osl/mutex.hxx>
30 : : #include <osl/conditn.hxx>
31 : :
32 : : namespace framework{
33 : :
34 : : /*-************************************************************************************************************//**
35 : : @short implement a read/write lock with fairness between read/write accessors
36 : : @descr These implementation never should used as base class! Use it as a member every time.
37 : : Use ReadGuard and/or WriteGuard in your methods (which work with these lock)
38 : : to make your code threadsafe.
39 : : Fair means: All reading or writing threads are synchronized AND serialzed by using one
40 : : mutex. For reader this mutex is used to access internal variables of this lock only;
41 : : for writer this mutex is used to have an exclusiv access on your class member!
42 : : => It's a multi-reader/single-writer lock, which no preferred accessor.
43 : :
44 : : @implements IRWlock
45 : : @base INonCopyable
46 : : IRWLock
47 : :
48 : : @devstatus ready to use
49 : : *//*-*************************************************************************************************************/
50 : : class FairRWLock : public IRWLock
51 : : , private INonCopyable
52 : : {
53 : : //-------------------------------------------------------------------------------------------------------------
54 : : // public methods
55 : : //-------------------------------------------------------------------------------------------------------------
56 : : public:
57 : :
58 : : /*-****************************************************************************************************//**
59 : : @short standard ctor
60 : : @descr Initialize instance with right start values for correct working.
61 : : no reader could exist => m_nReadCount = 0
62 : : don't block first comming writer => m_aWriteCondition.set()
63 : :
64 : : @seealso -
65 : :
66 : : @param -
67 : : @return -
68 : :
69 : : @onerror -
70 : : *//*-*****************************************************************************************************/
71 : 0 : inline FairRWLock()
72 [ # # ][ # # ]: 0 : : m_nReadCount( 0 )
[ # # ]
73 : : {
74 [ # # ]: 0 : m_aWriteCondition.set();
75 : 0 : }
76 : :
77 : 0 : inline virtual ~FairRWLock()
78 [ # # ][ # # ]: 0 : {
[ # # ]
79 [ # # ]: 0 : }
80 : :
81 : : /*-****************************************************************************************************//**
82 : : @interface IRWLock
83 : : @short set lock for reading
84 : : @descr A guard should call this method to acquire read access on your member.
85 : : Writing isn't allowed then - but nobody could check it for you!
86 : :
87 : : @seealso method releaseReadAccess()
88 : :
89 : : @param -
90 : : @return -
91 : :
92 : : @onerror -
93 : : *//*-*****************************************************************************************************/
94 : 0 : inline virtual void acquireReadAccess()
95 : : {
96 : : // Put call in "SERIALIZE"-queue!
97 : : // After successfully acquiring this mutex we are alone ...
98 [ # # ]: 0 : ::osl::MutexGuard aSerializeGuard( m_aSerializer );
99 : :
100 : : // ... but we should synchronize us with other reader!
101 : : // May be - they will unregister himself by using releaseReadAccess()!
102 [ # # ]: 0 : ::osl::MutexGuard aAccessGuard( m_aAccessLock );
103 : :
104 : : // Now we must register us as reader by increasing counter.
105 : : // If this the first writer we must close door for possible writer.
106 : : // Other reader don't look for this barrier - they work parallel to us!
107 [ # # ]: 0 : if( m_nReadCount == 0 )
108 : : {
109 [ # # ]: 0 : m_aWriteCondition.reset();
110 : : }
111 [ # # ][ # # ]: 0 : ++m_nReadCount;
112 : 0 : }
113 : :
114 : : /*-****************************************************************************************************//**
115 : : @interface IRWLock
116 : : @short reset lock for reading
117 : : @descr A guard should call this method to release read access on your member.
118 : :
119 : : @seealso method acquireReadAccess()
120 : :
121 : : @param -
122 : : @return -
123 : :
124 : : @onerror -
125 : : *//*-*****************************************************************************************************/
126 : 0 : inline virtual void releaseReadAccess()
127 : : {
128 : : // The access lock is enough at this point
129 : : // because it's not allowed to wait for all reader or writer here!
130 : : // That will cause a deadlock!
131 [ # # ]: 0 : ::osl::MutexGuard aAccessGuard( m_aAccessLock );
132 : :
133 : : // Unregister as reader first!
134 : : // Open writer barrier then if it was the last reader.
135 : 0 : --m_nReadCount;
136 [ # # ]: 0 : if( m_nReadCount == 0 )
137 : : {
138 [ # # ]: 0 : m_aWriteCondition.set();
139 [ # # ]: 0 : }
140 : 0 : }
141 : :
142 : : /*-****************************************************************************************************//**
143 : : @interface IRWLock
144 : : @short set lock for writing
145 : : @descr A guard should call this method to acquire write access on your member.
146 : : Reading is allowed too - of course.
147 : : After successfully calling of this method you are the only writer.
148 : :
149 : : @seealso method releaseWriteAccess()
150 : :
151 : : @param -
152 : : @return -
153 : :
154 : : @onerror -
155 : : *//*-*****************************************************************************************************/
156 : 0 : inline virtual void acquireWriteAccess()
157 : : {
158 : : // You have to stand in our serialize-queue till all reader
159 : : // are registered (not for releasing them!) or writer finished their work!
160 : : // Don't use a guard to do so - because you must hold the mutex till
161 : : // you call releaseWriteAccess()!
162 : : // After successfully acquiring you have to wait for current working reader.
163 : : // Used condition will open by last gone reader object.
164 : 0 : m_aSerializer.acquire();
165 : 0 : m_aWriteCondition.wait();
166 : :
167 : : #ifdef ENABLE_MUTEXDEBUG
168 : : // A writer is an exclusiv accessor!
169 : : LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::acquireWriteAccess()", "No threadsafe code detected ... : Read count != 0!" )
170 : : #endif
171 : 0 : }
172 : :
173 : : /*-****************************************************************************************************//**
174 : : @interface IRWLock
175 : : @short reset lock for writing
176 : : @descr A guard should call this method to release write access on your member.
177 : :
178 : : @seealso method acquireWriteAccess()
179 : :
180 : : @param -
181 : : @return -
182 : :
183 : : @onerror -
184 : : *//*-*****************************************************************************************************/
185 : 0 : inline virtual void releaseWriteAccess()
186 : : {
187 : : // The only one you have to do here is to release
188 : : // hold seriliaze-mutex. All other user of these instance are blocked
189 : : // by these mutex!
190 : : // You don't need any other mutex here - you are the only one in the moment!
191 : :
192 : : #ifdef ENABLE_MUTEXDEBUG
193 : : // A writer is an exclusiv accessor!
194 : : LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::releaseWriteAccess()", "No threadsafe code detected ... : Read count != 0!" )
195 : : #endif
196 : :
197 : 0 : m_aSerializer.release();
198 : 0 : }
199 : :
200 : : /*-****************************************************************************************************//**
201 : : @interface IRWLock
202 : : @short downgrade a write access to a read access
203 : : @descr A guard should call this method to change a write to a read access.
204 : : New readers can work too - new writer are blocked!
205 : :
206 : : @attention Don't call this method if you are not a writer!
207 : : Results are not defined then ...
208 : : An upgrade can't be implemented realy ... because acquiring new access
209 : : will be the same - there no differences!
210 : :
211 : : @seealso -
212 : :
213 : : @param -
214 : : @return -
215 : :
216 : : @onerror -
217 : : *//*-*****************************************************************************************************/
218 : 0 : inline virtual void downgradeWriteAccess()
219 : : {
220 : : // You must be a writer to call this method!
221 : : // We can't check it - but otherwise it's your problem ...
222 : : // Thats why you don't need any mutex here.
223 : :
224 : : #ifdef ENABLE_MUTEXDEBUG
225 : : // A writer is an exclusiv accessor!
226 : : LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::downgradeWriteAccess()", "No threadsafe code detected ... : Read count != 0!" )
227 : : #endif
228 : :
229 : : // Register himself as "new" reader.
230 : : // This value must be 0 before - because we support single writer access only!
231 : 0 : ++m_nReadCount;
232 : : // Close barrier for other writer!
233 : : // Why?
234 : : // You hold the serializer mutex - next one can be a reader OR a writer.
235 : : // They must blocked then - because you will be a reader after this call
236 : : // and writer use this condition to wait for current reader!
237 : 0 : m_aWriteCondition.reset();
238 : : // Open door for next waiting thread in serialize queue!
239 : 0 : m_aSerializer.release();
240 : 0 : }
241 : :
242 : : //-------------------------------------------------------------------------------------------------------------
243 : : // private member
244 : : //-------------------------------------------------------------------------------------------------------------
245 : : private:
246 : :
247 : : ::osl::Mutex m_aAccessLock ; /// regulate access on internal member of this instance
248 : : ::osl::Mutex m_aSerializer ; /// serialze incoming read/write access threads
249 : : ::osl::Condition m_aWriteCondition ; /// a writer must wait till current working reader are gone
250 : : sal_Int32 m_nReadCount ; /// every reader is registered - the last one open the door for waiting writer
251 : :
252 : : }; // class FairRWLock
253 : :
254 : : } // namespace framework
255 : :
256 : : #endif // #ifndef __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_
257 : :
258 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|