mutex.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_mutex_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_mutex_H
  16. #pragma message("TBB Warning: tbb/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_mutex_H
  22. #define __TBB_mutex_H
  23. #define __TBB_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. //! Wrapper around the platform's native lock.
  36. /** @ingroup synchronization */
  37. class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::critical_section is deprecated, use std::mutex") mutex : internal::mutex_copy_deprecated_and_disabled {
  38. public:
  39. //! Construct unacquired mutex.
  40. mutex() {
  41. #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
  42. internal_construct();
  43. #else
  44. #if _WIN32||_WIN64
  45. InitializeCriticalSectionEx(&impl, 4000, 0);
  46. #else
  47. int error_code = pthread_mutex_init(&impl,NULL);
  48. if( error_code )
  49. tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
  50. #endif /* _WIN32||_WIN64*/
  51. #endif /* TBB_USE_ASSERT */
  52. };
  53. ~mutex() {
  54. #if TBB_USE_ASSERT
  55. internal_destroy();
  56. #else
  57. #if _WIN32||_WIN64
  58. DeleteCriticalSection(&impl);
  59. #else
  60. pthread_mutex_destroy(&impl);
  61. #endif /* _WIN32||_WIN64 */
  62. #endif /* TBB_USE_ASSERT */
  63. };
  64. class scoped_lock;
  65. friend class scoped_lock;
  66. //! The scoped locking pattern
  67. /** It helps to avoid the common problem of forgetting to release lock.
  68. It also nicely provides the "node" for queuing locks. */
  69. class scoped_lock : internal::no_copy {
  70. public:
  71. //! Construct lock that has not acquired a mutex.
  72. scoped_lock() : my_mutex(NULL) {};
  73. //! Acquire lock on given mutex.
  74. scoped_lock( mutex& mutex ) {
  75. acquire( mutex );
  76. }
  77. //! Release lock (if lock is held).
  78. ~scoped_lock() {
  79. if( my_mutex )
  80. release();
  81. }
  82. //! Acquire lock on given mutex.
  83. void acquire( mutex& mutex ) {
  84. #if TBB_USE_ASSERT
  85. internal_acquire(mutex);
  86. #else
  87. mutex.lock();
  88. my_mutex = &mutex;
  89. #endif /* TBB_USE_ASSERT */
  90. }
  91. //! Try acquire lock on given mutex.
  92. bool try_acquire( mutex& mutex ) {
  93. #if TBB_USE_ASSERT
  94. return internal_try_acquire (mutex);
  95. #else
  96. bool result = mutex.try_lock();
  97. if( result )
  98. my_mutex = &mutex;
  99. return result;
  100. #endif /* TBB_USE_ASSERT */
  101. }
  102. //! Release lock
  103. void release() {
  104. #if TBB_USE_ASSERT
  105. internal_release ();
  106. #else
  107. my_mutex->unlock();
  108. my_mutex = NULL;
  109. #endif /* TBB_USE_ASSERT */
  110. }
  111. private:
  112. //! The pointer to the current mutex to work
  113. mutex* my_mutex;
  114. //! All checks from acquire using mutex.state were moved here
  115. void __TBB_EXPORTED_METHOD internal_acquire( mutex& m );
  116. //! All checks from try_acquire using mutex.state were moved here
  117. bool __TBB_EXPORTED_METHOD internal_try_acquire( mutex& m );
  118. //! All checks from release using mutex.state were moved here
  119. void __TBB_EXPORTED_METHOD internal_release();
  120. friend class mutex;
  121. };
  122. // Mutex traits
  123. static const bool is_rw_mutex = false;
  124. static const bool is_recursive_mutex = false;
  125. static const bool is_fair_mutex = false;
  126. // ISO C++0x compatibility methods
  127. //! Acquire lock
  128. void lock() {
  129. #if TBB_USE_ASSERT
  130. aligned_space<scoped_lock> tmp;
  131. new(tmp.begin()) scoped_lock(*this);
  132. #else
  133. #if _WIN32||_WIN64
  134. EnterCriticalSection(&impl);
  135. #else
  136. int error_code = pthread_mutex_lock(&impl);
  137. if( error_code )
  138. tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_lock failed");
  139. #endif /* _WIN32||_WIN64 */
  140. #endif /* TBB_USE_ASSERT */
  141. }
  142. //! Try acquiring lock (non-blocking)
  143. /** Return true if lock acquired; false otherwise. */
  144. bool try_lock() {
  145. #if TBB_USE_ASSERT
  146. aligned_space<scoped_lock> tmp;
  147. scoped_lock& s = *tmp.begin();
  148. s.my_mutex = NULL;
  149. return s.internal_try_acquire(*this);
  150. #else
  151. #if _WIN32||_WIN64
  152. return TryEnterCriticalSection(&impl)!=0;
  153. #else
  154. return pthread_mutex_trylock(&impl)==0;
  155. #endif /* _WIN32||_WIN64 */
  156. #endif /* TBB_USE_ASSERT */
  157. }
  158. //! Release lock
  159. void unlock() {
  160. #if TBB_USE_ASSERT
  161. aligned_space<scoped_lock> tmp;
  162. scoped_lock& s = *tmp.begin();
  163. s.my_mutex = this;
  164. s.internal_release();
  165. #else
  166. #if _WIN32||_WIN64
  167. LeaveCriticalSection(&impl);
  168. #else
  169. pthread_mutex_unlock(&impl);
  170. #endif /* _WIN32||_WIN64 */
  171. #endif /* TBB_USE_ASSERT */
  172. }
  173. //! Return native_handle
  174. #if _WIN32||_WIN64
  175. typedef LPCRITICAL_SECTION native_handle_type;
  176. #else
  177. typedef pthread_mutex_t* native_handle_type;
  178. #endif
  179. native_handle_type native_handle() { return (native_handle_type) &impl; }
  180. enum state_t {
  181. INITIALIZED=0x1234,
  182. DESTROYED=0x789A,
  183. HELD=0x56CD
  184. };
  185. private:
  186. #if _WIN32||_WIN64
  187. CRITICAL_SECTION impl;
  188. enum state_t state;
  189. #else
  190. pthread_mutex_t impl;
  191. #endif /* _WIN32||_WIN64 */
  192. //! All checks from mutex constructor using mutex.state were moved here
  193. void __TBB_EXPORTED_METHOD internal_construct();
  194. //! All checks from mutex destructor using mutex.state were moved here
  195. void __TBB_EXPORTED_METHOD internal_destroy();
  196. #if _WIN32||_WIN64
  197. public:
  198. //! Set the internal state
  199. void set_state( state_t to ) { state = to; }
  200. #endif
  201. };
  202. __TBB_DEFINE_PROFILING_SET_NAME(mutex)
  203. } // namespace tbb
  204. #include "internal/_warning_suppress_disable_notice.h"
  205. #undef __TBB_mutex_H_include_area
  206. #endif /* __TBB_mutex_H */