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"
29 /** A mutual exclusion synchronization object
31 class SAL_WARN_UNUSED Mutex
{
35 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
36 @see ::osl_createMutex()
40 mutex
= osl_createMutex();
43 /** Release the OS-structures and free mutex data-structure.
44 @see ::osl_destroyMutex()
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()
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()
66 return osl_tryToAcquireMutex(mutex
);
69 /** Release the mutex.
70 @return false if system-call fails.
71 @see ::osl_releaseMutex()
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());
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
116 Guard(const Guard
&) SAL_DELETED_FUNCTION
;
117 Guard
& operator=(const Guard
&) SAL_DELETED_FUNCTION
;
123 /** Acquires the object specified as parameter.
125 Guard(T
* pT_
) : pT(pT_
)
131 /** Acquires the object specified as parameter.
133 Guard(T
& t
) : pT(&t
)
138 /** Releases the mutex or interface. */
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
154 ClearableGuard( const ClearableGuard
& ) SAL_DELETED_FUNCTION
;
155 ClearableGuard
& operator=(const ClearableGuard
&) SAL_DELETED_FUNCTION
;
161 /** Acquires the object specified as parameter.
163 ClearableGuard(T
* pT_
) : pT(pT_
)
169 /** Acquires the object specified as parameter.
171 ClearableGuard(T
& t
) : pT(&t
)
176 /** Releases the mutex or interface if not already released by clear().
184 /** Releases the mutex or interface.
188 #ifdef LIBO_INTERNAL_ONLY
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
208 class ResettableGuard
: public ClearableGuard
< T
>
210 ResettableGuard(const ResettableGuard
&) SAL_DELETED_FUNCTION
;
211 ResettableGuard
& operator=(const ResettableGuard
&) SAL_DELETED_FUNCTION
;
217 /** Acquires the object specified as parameter.
219 ResettableGuard( T
* pT_
) :
220 ClearableGuard
<T
>( pT_
),
224 /** Acquires the object specified as parameter.
226 ResettableGuard( T
& rT
) :
227 ClearableGuard
<T
>( rT
),
231 /** Re-acquires the mutex or interface.
235 #ifdef LIBO_INTERNAL_ONLY
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: */