sprint 1-alpha
sprint/thread/rwmutex.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   SPRINT::mutex
00003 *
00004 *   Copyright (C) 2003-2007  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _RWMUTEX_H
00023 #define _RWMUTEX_H
00024 
00025 // @todo: not final version. rewrite it
00026          
00031 #include <sprint/mutex.h>
00032 
00033 namespace sprint {
00034 
00036 class ReadWriteMutex {
00037         sprint::mutex reader, writer, mutex;
00038         int readercount;
00039         public:
00040                 ReadWriteMutex() : readercount(0)
00041                         {
00042                         }
00043                 ~ReadWriteMutex()
00044                         {
00045                         }
00046                         
00047                 void acquireReadLock()
00048                 {
00049                  // check point per verificare che un writer sia in coda
00050                  reader.lock();
00051                  
00052                  // lock for readercount
00053                  mutex.lock();
00054                  if(++readercount==1)
00055                         writer.lock();  // blocca l'ingresso di scrittori
00056                  mutex.unlock();
00057                  
00058                  reader.unlock();
00059                 }
00060                 
00061                 void releaseReadLock()
00062                 {
00063 
00064                  // lock for readercount
00065                  mutex.lock();
00066            if(--readercount==0)
00067                         writer.unlock();
00068                  mutex.unlock();
00069                 }
00070 
00071                 void acquireWriteLock()
00072                 {
00073                  reader.lock(); // blocca l'ingresso di ulteriori lettori
00074                  writer.lock(); // attende che eventuali lettori abbiano terminato
00075                 }
00076                 
00077                 void releaseWriteLock()
00078                 {
00079                  writer.unlock();
00080                  reader.unlock();
00081                 }
00082 
00083 };
00084 
00086 class ScopedReadLock
00087     {
00088     public:
00089         ScopedReadLock(ReadWriteMutex& rwLock) :
00090             m_rwLock(rwLock)
00091         {
00092             m_rwLock.acquireReadLock();
00093         }
00094 
00095         ~ScopedReadLock()
00096         {
00097             m_rwLock.releaseReadLock();
00098         }
00099 
00100     private:
00101         ReadWriteMutex& m_rwLock;
00102     };
00103 
00105 class ScopedWriteLock
00106     {
00107     public:
00108         ScopedWriteLock(ReadWriteMutex& rwLock) :
00109             m_rwLock(rwLock)
00110         {
00111             m_rwLock.acquireWriteLock();
00112         }
00113 
00114         ~ScopedWriteLock()
00115         {
00116             m_rwLock.releaseWriteLock();
00117         }
00118 
00119     private:
00120         ReadWriteMutex& m_rwLock;
00121     };
00122 
00123 }
00124 
00125 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines