LibreOffice
LibreOffice 7.4 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:
43  {
44  mutex = osl_createMutex();
45  }
46 
51  {
52  osl_destroyMutex(mutex);
53  }
54 
59  bool acquire()
60  {
61  return osl_acquireMutex(mutex);
62  }
63 
68  bool tryToAcquire()
69  {
70  return osl_tryToAcquireMutex(mutex);
71  }
72 
77  bool release()
78  {
79  return osl_releaseMutex(mutex);
80  }
81 
88  static Mutex * getGlobalMutex()
89  {
90  return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
91  }
92 
93  private:
94  oslMutex mutex;
95 
96  // access to the oslMutex
98 
106 
110  Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
111  };
112 
120  template<class T>
121  class Guard
122  {
124  Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
125 
126  protected:
127  T * pT;
128 
129  public:
132  Guard(T * pT_) : pT(pT_)
133  {
134  assert(pT != NULL);
135  pT->acquire();
136  }
137 
140  Guard(T & t) : pT(&t)
141  {
142  pT->acquire();
143  }
144 
147  {
148  pT->release();
149  }
150  };
151 
158  template<class T>
160  {
163 
164  protected:
165  T * pT;
166 
167  public:
170  ClearableGuard(T * pT_) : pT(pT_)
171  {
172  assert(pT != NULL);
173  pT->acquire();
174  }
175 
178  ClearableGuard(T & t) : pT(&t)
179  {
180  pT->acquire();
181  }
182 
186  {
187  if (pT)
188  pT->release();
189  }
190 
193  void clear()
194  {
195 #ifdef LIBO_INTERNAL_ONLY
196  assert(pT);
197 #else
198  if (pT)
199 #endif
200  {
201  pT->release();
202  pT = NULL;
203  }
204  }
205  };
206 
214  template< class T >
215  class ResettableGuard : public ClearableGuard< T >
216  {
219 
220  protected:
222 
223  public:
226  ResettableGuard( T* pT_ ) :
227  ClearableGuard<T>( pT_ ),
228  pResetT( pT_ )
229  {}
230 
233  ResettableGuard( T& rT ) :
234  ClearableGuard<T>( rT ),
235  pResetT( &rT )
236  {}
237 
240  void reset()
241  {
242 #ifdef LIBO_INTERNAL_ONLY
243  assert(!this->pT);
244 #endif
245  if (pResetT)
246  {
247  this->pT = pResetT;
248  this->pT->acquire();
249  }
250  }
251  };
252 
256 }
257 
258 #endif // INCLUDED_OSL_MUTEX_HXX
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
struct _oslMutexImpl * oslMutex
Definition: mutex.h:37
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
Definition: condition.hxx:31
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:254
Guard< Mutex > MutexGuard
Definition: mutex.hxx:253
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:255
A mutual exclusion synchronization object.
Definition: mutex.hxx:35
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:59
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:50
Mutex()
Create a mutex.
Definition: mutex.hxx:42
bool release()
Release the mutex.
Definition: mutex.hxx:77
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:88
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:68
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:122
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:140
T * pT
Definition: mutex.hxx:127
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:146
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:132
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:160
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:185
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:193
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:178
T * pT
Definition: mutex.hxx:165
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:170
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:216
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:233
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:240
T * pResetT
Definition: mutex.hxx:221
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:226