1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 /** A mutual exclusion synchronization object
30 class SAL_WARN_UNUSED Mutex
{
34 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
35 @see ::osl_createMutex()
39 mutex
= osl_createMutex();
42 /** Release the OS-structures and free mutex data-structure.
43 @see ::osl_destroyMutex()
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()
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()
65 return osl_tryToAcquireMutex(mutex
);
68 /** Release the mutex.
69 @return false if system-call fails.
70 @see ::osl_releaseMutex()
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());
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.
111 Guard( const Guard
& ) SAL_DELETED_FUNCTION
;
112 const Guard
& operator = ( const Guard
& ) SAL_DELETED_FUNCTION
;
118 /** Acquires the object specified as parameter.
120 Guard(T
* pT_
) : pT(pT_
)
125 /** Acquires the object specified as parameter.
127 Guard(T
& t
) : pT(&t
)
132 /** Releases the mutex or interface. */
139 /** A helper class for mutex objects and interfaces.
145 ClearableGuard( const ClearableGuard
& ) SAL_DELETED_FUNCTION
;
146 const ClearableGuard
& operator = ( const ClearableGuard
& )
147 SAL_DELETED_FUNCTION
;
152 /** Acquires the object specified as parameter.
154 ClearableGuard(T
* pT_
) : pT(pT_
)
159 /** Acquires the object specified as parameter.
161 ClearableGuard(T
& t
) : pT(&t
)
166 /** Releases the mutex or interface if not already released by clear().
174 /** Releases the mutex or interface.
186 /** A helper class for mutex objects and interfaces.
189 class ResettableGuard
: public ClearableGuard
< T
>
192 ResettableGuard(ResettableGuard
&) SAL_DELETED_FUNCTION
;
193 void operator =(ResettableGuard
&) SAL_DELETED_FUNCTION
;
198 /** Acquires the object specified as parameter.
200 ResettableGuard( T
* pT_
) :
201 ClearableGuard
<T
>( pT_
),
205 /** Acquires the object specified as parameter.
207 ResettableGuard( T
& rT
) :
208 ClearableGuard
<T
>( rT
),
212 /** Re-acquires the mutex or interface.
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: */