1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_ 29*cdf0e10cSrcweir #define __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_ 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 32*cdf0e10cSrcweir // my own includes 33*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <threadhelp/inoncopyable.h> 36*cdf0e10cSrcweir #include <threadhelp/irwlock.h> 37*cdf0e10cSrcweir #include <macros/debug.hxx> 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 40*cdf0e10cSrcweir // interface includes 41*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 42*cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 45*cdf0e10cSrcweir // other includes 46*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 47*cdf0e10cSrcweir #include <osl/mutex.hxx> 48*cdf0e10cSrcweir #include <osl/conditn.hxx> 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 51*cdf0e10cSrcweir // namespace 52*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir namespace framework{ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 57*cdf0e10cSrcweir // const 58*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 61*cdf0e10cSrcweir // declarations 62*cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir /*-************************************************************************************************************//** 65*cdf0e10cSrcweir @short implement a read/write lock with fairness between read/write accessors 66*cdf0e10cSrcweir @descr These implementation never should used as base class! Use it as a member every time. 67*cdf0e10cSrcweir Use ReadGuard and/or WriteGuard in your methods (which work with these lock) 68*cdf0e10cSrcweir to make your code threadsafe. 69*cdf0e10cSrcweir Fair means: All reading or writing threads are synchronized AND serialzed by using one 70*cdf0e10cSrcweir mutex. For reader this mutex is used to access internal variables of this lock only; 71*cdf0e10cSrcweir for writer this mutex is used to have an exclusiv access on your class member! 72*cdf0e10cSrcweir => It's a multi-reader/single-writer lock, which no preferred accessor. 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir @implements IRWlock 75*cdf0e10cSrcweir @base INonCopyable 76*cdf0e10cSrcweir IRWLock 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir @devstatus ready to use 79*cdf0e10cSrcweir *//*-*************************************************************************************************************/ 80*cdf0e10cSrcweir class FairRWLock : public IRWLock 81*cdf0e10cSrcweir , private INonCopyable 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 84*cdf0e10cSrcweir // public methods 85*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 86*cdf0e10cSrcweir public: 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /*-****************************************************************************************************//** 89*cdf0e10cSrcweir @short standard ctor 90*cdf0e10cSrcweir @descr Initialize instance with right start values for correct working. 91*cdf0e10cSrcweir no reader could exist => m_nReadCount = 0 92*cdf0e10cSrcweir don't block first comming writer => m_aWriteCondition.set() 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir @seealso - 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir @param - 97*cdf0e10cSrcweir @return - 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir @onerror - 100*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 101*cdf0e10cSrcweir inline FairRWLock() 102*cdf0e10cSrcweir : m_nReadCount( 0 ) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir m_aWriteCondition.set(); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir inline virtual ~FairRWLock() 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir /*-****************************************************************************************************//** 112*cdf0e10cSrcweir @interface IRWLock 113*cdf0e10cSrcweir @short set lock for reading 114*cdf0e10cSrcweir @descr A guard should call this method to acquire read access on your member. 115*cdf0e10cSrcweir Writing isn't allowed then - but nobody could check it for you! 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir @seealso method releaseReadAccess() 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir @param - 120*cdf0e10cSrcweir @return - 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir @onerror - 123*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 124*cdf0e10cSrcweir inline virtual void acquireReadAccess() 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir // Put call in "SERIALIZE"-queue! 127*cdf0e10cSrcweir // After successful acquiring this mutex we are alone ... 128*cdf0e10cSrcweir ::osl::MutexGuard aSerializeGuard( m_aSerializer ); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir // ... but we should synchronize us with other reader! 131*cdf0e10cSrcweir // May be - they will unregister himself by using releaseReadAccess()! 132*cdf0e10cSrcweir ::osl::MutexGuard aAccessGuard( m_aAccessLock ); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir // Now we must register us as reader by increasing counter. 135*cdf0e10cSrcweir // If this the first writer we must close door for possible writer. 136*cdf0e10cSrcweir // Other reader don't look for this barrier - they work parallel to us! 137*cdf0e10cSrcweir if( m_nReadCount == 0 ) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir m_aWriteCondition.reset(); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir ++m_nReadCount; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir /*-****************************************************************************************************//** 145*cdf0e10cSrcweir @interface IRWLock 146*cdf0e10cSrcweir @short reset lock for reading 147*cdf0e10cSrcweir @descr A guard should call this method to release read access on your member. 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir @seealso method acquireReadAccess() 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir @param - 152*cdf0e10cSrcweir @return - 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir @onerror - 155*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 156*cdf0e10cSrcweir inline virtual void releaseReadAccess() 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir // The access lock is enough at this point 159*cdf0e10cSrcweir // because it's not allowed to wait for all reader or writer here! 160*cdf0e10cSrcweir // That will cause a deadlock! 161*cdf0e10cSrcweir ::osl::MutexGuard aAccessGuard( m_aAccessLock ); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // Unregister as reader first! 164*cdf0e10cSrcweir // Open writer barrier then if it was the last reader. 165*cdf0e10cSrcweir --m_nReadCount; 166*cdf0e10cSrcweir if( m_nReadCount == 0 ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir m_aWriteCondition.set(); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir /*-****************************************************************************************************//** 173*cdf0e10cSrcweir @interface IRWLock 174*cdf0e10cSrcweir @short set lock for writing 175*cdf0e10cSrcweir @descr A guard should call this method to acquire write access on your member. 176*cdf0e10cSrcweir Reading is allowed too - of course. 177*cdf0e10cSrcweir After successfully calling of this method you are the only writer. 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir @seealso method releaseWriteAccess() 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir @param - 182*cdf0e10cSrcweir @return - 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir @onerror - 185*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 186*cdf0e10cSrcweir inline virtual void acquireWriteAccess() 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir // You have to stand in our serialize-queue till all reader 189*cdf0e10cSrcweir // are registered (not for releasing them!) or writer finished their work! 190*cdf0e10cSrcweir // Don't use a guard to do so - because you must hold the mutex till 191*cdf0e10cSrcweir // you call releaseWriteAccess()! 192*cdf0e10cSrcweir // After succesfull acquire you have to wait for current working reader. 193*cdf0e10cSrcweir // Used condition will open by last gone reader object. 194*cdf0e10cSrcweir m_aSerializer.acquire(); 195*cdf0e10cSrcweir m_aWriteCondition.wait(); 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir #ifdef ENABLE_MUTEXDEBUG 198*cdf0e10cSrcweir // A writer is an exclusiv accessor! 199*cdf0e10cSrcweir LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::acquireWriteAccess()", "No threadsafe code detected ... : Read count != 0!" ) 200*cdf0e10cSrcweir #endif 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir /*-****************************************************************************************************//** 204*cdf0e10cSrcweir @interface IRWLock 205*cdf0e10cSrcweir @short reset lock for writing 206*cdf0e10cSrcweir @descr A guard should call this method to release write access on your member. 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir @seealso method acquireWriteAccess() 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir @param - 211*cdf0e10cSrcweir @return - 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir @onerror - 214*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 215*cdf0e10cSrcweir inline virtual void releaseWriteAccess() 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir // The only one you have to do here is to release 218*cdf0e10cSrcweir // hold seriliaze-mutex. All other user of these instance are blocked 219*cdf0e10cSrcweir // by these mutex! 220*cdf0e10cSrcweir // You don't need any other mutex here - you are the only one in the moment! 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir #ifdef ENABLE_MUTEXDEBUG 223*cdf0e10cSrcweir // A writer is an exclusiv accessor! 224*cdf0e10cSrcweir LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::releaseWriteAccess()", "No threadsafe code detected ... : Read count != 0!" ) 225*cdf0e10cSrcweir #endif 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir m_aSerializer.release(); 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir /*-****************************************************************************************************//** 231*cdf0e10cSrcweir @interface IRWLock 232*cdf0e10cSrcweir @short downgrade a write access to a read access 233*cdf0e10cSrcweir @descr A guard should call this method to change a write to a read access. 234*cdf0e10cSrcweir New readers can work too - new writer are blocked! 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir @attention Don't call this method if you are not a writer! 237*cdf0e10cSrcweir Results are not defined then ... 238*cdf0e10cSrcweir An upgrade can't be implemented realy ... because acquiring new access 239*cdf0e10cSrcweir will be the same - there no differences! 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir @seealso - 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir @param - 244*cdf0e10cSrcweir @return - 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir @onerror - 247*cdf0e10cSrcweir *//*-*****************************************************************************************************/ 248*cdf0e10cSrcweir inline virtual void downgradeWriteAccess() 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir // You must be a writer to call this method! 251*cdf0e10cSrcweir // We can't check it - but otherwise it's your problem ... 252*cdf0e10cSrcweir // Thats why you don't need any mutex here. 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir #ifdef ENABLE_MUTEXDEBUG 255*cdf0e10cSrcweir // A writer is an exclusiv accessor! 256*cdf0e10cSrcweir LOG_ASSERT2( m_nReadCount!=0, "FairRWLock::downgradeWriteAccess()", "No threadsafe code detected ... : Read count != 0!" ) 257*cdf0e10cSrcweir #endif 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir // Register himself as "new" reader. 260*cdf0e10cSrcweir // This value must be 0 before - because we support single writer access only! 261*cdf0e10cSrcweir ++m_nReadCount; 262*cdf0e10cSrcweir // Close barrier for other writer! 263*cdf0e10cSrcweir // Why? 264*cdf0e10cSrcweir // You hold the serializer mutex - next one can be a reader OR a writer. 265*cdf0e10cSrcweir // They must blocked then - because you will be a reader after this call 266*cdf0e10cSrcweir // and writer use this condition to wait for current reader! 267*cdf0e10cSrcweir m_aWriteCondition.reset(); 268*cdf0e10cSrcweir // Open door for next waiting thread in serialize queue! 269*cdf0e10cSrcweir m_aSerializer.release(); 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 273*cdf0e10cSrcweir // private member 274*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------- 275*cdf0e10cSrcweir private: 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir ::osl::Mutex m_aAccessLock ; /// regulate access on internal member of this instance 278*cdf0e10cSrcweir ::osl::Mutex m_aSerializer ; /// serialze incoming read/write access threads 279*cdf0e10cSrcweir ::osl::Condition m_aWriteCondition ; /// a writer must wait till current working reader are gone 280*cdf0e10cSrcweir sal_Int32 m_nReadCount ; /// every reader is registered - the last one open the door for waiting writer 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir }; // class FairRWLock 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir } // namespace framework 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir #endif // #ifndef __FRAMEWORK_THREADHELP_FAIRRWLOCK_HXX_ 287