1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mutex.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _OSL_MUTEX_HXX_
32 #define _OSL_MUTEX_HXX_
36 #include <osl/mutex.h>
41 /** A mutual exclusion synchronization object
46 /** Create a thread-local mutex.
47 @return 0 if the mutex could not be created, otherwise a handle to the mutex.
48 @seealso ::osl_createMutex()
52 mutex
= osl_createMutex();
55 /** Release the OS-structures and free mutex data-structure.
56 @seealso ::osl_destroyMutex()
60 osl_destroyMutex(mutex
);
63 /** Acquire the mutex, block if already acquired by another thread.
64 @return sal_False if system-call fails.
65 @seealso ::osl_acquireMutex()
69 return osl_acquireMutex(mutex
);
72 /** Try to acquire the mutex without blocking.
73 @return sal_False if it could not be acquired.
74 @seealso ::osl_tryToAcquireMutex()
76 sal_Bool
tryToAcquire()
78 return osl_tryToAcquireMutex(mutex
);
81 /** Release the mutex.
82 @return sal_False if system-call fails.
83 @seealso ::osl_releaseMutex()
87 return osl_releaseMutex(mutex
);
90 /** Returns a global static mutex object.
91 The global and static mutex object can be used to initialize other
92 static objects in a thread safe manner.
93 @return the global mutex object
94 @seealso ::osl_getGlobalMutex()
96 static Mutex
* getGlobalMutex()
98 return (Mutex
*)osl_getGlobalMutex();
104 /** The underlying oslMutex has no reference count.
106 Since the underlying oslMutex is not a reference counted object, copy
107 constructed Mutex may work on an already destructed oslMutex object.
112 /** The underlying oslMutex has no reference count.
114 When destructed, the Mutex object destroys the undelying oslMutex,
115 which might cause severe problems in case it's a temporary object.
118 Mutex(oslMutex Mutex
);
120 /** This assignment operator is private for the same reason as
121 the copy constructor.
123 Mutex
& operator= (const Mutex
&);
125 /** This assignment operator is private for the same reason as
126 the constructor taking a oslMutex argument.
128 Mutex
& operator= (oslMutex
);
131 /** A helper class for mutex objects and interfaces.
137 Guard( const Guard
& );
138 const Guard
& operator = ( const Guard
& );
144 /** Acquires the object specified as parameter.
146 Guard(T
* pT_
) : pT(pT_
)
151 /** Acquires the object specified as parameter.
153 Guard(T
& t
) : pT(&t
)
158 /** Releases the mutex or interface. */
165 /** A helper class for mutex objects and interfaces.
171 ClearableGuard( const ClearableGuard
& );
172 const ClearableGuard
& operator = ( const ClearableGuard
& );
177 /** Acquires the object specified as parameter.
179 ClearableGuard(T
* pT_
) : pT(pT_
)
184 /** Acquires the object specified as parameter.
186 ClearableGuard(T
& t
) : pT(&t
)
191 /** Releases the mutex or interface if not already released by clear().
199 /** Releases the mutex or interface.
211 /** A helper class for mutex objects and interfaces.
214 class ResettableGuard
: public ClearableGuard
< T
>
217 ResettableGuard(ResettableGuard
&); // not defined
218 void operator =(ResettableGuard
&); // not defined
223 /** Acquires the object specified as parameter.
225 ResettableGuard( T
* pT_
) :
226 ClearableGuard
<T
>( pT_
),
230 /** Acquires the object specified as parameter.
232 ResettableGuard( T
& rT
) :
233 ClearableGuard
<T
>( rT
),
237 /** Re-aquires the mutex or interface.
249 typedef Guard
<Mutex
> MutexGuard
;
250 typedef ClearableGuard
<Mutex
> ClearableMutexGuard
;
251 typedef ResettableGuard
< Mutex
> ResettableMutexGuard
;
254 #endif /* __cplusplus */
255 #endif /* _OSL_MUTEX_HXX_ */