ScopedReadWriteLock.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_ScopedReadWriteLock_hxx
00024 #define __ccxx_ScopedReadWriteLock_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/ReadWriteLock.h++>
00028 
00029 namespace ccxx {
00030 
00050 class /* COMMONCPP_API */ ScopedReadLock
00051 {
00052   public:
00053 
00058   ScopedReadLock(ReadWriteLock& rwlock) throw()
00059     : _rwlock(rwlock), _once(false)
00060   {
00061     _rwlock.lockRead();
00062   }
00063 
00067   ~ScopedReadLock() throw()
00068   {
00069     _rwlock.unlock();
00070   }
00071 
00073   inline bool testOnce() throw()
00074   { bool f = _once; _once = false; return(f); }
00077   private:
00078 
00079   ReadWriteLock& _rwlock;
00080   bool _once;
00081 
00082   CCXX_COPY_DECLS(ScopedReadLock);
00083 };
00084 
00104 class /* COMMONCPP_API */ ScopedWriteLock
00105 {
00106   public:
00107 
00111   ScopedWriteLock(ReadWriteLock& rwlock) throw()
00112     : _rwlock(rwlock), _once(true)
00113   {
00114     _rwlock.lockWrite();
00115   }
00116 
00118   ~ScopedWriteLock() throw()
00119   {
00120     _rwlock.unlock();
00121   }
00122 
00124   inline bool testOnce() throw()
00125   { bool f = _once; _once = false; return(f); }
00128   private:
00129 
00130   ReadWriteLock& _rwlock;
00131   bool _once;
00132 
00133   CCXX_COPY_DECLS(ScopedWriteLock);
00134 };
00135 
00136 }; // namespace ccxx
00137 
00142 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
00143 
00144 #define synchronized_read(LOCK)                         \
00145   for(commoncpp::ScopedReadLock LOCK ## _locker(LOCK);  \
00146       LOCK ## _locker.testOnce();)
00147 
00148 #else
00149 
00150 #define synchronized_read(LOCK)                                         \
00151   for(commoncpp::ScopedReadLock                                         \
00152         CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker)(LOCK);           \
00153       CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker).testOnce();)
00154 
00155 #endif
00156 
00161 #if (defined CCXX_OS_WINDOWS) && (defined _MSC_VER) && (_MSC_VER < 1400)
00162 
00163 #define synchronized_write(LOCK)                        \
00164   for(commoncpp::ScopedWriteLock LOCK ## _locker(LOCK); \
00165       LOCK ## _locker.testOnce();)
00166 
00167 #else
00168 
00169 #define synchronized_write(LOCK)                                        \
00170   for(commoncpp::ScopedWriteLock                                        \
00171         CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker)(LOCK);           \
00172       CCXX_OS_UNIQUE_VARNAME(LOCK, __LINE__, locker).testOnce();)
00173 
00174 #endif
00175 
00176 #endif // __ccxx_ScopedReadWriteLock_hxx
00177 
00178 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3