LibreOffice
LibreOffice 4.3 SDK C/C++ API Reference
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  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_OSL_MUTEX_HXX
21 #define INCLUDED_OSL_MUTEX_HXX
22 
23 #ifdef __cplusplus
24 
25 #include <osl/mutex.h>
26 
27 
28 namespace osl
29 {
33 
34  public:
40  {
41  mutex = osl_createMutex();
42  }
43 
48  {
49  osl_destroyMutex(mutex);
50  }
51 
56  bool acquire()
57  {
58  return osl_acquireMutex(mutex);
59  }
60 
65  bool tryToAcquire()
66  {
67  return osl_tryToAcquireMutex(mutex);
68  }
69 
74  bool release()
75  {
76  return osl_releaseMutex(mutex);
77  }
78 
85  static Mutex * getGlobalMutex()
86  {
87  return (Mutex *)osl_getGlobalMutex();
88  }
89 
90  private:
91  oslMutex mutex;
92 
99  Mutex(const Mutex&);
100 
108 
112  Mutex& operator= (const Mutex&);
113 
117  Mutex& operator= (oslMutex);
118  };
119 
122  template<class T>
123  class Guard
124  {
125  private:
126  Guard( const Guard& );
127  const Guard& operator = ( const Guard& );
128 
129  protected:
130  T * pT;
131  public:
132 
135  Guard(T * pT_) : pT(pT_)
136  {
137  pT->acquire();
138  }
139 
142  Guard(T & t) : pT(&t)
143  {
144  pT->acquire();
145  }
146 
149  {
150  pT->release();
151  }
152  };
153 
156  template<class T>
158  {
159  private:
160  ClearableGuard( const ClearableGuard& );
161  const ClearableGuard& operator = ( const ClearableGuard& );
162  protected:
163  T * pT;
164  public:
165 
168  ClearableGuard(T * pT_) : pT(pT_)
169  {
170  pT->acquire();
171  }
172 
175  ClearableGuard(T & t) : pT(&t)
176  {
177  pT->acquire();
178  }
179 
183  {
184  if (pT)
185  pT->release();
186  }
187 
190  void clear()
191  {
192  if(pT)
193  {
194  pT->release();
195  pT = NULL;
196  }
197  }
198  };
199 
202  template< class T >
203  class ResettableGuard : public ClearableGuard< T >
204  {
205  private:
206  ResettableGuard(ResettableGuard &); // not defined
207  void operator =(ResettableGuard &); // not defined
208 
209  protected:
211  public:
214  ResettableGuard( T* pT_ ) :
215  ClearableGuard<T>( pT_ ),
216  pResetT( pT_ )
217  {}
218 
221  ResettableGuard( T& rT ) :
222  ClearableGuard<T>( rT ),
223  pResetT( &rT )
224  {}
225 
228  void reset()
229  {
230  if( pResetT )
231  {
232  this->pT = pResetT;
233  this->pT->acquire();
234  }
235  }
236  };
237 
241 }
242 
243 #endif /* __cplusplus */
244 #endif // INCLUDED_OSL_MUTEX_HXX
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:123
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:175
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:65
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:142
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:239
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:190
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:182
void reset()
Re-aquires the mutex or interface.
Definition: mutex.hxx:228
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:221
T * pT
Definition: mutex.hxx:130
struct _oslMutexImpl * oslMutex
Definition: mutex.h:33
Mutex()
Create a mutex.
Definition: mutex.hxx:39
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:47
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:214
Definition: conditn.hxx:30
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:56
T * pT
Definition: mutex.hxx:163
T * pResetT
Definition: mutex.hxx:210
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:240
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:157
A mutual exclusion synchronization object.
Definition: mutex.hxx:32
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:148
Guard< Mutex > MutexGuard
Definition: mutex.hxx:238
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:168
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:203
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:603
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:135
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:85
bool release()
Release the mutex.
Definition: mutex.hxx:74
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.