recursive_mutex.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "internal/_deprecated_header_message_guard.h"
  14. #if !defined(__TBB_show_deprecation_message_recursive_mutex_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_recursive_mutex_H
  16. #pragma message("TBB Warning: tbb/recursive_mutex.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
  17. #endif
  18. #if defined(__TBB_show_deprecated_header_message)
  19. #undef __TBB_show_deprecated_header_message
  20. #endif
  21. #ifndef __TBB_recursive_mutex_H
  22. #define __TBB_recursive_mutex_H
  23. #define __TBB_recursive_mutex_H_include_area
  24. #include "internal/_warning_suppress_enable_notice.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 __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::recursive_mutex is deprecated, use std::recursive_mutex")
  39. recursive_mutex : internal::mutex_copy_deprecated_and_disabled {
  40. public:
  41. //! Construct unacquired recursive_mutex.
  42. recursive_mutex() {
  43. #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
  44. internal_construct();
  45. #else
  46. #if _WIN32||_WIN64
  47. InitializeCriticalSectionEx(&impl, 4000, 0);
  48. #else
  49. pthread_mutexattr_t mtx_attr;
  50. int error_code = pthread_mutexattr_init( &mtx_attr );
  51. if( error_code )
  52. tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed");
  53. pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
  54. error_code = pthread_mutex_init( &impl, &mtx_attr );
  55. if( error_code )
  56. tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed");
  57. pthread_mutexattr_destroy( &mtx_attr );
  58. #endif /* _WIN32||_WIN64*/
  59. #endif /* TBB_USE_ASSERT */
  60. };
  61. ~recursive_mutex() {
  62. #if TBB_USE_ASSERT
  63. internal_destroy();
  64. #else
  65. #if _WIN32||_WIN64
  66. DeleteCriticalSection(&impl);
  67. #else
  68. pthread_mutex_destroy(&impl);
  69. #endif /* _WIN32||_WIN64 */
  70. #endif /* TBB_USE_ASSERT */
  71. };
  72. class scoped_lock;
  73. friend class scoped_lock;
  74. //! The scoped locking pattern
  75. /** It helps to avoid the common problem of forgetting to release lock.
  76. It also nicely provides the "node" for queuing locks. */
  77. class scoped_lock: internal::no_copy {
  78. public:
  79. //! Construct lock that has not acquired a recursive_mutex.
  80. scoped_lock() : my_mutex(NULL) {};
  81. //! Acquire lock on given mutex.
  82. scoped_lock( recursive_mutex& mutex ) {
  83. #if TBB_USE_ASSERT
  84. my_mutex = &mutex;
  85. #endif /* TBB_USE_ASSERT */
  86. acquire( mutex );
  87. }
  88. //! Release lock (if lock is held).
  89. ~scoped_lock() {
  90. if( my_mutex )
  91. release();
  92. }
  93. //! Acquire lock on given mutex.
  94. void acquire( recursive_mutex& mutex ) {
  95. #if TBB_USE_ASSERT
  96. internal_acquire( mutex );
  97. #else
  98. my_mutex = &mutex;
  99. mutex.lock();
  100. #endif /* TBB_USE_ASSERT */
  101. }
  102. //! Try acquire lock on given recursive_mutex.
  103. bool try_acquire( recursive_mutex& mutex ) {
  104. #if TBB_USE_ASSERT
  105. return internal_try_acquire( mutex );
  106. #else
  107. bool result = mutex.try_lock();
  108. if( result )
  109. my_mutex = &mutex;
  110. return result;
  111. #endif /* TBB_USE_ASSERT */
  112. }
  113. //! Release lock
  114. void release() {
  115. #if TBB_USE_ASSERT
  116. internal_release();
  117. #else
  118. my_mutex->unlock();
  119. my_mutex = NULL;
  120. #endif /* TBB_USE_ASSERT */
  121. }
  122. private:
  123. //! The pointer to the current recursive_mutex to work
  124. recursive_mutex* my_mutex;
  125. //! All checks from acquire using mutex.state were moved here
  126. void __TBB_EXPORTED_METHOD internal_acquire( recursive_mutex& m );
  127. //! All checks from try_acquire using mutex.state were moved here
  128. bool __TBB_EXPORTED_METHOD internal_try_acquire( recursive_mutex& m );
  129. //! All checks from release using mutex.state were moved here
  130. void __TBB_EXPORTED_METHOD internal_release();
  131. friend class recursive_mutex;
  132. };
  133. // Mutex traits
  134. static const bool is_rw_mutex = false;
  135. static const bool is_recursive_mutex = true;
  136. static const bool is_fair_mutex = false;
  137. // C++0x compatibility interface
  138. //! Acquire lock
  139. void lock() {
  140. #if TBB_USE_ASSERT
  141. aligned_space<scoped_lock> tmp;
  142. new(tmp.begin()) scoped_lock(*this);
  143. #else
  144. #if _WIN32||_WIN64
  145. EnterCriticalSection(&impl);
  146. #else
  147. int error_code = pthread_mutex_lock(&impl);
  148. if( error_code )
  149. tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_lock failed");
  150. #endif /* _WIN32||_WIN64 */
  151. #endif /* TBB_USE_ASSERT */
  152. }
  153. //! Try acquiring lock (non-blocking)
  154. /** Return true if lock acquired; false otherwise. */
  155. bool try_lock() {
  156. #if TBB_USE_ASSERT
  157. aligned_space<scoped_lock> tmp;
  158. return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
  159. #else
  160. #if _WIN32||_WIN64
  161. return TryEnterCriticalSection(&impl)!=0;
  162. #else
  163. return pthread_mutex_trylock(&impl)==0;
  164. #endif /* _WIN32||_WIN64 */
  165. #endif /* TBB_USE_ASSERT */
  166. }
  167. //! Release lock
  168. void unlock() {
  169. #if TBB_USE_ASSERT
  170. aligned_space<scoped_lock> tmp;
  171. scoped_lock& s = *tmp.begin();
  172. s.my_mutex = this;
  173. s.internal_release();
  174. #else
  175. #if _WIN32||_WIN64
  176. LeaveCriticalSection(&impl);
  177. #else
  178. pthread_mutex_unlock(&impl);
  179. #endif /* _WIN32||_WIN64 */
  180. #endif /* TBB_USE_ASSERT */
  181. }
  182. //! Return native_handle
  183. #if _WIN32||_WIN64
  184. typedef LPCRITICAL_SECTION native_handle_type;
  185. #else
  186. typedef pthread_mutex_t* native_handle_type;
  187. #endif
  188. native_handle_type native_handle() { return (native_handle_type) &impl; }
  189. private:
  190. #if _WIN32||_WIN64
  191. CRITICAL_SECTION impl;
  192. enum state_t {
  193. INITIALIZED=0x1234,
  194. DESTROYED=0x789A,
  195. } state;
  196. #else
  197. pthread_mutex_t impl;
  198. #endif /* _WIN32||_WIN64 */
  199. //! All checks from mutex constructor using mutex.state were moved here
  200. void __TBB_EXPORTED_METHOD internal_construct();
  201. //! All checks from mutex destructor using mutex.state were moved here
  202. void __TBB_EXPORTED_METHOD internal_destroy();
  203. };
  204. __TBB_DEFINE_PROFILING_SET_NAME(recursive_mutex)
  205. } // namespace tbb
  206. #include "internal/_warning_suppress_disable_notice.h"
  207. #undef __TBB_recursive_mutex_H_include_area
  208. #endif /* __TBB_recursive_mutex_H */