1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _VOS_MUTEX_HXX_ 29 #define _VOS_MUTEX_HXX_ 30 31 # include <vos/types.hxx> 32 # include <vos/object.hxx> 33 # include <osl/mutex.h> 34 35 namespace vos 36 { 37 38 39 /** IMutex interface 40 41 @author Bernd Hofner 42 @version 1.0 43 */ 44 45 class IMutex 46 { 47 public: 48 49 /// Blocks if Mutex is already in use 50 virtual void SAL_CALL acquire()= 0; 51 52 // Tries to get the mutex without blocking. 53 virtual sal_Bool SAL_CALL tryToAcquire()= 0; 54 55 /// releases the mutex. 56 virtual void SAL_CALL release()= 0; 57 58 protected: 59 IMutex() { } 60 virtual ~IMutex() { } 61 62 }; 63 64 // ---------------------------------------------------------- 65 66 /** OMutex 67 68 @author Bernd Hofner 69 @version 1.0 70 */ 71 72 class OMutex : public OObject, public IMutex 73 { 74 VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OMutex, vos)); 75 76 public: 77 static IMutex& SAL_CALL getGlobalMutex(); 78 79 /// Creates mutex 80 OMutex(); 81 /// Implicitly destroys mutex 82 virtual ~OMutex(); 83 84 /// Blocks if Mutex is already in use 85 virtual void SAL_CALL acquire(); 86 87 /** Tries to get the mutex without blocking. 88 @return True if mutex could be obtained, otherwise False 89 */ 90 virtual sal_Bool SAL_CALL tryToAcquire(); 91 92 /// releases the mutex. 93 virtual void SAL_CALL release(); 94 95 protected: 96 oslMutex m_Impl; 97 98 private: 99 // disable copy/assignment 100 OMutex(const OMutex&); 101 OMutex& SAL_CALL operator= (const OMutex&); 102 }; 103 104 // ********************************************************************************* 105 106 /** OGuard 107 108 @author Bernd Hofner 109 @version 1.0 110 */ 111 112 class OGuard 113 { 114 OGuard( const OGuard& ); 115 const OGuard& operator = ( const OGuard& ); 116 public: 117 /** Acquires mutex 118 @param pMutex pointer to mutex which is to be acquired */ 119 OGuard(IMutex* pMutex) 120 : m_rMutex( *pMutex ) 121 { // only for compatible reasons 122 m_rMutex.acquire(); 123 } 124 OGuard(IMutex & rMutex) 125 : m_rMutex( rMutex ) 126 { 127 m_rMutex.acquire(); 128 } 129 130 /** Releases mutex. */ 131 virtual ~OGuard() 132 { 133 m_rMutex.release(); 134 } 135 136 protected: 137 IMutex& m_rMutex; 138 }; 139 140 /** A guard that can release the mutex with the clear method. 141 142 @author Bernd Hofner 143 @version 1.0 144 */ 145 class OClearableGuard 146 { 147 OClearableGuard( const OClearableGuard& ); 148 const OClearableGuard& operator = ( const OClearableGuard& ); 149 public: 150 /** Acquires mutex 151 @param pMutex pointer to mutex which is to be acquired */ 152 OClearableGuard(IMutex & rMutex) 153 : m_pMutex( &rMutex ) 154 { 155 m_pMutex->acquire(); 156 } 157 158 /** Releases mutex. */ 159 virtual ~OClearableGuard() 160 { 161 if( m_pMutex ) 162 m_pMutex->release(); 163 } 164 165 /** Releases mutex. */ 166 void SAL_CALL clear() 167 { 168 if( m_pMutex ) 169 { 170 m_pMutex->release(); 171 m_pMutex = NULL; 172 } 173 } 174 protected: 175 IMutex* m_pMutex; 176 }; 177 178 } 179 180 181 #endif //_VOS_MUTEX_HXX_ 182 183 184