rw_lock.h 3.8 KB

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