1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_________________________________________________________________________________________________________________
28cdf0e10cSrcweir //	my own includes
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir #include <threadhelp/lockhelper.hxx>
31cdf0e10cSrcweir #include <general.h>
32cdf0e10cSrcweir #include <macros/debug.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <macros/generic.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //_________________________________________________________________________________________________________________
37cdf0e10cSrcweir //	interface includes
38cdf0e10cSrcweir //_________________________________________________________________________________________________________________
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //_________________________________________________________________________________________________________________
41cdf0e10cSrcweir //	other includes
42cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43cdf0e10cSrcweir #include <vos/process.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46cdf0e10cSrcweir //	namespace
47cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace framework{
50cdf0e10cSrcweir 
51cdf0e10cSrcweir //_________________________________________________________________________________________________________________
52cdf0e10cSrcweir //	const
53cdf0e10cSrcweir //_________________________________________________________________________________________________________________
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //_________________________________________________________________________________________________________________
56cdf0e10cSrcweir //	declarations
57cdf0e10cSrcweir //_________________________________________________________________________________________________________________
58cdf0e10cSrcweir 
59cdf0e10cSrcweir /*-************************************************************************************************************//**
60cdf0e10cSrcweir     @short      use ctor to initialize instance
61cdf0e10cSrcweir     @descr      We must initialize our member "m_eLockType". This value specify handling of locking.
62cdf0e10cSrcweir                 User use this helper as parameter for a guard creation.
63cdf0e10cSrcweir                 These guard use "m_eLockType" to set lock in the right way by using right mutex or rw-lock.
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     @seealso    enum ELockType
66cdf0e10cSrcweir     @seealso    class ReadGuard
67cdf0e10cSrcweir     @seealso    class WriteGuard
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     @param      "rSolarMutex", for some components we must be "vcl-free"! So we can't work with our solar mutex
70cdf0e10cSrcweir                                 directly. User must set his reference at this instance - so we can work with it!
71cdf0e10cSrcweir     @return     -
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     @onerror    -
74cdf0e10cSrcweir *//*-*************************************************************************************************************/
75cdf0e10cSrcweir LockHelper::LockHelper( ::vos::IMutex* pSolarMutex )
76cdf0e10cSrcweir     :   m_pFairRWLock       ( NULL )
77cdf0e10cSrcweir     ,   m_pOwnMutex         ( NULL )
78cdf0e10cSrcweir     ,   m_pSolarMutex       ( NULL )
79cdf0e10cSrcweir     ,   m_pShareableOslMutex( NULL )
80cdf0e10cSrcweir     ,   m_bDummySolarMutex  ( sal_False )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     m_eLockType = implts_getLockType();
83cdf0e10cSrcweir     switch( m_eLockType )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
86cdf0e10cSrcweir         case E_OWNMUTEX     :   {
87cdf0e10cSrcweir                                     m_pOwnMutex = new ::osl::Mutex;
88cdf0e10cSrcweir                                 }
89cdf0e10cSrcweir                                 break;
90cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
91cdf0e10cSrcweir                                     if( pSolarMutex == NULL )
92cdf0e10cSrcweir                                     {
93cdf0e10cSrcweir                                         m_pSolarMutex      = new ::vos::OMutex;
94cdf0e10cSrcweir                                         m_bDummySolarMutex = sal_True;
95cdf0e10cSrcweir                                     }
96cdf0e10cSrcweir                                     else
97cdf0e10cSrcweir                                     {
98cdf0e10cSrcweir                                         m_pSolarMutex = pSolarMutex;
99cdf0e10cSrcweir                                     }
100cdf0e10cSrcweir                                 }
101cdf0e10cSrcweir                                 break;
102cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
103cdf0e10cSrcweir                                     m_pFairRWLock = new FairRWLock;
104cdf0e10cSrcweir                                 }
105cdf0e10cSrcweir                                 break;
106cdf0e10cSrcweir         #ifdef ENABLE_ASSERTIONS
107cdf0e10cSrcweir         default             :   LOG_ASSERT2( m_eLockType!=E_NOTHING, "LockHelper::ctor()", "Invalid lock type found .. so code will not be threadsafe!" )
108cdf0e10cSrcweir         #endif
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir /*-************************************************************************************************************//**
113cdf0e10cSrcweir     @short      default dtor to release safed pointer
114cdf0e10cSrcweir     @descr      We have created dynamical mutex- or lock-member ... or we hold a pointer to external objects.
115cdf0e10cSrcweir                 We must release it!
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     @seealso    ctor()
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     @param      -
120cdf0e10cSrcweir     @return     -
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     @onerror    -
123cdf0e10cSrcweir *//*-*************************************************************************************************************/
124cdf0e10cSrcweir LockHelper::~LockHelper()
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     if( m_pShareableOslMutex != NULL )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         // Sometimes we hold two pointer to same object!
129cdf0e10cSrcweir         // (e.g. if m_eLockType==E_OWNMUTEX!)
130cdf0e10cSrcweir         // So we should forget it ... but don't delete it twice!
131cdf0e10cSrcweir         if( m_pShareableOslMutex != m_pOwnMutex )
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             delete m_pShareableOslMutex;
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         m_pShareableOslMutex = NULL;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir     if( m_pOwnMutex != NULL )
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         delete m_pOwnMutex;
140cdf0e10cSrcweir         m_pOwnMutex = NULL;
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir     if( m_pSolarMutex != NULL )
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         if (m_bDummySolarMutex)
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             delete static_cast<vos::OMutex*>(m_pSolarMutex);
147cdf0e10cSrcweir             m_bDummySolarMutex = sal_False;
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir         m_pSolarMutex = NULL;
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir     if( m_pFairRWLock != NULL )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         delete m_pFairRWLock;
154cdf0e10cSrcweir         m_pFairRWLock = NULL;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir /*-************************************************************************************************************//**
159cdf0e10cSrcweir     @interface  IMutex
160cdf0e10cSrcweir     @short      set an exclusiv lock
161cdf0e10cSrcweir     @descr      We must match this lock call with current set lock type and used lock member.
162cdf0e10cSrcweir                 If a mutex should be used - it will be easy ... but if a rw-lock should be used
163cdf0e10cSrcweir                 we must simulate it as a write access!
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
166cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     @seealso    method acquireWriteAccess()
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     @param      -
171cdf0e10cSrcweir     @return     -
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     @onerror    -
174cdf0e10cSrcweir *//*-*************************************************************************************************************/
175cdf0e10cSrcweir void LockHelper::acquire()
176cdf0e10cSrcweir {
177cdf0e10cSrcweir     switch( m_eLockType )
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
180cdf0e10cSrcweir         case E_OWNMUTEX     :   {
181cdf0e10cSrcweir                                     m_pOwnMutex->acquire();
182cdf0e10cSrcweir                                 }
183cdf0e10cSrcweir                                 break;
184cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
185cdf0e10cSrcweir                                     m_pSolarMutex->acquire();
186cdf0e10cSrcweir                                 }
187cdf0e10cSrcweir                                 break;
188cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
189cdf0e10cSrcweir                                     m_pFairRWLock->acquireWriteAccess();
190cdf0e10cSrcweir                                 }
191cdf0e10cSrcweir                                 break;
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir /*-************************************************************************************************************//**
196cdf0e10cSrcweir     @interface  IMutex
197cdf0e10cSrcweir     @short      release exclusiv lock
198cdf0e10cSrcweir     @descr      We must match this unlock call with current set lock type and used lock member.
199cdf0e10cSrcweir                 If a mutex should be used - it will be easy ... but if a rw-lock should be used
200cdf0e10cSrcweir                 we must simulate it as a write access!
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
203cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     @seealso    method releaseWriteAccess()
206cdf0e10cSrcweir 
207cdf0e10cSrcweir     @param      -
208cdf0e10cSrcweir     @return     -
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     @onerror    -
211cdf0e10cSrcweir *//*-*************************************************************************************************************/
212cdf0e10cSrcweir void LockHelper::release()
213cdf0e10cSrcweir {
214cdf0e10cSrcweir     switch( m_eLockType )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
217cdf0e10cSrcweir         case E_OWNMUTEX     :   {
218cdf0e10cSrcweir                                     m_pOwnMutex->release();
219cdf0e10cSrcweir                                 }
220cdf0e10cSrcweir                                 break;
221cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
222cdf0e10cSrcweir                                     m_pSolarMutex->release();
223cdf0e10cSrcweir                                 }
224cdf0e10cSrcweir                                 break;
225cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
226cdf0e10cSrcweir                                     m_pFairRWLock->releaseWriteAccess();
227cdf0e10cSrcweir                                 }
228cdf0e10cSrcweir                                 break;
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir /*-************************************************************************************************************//**
233cdf0e10cSrcweir     @interface  IRWLock
234cdf0e10cSrcweir     @short      set lock for reading
235cdf0e10cSrcweir     @descr      A guard should call this method to acquire read access on your member.
236cdf0e10cSrcweir                 Writing isn't allowed then - but nobody could check it for you!
237cdf0e10cSrcweir                 We use m_eLockType to differ between all possible "lock-member"!!!
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
240cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
241cdf0e10cSrcweir 
242cdf0e10cSrcweir     @seealso    method releaseReadAccess()
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     @param      -
245cdf0e10cSrcweir     @return     -
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     @onerror    -
248cdf0e10cSrcweir *//*-*************************************************************************************************************/
249cdf0e10cSrcweir void LockHelper::acquireReadAccess()
250cdf0e10cSrcweir {
251cdf0e10cSrcweir     switch( m_eLockType )
252cdf0e10cSrcweir     {
253cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
254cdf0e10cSrcweir         case E_OWNMUTEX     :   {
255cdf0e10cSrcweir                                     m_pOwnMutex->acquire();
256cdf0e10cSrcweir                                 }
257cdf0e10cSrcweir                                 break;
258cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
259cdf0e10cSrcweir                                     m_pSolarMutex->acquire();
260cdf0e10cSrcweir                                 }
261cdf0e10cSrcweir                                 break;
262cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
263cdf0e10cSrcweir                                     m_pFairRWLock->acquireReadAccess();
264cdf0e10cSrcweir                                 }
265cdf0e10cSrcweir                                 break;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir /*-************************************************************************************************************//**
270cdf0e10cSrcweir     @interface  IRWLock
271cdf0e10cSrcweir     @short      reset lock for reading
272cdf0e10cSrcweir     @descr      A guard should call this method to release read access on your member.
273cdf0e10cSrcweir                 We use m_eLockType to differ between all possible "lock-member"!!!
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
276cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     @seealso    method acquireReadAccess()
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     @param      -
281cdf0e10cSrcweir     @return     -
282cdf0e10cSrcweir 
283cdf0e10cSrcweir     @onerror    -
284cdf0e10cSrcweir *//*-*************************************************************************************************************/
285cdf0e10cSrcweir void LockHelper::releaseReadAccess()
286cdf0e10cSrcweir {
287cdf0e10cSrcweir     switch( m_eLockType )
288cdf0e10cSrcweir     {
289cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
290cdf0e10cSrcweir         case E_OWNMUTEX     :   {
291cdf0e10cSrcweir                                     m_pOwnMutex->release();
292cdf0e10cSrcweir                                 }
293cdf0e10cSrcweir                                 break;
294cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
295cdf0e10cSrcweir                                     m_pSolarMutex->release();
296cdf0e10cSrcweir                                 }
297cdf0e10cSrcweir                                 break;
298cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
299cdf0e10cSrcweir                                     m_pFairRWLock->releaseReadAccess();
300cdf0e10cSrcweir                                 }
301cdf0e10cSrcweir                                 break;
302cdf0e10cSrcweir     }
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir /*-************************************************************************************************************//**
306cdf0e10cSrcweir     @interface  IRWLock
307cdf0e10cSrcweir     @short      set lock for writing
308cdf0e10cSrcweir     @descr      A guard should call this method to acquire write access on your member.
309cdf0e10cSrcweir                 Reading is allowed too - of course.
310cdf0e10cSrcweir                 After successfully calling of this method you are the only writer.
311cdf0e10cSrcweir                 We use m_eLockType to differ between all possible "lock-member"!!!
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
314cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
315cdf0e10cSrcweir 
316cdf0e10cSrcweir     @seealso    method releaseWriteAccess()
317cdf0e10cSrcweir 
318cdf0e10cSrcweir     @param      -
319cdf0e10cSrcweir     @return     -
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     @onerror    -
322cdf0e10cSrcweir *//*-*************************************************************************************************************/
323cdf0e10cSrcweir void LockHelper::acquireWriteAccess()
324cdf0e10cSrcweir {
325cdf0e10cSrcweir     switch( m_eLockType )
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
328cdf0e10cSrcweir         case E_OWNMUTEX     :   {
329cdf0e10cSrcweir                                     m_pOwnMutex->acquire();
330cdf0e10cSrcweir                                 }
331cdf0e10cSrcweir                                 break;
332cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
333cdf0e10cSrcweir                                     m_pSolarMutex->acquire();
334cdf0e10cSrcweir                                 }
335cdf0e10cSrcweir                                 break;
336cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
337cdf0e10cSrcweir                                     m_pFairRWLock->acquireWriteAccess();
338cdf0e10cSrcweir                                 }
339cdf0e10cSrcweir                                 break;
340cdf0e10cSrcweir     }
341cdf0e10cSrcweir }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir /*-************************************************************************************************************//**
344cdf0e10cSrcweir     @interface  IRWLock
345cdf0e10cSrcweir     @short      reset lock for writing
346cdf0e10cSrcweir     @descr      A guard should call this method to release write access on your member.
347cdf0e10cSrcweir                 We use m_eLockType to differ between all possible "lock-member"!!!
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     @attention  If a shareable osl mutex exist, he must be used as twice!
350cdf0e10cSrcweir                 It's neccessary for some cppu-helper classes ...
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     @seealso    method acquireWriteAccess()
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     @param      -
355cdf0e10cSrcweir     @return     -
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     @onerror    -
358cdf0e10cSrcweir *//*-*************************************************************************************************************/
359cdf0e10cSrcweir void LockHelper::releaseWriteAccess()
360cdf0e10cSrcweir {
361cdf0e10cSrcweir     switch( m_eLockType )
362cdf0e10cSrcweir     {
363cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
364cdf0e10cSrcweir         case E_OWNMUTEX     :   {
365cdf0e10cSrcweir                                     m_pOwnMutex->release();
366cdf0e10cSrcweir                                 }
367cdf0e10cSrcweir                                 break;
368cdf0e10cSrcweir         case E_SOLARMUTEX   :   {
369cdf0e10cSrcweir                                     m_pSolarMutex->release();
370cdf0e10cSrcweir                                 }
371cdf0e10cSrcweir                                 break;
372cdf0e10cSrcweir         case E_FAIRRWLOCK   :   {
373cdf0e10cSrcweir                                     m_pFairRWLock->releaseWriteAccess();
374cdf0e10cSrcweir                                 }
375cdf0e10cSrcweir                                 break;
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir /*-************************************************************************************************************//**
380cdf0e10cSrcweir     @interface  IRWLock
381cdf0e10cSrcweir     @short      downgrade a write access to a read access
382cdf0e10cSrcweir     @descr      A guard should call this method to change a write to a read access.
383cdf0e10cSrcweir                 New readers can work too - new writer are blocked!
384cdf0e10cSrcweir                 We use m_eLockType to differ between all possible "lock-member"!!!
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     @attention  Ignore shareable mutex(!) - because this call never should release a lock completly!
387cdf0e10cSrcweir                 We change a write access to a read access only.
388cdf0e10cSrcweir 
389cdf0e10cSrcweir     @attention  a) Don't call this method if you are not a writer!
390cdf0e10cSrcweir                     Results are not defined then ...
391cdf0e10cSrcweir                     An upgrade can't be implemented realy ... because acquiring new access
392cdf0e10cSrcweir                     will be the same - there no differences!
393cdf0e10cSrcweir                 b) Without function if m_eLockTyp is different from E_FAIRRWLOCK(!) ...
394cdf0e10cSrcweir                     because, a mutex don't support it realy.
395cdf0e10cSrcweir 
396cdf0e10cSrcweir     @seealso    -
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     @param      -
399cdf0e10cSrcweir     @return     -
400cdf0e10cSrcweir 
401cdf0e10cSrcweir     @onerror    -
402cdf0e10cSrcweir *//*-*************************************************************************************************************/
403cdf0e10cSrcweir void LockHelper::downgradeWriteAccess()
404cdf0e10cSrcweir {
405cdf0e10cSrcweir     switch( m_eLockType )
406cdf0e10cSrcweir     {
407cdf0e10cSrcweir         case E_NOTHING      :   break; // There is nothing to do ...
408cdf0e10cSrcweir         case E_OWNMUTEX     :   break; // Not supported for mutex!
409cdf0e10cSrcweir         case E_SOLARMUTEX   :   break; // Not supported for mutex!
410cdf0e10cSrcweir         case E_FAIRRWLOCK   :   m_pFairRWLock->downgradeWriteAccess();
411cdf0e10cSrcweir                                 break;
412cdf0e10cSrcweir     }
413cdf0e10cSrcweir }
414cdf0e10cSrcweir 
415cdf0e10cSrcweir /*-************************************************************************************************************//**
416cdf0e10cSrcweir     @short      return a reference to a static lock helper
417cdf0e10cSrcweir     @descr      Sometimes we need the global mutex or rw-lock! (e.g. in our own static methods)
418cdf0e10cSrcweir                 But it's not a good idea to use these global one very often ...
419cdf0e10cSrcweir                 Thats why we use this little helper method.
420cdf0e10cSrcweir                 We create our own "class global static" lock.
421cdf0e10cSrcweir                 It will be created at first call only!
422cdf0e10cSrcweir                 All other requests use these created one then directly.
423cdf0e10cSrcweir 
424cdf0e10cSrcweir     @seealso    -
425cdf0e10cSrcweir 
426cdf0e10cSrcweir     @param      -
427cdf0e10cSrcweir     @return     A reference to a static mutex/lock member.
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     @onerror    No error should occure.
430cdf0e10cSrcweir *//*-*************************************************************************************************************/
431cdf0e10cSrcweir LockHelper& LockHelper::getGlobalLock( ::vos::IMutex* pSolarMutex )
432cdf0e10cSrcweir {
433cdf0e10cSrcweir     // Initialize static "member" only for one time!
434cdf0e10cSrcweir     // Algorithm:
435cdf0e10cSrcweir     // a) Start with an invalid lock (NULL pointer)
436cdf0e10cSrcweir     // b) If these method first called (lock not already exist!) ...
437cdf0e10cSrcweir     // c) ... we must create a new one. Protect follow code with the global mutex -
438cdf0e10cSrcweir     //    (It must be - we create a static variable!)
439cdf0e10cSrcweir     // d) Check pointer again - because ... another instance of our class could be faster then these one!
440cdf0e10cSrcweir     // e) Create the new lock and set it for return on static variable.
441cdf0e10cSrcweir     // f) Return new created or already existing lock object.
442cdf0e10cSrcweir     static LockHelper* pLock = NULL;
443cdf0e10cSrcweir     if( pLock == NULL )
444cdf0e10cSrcweir     {
445cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
446cdf0e10cSrcweir         if( pLock == NULL )
447cdf0e10cSrcweir         {
448cdf0e10cSrcweir             static LockHelper aLock( pSolarMutex );
449cdf0e10cSrcweir             pLock = &aLock;
450cdf0e10cSrcweir         }
451cdf0e10cSrcweir     }
452cdf0e10cSrcweir     return *pLock;
453cdf0e10cSrcweir }
454cdf0e10cSrcweir 
455cdf0e10cSrcweir /*-************************************************************************************************************//**
456cdf0e10cSrcweir     @short      return a reference to shared mutex member
457cdf0e10cSrcweir     @descr      Sometimes we need a osl-mutex for sharing with our uno helper ...
458cdf0e10cSrcweir                 What can we do?
459cdf0e10cSrcweir                 a) If we have an initialized "own mutex" ... we can use it!
460cdf0e10cSrcweir                 b) Otherwhise we must use a different mutex member :-(
461cdf0e10cSrcweir                 I HOPE IT WORKS!
462cdf0e10cSrcweir 
463cdf0e10cSrcweir     @seealso    -
464cdf0e10cSrcweir 
465cdf0e10cSrcweir     @param      -
466cdf0e10cSrcweir     @return     A reference to a shared mutex.
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     @onerror    No error should occure.
469cdf0e10cSrcweir *//*-*************************************************************************************************************/
470cdf0e10cSrcweir ::osl::Mutex& LockHelper::getShareableOslMutex()
471cdf0e10cSrcweir {
472cdf0e10cSrcweir     if( m_pShareableOslMutex == NULL )
473cdf0e10cSrcweir     {
474cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
475cdf0e10cSrcweir         if( m_pShareableOslMutex == NULL )
476cdf0e10cSrcweir         {
477cdf0e10cSrcweir             switch( m_eLockType )
478cdf0e10cSrcweir             {
479cdf0e10cSrcweir                 case E_OWNMUTEX     :   {
480cdf0e10cSrcweir                                             m_pShareableOslMutex = m_pOwnMutex;
481cdf0e10cSrcweir                                         }
482cdf0e10cSrcweir                                         break;
483cdf0e10cSrcweir                 default             :   {
484cdf0e10cSrcweir                                             m_pShareableOslMutex = new ::osl::Mutex;
485cdf0e10cSrcweir                                         }
486cdf0e10cSrcweir                                         break;
487cdf0e10cSrcweir             }
488cdf0e10cSrcweir         }
489cdf0e10cSrcweir     }
490cdf0e10cSrcweir     return *m_pShareableOslMutex;
491cdf0e10cSrcweir }
492cdf0e10cSrcweir 
493cdf0e10cSrcweir /*-************************************************************************************************************//**
494cdf0e10cSrcweir     @short      search for right lock type, which should be used by an instance of this struct
495cdf0e10cSrcweir     @descr      We must initialize our member "m_eLockType". This value specify handling of locking.
496cdf0e10cSrcweir                 How we can do that? We search for an environment variable. We do it only for one time ....
497cdf0e10cSrcweir                 because the environment is fix. So we safe this value and use it for all further requests.
498cdf0e10cSrcweir                 If no variable could be found - we use a fallback!
499cdf0e10cSrcweir 
500cdf0e10cSrcweir     @attention  We have numbered all our enum values for ELockType. So we can use it as value of searched
501cdf0e10cSrcweir                 environment variable too!
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     @seealso    enum ELockType
504cdf0e10cSrcweir     @seealso    environment LOCKTYPE
505cdf0e10cSrcweir 
506cdf0e10cSrcweir     @param      -
507cdf0e10cSrcweir     @return     A reference to a created and right initialized lock type!
508cdf0e10cSrcweir 
509cdf0e10cSrcweir     @onerror    We use a fallback!
510cdf0e10cSrcweir *//*-*************************************************************************************************************/
511cdf0e10cSrcweir ELockType& LockHelper::implts_getLockType()
512cdf0e10cSrcweir {
513cdf0e10cSrcweir     // Initialize static "member" only for one time!
514cdf0e10cSrcweir     // Algorithm:
515cdf0e10cSrcweir     // a) Start with an invalid variable (NULL pointer)
516cdf0e10cSrcweir     // b) If these method first called (value not already exist!) ...
517cdf0e10cSrcweir     // c) ... we must create a new one. Protect follow code with the global mutex -
518cdf0e10cSrcweir     //    (It must be - we create a static variable!)
519cdf0e10cSrcweir     // d) Check pointer again - because ... another instance of our class could be faster then these one!
520cdf0e10cSrcweir     // e) Create the new static variable, get value from the environment and set it
521cdf0e10cSrcweir     // f) Return new created or already existing static variable.
522cdf0e10cSrcweir     static ELockType* pType = NULL;
523cdf0e10cSrcweir     if( pType == NULL )
524cdf0e10cSrcweir     {
525cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
526cdf0e10cSrcweir         if( pType == NULL )
527cdf0e10cSrcweir         {
528cdf0e10cSrcweir             static ELockType eType = FALLBACK_LOCKTYPE;
529cdf0e10cSrcweir 
530cdf0e10cSrcweir             ::vos::OStartupInfo aEnvironment;
531cdf0e10cSrcweir             ::rtl::OUString     sValue      ;
532cdf0e10cSrcweir             if( aEnvironment.getEnvironment( ENVVAR_LOCKTYPE, sValue ) == ::vos::OStartupInfo::E_None )
533cdf0e10cSrcweir             {
534cdf0e10cSrcweir                 eType = (ELockType)(sValue.toInt32());
535cdf0e10cSrcweir             }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir             LOG_LOCKTYPE( FALLBACK_LOCKTYPE, eType )
538cdf0e10cSrcweir 
539cdf0e10cSrcweir             pType = &eType;
540cdf0e10cSrcweir         }
541cdf0e10cSrcweir     }
542cdf0e10cSrcweir     return *pType;
543cdf0e10cSrcweir }
544cdf0e10cSrcweir 
545cdf0e10cSrcweir } //  namespace framework
546