safe_binary_mutex.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // 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>;
  47. using StdMutexType = THREADING_NAMESPACE::mutex;
  48. mutable THREADING_NAMESPACE::mutex mutex;
  49. static thread_local uint32_t count;
  50. public:
  51. _ALWAYS_INLINE_ void lock() const {
  52. if (++count == 1) {
  53. mutex.lock();
  54. }
  55. }
  56. _ALWAYS_INLINE_ void unlock() const {
  57. DEV_ASSERT(count);
  58. if (--count == 0) {
  59. mutex.unlock();
  60. }
  61. }
  62. _ALWAYS_INLINE_ bool try_lock() const {
  63. if (count) {
  64. count++;
  65. return true;
  66. } else {
  67. if (mutex.try_lock()) {
  68. count++;
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. }
  75. ~SafeBinaryMutex() {
  76. DEV_ASSERT(!count);
  77. }
  78. };
  79. // This specialization is needed so manual locking and MutexLock can be used
  80. // at the same time on a SafeBinaryMutex.
  81. template <int Tag>
  82. class MutexLock<SafeBinaryMutex<Tag>> {
  83. friend class ConditionVariable;
  84. THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> lock;
  85. public:
  86. _ALWAYS_INLINE_ explicit MutexLock(const SafeBinaryMutex<Tag> &p_mutex) :
  87. lock(p_mutex.mutex) {
  88. SafeBinaryMutex<Tag>::count++;
  89. };
  90. _ALWAYS_INLINE_ ~MutexLock() {
  91. SafeBinaryMutex<Tag>::count--;
  92. };
  93. };
  94. #else // No threads.
  95. template <int Tag>
  96. class SafeBinaryMutex : public MutexImpl {
  97. static thread_local uint32_t count;
  98. };
  99. template <int Tag>
  100. class MutexLock<SafeBinaryMutex<Tag>> {
  101. public:
  102. MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {}
  103. ~MutexLock() {}
  104. };
  105. #endif // THREADS_ENABLED
  106. #endif // SAFE_BINARY_MUTEX_H