xref: /trunk/main/vos/inc/vos/mutex.hxx (revision d98e0520)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _VOS_MUTEX_HXX_
25 #define _VOS_MUTEX_HXX_
26 
27 #	include <vos/types.hxx>
28 #	include <vos/object.hxx>
29 #	include <osl/mutex.h>
30 #	include <vos/vosdllapi.h>
31 
32 namespace vos
33 {
34 
35 
36 /** IMutex interface
37 
38 	@author  Bernd Hofner
39 	@version 1.0
40 */
41 
42 class IMutex
43 {
44 public:
45 
46 	/// Blocks if Mutex is already in use
47 	virtual void SAL_CALL acquire()= 0;
48 
49 	// Tries to get the mutex without blocking.
50 	virtual sal_Bool SAL_CALL tryToAcquire()= 0;
51 
52 	/// releases the mutex.
53 	virtual void SAL_CALL release()= 0;
54 
55 protected:
IMutex()56 	IMutex() { }
~IMutex()57 	virtual ~IMutex() { }
58 
59 };
60 
61 // ----------------------------------------------------------
62 
63 /** OMutex
64 
65 	@author  Bernd Hofner
66 	@version 1.0
67 */
68 
69 class VOS_DLLPUBLIC OMutex : public OObject, public IMutex
70 {
71 	VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OMutex, vos));
72 
73 public:
74 	static IMutex& SAL_CALL getGlobalMutex();
75 
76 	/// Creates mutex
77 	OMutex();
78 	/// Implicitly destroys mutex
79     virtual ~OMutex();
80 
81 	/// Blocks if Mutex is already in use
82 	virtual void SAL_CALL acquire();
83 
84 	/** Tries to get the mutex without blocking.
85 		@return True if mutex could be obtained, otherwise False
86 	*/
87 	virtual sal_Bool SAL_CALL tryToAcquire();
88 
89 	/// releases the mutex.
90 	virtual void SAL_CALL release();
91 
92 protected:
93 	oslMutex	m_Impl;
94 
95 private:
96 	// disable copy/assignment
97 	OMutex(const OMutex&);
98 	OMutex& SAL_CALL operator= (const OMutex&);
99 };
100 
101 // *********************************************************************************
102 
103 /** OGuard
104 
105 	@author  Bernd Hofner
106 	@version 1.0
107 */
108 
109 class OGuard
110 {
111 	OGuard( const OGuard& );
112 	const OGuard& operator = ( const OGuard& );
113 public:
114 	/** Acquires mutex
115 	    @param pMutex pointer to mutex which is to be acquired	*/
OGuard(IMutex * pMutex)116 	OGuard(IMutex* pMutex)
117 		: m_rMutex( *pMutex )
118 	{	// only for compatible reasons
119 		m_rMutex.acquire();
120 	}
OGuard(IMutex & rMutex)121 	OGuard(IMutex & rMutex)
122 		: m_rMutex( rMutex )
123 	{
124 		m_rMutex.acquire();
125 	}
126 
127 	/** Releases mutex. */
~OGuard()128 	virtual ~OGuard()
129 	{
130 		m_rMutex.release();
131 	}
132 
133 protected:
134 	IMutex& m_rMutex;
135 };
136 
137 /** A guard that can release the mutex with the clear method.
138 
139 	@author  Bernd Hofner
140 	@version 1.0
141 */
142 class OClearableGuard
143 {
144 	OClearableGuard( const OClearableGuard& );
145 	const OClearableGuard& operator = ( const OClearableGuard& );
146 public:
147 	/** Acquires mutex
148 	    @param pMutex pointer to mutex which is to be acquired	*/
OClearableGuard(IMutex & rMutex)149 	OClearableGuard(IMutex & rMutex)
150 		: m_pMutex( &rMutex )
151 	{
152 		m_pMutex->acquire();
153 	}
154 
155 	/** Releases mutex. */
~OClearableGuard()156 	virtual ~OClearableGuard()
157 	{
158 		if( m_pMutex )
159 			m_pMutex->release();
160 	}
161 
162 	/** Releases mutex. */
clear()163 	void SAL_CALL clear()
164 	{
165 		if( m_pMutex )
166 		{
167 			m_pMutex->release();
168 			m_pMutex = NULL;
169 		}
170 	}
171 protected:
172 	IMutex* m_pMutex;
173 };
174 
175 }
176 
177 
178 #endif  //_VOS_MUTEX_HXX_
179 
180 
181