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