rw_lock.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**************************************************************************/
  2. /* rw_lock.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 RW_LOCK_H
  31. #define RW_LOCK_H
  32. #include "core/typedefs.h"
  33. #ifdef MINGW_ENABLED
  34. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  35. #include "thirdparty/mingw-std-threads/mingw.shared_mutex.h"
  36. #define THREADING_NAMESPACE mingw_stdthread
  37. #else
  38. #include <shared_mutex>
  39. #define THREADING_NAMESPACE std
  40. #endif
  41. class RWLock {
  42. mutable THREADING_NAMESPACE::shared_timed_mutex mutex;
  43. public:
  44. // Lock the RWLock, block if locked by someone else.
  45. _ALWAYS_INLINE_ void read_lock() const {
  46. mutex.lock_shared();
  47. }
  48. // Unlock the RWLock, let other threads continue.
  49. _ALWAYS_INLINE_ void read_unlock() const {
  50. mutex.unlock_shared();
  51. }
  52. // Attempt to lock the RWLock for reading. True on success, false means it can't lock.
  53. _ALWAYS_INLINE_ bool read_try_lock() const {
  54. return mutex.try_lock_shared();
  55. }
  56. // Lock the RWLock, block if locked by someone else.
  57. _ALWAYS_INLINE_ void write_lock() {
  58. mutex.lock();
  59. }
  60. // Unlock the RWLock, let other threads continue.
  61. _ALWAYS_INLINE_ void write_unlock() {
  62. mutex.unlock();
  63. }
  64. // Attempt to lock the RWLock for writing. True on success, false means it can't lock.
  65. _ALWAYS_INLINE_ bool write_try_lock() {
  66. return mutex.try_lock();
  67. }
  68. };
  69. class RWLockRead {
  70. const RWLock &lock;
  71. public:
  72. _ALWAYS_INLINE_ RWLockRead(const RWLock &p_lock) :
  73. lock(p_lock) {
  74. lock.read_lock();
  75. }
  76. _ALWAYS_INLINE_ ~RWLockRead() {
  77. lock.read_unlock();
  78. }
  79. };
  80. class RWLockWrite {
  81. RWLock &lock;
  82. public:
  83. _ALWAYS_INLINE_ RWLockWrite(RWLock &p_lock) :
  84. lock(p_lock) {
  85. lock.write_lock();
  86. }
  87. _ALWAYS_INLINE_ ~RWLockWrite() {
  88. lock.write_unlock();
  89. }
  90. };
  91. #endif // RW_LOCK_H