LibreOffice
LibreOffice 6.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 #ifndef INCLUDED_OSL_MUTEX_HXX
21 #define INCLUDED_OSL_MUTEX_HXX
22 
23 #include "osl/mutex.h"
24 
25 #include <cassert>
26 
27 namespace osl
28 {
32 
33  public:
39  {
40  mutex = osl_createMutex();
41  }
42 
47  {
48  osl_destroyMutex(mutex);
49  }
50 
55  bool acquire()
56  {
57  return osl_acquireMutex(mutex);
58  }
59 
64  bool tryToAcquire()
65  {
66  return osl_tryToAcquireMutex(mutex);
67  }
68 
73  bool release()
74  {
75  return osl_releaseMutex(mutex);
76  }
77 
84  static Mutex * getGlobalMutex()
85  {
86  return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
87  }
88 
89  private:
90  oslMutex mutex;
91 
99 
103  Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
104  };
105 
113  template<class T>
114  class Guard
115  {
117  Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
118 
119  protected:
120  T * pT;
121 
122  public:
125  Guard(T * pT_) : pT(pT_)
126  {
127  assert(pT != NULL);
128  pT->acquire();
129  }
130 
133  Guard(T & t) : pT(&t)
134  {
135  pT->acquire();
136  }
137 
140  {
141  pT->release();
142  }
143  };
144 
151  template<class T>
153  {
156 
157  protected:
158  T * pT;
159 
160  public:
163  ClearableGuard(T * pT_) : pT(pT_)
164  {
165  assert(pT != NULL);
166  pT->acquire();
167  }
168 
171  ClearableGuard(T & t) : pT(&t)
172  {
173  pT->acquire();
174  }
175 
179  {
180  if (pT)
181  pT->release();
182  }
183 
186  void clear()
187  {
188 #ifdef LIBO_INTERNAL_ONLY
189  assert(pT);
190 #else
191  if (pT)
192 #endif
193  {
194  pT->release();
195  pT = NULL;
196  }
197  }
198  };
199 
207  template< class T >
208  class ResettableGuard : public ClearableGuard< T >
209  {
212 
213  protected:
215 
216  public:
219  ResettableGuard( T* pT_ ) :
220  ClearableGuard<T>( pT_ ),
221  pResetT( pT_ )
222  {}
223 
226  ResettableGuard( T& rT ) :
227  ClearableGuard<T>( rT ),
228  pResetT( &rT )
229  {}
230 
233  void reset()
234  {
235 #ifdef LIBO_INTERNAL_ONLY
236  assert(!this->pT);
237 #endif
238  if (pResetT)
239  {
240  this->pT = pResetT;
241  this->pT->acquire();
242  }
243  }
244  };
245 
249 }
250 
251 #endif // INCLUDED_OSL_MUTEX_HXX
252 
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl::ClearableGuard
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:153
osl_releaseMutex
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
osl
Definition: condition.hxx:28
osl_tryToAcquireMutex
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
osl::Mutex::tryToAcquire
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:64
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:578
osl::ResettableGuard::reset
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:233
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition: mutex.hxx:246
osl::ClearableGuard::ClearableGuard
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:171
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:115
osl_getGlobalMutex
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
osl::Guard::Guard
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:125
osl::ClearableGuard::clear
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:186
osl::ResettableGuard
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:209
osl::ClearableMutexGuard
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:247
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:31
osl_createMutex
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
oslMutex
struct _oslMutexImpl * oslMutex
Definition: mutex.h:33
osl::ResettableGuard::pResetT
T * pResetT
Definition: mutex.hxx:214
mutex.h
osl::Mutex::getGlobalMutex
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:84
osl::Mutex::acquire
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:55
osl::Guard::Guard
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:133
osl::Guard::pT
T * pT
Definition: mutex.hxx:120
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:396
osl_acquireMutex
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
osl_destroyMutex
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
osl::ClearableGuard::pT
T * pT
Definition: mutex.hxx:158
osl::Mutex::release
bool release()
Release the mutex.
Definition: mutex.hxx:73
osl::Mutex::~Mutex
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:46
osl::Guard::~Guard
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:139
osl::ClearableGuard::~ClearableGuard
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:178
osl::ResettableGuard::ResettableGuard
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:226
osl::ResettableMutexGuard
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:248
osl::ResettableGuard::ResettableGuard
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:219
osl::ClearableGuard::ClearableGuard
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:163
osl::Mutex::Mutex
Mutex()
Create a mutex.
Definition: mutex.hxx:38