mutex.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2005-2015 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks. Threading Building Blocks is free software;
  4. you can redistribute it and/or modify it under the terms of the GNU General Public License
  5. version 2 as published by the Free Software Foundation. Threading Building Blocks is
  6. distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the GNU General Public License for more details. You should have received a copy of
  9. the GNU General Public License along with Threading Building Blocks; if not, write to the
  10. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  11. As a special exception, you may use this file as part of a free software library without
  12. restriction. Specifically, if other files instantiate templates or use macros or inline
  13. functions from this file, or you compile this file and link it with other files to produce
  14. an executable, this file does not by itself cause the resulting executable to be covered
  15. by the GNU General Public License. This exception does not however invalidate any other
  16. reasons why the executable file might be covered by the GNU General Public License.
  17. */
  18. #ifndef __TBB_mutex_H
  19. #define __TBB_mutex_H
  20. #if _WIN32||_WIN64
  21. #include "machine/windows_api.h"
  22. #else
  23. #include <pthread.h>
  24. #endif /* _WIN32||_WIN64 */
  25. #include <new>
  26. #include "aligned_space.h"
  27. #include "tbb_stddef.h"
  28. #include "tbb_profiling.h"
  29. namespace tbb {
  30. //! Wrapper around the platform's native reader-writer lock.
  31. /** For testing purposes only.
  32. @ingroup synchronization */
  33. class mutex : internal::mutex_copy_deprecated_and_disabled {
  34. public:
  35. //! Construct unacquired mutex.
  36. mutex() {
  37. #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
  38. internal_construct();
  39. #else
  40. #if _WIN32||_WIN64
  41. InitializeCriticalSectionEx(&impl, 4000, 0);
  42. #else
  43. int error_code = pthread_mutex_init(&impl,NULL);
  44. if( error_code )
  45. tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
  46. #endif /* _WIN32||_WIN64*/
  47. #endif /* TBB_USE_ASSERT */
  48. };
  49. ~mutex() {
  50. #if TBB_USE_ASSERT
  51. internal_destroy();
  52. #else
  53. #if _WIN32||_WIN64
  54. DeleteCriticalSection(&impl);
  55. #else
  56. pthread_mutex_destroy(&impl);
  57. #endif /* _WIN32||_WIN64 */
  58. #endif /* TBB_USE_ASSERT */
  59. };
  60. class scoped_lock;
  61. friend class scoped_lock;
  62. //! The scoped locking pattern
  63. /** It helps to avoid the common problem of forgetting to release lock.
  64. It also nicely provides the "node" for queuing locks. */
  65. class scoped_lock : internal::no_copy {
  66. public:
  67. //! Construct lock that has not acquired a mutex.
  68. scoped_lock() : my_mutex(NULL) {};
  69. //! Acquire lock on given mutex.
  70. scoped_lock( mutex& mutex ) {
  71. acquire( mutex );
  72. }
  73. //! Release lock (if lock is held).
  74. ~scoped_lock() {
  75. if( my_mutex )
  76. release();
  77. }
  78. //! Acquire lock on given mutex.
  79. void acquire( mutex& mutex ) {
  80. #if TBB_USE_ASSERT
  81. internal_acquire(mutex);
  82. #else
  83. mutex.lock();
  84. my_mutex = &mutex;
  85. #endif /* TBB_USE_ASSERT */
  86. }
  87. //! Try acquire lock on given mutex.
  88. bool try_acquire( mutex& mutex ) {
  89. #if TBB_USE_ASSERT
  90. return internal_try_acquire (mutex);
  91. #else
  92. bool result = mutex.try_lock();
  93. if( result )
  94. my_mutex = &mutex;
  95. return result;
  96. #endif /* TBB_USE_ASSERT */
  97. }
  98. //! Release lock
  99. void release() {
  100. #if TBB_USE_ASSERT
  101. internal_release ();
  102. #else
  103. my_mutex->unlock();
  104. my_mutex = NULL;
  105. #endif /* TBB_USE_ASSERT */
  106. }
  107. private:
  108. //! The pointer to the current mutex to work
  109. mutex* my_mutex;
  110. //! All checks from acquire using mutex.state were moved here
  111. void __TBB_EXPORTED_METHOD internal_acquire( mutex& m );
  112. //! All checks from try_acquire using mutex.state were moved here
  113. bool __TBB_EXPORTED_METHOD internal_try_acquire( mutex& m );
  114. //! All checks from release using mutex.state were moved here
  115. void __TBB_EXPORTED_METHOD internal_release();
  116. friend class mutex;
  117. };
  118. // Mutex traits
  119. static const bool is_rw_mutex = false;
  120. static const bool is_recursive_mutex = false;
  121. static const bool is_fair_mutex = false;
  122. // ISO C++0x compatibility methods
  123. //! Acquire lock
  124. void lock() {
  125. #if TBB_USE_ASSERT
  126. aligned_space<scoped_lock> tmp;
  127. new(tmp.begin()) scoped_lock(*this);
  128. #else
  129. #if _WIN32||_WIN64
  130. EnterCriticalSection(&impl);
  131. #else
  132. int error_code = pthread_mutex_lock(&impl);
  133. if( error_code )
  134. tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_lock failed");
  135. #endif /* _WIN32||_WIN64 */
  136. #endif /* TBB_USE_ASSERT */
  137. }
  138. //! Try acquiring lock (non-blocking)
  139. /** Return true if lock acquired; false otherwise. */
  140. bool try_lock() {
  141. #if TBB_USE_ASSERT
  142. aligned_space<scoped_lock> tmp;
  143. scoped_lock& s = *tmp.begin();
  144. s.my_mutex = NULL;
  145. return s.internal_try_acquire(*this);
  146. #else
  147. #if _WIN32||_WIN64
  148. return TryEnterCriticalSection(&impl)!=0;
  149. #else
  150. return pthread_mutex_trylock(&impl)==0;
  151. #endif /* _WIN32||_WIN64 */
  152. #endif /* TBB_USE_ASSERT */
  153. }
  154. //! Release lock
  155. void unlock() {
  156. #if TBB_USE_ASSERT
  157. aligned_space<scoped_lock> tmp;
  158. scoped_lock& s = *tmp.begin();
  159. s.my_mutex = this;
  160. s.internal_release();
  161. #else
  162. #if _WIN32||_WIN64
  163. LeaveCriticalSection(&impl);
  164. #else
  165. pthread_mutex_unlock(&impl);
  166. #endif /* _WIN32||_WIN64 */
  167. #endif /* TBB_USE_ASSERT */
  168. }
  169. //! Return native_handle
  170. #if _WIN32||_WIN64
  171. typedef LPCRITICAL_SECTION native_handle_type;
  172. #else
  173. typedef pthread_mutex_t* native_handle_type;
  174. #endif
  175. native_handle_type native_handle() { return (native_handle_type) &impl; }
  176. enum state_t {
  177. INITIALIZED=0x1234,
  178. DESTROYED=0x789A,
  179. HELD=0x56CD
  180. };
  181. private:
  182. #if _WIN32||_WIN64
  183. CRITICAL_SECTION impl;
  184. enum state_t state;
  185. #else
  186. pthread_mutex_t impl;
  187. #endif /* _WIN32||_WIN64 */
  188. //! All checks from mutex constructor using mutex.state were moved here
  189. void __TBB_EXPORTED_METHOD internal_construct();
  190. //! All checks from mutex destructor using mutex.state were moved here
  191. void __TBB_EXPORTED_METHOD internal_destroy();
  192. #if _WIN32||_WIN64
  193. public:
  194. //! Set the internal state
  195. void set_state( state_t to ) { state = to; }
  196. #endif
  197. };
  198. __TBB_DEFINE_PROFILING_SET_NAME(mutex)
  199. } // namespace tbb
  200. #endif /* __TBB_mutex_H */