safe_binary_mutex.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* safe_binary_mutex.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/os/mutex.h"
  33. #include "core/typedefs.h"
  34. #ifdef THREADS_ENABLED
  35. GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wundefined-var-template")
  36. // A very special kind of mutex, used in scenarios where these
  37. // requirements hold at the same time:
  38. // - Must be used with a condition variable (only binary mutexes are suitable).
  39. // - Must have recursive semnantics (or simulate, as this one does).
  40. // The implementation keeps the lock count in TS. Therefore, only
  41. // one object of each version of the template can exists; hence the Tag argument.
  42. // Tags must be unique across the Godot codebase.
  43. // Also, don't forget to declare the thread_local variable on each use.
  44. template <int Tag>
  45. class SafeBinaryMutex {
  46. friend class MutexLock<SafeBinaryMutex<Tag>>;
  47. using StdMutexType = THREADING_NAMESPACE::mutex;
  48. mutable THREADING_NAMESPACE::mutex mutex;
  49. struct TLSData {
  50. mutable THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
  51. uint32_t count = 0;
  52. TLSData(SafeBinaryMutex<Tag> &p_mutex) :
  53. lock(p_mutex.mutex, THREADING_NAMESPACE::defer_lock) {}
  54. };
  55. static thread_local TLSData tls_data;
  56. public:
  57. _ALWAYS_INLINE_ void lock() const {
  58. if (++tls_data.count == 1) {
  59. tls_data.lock.lock();
  60. }
  61. }
  62. _ALWAYS_INLINE_ void unlock() const {
  63. DEV_ASSERT(tls_data.count);
  64. if (--tls_data.count == 0) {
  65. tls_data.lock.unlock();
  66. }
  67. }
  68. _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock() const {
  69. return const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(tls_data.lock);
  70. }
  71. _ALWAYS_INLINE_ SafeBinaryMutex() {
  72. }
  73. _ALWAYS_INLINE_ ~SafeBinaryMutex() {
  74. DEV_ASSERT(!tls_data.count);
  75. }
  76. };
  77. template <int Tag>
  78. class MutexLock<SafeBinaryMutex<Tag>> {
  79. friend class ConditionVariable;
  80. const SafeBinaryMutex<Tag> &mutex;
  81. public:
  82. explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
  83. mutex(p_mutex) {
  84. mutex.lock();
  85. }
  86. ~MutexLock() {
  87. mutex.unlock();
  88. }
  89. _ALWAYS_INLINE_ void temp_relock() const {
  90. mutex.lock();
  91. }
  92. _ALWAYS_INLINE_ void temp_unlock() const {
  93. mutex.unlock();
  94. }
  95. // TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
  96. };
  97. GODOT_CLANG_WARNING_POP
  98. #else // No threads.
  99. template <int Tag>
  100. class SafeBinaryMutex {
  101. struct TLSData {
  102. TLSData(SafeBinaryMutex<Tag> &p_mutex) {}
  103. };
  104. static thread_local TLSData tls_data;
  105. public:
  106. void lock() const {}
  107. void unlock() const {}
  108. };
  109. template <int Tag>
  110. class MutexLock<SafeBinaryMutex<Tag>> {
  111. public:
  112. MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
  113. ~MutexLock() {}
  114. void temp_relock() const {}
  115. void temp_unlock() const {}
  116. };
  117. #endif // THREADS_ENABLED