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 31 namespace vos 32 { 33 34 35 /** IMutex interface 36 37 @author Bernd Hofner 38 @version 1.0 39 */ 40 41 class IMutex 42 { 43 public: 44 45 /// Blocks if Mutex is already in use 46 virtual void SAL_CALL acquire()= 0; 47 48 // Tries to get the mutex without blocking. 49 virtual sal_Bool SAL_CALL tryToAcquire()= 0; 50 51 /// releases the mutex. 52 virtual void SAL_CALL release()= 0; 53 54 protected: IMutex()55 IMutex() { } ~IMutex()56 virtual ~IMutex() { } 57 58 }; 59 60 // ---------------------------------------------------------- 61 62 /** OMutex 63 64 @author Bernd Hofner 65 @version 1.0 66 */ 67 68 class OMutex : public OObject, public IMutex 69 { 70 VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OMutex, vos)); 71 72 public: 73 static IMutex& SAL_CALL getGlobalMutex(); 74 75 /// Creates mutex 76 OMutex(); 77 /// Implicitly destroys mutex 78 virtual ~OMutex(); 79 80 /// Blocks if Mutex is already in use 81 virtual void SAL_CALL acquire(); 82 83 /** Tries to get the mutex without blocking. 84 @return True if mutex could be obtained, otherwise False 85 */ 86 virtual sal_Bool SAL_CALL tryToAcquire(); 87 88 /// releases the mutex. 89 virtual void SAL_CALL release(); 90 91 protected: 92 oslMutex m_Impl; 93 94 private: 95 // disable copy/assignment 96 OMutex(const OMutex&); 97 OMutex& SAL_CALL operator= (const OMutex&); 98 }; 99 100 // ********************************************************************************* 101 102 /** OGuard 103 104 @author Bernd Hofner 105 @version 1.0 106 */ 107 108 class OGuard 109 { 110 OGuard( const OGuard& ); 111 const OGuard& operator = ( const OGuard& ); 112 public: 113 /** Acquires mutex 114 @param pMutex pointer to mutex which is to be acquired */ OGuard(IMutex * pMutex)115 OGuard(IMutex* pMutex) 116 : m_rMutex( *pMutex ) 117 { // only for compatible reasons 118 m_rMutex.acquire(); 119 } OGuard(IMutex & rMutex)120 OGuard(IMutex & rMutex) 121 : m_rMutex( rMutex ) 122 { 123 m_rMutex.acquire(); 124 } 125 126 /** Releases mutex. */ ~OGuard()127 virtual ~OGuard() 128 { 129 m_rMutex.release(); 130 } 131 132 protected: 133 IMutex& m_rMutex; 134 }; 135 136 /** A guard that can release the mutex with the clear method. 137 138 @author Bernd Hofner 139 @version 1.0 140 */ 141 class OClearableGuard 142 { 143 OClearableGuard( const OClearableGuard& ); 144 const OClearableGuard& operator = ( const OClearableGuard& ); 145 public: 146 /** Acquires mutex 147 @param pMutex pointer to mutex which is to be acquired */ OClearableGuard(IMutex & rMutex)148 OClearableGuard(IMutex & rMutex) 149 : m_pMutex( &rMutex ) 150 { 151 m_pMutex->acquire(); 152 } 153 154 /** Releases mutex. */ ~OClearableGuard()155 virtual ~OClearableGuard() 156 { 157 if( m_pMutex ) 158 m_pMutex->release(); 159 } 160 161 /** Releases mutex. */ clear()162 void SAL_CALL clear() 163 { 164 if( m_pMutex ) 165 { 166 m_pMutex->release(); 167 m_pMutex = NULL; 168 } 169 } 170 protected: 171 IMutex* m_pMutex; 172 }; 173 174 } 175 176 177 #endif //_VOS_MUTEX_HXX_ 178 179 180