LibreOffice
LibreOffice 24.2 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 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_OSL_MUTEX_HXX
25 #define INCLUDED_OSL_MUTEX_HXX
26 
27 #include "osl/mutex.h"
28 
29 #include <cassert>
30 
31 namespace osl
32 {
36 
37  public:
45  {
46  mutex = osl_createMutex();
47  }
48 
53  {
54  osl_destroyMutex(mutex);
55  }
56 
61  bool acquire()
62  {
63  return osl_acquireMutex(mutex);
64  }
65 
70  bool tryToAcquire()
71  {
72  return osl_tryToAcquireMutex(mutex);
73  }
74 
79  bool release()
80  {
81  return osl_releaseMutex(mutex);
82  }
83 
90  static Mutex * getGlobalMutex()
91  {
92  return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
93  }
94 
95  private:
96  oslMutex mutex;
97 
98  // access to the oslMutex
100 
108 
112  Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
113  };
114 
122  template<class T>
123  class Guard
124  {
126  Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
127 
128  protected:
129  T * pT;
130 
131  public:
134  Guard(T * pT_) : pT(pT_)
135  {
136  assert(pT != NULL);
137  pT->acquire();
138  }
139 
142  Guard(T & t) : pT(&t)
143  {
144  pT->acquire();
145  }
146 
149  {
150  pT->release();
151  }
152  };
153 
160  template<class T>
162  {
165 
166  protected:
167  T * pT;
168 
169  public:
172  ClearableGuard(T * pT_) : pT(pT_)
173  {
174  assert(pT != NULL);
175  pT->acquire();
176  }
177 
180  ClearableGuard(T & t) : pT(&t)
181  {
182  pT->acquire();
183  }
184 
188  {
189  if (pT)
190  pT->release();
191  }
192 
195  void clear()
196  {
197 #ifdef LIBO_INTERNAL_ONLY
198  assert(pT);
199 #else
200  if (pT)
201 #endif
202  {
203  pT->release();
204  pT = NULL;
205  }
206  }
207  };
208 
216  template< class T >
217  class ResettableGuard : public ClearableGuard< T >
218  {
221 
222  protected:
224 
225  public:
228  ResettableGuard( T* pT_ ) :
229  ClearableGuard<T>( pT_ ),
230  pResetT( pT_ )
231  {}
232 
235  ResettableGuard( T& rT ) :
236  ClearableGuard<T>( rT ),
237  pResetT( &rT )
238  {}
239 
242  void reset()
243  {
244 #ifdef LIBO_INTERNAL_ONLY
245  assert(!this->pT);
246 #endif
247  if (pResetT)
248  {
249  this->pT = pResetT;
250  this->pT->acquire();
251  }
252  }
253  };
254 
258 }
259 
260 #endif // INCLUDED_OSL_MUTEX_HXX
261 
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:161
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:70
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:180
Definition: condition.hxx:31
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:52
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:90
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:148
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
bool release()
Release the mutex.
Definition: mutex.hxx:79
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:134
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:142
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:123
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:187
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:195
Guard< Mutex > MutexGuard
Definition: mutex.hxx:255
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:256
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:217
A mutual exclusion synchronization object.
Definition: mutex.hxx:35
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:242
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:235
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:61
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:257
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:228
T * pResetT
Definition: mutex.hxx:223
T * pT
Definition: mutex.hxx:167
T * pT
Definition: mutex.hxx:129
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
struct _oslMutexImpl * oslMutex
Definition: mutex.h:37
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:172
Mutex()
Create a mutex.
Definition: mutex.hxx:44