mutex.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifdef MINGW_ENABLED
  35. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  36. #include "thirdparty/mingw-std-threads/mingw.mutex.h"
  37. #define THREADING_NAMESPACE mingw_stdthread
  38. #else
  39. #include <mutex>
  40. #define THREADING_NAMESPACE std
  41. #endif
  42. #ifdef THREADS_ENABLED
  43. template <typename MutexT>
  44. class MutexLock;
  45. template <typename StdMutexT>
  46. class MutexImpl {
  47. friend class MutexLock<MutexImpl<StdMutexT>>;
  48. using StdMutexType = StdMutexT;
  49. mutable StdMutexT mutex;
  50. public:
  51. _ALWAYS_INLINE_ void lock() const {
  52. mutex.lock();
  53. }
  54. _ALWAYS_INLINE_ void unlock() const {
  55. mutex.unlock();
  56. }
  57. _ALWAYS_INLINE_ bool try_lock() const {
  58. return mutex.try_lock();
  59. }
  60. };
  61. template <typename MutexT>
  62. class MutexLock {
  63. mutable THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
  64. public:
  65. explicit MutexLock(const MutexT &p_mutex) :
  66. lock(p_mutex.mutex) {}
  67. // Clarification: all the funny syntax is needed so this function exists only for binary mutexes.
  68. template <typename T = MutexT>
  69. _ALWAYS_INLINE_ THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &_get_lock(
  70. typename std::enable_if<std::is_same<T, THREADING_NAMESPACE::mutex>::value> * = nullptr) const {
  71. return lock;
  72. }
  73. _ALWAYS_INLINE_ void temp_relock() const {
  74. lock.lock();
  75. }
  76. _ALWAYS_INLINE_ void temp_unlock() const {
  77. lock.unlock();
  78. }
  79. // TODO: Implement a `try_temp_relock` if needed (will also need a dummy method below).
  80. };
  81. using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use
  82. using BinaryMutex = MutexImpl<THREADING_NAMESPACE::mutex>; // Non-recursive, handle with care
  83. extern template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>;
  84. extern template class MutexImpl<THREADING_NAMESPACE::mutex>;
  85. extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>;
  86. extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>;
  87. #else // No threads.
  88. class MutexImpl {
  89. mutable THREADING_NAMESPACE::mutex mutex;
  90. public:
  91. void lock() const {}
  92. void unlock() const {}
  93. bool try_lock() const { return true; }
  94. };
  95. template <typename MutexT>
  96. class MutexLock {
  97. public:
  98. MutexLock(const MutexT &p_mutex) {}
  99. void temp_relock() const {}
  100. void temp_unlock() const {}
  101. };
  102. using Mutex = MutexImpl;
  103. using BinaryMutex = MutexImpl;
  104. #endif // THREADS_ENABLED
  105. #endif // MUTEX_H