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 .
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"
33 /** A mutual exclusion synchronization object
35 class SAL_WARN_UNUSED Mutex
{
40 The mutex value is 0 if it could not be created, otherwise a handle to the mutex.
42 @see ::osl_createMutex()
46 mutex
= osl_createMutex();
49 /** Release the OS-structures and free mutex data-structure.
50 @see ::osl_destroyMutex()
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()
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()
72 return osl_tryToAcquireMutex(mutex
);
75 /** Release the mutex.
76 @return false if system-call fails.
77 @see ::osl_releaseMutex()
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());
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
125 Guard(const Guard
&) SAL_DELETED_FUNCTION
;
126 Guard
& operator=(const Guard
&) SAL_DELETED_FUNCTION
;
132 /** Acquires the object specified as parameter.
134 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 /** 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
163 ClearableGuard( const ClearableGuard
& ) SAL_DELETED_FUNCTION
;
164 ClearableGuard
& operator=(const ClearableGuard
&) SAL_DELETED_FUNCTION
;
170 /** Acquires the object specified as parameter.
172 ClearableGuard(T
* pT_
) : pT(pT_
)
178 /** Acquires the object specified as parameter.
180 ClearableGuard(T
& t
) : pT(&t
)
185 /** Releases the mutex or interface if not already released by clear().
193 /** Releases the mutex or interface.
197 #ifdef LIBO_INTERNAL_ONLY
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
217 class ResettableGuard
: public ClearableGuard
< T
>
219 ResettableGuard(const ResettableGuard
&) SAL_DELETED_FUNCTION
;
220 ResettableGuard
& operator=(const ResettableGuard
&) SAL_DELETED_FUNCTION
;
226 /** Acquires the object specified as parameter.
228 ResettableGuard( T
* pT_
) :
229 ClearableGuard
<T
>( pT_
),
233 /** Acquires the object specified as parameter.
235 ResettableGuard( T
& rT
) :
236 ClearableGuard
<T
>( rT
),
240 /** Re-acquires the mutex or interface.
244 #ifdef LIBO_INTERNAL_ONLY
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: */