Fix typo
[LibreOffice.git] / include / osl / mutex.hxx
blob481a2bb55002a81659fb7abe55edf1ca98e82b3c
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_MUTEX_HXX
25 #define INCLUDED_OSL_MUTEX_HXX
27 #include "osl/mutex.h"
29 #include <cassert>
31 namespace osl
33 /** A mutual exclusion synchronization object
35 class SAL_WARN_UNUSED Mutex {
37 public:
38 /** Create a mutex.
40 The mutex value is 0 if it could not be created, otherwise a handle to the mutex.
42 @see ::osl_createMutex()
44 Mutex()
46 mutex = osl_createMutex();
49 /** Release the OS-structures and free mutex data-structure.
50 @see ::osl_destroyMutex()
52 ~Mutex()
54 osl_destroyMutex(mutex);
57 /** Acquire the mutex, block if already acquired by another thread.
58 @return false if system-call fails.
59 @see ::osl_acquireMutex()
61 bool acquire()
63 return osl_acquireMutex(mutex);
66 /** Try to acquire the mutex without blocking.
67 @return false if it could not be acquired.
68 @see ::osl_tryToAcquireMutex()
70 bool tryToAcquire()
72 return osl_tryToAcquireMutex(mutex);
75 /** Release the mutex.
76 @return false if system-call fails.
77 @see ::osl_releaseMutex()
79 bool release()
81 return osl_releaseMutex(mutex);
84 /** Returns a global static mutex object.
85 The global and static mutex object can be used to initialize other
86 static objects in a thread safe manner.
87 @return the global mutex object
88 @see ::osl_getGlobalMutex()
90 static Mutex * getGlobalMutex()
92 return reinterpret_cast<Mutex *>(osl_getGlobalMutex());
95 private:
96 oslMutex mutex;
98 // access to the oslMutex
99 friend oslMutex* SAL_CALL ::osl_getGlobalMutex();
101 /** The underlying oslMutex has no reference count.
103 Since the underlying oslMutex is not a reference counted object, copy
104 constructed Mutex may work on an already destructed oslMutex object.
107 Mutex(const Mutex&) SAL_DELETED_FUNCTION;
109 /** This assignment operator is deleted for the same reason as
110 the copy constructor.
112 Mutex& operator= (const Mutex&) SAL_DELETED_FUNCTION;
115 /** Object lifetime scoped mutex object or interface lock.
117 * Acquires the template object on construction and releases it on
118 * destruction.
120 * @see MutexGuard
122 template<class T>
123 class Guard
125 Guard(const Guard&) SAL_DELETED_FUNCTION;
126 Guard& operator=(const Guard&) SAL_DELETED_FUNCTION;
128 protected:
129 T * pT;
131 public:
132 /** Acquires the object specified as parameter.
134 Guard(T * pT_) : pT(pT_)
136 assert(pT != NULL);
137 pT->acquire();
140 /** Acquires the object specified as parameter.
142 Guard(T & t) : pT(&t)
144 pT->acquire();
147 /** Releases the mutex or interface. */
148 ~Guard()
150 pT->release();
154 /** Object lifetime scoped mutex object or interface lock with unlock.
156 * Use this if you can't use scoped code blocks and Guard.
158 * @see ClearableMutexGuard, Guard
160 template<class T>
161 class ClearableGuard
163 ClearableGuard( const ClearableGuard& ) SAL_DELETED_FUNCTION;
164 ClearableGuard& operator=(const ClearableGuard&) SAL_DELETED_FUNCTION;
166 protected:
167 T * pT;
169 public:
170 /** Acquires the object specified as parameter.
172 ClearableGuard(T * pT_) : pT(pT_)
174 assert(pT != NULL);
175 pT->acquire();
178 /** Acquires the object specified as parameter.
180 ClearableGuard(T & t) : pT(&t)
182 pT->acquire();
185 /** Releases the mutex or interface if not already released by clear().
187 ~ClearableGuard()
189 if (pT)
190 pT->release();
193 /** Releases the mutex or interface.
195 void clear()
197 #ifdef LIBO_INTERNAL_ONLY
198 assert(pT);
199 #else
200 if (pT)
201 #endif
203 pT->release();
204 pT = NULL;
209 /** Template for temporary releasable mutex objects and interfaces locks.
211 * Use this if you want to acquire a lock but need to temporary release
212 * it and can't use multiple scoped Guard objects.
214 * @see ResettableMutexGuard
216 template< class T >
217 class ResettableGuard : public ClearableGuard< T >
219 ResettableGuard(const ResettableGuard&) SAL_DELETED_FUNCTION;
220 ResettableGuard& operator=(const ResettableGuard&) SAL_DELETED_FUNCTION;
222 protected:
223 T* pResetT;
225 public:
226 /** Acquires the object specified as parameter.
228 ResettableGuard( T* pT_ ) :
229 ClearableGuard<T>( pT_ ),
230 pResetT( pT_ )
233 /** Acquires the object specified as parameter.
235 ResettableGuard( T& rT ) :
236 ClearableGuard<T>( rT ),
237 pResetT( &rT )
240 /** Re-acquires the mutex or interface.
242 void reset()
244 #ifdef LIBO_INTERNAL_ONLY
245 assert(!this->pT);
246 #endif
247 if (pResetT)
249 this->pT = pResetT;
250 this->pT->acquire();
255 typedef Guard<Mutex> MutexGuard;
256 typedef ClearableGuard<Mutex> ClearableMutexGuard;
257 typedef ResettableGuard< Mutex > ResettableMutexGuard;
260 #endif // INCLUDED_OSL_MUTEX_HXX
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */