Mutex.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Mutex.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Mutex.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Mutex
  9. //
  10. // Definition of the Mutex and FastMutex classes.
  11. //
  12. // Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Mutex_INCLUDED
  18. #define Foundation_Mutex_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #include "Poco/ScopedLock.h"
  22. #if defined(POCO_OS_FAMILY_WINDOWS)
  23. #if defined(_WIN32_WCE)
  24. #include "Poco/Mutex_WINCE.h"
  25. #else
  26. #include "Poco/Mutex_WIN32.h"
  27. #endif
  28. #elif defined(POCO_VXWORKS)
  29. #include "Poco/Mutex_VX.h"
  30. #else
  31. #include "Poco/Mutex_POSIX.h"
  32. #endif
  33. namespace Poco {
  34. class Foundation_API Mutex: private MutexImpl
  35. /// A Mutex (mutual exclusion) is a synchronization
  36. /// mechanism used to control access to a shared resource
  37. /// in a concurrent (multithreaded) scenario.
  38. /// Mutexes are recursive, that is, the same mutex can be
  39. /// locked multiple times by the same thread (but, of course,
  40. /// not by other threads).
  41. /// Using the ScopedLock class is the preferred way to automatically
  42. /// lock and unlock a mutex.
  43. {
  44. public:
  45. typedef Poco::ScopedLock<Mutex> ScopedLock;
  46. Mutex();
  47. /// creates the Mutex.
  48. ~Mutex();
  49. /// destroys the Mutex.
  50. void lock();
  51. /// Locks the mutex. Blocks if the mutex
  52. /// is held by another thread.
  53. void lock(long milliseconds);
  54. /// Locks the mutex. Blocks up to the given number of milliseconds
  55. /// if the mutex is held by another thread. Throws a TimeoutException
  56. /// if the mutex can not be locked within the given timeout.
  57. ///
  58. /// Performance Note: On most platforms (including Windows), this member function is
  59. /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
  60. /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
  61. bool tryLock();
  62. /// Tries to lock the mutex. Returns false immediately
  63. /// if the mutex is already held by another thread.
  64. /// Returns true if the mutex was successfully locked.
  65. bool tryLock(long milliseconds);
  66. /// Locks the mutex. Blocks up to the given number of milliseconds
  67. /// if the mutex is held by another thread.
  68. /// Returns true if the mutex was successfully locked.
  69. ///
  70. /// Performance Note: On most platforms (including Windows), this member function is
  71. /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
  72. /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
  73. void unlock();
  74. /// Unlocks the mutex so that it can be acquired by
  75. /// other threads.
  76. private:
  77. Mutex(const Mutex&);
  78. Mutex& operator = (const Mutex&);
  79. };
  80. class Foundation_API FastMutex: private FastMutexImpl
  81. /// A FastMutex (mutual exclusion) is similar to a Mutex.
  82. /// Unlike a Mutex, however, a FastMutex is not recursive,
  83. /// which means that a deadlock will occur if the same
  84. /// thread tries to lock a mutex it has already locked again.
  85. /// Locking a FastMutex is faster than locking a recursive Mutex.
  86. /// Using the ScopedLock class is the preferred way to automatically
  87. /// lock and unlock a mutex.
  88. {
  89. public:
  90. typedef Poco::ScopedLock<FastMutex> ScopedLock;
  91. FastMutex();
  92. /// creates the Mutex.
  93. ~FastMutex();
  94. /// destroys the Mutex.
  95. void lock();
  96. /// Locks the mutex. Blocks if the mutex
  97. /// is held by another thread.
  98. void lock(long milliseconds);
  99. /// Locks the mutex. Blocks up to the given number of milliseconds
  100. /// if the mutex is held by another thread. Throws a TimeoutException
  101. /// if the mutex can not be locked within the given timeout.
  102. ///
  103. /// Performance Note: On most platforms (including Windows), this member function is
  104. /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
  105. /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
  106. bool tryLock();
  107. /// Tries to lock the mutex. Returns false immediately
  108. /// if the mutex is already held by another thread.
  109. /// Returns true if the mutex was successfully locked.
  110. bool tryLock(long milliseconds);
  111. /// Locks the mutex. Blocks up to the given number of milliseconds
  112. /// if the mutex is held by another thread.
  113. /// Returns true if the mutex was successfully locked.
  114. ///
  115. /// Performance Note: On most platforms (including Windows), this member function is
  116. /// implemented using a loop calling (the equivalent of) tryLock() and Thread::sleep().
  117. /// On POSIX platforms that support pthread_mutex_timedlock(), this is used.
  118. void unlock();
  119. /// Unlocks the mutex so that it can be acquired by
  120. /// other threads.
  121. private:
  122. FastMutex(const FastMutex&);
  123. FastMutex& operator = (const FastMutex&);
  124. };
  125. class Foundation_API NullMutex
  126. /// A NullMutex is an empty mutex implementation
  127. /// which performs no locking at all. Useful in policy driven design
  128. /// where the type of mutex used can be now a template parameter allowing the user to switch
  129. /// between thread-safe and not thread-safe depending on his need
  130. /// Works with the ScopedLock class
  131. {
  132. public:
  133. typedef Poco::ScopedLock<NullMutex> ScopedLock;
  134. NullMutex()
  135. /// Creates the NullMutex.
  136. {
  137. }
  138. ~NullMutex()
  139. /// Destroys the NullMutex.
  140. {
  141. }
  142. void lock()
  143. /// Does nothing.
  144. {
  145. }
  146. void lock(long)
  147. /// Does nothing.
  148. {
  149. }
  150. bool tryLock()
  151. /// Does nothing and always returns true.
  152. {
  153. return true;
  154. }
  155. bool tryLock(long)
  156. /// Does nothing and always returns true.
  157. {
  158. return true;
  159. }
  160. void unlock()
  161. /// Does nothing.
  162. {
  163. }
  164. };
  165. //
  166. // inlines
  167. //
  168. inline void Mutex::lock()
  169. {
  170. lockImpl();
  171. }
  172. inline void Mutex::lock(long milliseconds)
  173. {
  174. if (!tryLockImpl(milliseconds))
  175. throw TimeoutException();
  176. }
  177. inline bool Mutex::tryLock()
  178. {
  179. return tryLockImpl();
  180. }
  181. inline bool Mutex::tryLock(long milliseconds)
  182. {
  183. return tryLockImpl(milliseconds);
  184. }
  185. inline void Mutex::unlock()
  186. {
  187. unlockImpl();
  188. }
  189. inline void FastMutex::lock()
  190. {
  191. lockImpl();
  192. }
  193. inline void FastMutex::lock(long milliseconds)
  194. {
  195. if (!tryLockImpl(milliseconds))
  196. throw TimeoutException();
  197. }
  198. inline bool FastMutex::tryLock()
  199. {
  200. return tryLockImpl();
  201. }
  202. inline bool FastMutex::tryLock(long milliseconds)
  203. {
  204. return tryLockImpl(milliseconds);
  205. }
  206. inline void FastMutex::unlock()
  207. {
  208. unlockImpl();
  209. }
  210. } // namespace Poco
  211. #endif // Foundation_Mutex_INCLUDED