rw_lock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* rw_lock.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 RWLOCK_H
  31. #define RWLOCK_H
  32. #include "core/error_list.h"
  33. #if !defined(NO_THREADS)
  34. #include <shared_mutex>
  35. class RWLock {
  36. mutable std::shared_timed_mutex mutex;
  37. public:
  38. // Lock the rwlock, block if locked by someone else
  39. void read_lock() const {
  40. mutex.lock_shared();
  41. }
  42. // Unlock the rwlock, let other threads continue
  43. void read_unlock() const {
  44. mutex.unlock_shared();
  45. }
  46. // Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock.
  47. Error read_try_lock() const {
  48. return mutex.try_lock_shared() ? OK : ERR_BUSY;
  49. }
  50. // Lock the rwlock, block if locked by someone else
  51. void write_lock() {
  52. mutex.lock();
  53. }
  54. // Unlock the rwlock, let other thwrites continue
  55. void write_unlock() {
  56. mutex.unlock();
  57. }
  58. // Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock.
  59. Error write_try_lock() {
  60. return mutex.try_lock() ? OK : ERR_BUSY;
  61. }
  62. };
  63. #else
  64. class RWLock {
  65. public:
  66. void read_lock() const {}
  67. void read_unlock() const {}
  68. Error read_try_lock() const { return OK; }
  69. void write_lock() {}
  70. void write_unlock() {}
  71. Error write_try_lock() { return OK; }
  72. };
  73. #endif
  74. class RWLockRead {
  75. const RWLock &lock;
  76. public:
  77. RWLockRead(const RWLock &p_lock) :
  78. lock(p_lock) {
  79. lock.read_lock();
  80. }
  81. ~RWLockRead() {
  82. lock.read_unlock();
  83. }
  84. };
  85. class RWLockWrite {
  86. RWLock &lock;
  87. public:
  88. RWLockWrite(RWLock &p_lock) :
  89. lock(p_lock) {
  90. lock.write_lock();
  91. }
  92. ~RWLockWrite() {
  93. lock.write_unlock();
  94. }
  95. };
  96. #endif // RWLOCK_H