LibreOffice
LibreOffice 4.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  * 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 #include <osl/mutex.h>
24 
25 
26 namespace osl
27 {
31 
32  public:
38  {
39  mutex = osl_createMutex();
40  }
41 
46  {
47  osl_destroyMutex(mutex);
48  }
49 
54  bool acquire()
55  {
56  return osl_acquireMutex(mutex);
57  }
58 
63  bool tryToAcquire()
64  {
65  return osl_tryToAcquireMutex(mutex);
66  }
67 
72  bool release()
73  {
74  return osl_releaseMutex(mutex);
75  }
76 
83  static Mutex * getGlobalMutex()
84  {
85  return (Mutex *)osl_getGlobalMutex();
86  }
87 
88  private:
89  oslMutex mutex;
90 
97  Mutex(const Mutex&);
98 
106 
110  Mutex& operator= (const Mutex&);
111 
115  Mutex& operator= (oslMutex);
116  };
117 
120  template<class T>
121  class Guard
122  {
123  private:
124  Guard( const Guard& );
125  const Guard& operator = ( const Guard& );
126 
127  protected:
128  T * pT;
129  public:
130 
133  Guard(T * pT_) : pT(pT_)
134  {
135  pT->acquire();
136  }
137 
140  Guard(T & t) : pT(&t)
141  {
142  pT->acquire();
143  }
144 
147  {
148  pT->release();
149  }
150  };
151 
154  template<class T>
156  {
157  private:
158  ClearableGuard( const ClearableGuard& );
159  const ClearableGuard& operator = ( const ClearableGuard& );
160  protected:
161  T * pT;
162  public:
163 
166  ClearableGuard(T * pT_) : pT(pT_)
167  {
168  pT->acquire();
169  }
170 
173  ClearableGuard(T & t) : pT(&t)
174  {
175  pT->acquire();
176  }
177 
181  {
182  if (pT)
183  pT->release();
184  }
185 
188  void clear()
189  {
190  if(pT)
191  {
192  pT->release();
193  pT = NULL;
194  }
195  }
196  };
197 
200  template< class T >
201  class ResettableGuard : public ClearableGuard< T >
202  {
203  private:
204  ResettableGuard(ResettableGuard &); // not defined
205  void operator =(ResettableGuard &); // not defined
206 
207  protected:
209  public:
212  ResettableGuard( T* pT_ ) :
213  ClearableGuard<T>( pT_ ),
214  pResetT( pT_ )
215  {}
216 
219  ResettableGuard( T& rT ) :
220  ClearableGuard<T>( rT ),
221  pResetT( &rT )
222  {}
223 
226  void reset()
227  {
228  if( pResetT )
229  {
230  this->pT = pResetT;
231  this->pT->acquire();
232  }
233  }
234  };
235 
239 }
240 
241 #endif // INCLUDED_OSL_MUTEX_HXX
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:63
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:121
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:173
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:201
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:140
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:237
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:188
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:180
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:54
T * pT
Definition: mutex.hxx:128
Mutex()
Create a mutex.
Definition: mutex.hxx:37
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:45
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:238
Guard< Mutex > MutexGuard
Definition: mutex.hxx:236
void reset()
Re-aquires the mutex or interface.
Definition: mutex.hxx:226
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:219
T * pT
Definition: mutex.hxx:161
Definition: conditn.hxx:28
bool release()
Release the mutex.
Definition: mutex.hxx:72
T * pResetT
Definition: mutex.hxx:208
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:212
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:155
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:146
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:166
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:83
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:133
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
struct _oslMutexImpl * oslMutex
Definition: mutex.h:33
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:588
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.