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.

131 lines
4.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_queuing_mutex_H
  24. #define __TBB_queuing_mutex_H
  25. #include "tbb_config.h"
  26. #if !TBB_USE_EXCEPTIONS && _MSC_VER
  27. // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
  28. #pragma warning (push)
  29. #pragma warning (disable: 4530)
  30. #endif
  31. #include <cstring>
  32. #if !TBB_USE_EXCEPTIONS && _MSC_VER
  33. #pragma warning (pop)
  34. #endif
  35. #include "atomic.h"
  36. #include "tbb_profiling.h"
  37. namespace tbb {
  38. //! Queuing mutex with local-only spinning.
  39. /** @ingroup synchronization */
  40. class queuing_mutex {
  41. public:
  42. //! Construct unacquired mutex.
  43. queuing_mutex() {
  44. q_tail = NULL;
  45. #if TBB_USE_THREADING_TOOLS
  46. internal_construct();
  47. #endif
  48. }
  49. //! The scoped locking pattern
  50. /** It helps to avoid the common problem of forgetting to release lock.
  51. It also nicely provides the "node" for queuing locks. */
  52. class scoped_lock: internal::no_copy {
  53. //! Initialize fields to mean "no lock held".
  54. void initialize() {
  55. mutex = NULL;
  56. #if TBB_USE_ASSERT
  57. internal::poison_pointer(next);
  58. #endif /* TBB_USE_ASSERT */
  59. }
  60. public:
  61. //! Construct lock that has not acquired a mutex.
  62. /** Equivalent to zero-initialization of *this. */
  63. scoped_lock() {initialize();}
  64. //! Acquire lock on given mutex.
  65. scoped_lock( queuing_mutex& m ) {
  66. initialize();
  67. acquire(m);
  68. }
  69. //! Release lock (if lock is held).
  70. ~scoped_lock() {
  71. if( mutex ) release();
  72. }
  73. //! Acquire lock on given mutex.
  74. void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m );
  75. //! Acquire lock on given mutex if free (i.e. non-blocking)
  76. bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m );
  77. //! Release lock.
  78. void __TBB_EXPORTED_METHOD release();
  79. private:
  80. //! The pointer to the mutex owned, or NULL if not holding a mutex.
  81. queuing_mutex* mutex;
  82. //! The pointer to the next competitor for a mutex
  83. scoped_lock *next;
  84. //! The local spin-wait variable
  85. /** Inverted (0 - blocked, 1 - acquired the mutex) for the sake of
  86. zero-initialization. Defining it as an entire word instead of
  87. a byte seems to help performance slightly. */
  88. uintptr_t going;
  89. };
  90. void __TBB_EXPORTED_METHOD internal_construct();
  91. // Mutex traits
  92. static const bool is_rw_mutex = false;
  93. static const bool is_recursive_mutex = false;
  94. static const bool is_fair_mutex = true;
  95. private:
  96. //! The last competitor requesting the lock
  97. atomic<scoped_lock*> q_tail;
  98. };
  99. __TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex)
  100. } // namespace tbb
  101. #endif /* __TBB_queuing_mutex_H */