LibreOffice
LibreOffice 7.1 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
27namespace 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
65 {
66 return osl_tryToAcquireMutex(mutex);
67 }
68
73 bool release()
74 {
75 return osl_releaseMutex(mutex);
76 }
77
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
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: */
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:374
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:558
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
struct _oslMutexImpl * oslMutex
Definition: mutex.h:33
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: component.hxx:30
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:247
Guard< Mutex > MutexGuard
Definition: mutex.hxx:246
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:248
A mutual exclusion synchronization object.
Definition: mutex.hxx:31
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:84
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:55
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:46
Mutex()
Create a mutex.
Definition: mutex.hxx:38
bool release()
Release the mutex.
Definition: mutex.hxx:73
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:64
Object lifetime scoped mutex object or interface lock.
Definition: mutex.hxx:115
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:133
T * pT
Definition: mutex.hxx:120
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:139
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:125
Object lifetime scoped mutex object or interface lock with unlock.
Definition: mutex.hxx:153
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:178
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:186
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:171
T * pT
Definition: mutex.hxx:158
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:163
Template for temporary releasable mutex objects and interfaces locks.
Definition: mutex.hxx:209
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:226
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:233
T * pResetT
Definition: mutex.hxx:214
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:219