critical_section.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_critical_section_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_critical_section_H
  16. #pragma message("TBB Warning: tbb/critical_section.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_CRITICAL_SECTION_H_
  22. #define _TBB_CRITICAL_SECTION_H_
  23. #define __TBB_critical_section_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. #include <errno.h>
  30. #endif // _WIN32||WIN64
  31. #include "tbb_stddef.h"
  32. #include "tbb_thread.h"
  33. #include "tbb_exception.h"
  34. #include "tbb_profiling.h"
  35. namespace tbb {
  36. namespace internal {
  37. class critical_section_v4 : internal::no_copy {
  38. #if _WIN32||_WIN64
  39. CRITICAL_SECTION my_impl;
  40. #else
  41. pthread_mutex_t my_impl;
  42. #endif
  43. tbb_thread::id my_tid;
  44. public:
  45. void __TBB_EXPORTED_METHOD internal_construct();
  46. critical_section_v4() {
  47. #if _WIN32||_WIN64
  48. InitializeCriticalSectionEx( &my_impl, 4000, 0 );
  49. #else
  50. pthread_mutex_init(&my_impl, NULL);
  51. #endif
  52. internal_construct();
  53. }
  54. ~critical_section_v4() {
  55. __TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
  56. #if _WIN32||_WIN64
  57. DeleteCriticalSection(&my_impl);
  58. #else
  59. pthread_mutex_destroy(&my_impl);
  60. #endif
  61. }
  62. class scoped_lock : internal::no_copy {
  63. private:
  64. critical_section_v4 &my_crit;
  65. public:
  66. scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
  67. my_crit.lock();
  68. }
  69. ~scoped_lock() {
  70. my_crit.unlock();
  71. }
  72. };
  73. void lock() {
  74. tbb_thread::id local_tid = this_tbb_thread::get_id();
  75. if(local_tid == my_tid) throw_exception( eid_improper_lock );
  76. #if _WIN32||_WIN64
  77. EnterCriticalSection( &my_impl );
  78. #else
  79. int rval = pthread_mutex_lock(&my_impl);
  80. __TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
  81. #endif
  82. __TBB_ASSERT(my_tid == tbb_thread::id(), NULL);
  83. my_tid = local_tid;
  84. }
  85. bool try_lock() {
  86. bool gotlock;
  87. tbb_thread::id local_tid = this_tbb_thread::get_id();
  88. if(local_tid == my_tid) return false;
  89. #if _WIN32||_WIN64
  90. gotlock = TryEnterCriticalSection( &my_impl ) != 0;
  91. #else
  92. int rval = pthread_mutex_trylock(&my_impl);
  93. // valid returns are 0 (locked) and [EBUSY]
  94. __TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
  95. gotlock = rval == 0;
  96. #endif
  97. if(gotlock) {
  98. my_tid = local_tid;
  99. }
  100. return gotlock;
  101. }
  102. void unlock() {
  103. __TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
  104. my_tid = tbb_thread::id();
  105. #if _WIN32||_WIN64
  106. LeaveCriticalSection( &my_impl );
  107. #else
  108. int rval = pthread_mutex_unlock(&my_impl);
  109. __TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
  110. #endif
  111. }
  112. static const bool is_rw_mutex = false;
  113. static const bool is_recursive_mutex = false;
  114. static const bool is_fair_mutex = true;
  115. }; // critical_section_v4
  116. } // namespace internal
  117. __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::critical_section is deprecated, use std::mutex") typedef internal::critical_section_v4 critical_section;
  118. __TBB_DEFINE_PROFILING_SET_NAME(critical_section)
  119. } // namespace tbb
  120. #include "internal/_warning_suppress_disable_notice.h"
  121. #undef __TBB_critical_section_H_include_area
  122. #endif // _TBB_CRITICAL_SECTION_H_