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.

141 lines
4.4 KiB

  1. /*
  2. Copyright 2005-2013 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_CRITICAL_SECTION_H_
  24. #define _TBB_CRITICAL_SECTION_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. typedef internal::critical_section_v4 critical_section;
  118. __TBB_DEFINE_PROFILING_SET_NAME(critical_section)
  119. } // namespace tbb
  120. #endif // _TBB_CRITICAL_SECTION_H_