Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / osl / mutex.hxx
blobf42323b9fd0a76e389e6a378f1c8d0718ea34c0a
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>
26 namespace osl
28 /** A mutual exclusion synchronization object
30 class SAL_WARN_UNUSED Mutex {
32 public:
33 /** Create a mutex.
34 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
35 @see ::osl_createMutex()
37 Mutex()
39 mutex = osl_createMutex();
42 /** Release the OS-structures and free mutex data-structure.
43 @see ::osl_destroyMutex()
45 ~Mutex()
47 osl_destroyMutex(mutex);
50 /** Acquire the mutex, block if already acquired by another thread.
51 @return false if system-call fails.
52 @see ::osl_acquireMutex()
54 bool acquire()
56 return osl_acquireMutex(mutex);
59 /** Try to acquire the mutex without blocking.
60 @return false if it could not be acquired.
61 @see ::osl_tryToAcquireMutex()
63 bool tryToAcquire()
65 return osl_tryToAcquireMutex(mutex);
68 /** Release the mutex.
69 @return false if system-call fails.
70 @see ::osl_releaseMutex()
72 bool release()
74 return osl_releaseMutex(mutex);
77 /** Returns a global static mutex object.
78 The global and static mutex object can be used to initialize other
79 static objects in a thread safe manner.
80 @return the global mutex object
81 @see ::osl_getGlobalMutex()
83 static Mutex * getGlobalMutex()
85 return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
88 private:
89 oslMutex mutex;
91 /** The underlying oslMutex has no reference count.
93 Since the underlying oslMutex is not a reference counted object, copy
94 constructed Mutex may work on an already destructed oslMutex object.
97 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
99 /** This assignment operator is deleted for the same reason as
100 the copy constructor.
102 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
105 /** A helper class for mutex objects and interfaces.
107 template<class T>
108 class Guard
110 private:
111 Guard( const Guard& ) SAL_DELETED_FUNCTION;
112 const Guard& operator = ( const Guard& ) SAL_DELETED_FUNCTION;
114 protected:
115 T * pT;
116 public:
118 /** Acquires the object specified as parameter.
120 Guard(T * pT_) : pT(pT_)
122 pT->acquire();
125 /** Acquires the object specified as parameter.
127 Guard(T & t) : pT(&t)
129 pT->acquire();
132 /** Releases the mutex or interface. */
133 ~Guard()
135 pT->release();
139 /** A helper class for mutex objects and interfaces.
141 template<class T>
142 class ClearableGuard
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:
152 /** Acquires the object specified as parameter.
154 ClearableGuard(T * pT_) : pT(pT_)
156 pT->acquire();
159 /** Acquires the object specified as parameter.
161 ClearableGuard(T & t) : pT(&t)
163 pT->acquire();
166 /** Releases the mutex or interface if not already released by clear().
168 ~ClearableGuard()
170 if (pT)
171 pT->release();
174 /** Releases the mutex or interface.
176 void clear()
178 if(pT)
180 pT->release();
181 pT = NULL;
186 /** A helper class for mutex objects and interfaces.
188 template< class T >
189 class ResettableGuard : public ClearableGuard< T >
191 private:
192 ResettableGuard(ResettableGuard &) SAL_DELETED_FUNCTION;
193 void operator =(ResettableGuard &) SAL_DELETED_FUNCTION;
195 protected:
196 T* pResetT;
197 public:
198 /** Acquires the object specified as parameter.
200 ResettableGuard( T* pT_ ) :
201 ClearableGuard<T>( pT_ ),
202 pResetT( pT_ )
205 /** Acquires the object specified as parameter.
207 ResettableGuard( T& rT ) :
208 ClearableGuard<T>( rT ),
209 pResetT( &rT )
212 /** Re-acquires the mutex or interface.
214 void reset()
216 if( pResetT )
218 this->pT = pResetT;
219 this->pT->acquire();
224 typedef Guard<Mutex> MutexGuard;
225 typedef ClearableGuard<Mutex> ClearableMutexGuard;
226 typedef ResettableGuard< Mutex > ResettableMutexGuard;
229 #endif // INCLUDED_OSL_MUTEX_HXX
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */