LibreOffice: osl/mutex.hxx Source File

LibreOffice LibreOffice 5.1 SDK C/C++ API Reference

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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
26 namespace osl
27 {
30 class SAL_WARN_UNUSED Mutex {
31
32 public:
37 Mutex()
38 {
39 mutex = osl_createMutex();
40 }
41
45 ~Mutex()
46 {
47 osl_destroyMutex(mutex);
48 }
49
54 bool acquire()
55 {
56 return osl_acquireMutex(mutex);
57 }
58
63 bool tryToAcquire()
64 {
65 return osl_tryToAcquireMutex(mutex);
66 }
67
72 bool release()
73 {
74 return osl_releaseMutex(mutex);
75 }
76
83 static Mutex * getGlobalMutex()
84 {
85 return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
86 }
87
88 private:
89 oslMutex mutex;
90
97 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
98
102 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
103 };
104
107 template<class T>
108 class Guard
109 {
110 private:
111 Guard( const Guard& ) SAL_DELETED_FUNCTION;
112 const Guard& operator = ( const Guard& ) SAL_DELETED_FUNCTION;
113
114 protected:
115 T * pT;
116 public:
117
120 Guard(T * pT_) : pT(pT_)
121 {
122 pT->acquire();
123 }
124
127 Guard(T & t) : pT(&t)
128 {
129 pT->acquire();
130 }
131
133 ~Guard()
134 {
135 pT->release();
136 }
137 };
138
141 template<class T>
142 class ClearableGuard
143 {
144 private:
145 ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
146 const ClearableGuard& operator = ( const ClearableGuard& )
147 SAL_DELETED_FUNCTION;
148 protected:
149 T * pT;
150 public:
151
154 ClearableGuard(T * pT_) : pT(pT_)
155 {
156 pT->acquire();
157 }
158
161 ClearableGuard(T & t) : pT(&t)
162 {
163 pT->acquire();
164 }
165
168 ~ClearableGuard()
169 {
170 if (pT)
171 pT->release();
172 }
173
176 void clear()
177 {
178 if(pT)
179 {
180 pT->release();
181 pT = NULL;
182 }
183 }
184 };
185
188 template< class T >
189 class ResettableGuard : public ClearableGuard< T >
190 {
191 private:
192 ResettableGuard(ResettableGuard &) SAL_DELETED_FUNCTION;
193 void operator =(ResettableGuard &) SAL_DELETED_FUNCTION;
194
195 protected:
196 T* pResetT;
197 public:
200 ResettableGuard( T* pT_ ) :
201 ClearableGuard<T>( pT_ ),
202 pResetT( pT_ )
203 {}
204
207 ResettableGuard( T& rT ) :
208 ClearableGuard<T>( rT ),
209 pResetT( &rT )
210 {}
211
214 void reset()
215 {
216 if( pResetT )
217 {
218 this->pT = pResetT;
219 this->pT->acquire();
220 }
221 }
222 };
223
224 typedef Guard<Mutex> MutexGuard;
225 typedef ClearableGuard<Mutex> ClearableMutexGuard;
226 typedef ResettableGuard< Mutex > ResettableMutexGuard;
227 }
228
229 #endif // INCLUDED_OSL_MUTEX_HXX
230
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
osl::Mutex::tryToAcquire
bool tryToAcquire()
Try to acquire the mutex without blocking.
Definition: mutex.hxx:63
osl::ClearableGuard::ClearableGuard
ClearableGuard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:161
osl::ClearableGuard::clear
void clear()
Releases the mutex or interface.
Definition: mutex.hxx:176
osl::Guard::pT
T * pT
Definition: mutex.hxx:115
osl::Mutex
A mutual exclusion synchronization object.
Definition: mutex.hxx:30
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition: types.h:408
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:612
osl::ResettableGuard
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:189
mutex.h
osl::ClearableGuard::~ClearableGuard
~ClearableGuard()
Releases the mutex or interface if not already released by clear().
Definition: mutex.hxx:168
osl_destroyMutex
SAL_DLLPUBLIC void osl_destroyMutex(oslMutex Mutex)
Release the OS-structures and free mutex data-structure.
osl::Mutex::acquire
bool acquire()
Acquire the mutex, block if already acquired by another thread.
Definition: mutex.hxx:54
osl::ClearableMutexGuard
ClearableGuard< Mutex > ClearableMutexGuard
Definition: mutex.hxx:225
osl::Guard::Guard
Guard(T &t)
Acquires the object specified as parameter.
Definition: mutex.hxx:127
osl::ClearableGuard
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:142
osl::Mutex::Mutex
Mutex()
Create a mutex.
Definition: mutex.hxx:37
osl::ResettableMutexGuard
ResettableGuard< Mutex > ResettableMutexGuard
Definition: mutex.hxx:226
osl::Mutex::~Mutex
~Mutex()
Release the OS-structures and free mutex data-structure.
Definition: mutex.hxx:45
osl::ClearableGuard::pT
T * pT
Definition: mutex.hxx:149
osl::ResettableGuard::reset
void reset()
Re-acquires the mutex or interface.
Definition: mutex.hxx:214
osl::ResettableGuard::ResettableGuard
ResettableGuard(T &rT)
Acquires the object specified as parameter.
Definition: mutex.hxx:207
osl::Guard::~Guard
~Guard()
Releases the mutex or interface.
Definition: mutex.hxx:133
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition: mutex.hxx:224
osl::Mutex::release
bool release()
Release the mutex.
Definition: mutex.hxx:72
osl::ResettableGuard::pResetT
T * pResetT
Definition: mutex.hxx:196
osl_getGlobalMutex
SAL_DLLPUBLIC oslMutex * osl_getGlobalMutex(void)
Returns a unique and global mutex.
osl::ResettableGuard::ResettableGuard
ResettableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:200
osl_createMutex
SAL_DLLPUBLIC oslMutex osl_createMutex(void)
Create a mutex.
osl::Guard
A helper class for mutex objects and interfaces.
Definition: mutex.hxx:108
osl::Mutex::getGlobalMutex
static Mutex * getGlobalMutex()
Returns a global static mutex object.
Definition: mutex.hxx:83
osl::ClearableGuard::ClearableGuard
ClearableGuard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:154
osl
Definition: conditn.hxx:32
osl_acquireMutex
SAL_DLLPUBLIC sal_Bool osl_acquireMutex(oslMutex Mutex)
Acquire the mutex, block if already acquired by another thread.
oslMutex
struct _oslMutexImpl * oslMutex
Definition: mutex.h:33
osl::Guard::Guard
Guard(T *pT_)
Acquires the object specified as parameter.
Definition: mutex.hxx:120
osl_releaseMutex
SAL_DLLPUBLIC sal_Bool osl_releaseMutex(oslMutex Mutex)
Release the mutex.
osl_tryToAcquireMutex
SAL_DLLPUBLIC sal_Bool osl_tryToAcquireMutex(oslMutex Mutex)
Try to acquire the mutex without blocking.