update dev300-m58
[ooovba.git] / sal / inc / osl / mutex.hxx
blobd0d2fa27ecf76091a08031a5baf629f947d9d4d9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mutex.hxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _OSL_MUTEX_HXX_
32 #define _OSL_MUTEX_HXX_
34 #ifdef __cplusplus
36 #include <osl/mutex.h>
39 namespace osl
41 /** A mutual exclusion synchronization object
43 class Mutex {
45 public:
46 /** Create a thread-local mutex.
47 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
48 @seealso ::osl_createMutex()
50 Mutex()
52 mutex = osl_createMutex();
55 /** Release the OS-structures and free mutex data-structure.
56 @seealso ::osl_destroyMutex()
58 ~Mutex()
60 osl_destroyMutex(mutex);
63 /** Acquire the mutex, block if already acquired by another thread.
64 @return sal_False if system-call fails.
65 @seealso ::osl_acquireMutex()
67 sal_Bool acquire()
69 return osl_acquireMutex(mutex);
72 /** Try to acquire the mutex without blocking.
73 @return sal_False if it could not be acquired.
74 @seealso ::osl_tryToAcquireMutex()
76 sal_Bool tryToAcquire()
78 return osl_tryToAcquireMutex(mutex);
81 /** Release the mutex.
82 @return sal_False if system-call fails.
83 @seealso ::osl_releaseMutex()
85 sal_Bool release()
87 return osl_releaseMutex(mutex);
90 /** Returns a global static mutex object.
91 The global and static mutex object can be used to initialize other
92 static objects in a thread safe manner.
93 @return the global mutex object
94 @seealso ::osl_getGlobalMutex()
96 static Mutex * getGlobalMutex()
98 return (Mutex *)osl_getGlobalMutex();
101 private:
102 oslMutex mutex;
104 /** The underlying oslMutex has no reference count.
106 Since the underlying oslMutex is not a reference counted object, copy
107 constructed Mutex may work on an already destructed oslMutex object.
110 Mutex(const Mutex&);
112 /** The underlying oslMutex has no reference count.
114 When destructed, the Mutex object destroys the undelying oslMutex,
115 which might cause severe problems in case it's a temporary object.
118 Mutex(oslMutex Mutex);
120 /** This assignment operator is private for the same reason as
121 the copy constructor.
123 Mutex& operator= (const Mutex&);
125 /** This assignment operator is private for the same reason as
126 the constructor taking a oslMutex argument.
128 Mutex& operator= (oslMutex);
131 /** A helper class for mutex objects and interfaces.
133 template<class T>
134 class Guard
136 private:
137 Guard( const Guard& );
138 const Guard& operator = ( const Guard& );
140 protected:
141 T * pT;
142 public:
144 /** Acquires the object specified as parameter.
146 Guard(T * pT_) : pT(pT_)
148 pT->acquire();
151 /** Acquires the object specified as parameter.
153 Guard(T & t) : pT(&t)
155 pT->acquire();
158 /** Releases the mutex or interface. */
159 ~Guard()
161 pT->release();
165 /** A helper class for mutex objects and interfaces.
167 template<class T>
168 class ClearableGuard
170 private:
171 ClearableGuard( const ClearableGuard& );
172 const ClearableGuard& operator = ( const ClearableGuard& );
173 protected:
174 T * pT;
175 public:
177 /** Acquires the object specified as parameter.
179 ClearableGuard(T * pT_) : pT(pT_)
181 pT->acquire();
184 /** Acquires the object specified as parameter.
186 ClearableGuard(T & t) : pT(&t)
188 pT->acquire();
191 /** Releases the mutex or interface if not already released by clear().
193 ~ClearableGuard()
195 if (pT)
196 pT->release();
199 /** Releases the mutex or interface.
201 void clear()
203 if(pT)
205 pT->release();
206 pT = NULL;
211 /** A helper class for mutex objects and interfaces.
213 template< class T >
214 class ResettableGuard : public ClearableGuard< T >
216 private:
217 ResettableGuard(ResettableGuard &); // not defined
218 void operator =(ResettableGuard &); // not defined
220 protected:
221 T* pResetT;
222 public:
223 /** Acquires the object specified as parameter.
225 ResettableGuard( T* pT_ ) :
226 ClearableGuard<T>( pT_ ),
227 pResetT( pT_ )
230 /** Acquires the object specified as parameter.
232 ResettableGuard( T& rT ) :
233 ClearableGuard<T>( rT ),
234 pResetT( &rT )
237 /** Re-aquires the mutex or interface.
239 void reset()
241 if( pResetT )
243 this->pT = pResetT;
244 this->pT->acquire();
249 typedef Guard<Mutex> MutexGuard;
250 typedef ClearableGuard<Mutex> ClearableMutexGuard;
251 typedef ResettableGuard< Mutex > ResettableMutexGuard;
254 #endif /* __cplusplus */
255 #endif /* _OSL_MUTEX_HXX_ */