rw_lock.h 3.7 KB

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