00001 // 00002 // Mutex.h 00003 // 00004 // $Id: //poco/1.3/Foundation/include/Poco/Mutex.h#2 $ 00005 // 00006 // Library: Foundation 00007 // Package: Threading 00008 // Module: Mutex 00009 // 00010 // Definition of the Mutex and FastMutex classes. 00011 // 00012 // Copyright (c) 2004-2008, 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_Mutex_INCLUDED 00040 #define Foundation_Mutex_INCLUDED 00041 00042 00043 #include "Poco/Foundation.h" 00044 #include "Poco/Exception.h" 00045 #include "Poco/ScopedLock.h" 00046 00047 00048 #if defined(POCO_OS_FAMILY_WINDOWS) 00049 #include "Poco/Mutex_WIN32.h" 00050 #else 00051 #include "Poco/Mutex_POSIX.h" 00052 #endif 00053 00054 00055 namespace Poco { 00056 00057 00058 class Foundation_API Mutex: private MutexImpl 00067 { 00068 public: 00069 typedef Poco::ScopedLock<Mutex> ScopedLock; 00070 00071 Mutex(); 00073 00074 ~Mutex(); 00076 00077 void lock(); 00080 00081 void lock(long milliseconds); 00089 00090 bool tryLock(); 00094 00095 bool tryLock(long milliseconds); 00103 00104 void unlock(); 00107 00108 private: 00109 Mutex(const Mutex&); 00110 Mutex& operator = (const Mutex&); 00111 }; 00112 00113 00114 class Foundation_API FastMutex: private FastMutexImpl 00122 { 00123 public: 00124 typedef Poco::ScopedLock<FastMutex> ScopedLock; 00125 00126 FastMutex(); 00128 00129 ~FastMutex(); 00131 00132 void lock(); 00135 00136 void lock(long milliseconds); 00144 00145 bool tryLock(); 00149 00150 bool tryLock(long milliseconds); 00158 00159 void unlock(); 00162 00163 private: 00164 FastMutex(const FastMutex&); 00165 FastMutex& operator = (const FastMutex&); 00166 }; 00167 00168 00169 // 00170 // inlines 00171 // 00172 inline void Mutex::lock() 00173 { 00174 lockImpl(); 00175 } 00176 00177 00178 inline void Mutex::lock(long milliseconds) 00179 { 00180 if (!tryLockImpl(milliseconds)) 00181 throw TimeoutException(); 00182 } 00183 00184 00185 inline bool Mutex::tryLock() 00186 { 00187 return tryLockImpl(); 00188 } 00189 00190 00191 inline bool Mutex::tryLock(long milliseconds) 00192 { 00193 return tryLockImpl(milliseconds); 00194 } 00195 00196 00197 inline void Mutex::unlock() 00198 { 00199 unlockImpl(); 00200 } 00201 00202 00203 inline void FastMutex::lock() 00204 { 00205 lockImpl(); 00206 } 00207 00208 00209 inline void FastMutex::lock(long milliseconds) 00210 { 00211 if (!tryLockImpl(milliseconds)) 00212 throw TimeoutException(); 00213 } 00214 00215 00216 inline bool FastMutex::tryLock() 00217 { 00218 return tryLockImpl(); 00219 } 00220 00221 00222 inline bool FastMutex::tryLock(long milliseconds) 00223 { 00224 return tryLockImpl(milliseconds); 00225 } 00226 00227 00228 inline void FastMutex::unlock() 00229 { 00230 unlockImpl(); 00231 } 00232 00233 00234 } // namespace Poco 00235 00236 00237 #endif // Foundation_Mutex_INCLUDED