queuing_mutex.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #ifndef __TBB_queuing_mutex_H
  14. #define __TBB_queuing_mutex_H
  15. #define __TBB_queuing_mutex_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include <cstring>
  18. #include "atomic.h"
  19. #include "tbb_profiling.h"
  20. namespace tbb {
  21. //! Queuing mutex with local-only spinning.
  22. /** @ingroup synchronization */
  23. class queuing_mutex : internal::mutex_copy_deprecated_and_disabled {
  24. public:
  25. //! Construct unacquired mutex.
  26. queuing_mutex() {
  27. q_tail = NULL;
  28. #if TBB_USE_THREADING_TOOLS
  29. internal_construct();
  30. #endif
  31. }
  32. //! The scoped locking pattern
  33. /** It helps to avoid the common problem of forgetting to release lock.
  34. It also nicely provides the "node" for queuing locks. */
  35. class scoped_lock: internal::no_copy {
  36. //! Initialize fields to mean "no lock held".
  37. void initialize() {
  38. mutex = NULL;
  39. going = 0;
  40. #if TBB_USE_ASSERT
  41. internal::poison_pointer(next);
  42. #endif /* TBB_USE_ASSERT */
  43. }
  44. public:
  45. //! Construct lock that has not acquired a mutex.
  46. /** Equivalent to zero-initialization of *this. */
  47. scoped_lock() {initialize();}
  48. //! Acquire lock on given mutex.
  49. scoped_lock( queuing_mutex& m ) {
  50. initialize();
  51. acquire(m);
  52. }
  53. //! Release lock (if lock is held).
  54. ~scoped_lock() {
  55. if( mutex ) release();
  56. }
  57. //! Acquire lock on given mutex.
  58. void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m );
  59. //! Acquire lock on given mutex if free (i.e. non-blocking)
  60. bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m );
  61. //! Release lock.
  62. void __TBB_EXPORTED_METHOD release();
  63. private:
  64. //! The pointer to the mutex owned, or NULL if not holding a mutex.
  65. queuing_mutex* mutex;
  66. //! The pointer to the next competitor for a mutex
  67. scoped_lock *next;
  68. //! The local spin-wait variable
  69. /** Inverted (0 - blocked, 1 - acquired the mutex) for the sake of
  70. zero-initialization. Defining it as an entire word instead of
  71. a byte seems to help performance slightly. */
  72. uintptr_t going;
  73. };
  74. void __TBB_EXPORTED_METHOD internal_construct();
  75. // Mutex traits
  76. static const bool is_rw_mutex = false;
  77. static const bool is_recursive_mutex = false;
  78. static const bool is_fair_mutex = true;
  79. private:
  80. //! The last competitor requesting the lock
  81. atomic<scoped_lock*> q_tail;
  82. };
  83. __TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex)
  84. } // namespace tbb
  85. #include "internal/_warning_suppress_disable_notice.h"
  86. #undef __TBB_queuing_mutex_H_include_area
  87. #endif /* __TBB_queuing_mutex_H */