Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _OSL_MUTEX_HXX_
30 : : #define _OSL_MUTEX_HXX_
31 : :
32 : : #ifdef __cplusplus
33 : :
34 : : #include <osl/mutex.h>
35 : :
36 : :
37 : : namespace osl
38 : : {
39 : : /** A mutual exclusion synchronization object
40 : : */
41 : : class Mutex {
42 : :
43 : : public:
44 : : /** Create a thread-local mutex.
45 : : @return 0 if the mutex could not be created, otherwise a handle to the mutex.
46 : : @seealso ::osl_createMutex()
47 : : */
48 : 4474999 : Mutex()
49 : : {
50 : 4474999 : mutex = osl_createMutex();
51 : 4474999 : }
52 : :
53 : : /** Release the OS-structures and free mutex data-structure.
54 : : @seealso ::osl_destroyMutex()
55 : : */
56 : 4410703 : ~Mutex()
57 : : {
58 : 4410703 : osl_destroyMutex(mutex);
59 : 4410703 : }
60 : :
61 : : /** Acquire the mutex, block if already acquired by another thread.
62 : : @return sal_False if system-call fails.
63 : : @seealso ::osl_acquireMutex()
64 : : */
65 : 159064995 : sal_Bool acquire()
66 : : {
67 : 159064995 : return osl_acquireMutex(mutex);
68 : : }
69 : :
70 : : /** Try to acquire the mutex without blocking.
71 : : @return sal_False if it could not be acquired.
72 : : @seealso ::osl_tryToAcquireMutex()
73 : : */
74 : 44 : sal_Bool tryToAcquire()
75 : : {
76 : 44 : return osl_tryToAcquireMutex(mutex);
77 : : }
78 : :
79 : : /** Release the mutex.
80 : : @return sal_False if system-call fails.
81 : : @seealso ::osl_releaseMutex()
82 : : */
83 : 159065236 : sal_Bool release()
84 : : {
85 : 159065236 : return osl_releaseMutex(mutex);
86 : : }
87 : :
88 : : /** Returns a global static mutex object.
89 : : The global and static mutex object can be used to initialize other
90 : : static objects in a thread safe manner.
91 : : @return the global mutex object
92 : : @seealso ::osl_getGlobalMutex()
93 : : */
94 : 1356519 : static Mutex * getGlobalMutex()
95 : : {
96 : 1356519 : return (Mutex *)osl_getGlobalMutex();
97 : : }
98 : :
99 : : private:
100 : : oslMutex mutex;
101 : :
102 : : /** The underlying oslMutex has no reference count.
103 : :
104 : : Since the underlying oslMutex is not a reference counted object, copy
105 : : constructed Mutex may work on an already destructed oslMutex object.
106 : :
107 : : */
108 : : Mutex(const Mutex&);
109 : :
110 : : /** The underlying oslMutex has no reference count.
111 : :
112 : : When destructed, the Mutex object destroys the undelying oslMutex,
113 : : which might cause severe problems in case it's a temporary object.
114 : :
115 : : */
116 : : Mutex(oslMutex Mutex);
117 : :
118 : : /** This assignment operator is private for the same reason as
119 : : the copy constructor.
120 : : */
121 : : Mutex& operator= (const Mutex&);
122 : :
123 : : /** This assignment operator is private for the same reason as
124 : : the constructor taking a oslMutex argument.
125 : : */
126 : : Mutex& operator= (oslMutex);
127 : : };
128 : :
129 : : /** A helper class for mutex objects and interfaces.
130 : : */
131 : : template<class T>
132 : : class Guard
133 : : {
134 : : private:
135 : : Guard( const Guard& );
136 : : const Guard& operator = ( const Guard& );
137 : :
138 : : protected:
139 : : T * pT;
140 : : public:
141 : :
142 : : /** Acquires the object specified as parameter.
143 : : */
144 : 3722757 : Guard(T * pT_) : pT(pT_)
145 : : {
146 : 3722757 : pT->acquire();
147 : 3722757 : }
148 : :
149 : : /** Acquires the object specified as parameter.
150 : : */
151 : 138702884 : Guard(T & t) : pT(&t)
152 : : {
153 : 138702884 : pT->acquire();
154 : 138703342 : }
155 : :
156 : : /** Releases the mutex or interface. */
157 : 142411592 : ~Guard()
158 : : {
159 : 142411592 : pT->release();
160 : 142412066 : }
161 : : };
162 : :
163 : : /** A helper class for mutex objects and interfaces.
164 : : */
165 : : template<class T>
166 : : class ClearableGuard
167 : : {
168 : : private:
169 : : ClearableGuard( const ClearableGuard& );
170 : : const ClearableGuard& operator = ( const ClearableGuard& );
171 : : protected:
172 : : T * pT;
173 : : public:
174 : :
175 : : /** Acquires the object specified as parameter.
176 : : */
177 : 858 : ClearableGuard(T * pT_) : pT(pT_)
178 : : {
179 : 858 : pT->acquire();
180 : 858 : }
181 : :
182 : : /** Acquires the object specified as parameter.
183 : : */
184 : 11102939 : ClearableGuard(T & t) : pT(&t)
185 : : {
186 : 11102939 : pT->acquire();
187 : 11102941 : }
188 : :
189 : : /** Releases the mutex or interface if not already released by clear().
190 : : */
191 : 11103787 : ~ClearableGuard()
192 : : {
193 [ + + ]: 11103787 : if (pT)
194 : 2050690 : pT->release();
195 : 11103786 : }
196 : :
197 : : /** Releases the mutex or interface.
198 : : */
199 : 9348704 : void clear()
200 : : {
201 [ + + ]: 9348704 : if(pT)
202 : : {
203 : 9343161 : pT->release();
204 : 9343161 : pT = NULL;
205 : : }
206 : 9348704 : }
207 : : };
208 : :
209 : : /** A helper class for mutex objects and interfaces.
210 : : */
211 : : template< class T >
212 : 3923379 : class ResettableGuard : public ClearableGuard< T >
213 : : {
214 : : private:
215 : : ResettableGuard(ResettableGuard &); // not defined
216 : : void operator =(ResettableGuard &); // not defined
217 : :
218 : : protected:
219 : : T* pResetT;
220 : : public:
221 : : /** Acquires the object specified as parameter.
222 : : */
223 : 15 : ResettableGuard( T* pT_ ) :
224 : : ClearableGuard<T>( pT_ ),
225 : 15 : pResetT( pT_ )
226 : 15 : {}
227 : :
228 : : /** Acquires the object specified as parameter.
229 : : */
230 : 3923364 : ResettableGuard( T& rT ) :
231 : : ClearableGuard<T>( rT ),
232 : 3923364 : pResetT( &rT )
233 : 3923364 : {}
234 : :
235 : : /** Re-aquires the mutex or interface.
236 : : */
237 : 35914 : void reset()
238 : : {
239 [ + - ]: 35914 : if( pResetT )
240 : : {
241 : 35914 : this->pT = pResetT;
242 : 35914 : this->pT->acquire();
243 : : }
244 : 35914 : }
245 : : };
246 : :
247 : : typedef Guard<Mutex> MutexGuard;
248 : : typedef ClearableGuard<Mutex> ClearableMutexGuard;
249 : : typedef ResettableGuard< Mutex > ResettableMutexGuard;
250 : :
251 : : /** SolarMutex interface, needed for SolarMutex.
252 : : Deprecated, used just for Application::GetSolarMutex().
253 : : */
254 : : class SolarMutex
255 : : {
256 : : public:
257 : : /** Blocks if mutex is already in use
258 : : */
259 : : virtual void SAL_CALL acquire() = 0;
260 : :
261 : : /** Tries to get the mutex without blocking.
262 : : */
263 : : virtual sal_Bool SAL_CALL tryToAcquire() = 0;
264 : :
265 : : /** Releases the mutex.
266 : : */
267 : : virtual void SAL_CALL release() = 0;
268 : :
269 : : protected:
270 : 27806 : SolarMutex() {}
271 [ - + ]: 27025 : virtual ~SolarMutex() {}
272 : : };
273 : : typedef osl::Guard< SolarMutex > SolarGuard;
274 : : typedef osl::ClearableGuard< SolarMutex > ClearableSolarGuard;
275 : : typedef osl::ResettableGuard< SolarMutex > ResettableSolarGuard;
276 : : }
277 : :
278 : : #endif /* __cplusplus */
279 : : #endif /* _OSL_MUTEX_HXX_ */
280 : :
281 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|