Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

prwlock.h

Go to the documentation of this file.
00001 /*
00002  *   P::Classes - Portable C++ Application Framework
00003  *   Copyright (C) 2000-2003  Christian Prochnow <cproch@seculogix.de>
00004  *
00005  *   This library is free software; you can redistribute it and/or
00006  *   modify it under the terms of the GNU Lesser General Public
00007  *   License as published by the Free Software Foundation; either
00008  *   version 2 of the License, or (at your option) any later version.
00009  *
00010  *   This library is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *   Lesser General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU Lesser General Public
00016  *   License along with this library; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #ifndef _prwlock_h_
00021 #define _prwlock_h_
00022 
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pexception.h>
00025 
00026 namespace P {
00027 
00029 
00038 class PCORE_EXPORT RWLock {
00039   public:
00041 
00054     RWLock(const char* name = 0) throw(SyncError);
00055 
00057 
00063     ~RWLock() throw();
00064 
00066 
00073     void readLock() throw(SyncError);
00074 
00076 
00085     bool tryReadLock(unsigned int timeout) throw(SyncError);
00086 
00088 
00095     void writeLock() throw(SyncError);
00096 
00098 
00107     bool tryWriteLock(unsigned int timeout) throw(SyncError);
00108 
00110     void unlock() throw(SyncError);
00111 
00113 
00136     class ReadLock {
00137       public:
00138         inline ReadLock(RWLock& lock)
00139         : m_lock(lock) { lock.readLock(); }
00140 
00141         ~ReadLock()
00142         { m_lock.unlock(); }
00143 
00144         ReadLock& operator=(RWLock& mutex)
00145         {
00146           m_lock.unlock();
00147           m_lock = mutex;
00148           m_lock.readLock();
00149           return *this;
00150         }
00151 
00152       private:
00153         ReadLock(const ReadLock&);
00154         ReadLock& operator=(const ReadLock&);
00155 
00156         RWLock& m_lock;
00157     };
00158 
00160 
00183     class WriteLock {
00184       public:
00185         inline WriteLock(RWLock& lock)
00186         : m_lock(lock) { lock.writeLock(); }
00187 
00188         ~WriteLock()
00189         { m_lock.unlock(); }
00190 
00191         WriteLock& operator=(RWLock& mutex)
00192         {
00193           m_lock.unlock();
00194           m_lock = mutex;
00195           m_lock.writeLock();
00196           return *this;
00197         }
00198 
00199       private:
00200         WriteLock(const WriteLock&);
00201         WriteLock& operator=(const WriteLock&);
00202 
00203         RWLock& m_lock;
00204     };
00205     
00206   private:
00207     RWLock(const RWLock&);
00208     RWLock& operator=(const RWLock&);
00209   
00210     struct rwlock_handle_t;
00211     rwlock_handle_t* m_handle;
00212 };
00213 
00214 }
00215 
00216 #endif

Generated on Fri Mar 12 21:08:31 2004 for P::Classes by doxygen 1.3.3