LibreOffice: osl/mutex.hxx Source File

LibreOffice LibreOffice 7.3 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
31namespace osl
32{
35 class SAL_WARN_UNUSED Mutex {
36
37 public:
42 Mutex()
43 {
44 mutex = osl_createMutex();
45 }
46
50 ~Mutex()
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
97 friend oslMutex* SAL_CALL ::osl_getGlobalMutex();
98
105 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
106
110 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
111 };
112
120 template<class T>
121 class Guard
122 {
123 Guard(const Guard&) SAL_DELETED_FUNCTION;
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
146 ~Guard()
147 {
148 pT->release();
149 }
150 };
151
158 template<class T>
159 class ClearableGuard
160 {
161 ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
162 ClearableGuard& operator=(const ClearableGuard&) SAL_DELETED_FUNCTION;
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
185 ~ClearableGuard()
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 {
217 ResettableGuard(const ResettableGuard&) SAL_DELETED_FUNCTION;
218 ResettableGuard& operator=(const ResettableGuard&) SAL_DELETED_FUNCTION;
219
220 protected:
221 T* pResetT;
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
253 typedef Guard<Mutex> MutexGuard;
254 typedef ClearableGuard<Mutex> ClearableMutexGuard;
255 typedef ResettableGuard< Mutex > ResettableMutexGuard;
256}
257
258#endif // INCLUDED_OSL_MUTEX_HXX
259
260/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:378
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:587
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_getGlobalMutex
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
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:254
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition: mutex.hxx:253
osl::ResettableMutexGuard
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:255
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:35
osl::Mutex::getGlobalMutex
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:88
osl::Mutex::acquire
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:59
osl::Mutex::~Mutex
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:50
osl::Mutex::Mutex
Mutex()
Create a mutex.
Definition: mutex.hxx:42
osl::Mutex::release
bool release()
Release the mutex.
Definition: mutex.hxx:77
osl::Mutex::tryToAcquire
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:68
osl::Guard
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:122
osl::Guard::Guard
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:140
osl::Guard::pT
T * pT
Definition: mutex.hxx:127
osl::Guard::~Guard
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:146
osl::Guard::Guard
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:132
osl::ClearableGuard
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:160
osl::ClearableGuard::~ClearableGuard
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:185
osl::ClearableGuard::clear
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:193
osl::ClearableGuard::ClearableGuard
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:178
osl::ClearableGuard::pT
T * pT
Definition: mutex.hxx:165
osl::ClearableGuard::ClearableGuard
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:170
osl::ResettableGuard
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:216
osl::ResettableGuard::ResettableGuard
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:233
osl::ResettableGuard::reset
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:240
osl::ResettableGuard::pResetT
T * pResetT
Definition: mutex.hxx:221
osl::ResettableGuard::ResettableGuard
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:226