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_READGUARD_HXX_
21 : #define __FRAMEWORK_THREADHELP_READGUARD_HXX_
22 :
23 : #include <threadhelp/inoncopyable.h>
24 : #include <threadhelp/irwlock.h>
25 :
26 : #include <sal/types.h>
27 :
28 :
29 : namespace framework{
30 :
31 : /*-************************************************************************************************************//**
32 : @short implement a guard to set read locks
33 : @descr This guard should be used to set a lock for reading object internal member.
34 : Nobody can control it but don't use member after successfuly locking for writing!
35 : We never need a own mutex to safe our internal member access - because
36 : a guard is used as function-local member only. There exist no multithreaded access to it realy ...
37 :
38 : @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
39 : b) Use interface "IRWLock" of set LockHelper only - because we must support a finer granularity of locking.
40 : Interface "IMutex" should be used by easier guard implementations ... like "ResetableGuard"!
41 :
42 : @implements -
43 : @base INonCopyable
44 :
45 : @devstatus ready to use
46 : *//*-*************************************************************************************************************/
47 : class ReadGuard : private INonCopyable
48 : {
49 : //-------------------------------------------------------------------------------------------------------------
50 : // public methods
51 : //-------------------------------------------------------------------------------------------------------------
52 : public:
53 :
54 : /*-****************************************************************************************************//**
55 : @short ctor
56 : @descr These ctors initialize the guard with a reference to used lock member of object to protect.
57 : Null isn't allowed as value!
58 :
59 : @seealso -
60 :
61 : @param "pLock" ,reference to used lock member of object to protect
62 : @param "rLock" ,reference to used lock member of object to protect
63 : @return -
64 :
65 : @onerror -
66 : *//*-*****************************************************************************************************/
67 : inline ReadGuard( IRWLock* pLock )
68 : : m_pLock ( pLock )
69 : , m_bLocked ( sal_False )
70 : {
71 : lock();
72 : }
73 :
74 : //*********************************************************************************************************
75 306715 : inline ReadGuard( IRWLock& rLock )
76 : : m_pLock ( &rLock )
77 306715 : , m_bLocked ( sal_False )
78 : {
79 306715 : lock();
80 306715 : }
81 :
82 : /*-****************************************************************************************************//**
83 : @short dtor
84 : @descr We unlock the used lock member automaticly if user forget it.
85 :
86 : @seealso -
87 :
88 : @param -
89 : @return -
90 :
91 : @onerror -
92 : *//*-*****************************************************************************************************/
93 306714 : inline ~ReadGuard()
94 : {
95 306714 : unlock();
96 306715 : }
97 :
98 : /*-****************************************************************************************************//**
99 : @short set read lock
100 : @descr Call this method to set the read lock. The call will block till all current threads are synchronized!
101 :
102 : @seealso method unlock()
103 :
104 : @param -
105 : @return -
106 :
107 : @onerror -
108 : *//*-*****************************************************************************************************/
109 307515 : inline void lock()
110 : {
111 307515 : if( m_bLocked == sal_False )
112 : {
113 306919 : m_pLock->acquireReadAccess();
114 306919 : m_bLocked = sal_True;
115 : }
116 307515 : }
117 :
118 : /*-****************************************************************************************************//**
119 : @short unset read lock
120 : @descr Call this method to unlock the rw-lock temp.!
121 : Normaly we do it at dtor automaticly for you ...
122 :
123 : @seealso method lock()
124 :
125 : @param -
126 : @return -
127 :
128 : @onerror -
129 : *//*-*****************************************************************************************************/
130 429010 : inline void unlock()
131 : {
132 429010 : if( m_bLocked == sal_True )
133 : {
134 306919 : m_pLock->releaseReadAccess();
135 306919 : m_bLocked = sal_False;
136 : }
137 429010 : }
138 :
139 : //-------------------------------------------------------------------------------------------------------------
140 : // private methods
141 : //-------------------------------------------------------------------------------------------------------------
142 : private:
143 :
144 : /*-****************************************************************************************************//**
145 : @short disable using of these functions!
146 : @descr It's not allowed to use this methods. Different problem can occure otherwise.
147 : Thats why we disable it by make it private.
148 :
149 : @seealso other ctor
150 :
151 : @param -
152 : @return -
153 :
154 : @onerror -
155 : *//*-*****************************************************************************************************/
156 : ReadGuard();
157 :
158 : //-------------------------------------------------------------------------------------------------------------
159 : // private member
160 : //-------------------------------------------------------------------------------------------------------------
161 : private:
162 :
163 : IRWLock* m_pLock ; /// reference to lock-member of protected object
164 : sal_Bool m_bLocked ; /// protection against multiple lock calls without unlock!
165 :
166 : }; // class ReadGuard
167 :
168 : } // namespace framework
169 :
170 : #endif // #ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|