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