00001 // 00002 // RWLock.h 00003 // 00004 // $Id: //poco/1.3/Foundation/include/Poco/RWLock.h#1 $ 00005 // 00006 // Library: Foundation 00007 // Package: Threading 00008 // Module: RWLock 00009 // 00010 // Definition of the RWLock class. 00011 // 00012 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00013 // and Contributors. 00014 // 00015 // Permission is hereby granted, free of charge, to any person or organization 00016 // obtaining a copy of the software and accompanying documentation covered by 00017 // this license (the "Software") to use, reproduce, display, distribute, 00018 // execute, and transmit the Software, and to prepare derivative works of the 00019 // Software, and to permit third-parties to whom the Software is furnished to 00020 // do so, all subject to the following: 00021 // 00022 // The copyright notices in the Software and this entire statement, including 00023 // the above license grant, this restriction and the following disclaimer, 00024 // must be included in all copies of the Software, in whole or in part, and 00025 // all derivative works of the Software, unless such copies or derivative 00026 // works are solely in the form of machine-executable object code generated by 00027 // a source language processor. 00028 // 00029 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00030 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00031 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00032 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00033 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00034 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00035 // DEALINGS IN THE SOFTWARE. 00036 // 00037 00038 00039 #ifndef Foundation_RWLock_INCLUDED 00040 #define Foundation_RWLock_INCLUDED 00041 00042 00043 #include "Poco/Foundation.h" 00044 #include "Poco/Exception.h" 00045 00046 00047 #if defined(POCO_OS_FAMILY_WINDOWS) 00048 #include "Poco/RWLock_WIN32.h" 00049 #else 00050 #include "Poco/RWLock_POSIX.h" 00051 #endif 00052 00053 00054 namespace Poco { 00055 00056 00057 class ScopedRWLock; 00058 00059 00060 class Foundation_API RWLock: private RWLockImpl 00063 { 00064 public: 00065 typedef ScopedRWLock ScopedLock; 00066 00067 RWLock(); 00069 00070 ~RWLock(); 00072 00073 void readLock(); 00076 00077 bool tryReadLock(); 00080 00081 void writeLock(); 00085 00086 bool tryWriteLock(); 00091 00092 void unlock(); 00094 00095 private: 00096 RWLock(const RWLock&); 00097 RWLock& operator = (const RWLock&); 00098 }; 00099 00100 00101 class Foundation_API ScopedRWLock 00103 { 00104 public: 00105 ScopedRWLock(RWLock& rwl, bool write = false); 00106 ~ScopedRWLock(); 00107 00108 private: 00109 RWLock& _rwl; 00110 00111 ScopedRWLock(); 00112 ScopedRWLock(const ScopedRWLock&); 00113 ScopedRWLock& operator = (const ScopedRWLock&); 00114 }; 00115 00116 00117 // 00118 // inlines 00119 // 00120 inline void RWLock::readLock() 00121 { 00122 readLockImpl(); 00123 } 00124 00125 00126 inline bool RWLock::tryReadLock() 00127 { 00128 return tryReadLockImpl(); 00129 } 00130 00131 00132 inline void RWLock::writeLock() 00133 { 00134 writeLockImpl(); 00135 } 00136 00137 00138 inline bool RWLock::tryWriteLock() 00139 { 00140 return tryWriteLockImpl(); 00141 } 00142 00143 00144 inline void RWLock::unlock() 00145 { 00146 unlockImpl(); 00147 } 00148 00149 00150 inline ScopedRWLock::ScopedRWLock(RWLock& rwl, bool write): _rwl(rwl) 00151 { 00152 if (write) 00153 _rwl.writeLock(); 00154 else 00155 _rwl.readLock(); 00156 } 00157 00158 00159 inline ScopedRWLock::~ScopedRWLock() 00160 { 00161 _rwl.unlock(); 00162 } 00163 00164 00165 } // namespace Poco 00166 00167 00168 #endif // Foundation_RWLock_INCLUDED