Mutex.h 6.2 KB

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