AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mutex.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 
4 #ifndef INCLUDED_OSL_MUTEX_HXX
5 #define INCLUDED_OSL_MUTEX_HXX
6 
7 #include "osl/mutex.h"
8 
9 #include <cassert>
10 
11 namespace osl
12 {
16 
17  public:
25  {
26  mutex = osl_createMutex();
27  }
28 
33  {
34  osl_destroyMutex(mutex);
35  }
36 
41  bool acquire()
42  {
43  return osl_acquireMutex(mutex);
44  }
45 
50  bool tryToAcquire()
51  {
52  return osl_tryToAcquireMutex(mutex);
53  }
54 
59  bool release()
60  {
61  return osl_releaseMutex(mutex);
62  }
63 
70  static Mutex * getGlobalMutex()
71  {
72  return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
73  }
74 
75  private:
76  oslMutex mutex;
77 
78  // access to the oslMutex
80 
88 
92  Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
93  };
94 
102  template<class T>
103  class Guard
104  {
105  Guard(const Guard&) SAL_DELETED_FUNCTION;
106  Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
107 
108  protected:
109  T * pT;
110 
111  public:
114  Guard(T * pT_) : pT(pT_)
115  {
116  assert(pT != NULL);
117  pT->acquire();
118  }
119 
122  Guard(T & t) : pT(&t)
123  {
124  pT->acquire();
125  }
126 
129  {
130  pT->release();
131  }
132  };
133 
140  template<class T>
142  {
145 
146  protected:
147  T * pT;
148 
149  public:
152  ClearableGuard(T * pT_) : pT(pT_)
153  {
154  assert(pT != NULL);
155  pT->acquire();
156  }
157 
160  ClearableGuard(T & t) : pT(&t)
161  {
162  pT->acquire();
163  }
164 
168  {
169  if (pT)
170  pT->release();
171  }
172 
175  void clear()
176  {
177 #ifdef LIBO_INTERNAL_ONLY
178  assert(pT);
179 #else
180  if (pT)
181 #endif
182  {
183  pT->release();
184  pT = NULL;
185  }
186  }
187  };
188 
196  template< class T >
197  class ResettableGuard : public ClearableGuard< T >
198  {
201 
202  protected:
204 
205  public:
208  ResettableGuard( T* pT_ ) :
209  ClearableGuard<T>( pT_ ),
210  pResetT( pT_ )
211  {}
212 
215  ResettableGuard( T& rT ) :
216  ClearableGuard<T>( rT ),
217  pResetT( &rT )
218  {}
219 
222  void reset()
223  {
224 #ifdef LIBO_INTERNAL_ONLY
225  assert(!this->pT);
226 #endif
227  if (pResetT)
228  {
229  this->pT = pResetT;
230  this->pT->acquire();
231  }
232  }
233  };
234 
238 }
239 
240 #endif // INCLUDED_OSL_MUTEX_HXX
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:160
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
struct _oslMutexImpl * oslMutex
Definition: mutex.h:17
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:175
T * pResetT
Definition: mutex.hxx:203
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:222
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:167
bool release()
Release the mutex.
Definition: mutex.hxx:59
Guard< Mutex > MutexGuard
Definition: mutex.hxx:235
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:114
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:122
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:208
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:141
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:567
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:70
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:32
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:236
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:50
T * pT
Definition: mutex.hxx:147
T * pT
Definition: mutex.hxx:109
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:237
A mutual exclusion synchronization object.
Definition: mutex.hxx:15
Mutex()
Create a mutex.
Definition: mutex.hxx:24
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:103
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:152
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:215
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:128
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:41
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:358
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:197