mutex.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**************************************************************************/
  2. /* 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 MUTEX_H
  31. #define MUTEX_H
  32. #include "core/error/error_macros.h"
  33. #include "core/typedefs.h"
  34. #include <mutex>
  35. template <class MutexT>
  36. class MutexLock;
  37. template <class StdMutexT>
  38. class MutexImpl {
  39. friend class MutexLock<MutexImpl<StdMutexT>>;
  40. using StdMutexType = StdMutexT;
  41. mutable StdMutexT mutex;
  42. public:
  43. _ALWAYS_INLINE_ void lock() const {
  44. mutex.lock();
  45. }
  46. _ALWAYS_INLINE_ void unlock() const {
  47. mutex.unlock();
  48. }
  49. _ALWAYS_INLINE_ bool try_lock() const {
  50. return mutex.try_lock();
  51. }
  52. };
  53. // A very special kind of mutex, used in scenarios where these
  54. // requirements hold at the same time:
  55. // - Must be used with a condition variable (only binary mutexes are suitable).
  56. // - Must have recursive semnantics (or simulate, as this one does).
  57. // The implementation keeps the lock count in TS. Therefore, only
  58. // one object of each version of the template can exists; hence the Tag argument.
  59. // Tags must be unique across the Godot codebase.
  60. // Also, don't forget to declare the thread_local variable on each use.
  61. template <int Tag>
  62. class SafeBinaryMutex {
  63. friend class MutexLock<SafeBinaryMutex>;
  64. using StdMutexType = std::mutex;
  65. mutable std::mutex mutex;
  66. static thread_local uint32_t count;
  67. public:
  68. _ALWAYS_INLINE_ void lock() const {
  69. if (++count == 1) {
  70. mutex.lock();
  71. }
  72. }
  73. _ALWAYS_INLINE_ void unlock() const {
  74. DEV_ASSERT(count);
  75. if (--count == 0) {
  76. mutex.unlock();
  77. }
  78. }
  79. _ALWAYS_INLINE_ bool try_lock() const {
  80. if (count) {
  81. count++;
  82. return true;
  83. } else {
  84. if (mutex.try_lock()) {
  85. count++;
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. }
  92. ~SafeBinaryMutex() {
  93. DEV_ASSERT(!count);
  94. }
  95. };
  96. template <class MutexT>
  97. class MutexLock {
  98. friend class ConditionVariable;
  99. std::unique_lock<typename MutexT::StdMutexType> lock;
  100. public:
  101. _ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) :
  102. lock(p_mutex.mutex){};
  103. };
  104. // This specialization is needed so manual locking and MutexLock can be used
  105. // at the same time on a SafeBinaryMutex.
  106. template <int Tag>
  107. class MutexLock<SafeBinaryMutex<Tag>> {
  108. friend class ConditionVariable;
  109. std::unique_lock<std::mutex> lock;
  110. public:
  111. _ALWAYS_INLINE_ explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
  112. lock(p_mutex.mutex) {
  113. SafeBinaryMutex<Tag>::count++;
  114. };
  115. _ALWAYS_INLINE_ ~MutexLock() {
  116. SafeBinaryMutex<Tag>::count--;
  117. };
  118. };
  119. using Mutex = MutexImpl<std::recursive_mutex>; // Recursive, for general use
  120. using BinaryMutex = MutexImpl<std::mutex>; // Non-recursive, handle with care
  121. extern template class MutexImpl<std::recursive_mutex>;
  122. extern template class MutexImpl<std::mutex>;
  123. extern template class MutexLock<MutexImpl<std::recursive_mutex>>;
  124. extern template class MutexLock<MutexImpl<std::mutex>>;
  125. #endif // MUTEX_H