You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.1 KiB

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