rw_lock.h 3.5 KB

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