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