Bump version to 6.4-15
[LibreOffice.git] / include / osl / mutex.hxx
blobf984f035ca40053a5113816cbd309e61d1a3b2de
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_OSL_MUTEX_HXX
21 #define INCLUDED_OSL_MUTEX_HXX
23 #include "osl/mutex.h"
25 #include <cassert>
27 namespace osl
29 /** A mutual exclusion synchronization object
31 class SAL_WARN_UNUSED Mutex {
33 public:
34 /** Create a mutex.
35 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
36 @see ::osl_createMutex()
38 Mutex()
40 mutex = osl_createMutex();
43 /** Release the OS-structures and free mutex data-structure.
44 @see ::osl_destroyMutex()
46 ~Mutex()
48 osl_destroyMutex(mutex);
51 /** Acquire the mutex, block if already acquired by another thread.
52 @return false if system-call fails.
53 @see ::osl_acquireMutex()
55 bool acquire()
57 return osl_acquireMutex(mutex);
60 /** Try to acquire the mutex without blocking.
61 @return false if it could not be acquired.
62 @see ::osl_tryToAcquireMutex()
64 bool tryToAcquire()
66 return osl_tryToAcquireMutex(mutex);
69 /** Release the mutex.
70 @return false if system-call fails.
71 @see ::osl_releaseMutex()
73 bool release()
75 return osl_releaseMutex(mutex);
78 /** Returns a global static mutex object.
79 The global and static mutex object can be used to initialize other
80 static objects in a thread safe manner.
81 @return the global mutex object
82 @see ::osl_getGlobalMutex()
84 static Mutex * getGlobalMutex()
86 return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
89 private:
90 oslMutex mutex;
92 /** The underlying oslMutex has no reference count.
94 Since the underlying oslMutex is not a reference counted object, copy
95 constructed Mutex may work on an already destructed oslMutex object.
98 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
100 /** This assignment operator is deleted for the same reason as
101 the copy constructor.
103 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
106 /** Object lifetime scoped mutex object or interface lock.
108 * Acquires the template object on construction and releases it on
109 * destruction.
111 * @see MutexGuard
113 template<class T>
114 class Guard
116 Guard(const Guard&) SAL_DELETED_FUNCTION;
117 Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
119 protected:
120 T * pT;
122 public:
123 /** Acquires the object specified as parameter.
125 Guard(T * pT_) : pT(pT_)
127 assert(pT != NULL);
128 pT->acquire();
131 /** Acquires the object specified as parameter.
133 Guard(T & t) : pT(&t)
135 pT->acquire();
138 /** Releases the mutex or interface. */
139 ~Guard()
141 pT->release();
145 /** Object lifetime scoped mutex object or interface lock with unlock.
147 * Use this if you can't use scoped code blocks and Guard.
149 * @see ClearableMutexGuard, Guard
151 template<class T>
152 class ClearableGuard
154 ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
155 ClearableGuard& operator=(const ClearableGuard&) SAL_DELETED_FUNCTION;
157 protected:
158 T * pT;
160 public:
161 /** Acquires the object specified as parameter.
163 ClearableGuard(T * pT_) : pT(pT_)
165 assert(pT != NULL);
166 pT->acquire();
169 /** Acquires the object specified as parameter.
171 ClearableGuard(T & t) : pT(&t)
173 pT->acquire();
176 /** Releases the mutex or interface if not already released by clear().
178 ~ClearableGuard()
180 if (pT)
181 pT->release();
184 /** Releases the mutex or interface.
186 void clear()
188 #ifdef LIBO_INTERNAL_ONLY
189 assert(pT);
190 #else
191 if (pT)
192 #endif
194 pT->release();
195 pT = NULL;
200 /** Template for temporary releasable mutex objects and interfaces locks.
202 * Use this if you want to acquire a lock but need to temporary release
203 * it and can't use multiple scoped Guard objects.
205 * @see ResettableMutexGuard
207 template< class T >
208 class ResettableGuard : public ClearableGuard< T >
210 ResettableGuard(const ResettableGuard&) SAL_DELETED_FUNCTION;
211 ResettableGuard& operator=(const ResettableGuard&) SAL_DELETED_FUNCTION;
213 protected:
214 T* pResetT;
216 public:
217 /** Acquires the object specified as parameter.
219 ResettableGuard( T* pT_ ) :
220 ClearableGuard<T>( pT_ ),
221 pResetT( pT_ )
224 /** Acquires the object specified as parameter.
226 ResettableGuard( T& rT ) :
227 ClearableGuard<T>( rT ),
228 pResetT( &rT )
231 /** Re-acquires the mutex or interface.
233 void reset()
235 #ifdef LIBO_INTERNAL_ONLY
236 assert(!this->pT);
237 #endif
238 if (pResetT)
240 this->pT = pResetT;
241 this->pT->acquire();
246 typedef Guard<Mutex> MutexGuard;
247 typedef ClearableGuard<Mutex> ClearableMutexGuard;
248 typedef ResettableGuard< Mutex > ResettableMutexGuard;
251 #endif // INCLUDED_OSL_MUTEX_HXX
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */