mutex.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. friend class ConditionVariable;
  64. THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock;
  65. public:
  66. explicit MutexLock(const MutexT &p_mutex) :
  67. lock(p_mutex.mutex) {}
  68. };
  69. using Mutex = MutexImpl<THREADING_NAMESPACE::recursive_mutex>; // Recursive, for general use
  70. using BinaryMutex = MutexImpl<THREADING_NAMESPACE::mutex>; // Non-recursive, handle with care
  71. extern template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>;
  72. extern template class MutexImpl<THREADING_NAMESPACE::mutex>;
  73. extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>;
  74. extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>;
  75. #else // No threads.
  76. class MutexImpl {
  77. mutable THREADING_NAMESPACE::mutex mutex;
  78. public:
  79. void lock() const {}
  80. void unlock() const {}
  81. bool try_lock() const { return true; }
  82. };
  83. template <typename MutexT>
  84. class MutexLock {
  85. public:
  86. MutexLock(const MutexT &p_mutex) {}
  87. };
  88. using Mutex = MutexImpl;
  89. using BinaryMutex = MutexImpl;
  90. #endif // THREADS_ENABLED
  91. #endif // MUTEX_H