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 _OSL_MUTEX_HXX_
21 #define _OSL_MUTEX_HXX_
25 #include <osl/mutex.h>
30 /** A mutual exclusion synchronization object
32 class SAL_WARN_UNUSED Mutex
{
35 /** Create a thread-local mutex.
36 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
37 @see ::osl_createMutex()
41 mutex
= osl_createMutex();
44 /** Release the OS-structures and free mutex data-structure.
45 @see ::osl_destroyMutex()
49 osl_destroyMutex(mutex
);
52 /** Acquire the mutex, block if already acquired by another thread.
53 @return sal_False if system-call fails.
54 @see ::osl_acquireMutex()
58 return osl_acquireMutex(mutex
);
61 /** Try to acquire the mutex without blocking.
62 @return sal_False if it could not be acquired.
63 @see ::osl_tryToAcquireMutex()
65 sal_Bool
tryToAcquire()
67 return osl_tryToAcquireMutex(mutex
);
70 /** Release the mutex.
71 @return sal_False if system-call fails.
72 @see ::osl_releaseMutex()
76 return osl_releaseMutex(mutex
);
79 /** Returns a global static mutex object.
80 The global and static mutex object can be used to initialize other
81 static objects in a thread safe manner.
82 @return the global mutex object
83 @see ::osl_getGlobalMutex()
85 static Mutex
* getGlobalMutex()
87 return (Mutex
*)osl_getGlobalMutex();
93 /** The underlying oslMutex has no reference count.
95 Since the underlying oslMutex is not a reference counted object, copy
96 constructed Mutex may work on an already destructed oslMutex object.
101 /** The underlying oslMutex has no reference count.
103 When destructed, the Mutex object destroys the undelying oslMutex,
104 which might cause severe problems in case it's a temporary object.
107 Mutex(oslMutex Mutex
);
109 /** This assignment operator is private for the same reason as
110 the copy constructor.
112 Mutex
& operator= (const Mutex
&);
114 /** This assignment operator is private for the same reason as
115 the constructor taking a oslMutex argument.
117 Mutex
& operator= (oslMutex
);
120 /** A helper class for mutex objects and interfaces.
126 Guard( const Guard
& );
127 const Guard
& operator = ( const Guard
& );
133 /** Acquires the object specified as parameter.
135 Guard(T
* pT_
) : pT(pT_
)
140 /** Acquires the object specified as parameter.
142 Guard(T
& t
) : pT(&t
)
147 /** Releases the mutex or interface. */
154 /** A helper class for mutex objects and interfaces.
160 ClearableGuard( const ClearableGuard
& );
161 const ClearableGuard
& operator = ( const ClearableGuard
& );
166 /** Acquires the object specified as parameter.
168 ClearableGuard(T
* pT_
) : pT(pT_
)
173 /** Acquires the object specified as parameter.
175 ClearableGuard(T
& t
) : pT(&t
)
180 /** Releases the mutex or interface if not already released by clear().
188 /** Releases the mutex or interface.
200 /** A helper class for mutex objects and interfaces.
203 class ResettableGuard
: public ClearableGuard
< T
>
206 ResettableGuard(ResettableGuard
&); // not defined
207 void operator =(ResettableGuard
&); // not defined
212 /** Acquires the object specified as parameter.
214 ResettableGuard( T
* pT_
) :
215 ClearableGuard
<T
>( pT_
),
219 /** Acquires the object specified as parameter.
221 ResettableGuard( T
& rT
) :
222 ClearableGuard
<T
>( rT
),
226 /** Re-aquires the mutex or interface.
238 typedef Guard
<Mutex
> MutexGuard
;
239 typedef ClearableGuard
<Mutex
> ClearableMutexGuard
;
240 typedef ResettableGuard
< Mutex
> ResettableMutexGuard
;
243 #endif /* __cplusplus */
244 #endif /* _OSL_MUTEX_HXX_ */
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */