LibreOffice: osl/mutex.hxx Source File

LibreOffice LibreOffice 26.2 SDK C/C++ API Reference

Loading...
Searching...
No Matches
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
31namespace osl
32{
35 class SAL_WARN_UNUSED Mutex {
36
37 public:
44 Mutex()
45 {
46 mutex = osl_createMutex();
47 }
48
52 ~Mutex()
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
99 friend oslMutex* SAL_CALL ::osl_getGlobalMutex();
100
107 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
108
112 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
113 };
114
122 template<class T>
123 class Guard
124 {
125 Guard(const Guard&) SAL_DELETED_FUNCTION;
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
148 ~Guard()
149 {
150 pT->release();
151 }
152 };
153
160 template<class T>
161 class ClearableGuard
162 {
163 ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
164 ClearableGuard& operator=(const ClearableGuard&) SAL_DELETED_FUNCTION;
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
187 ~ClearableGuard()
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 {
219 ResettableGuard(const ResettableGuard&) SAL_DELETED_FUNCTION;
220 ResettableGuard& operator=(const ResettableGuard&) SAL_DELETED_FUNCTION;
221
222 protected:
223 T* pResetT;
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
255#ifdef LIBO_INTERNAL_ONLY
256 // A RAII helper to allow exception-safe scoped release of an acquired object
257 template<class ResettableGuard_t>
258 class ResettableGuardScopedReleaser
259 {
260 public:
261 ResettableGuardScopedReleaser(ResettableGuard_t& r)
262 : m_rResettableGuard(r)
263 {
264 m_rResettableGuard.clear();
265 }
266 ~ResettableGuardScopedReleaser() { m_rResettableGuard.reset(); }
267
268 private:
269 ResettableGuard_t& m_rResettableGuard;
270 };
271#endif
272
273 typedef Guard<Mutex> MutexGuard;
274 typedef ClearableGuard<Mutex> ClearableMutexGuard;
275 typedef ResettableGuard< Mutex > ResettableMutexGuard;
276#ifdef LIBO_INTERNAL_ONLY
277 typedef ResettableGuardScopedReleaser<ResettableMutexGuard> ResettableMutexGuardScopedReleaser;
278#endif
279}
280
281#endif // INCLUDED_OSL_MUTEX_HXX
282
283/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition types.h:396
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition types.h:611
mutex.h
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_releaseMutex
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
oslMutex
struct _oslMutexImpl * oslMutex
Definition mutex.h:37
osl_tryToAcquireMutex
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.
osl_createMutex
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
osl
Definition condition.hxx:31
osl::ClearableMutexGuard
ClearableGuard< Mutex > ClearableMutexGuard
Definition mutex.hxx:274
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition mutex.hxx:273
osl::ResettableMutexGuard
ResettableGuard< Mutex > ResettableMutexGuard
Definition mutex.hxx:275
osl::Mutex::getGlobalMutex
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition mutex.hxx:90
osl::Mutex::osl_getGlobalMutex
friend oslMutex * osl_getGlobalMutex()
Returns a unique and global mutex.
osl::Mutex::acquire
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition mutex.hxx:61
osl::Mutex::~Mutex
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition mutex.hxx:52
osl::Mutex::Mutex
Mutex()
Create a mutex.
Definition mutex.hxx:44
osl::Mutex::release
bool release()
Release the mutex.
Definition mutex.hxx:79
osl::Mutex::tryToAcquire
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition mutex.hxx:70
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition mutex.hxx:124
osl::Guard::Guard
Guard(T &t)
Acquires the object specified as parameter.
Definition mutex.hxx:142
osl::Guard< Mutex >::pT
Mutex * pT
Definition mutex.hxx:129
osl::Guard::~Guard
~Guard()
Releases the mutex or interface.
Definition mutex.hxx:148
osl::Guard::Guard
Guard(T *pT_)
Acquires the object specified as parameter.
Definition mutex.hxx:134
osl::ClearableGuard
Object lifetime scoped mutex object or interface lock with unlock.
Definition mutex.hxx:162
osl::ClearableGuard::~ClearableGuard
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition mutex.hxx:187
osl::ClearableGuard::clear
void clear()
Releases the mutex or interface.
Definition mutex.hxx:195
osl::ClearableGuard::ClearableGuard
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition mutex.hxx:180
osl::ClearableGuard< Mutex >::pT
Mutex * pT
Definition mutex.hxx:167
osl::ClearableGuard::ClearableGuard
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition mutex.hxx:172
osl::ResettableGuard< Mutex >
osl::ResettableGuard::ResettableGuard
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition mutex.hxx:235
osl::ResettableGuard::reset
void reset()
Re-acquires the mutex or interface.
Definition mutex.hxx:242
osl::ResettableGuard< Mutex >::pResetT
Mutex * pResetT
Definition mutex.hxx:223
osl::ResettableGuard::ResettableGuard
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition mutex.hxx:228