safe_binary_mutex.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef SAFE_BINARY_MUTEX_H
  31. #define SAFE_BINARY_MUTEX_H
  32. #include "core/error/error_macros.h"
  33. #include "core/os/mutex.h"
  34. #include "core/typedefs.h"
  35. #ifdef THREADS_ENABLED
  36. #ifdef __clang__
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wundefined-var-template"
  39. #endif
  40. // A very special kind of mutex, used in scenarios where these
  41. // requirements hold at the same time:
  42. // - Must be used with a condition variable (only binary mutexes are suitable).
  43. // - Must have recursive semnantics (or simulate, as this one does).
  44. // The implementation keeps the lock count in TS. Therefore, only
  45. // one object of each version of the template can exists; hence the Tag argument.
  46. // Tags must be unique across the Godot codebase.
  47. // Also, don't forget to declare the thread_local variable on each use.
  48. template <int Tag>
  49. class SafeBinaryMutex {
  50. friend class MutexLock<SafeBinaryMutex<Tag>>;
  51. using StdMutexType = THREADING_NAMESPACE::mutex;
  52. mutable THREADING_NAMESPACE::mutex mutex;
  53. struct TLSData {
  54. mutable THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
  55. uint32_t count = 0;
  56. TLSData(SafeBinaryMutex<Tag> &p_mutex) :
  57. lock(p_mutex.mutex, THREADING_NAMESPACE::defer_lock) {}
  58. };
  59. static thread_local TLSData tls_data;
  60. public:
  61. _ALWAYS_INLINE_ void lock() const {
  62. if (++tls_data.count == 1) {
  63. tls_data.lock.lock();
  64. }
  65. }
  66. _ALWAYS_INLINE_ void unlock() const {
  67. DEV_ASSERT(tls_data.count);
  68. if (--tls_data.count == 0) {
  69. tls_data.lock.unlock();
  70. }
  71. }
  72. _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock() const {
  73. return const_cast<THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &>(tls_data.lock);
  74. }
  75. _ALWAYS_INLINE_ SafeBinaryMutex() {
  76. }
  77. _ALWAYS_INLINE_ ~SafeBinaryMutex() {
  78. DEV_ASSERT(!tls_data.count);
  79. }
  80. };
  81. template <int Tag>
  82. class MutexLock<SafeBinaryMutex<Tag>> {
  83. friend class ConditionVariable;
  84. const SafeBinaryMutex<Tag> &mutex;
  85. public:
  86. explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
  87. mutex(p_mutex) {
  88. mutex.lock();
  89. }
  90. ~MutexLock() {
  91. mutex.unlock();
  92. }
  93. _ALWAYS_INLINE_ void temp_relock() const {
  94. mutex.lock();
  95. }
  96. _ALWAYS_INLINE_ void temp_unlock() const {
  97. mutex.unlock();
  98. }
  99. // TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
  100. };
  101. #ifdef __clang__
  102. #pragma clang diagnostic pop
  103. #endif
  104. #else // No threads.
  105. template <int Tag>
  106. class SafeBinaryMutex {
  107. struct TLSData {
  108. TLSData(SafeBinaryMutex<Tag> &p_mutex) {}
  109. };
  110. static thread_local TLSData tls_data;
  111. public:
  112. void lock() const {}
  113. void unlock() const {}
  114. };
  115. template <int Tag>
  116. class MutexLock<SafeBinaryMutex<Tag>> {
  117. public:
  118. MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
  119. ~MutexLock() {}
  120. void temp_relock() const {}
  121. void temp_unlock() const {}
  122. };
  123. #endif // THREADS_ENABLED
  124. #endif // SAFE_BINARY_MUTEX_H