RWMutex.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- RWMutex.h - Reader/Writer Mutual Exclusion Lock ----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the llvm::sys::RWMutex class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_RWMUTEX_H
  14. #define LLVM_SUPPORT_RWMUTEX_H
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/Threading.h"
  17. #include <cassert>
  18. namespace llvm
  19. {
  20. namespace sys
  21. {
  22. /// @brief Platform agnostic RWMutex class.
  23. class RWMutexImpl
  24. {
  25. /// @name Constructors
  26. /// @{
  27. public:
  28. /// Initializes the lock but doesn't acquire it.
  29. /// @brief Default Constructor.
  30. explicit RWMutexImpl();
  31. /// Releases and removes the lock
  32. /// @brief Destructor
  33. ~RWMutexImpl();
  34. /// @}
  35. /// @name Methods
  36. /// @{
  37. public:
  38. /// Attempts to unconditionally acquire the lock in reader mode. If the
  39. /// lock is held by a writer, this method will wait until it can acquire
  40. /// the lock.
  41. /// @returns false if any kind of error occurs, true otherwise.
  42. /// @brief Unconditionally acquire the lock in reader mode.
  43. bool reader_acquire();
  44. /// Attempts to release the lock in reader mode.
  45. /// @returns false if any kind of error occurs, true otherwise.
  46. /// @brief Unconditionally release the lock in reader mode.
  47. bool reader_release();
  48. /// Attempts to unconditionally acquire the lock in reader mode. If the
  49. /// lock is held by any readers, this method will wait until it can
  50. /// acquire the lock.
  51. /// @returns false if any kind of error occurs, true otherwise.
  52. /// @brief Unconditionally acquire the lock in writer mode.
  53. bool writer_acquire();
  54. /// Attempts to release the lock in writer mode.
  55. /// @returns false if any kind of error occurs, true otherwise.
  56. /// @brief Unconditionally release the lock in write mode.
  57. bool writer_release();
  58. //@}
  59. /// @name Platform Dependent Data
  60. /// @{
  61. private:
  62. #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
  63. void* data_; ///< We don't know what the data will be
  64. #endif
  65. /// @}
  66. /// @name Do Not Implement
  67. /// @{
  68. private:
  69. RWMutexImpl(const RWMutexImpl & original) = delete;
  70. void operator=(const RWMutexImpl &) = delete;
  71. /// @}
  72. };
  73. /// SmartMutex - An R/W mutex with a compile time constant parameter that
  74. /// indicates whether this mutex should become a no-op when we're not
  75. /// running in multithreaded mode.
  76. template<bool mt_only>
  77. class SmartRWMutex {
  78. RWMutexImpl impl;
  79. unsigned readers, writers;
  80. public:
  81. explicit SmartRWMutex() : impl(), readers(0), writers(0) { }
  82. bool lock_shared() {
  83. if (!mt_only || llvm_is_multithreaded())
  84. return impl.reader_acquire();
  85. // Single-threaded debugging code. This would be racy in multithreaded
  86. // mode, but provides not sanity checks in single threaded mode.
  87. ++readers;
  88. return true;
  89. }
  90. bool unlock_shared() {
  91. if (!mt_only || llvm_is_multithreaded())
  92. return impl.reader_release();
  93. // Single-threaded debugging code. This would be racy in multithreaded
  94. // mode, but provides not sanity checks in single threaded mode.
  95. assert(readers > 0 && "Reader lock not acquired before release!");
  96. --readers;
  97. return true;
  98. }
  99. bool lock() {
  100. if (!mt_only || llvm_is_multithreaded())
  101. return impl.writer_acquire();
  102. // Single-threaded debugging code. This would be racy in multithreaded
  103. // mode, but provides not sanity checks in single threaded mode.
  104. assert(writers == 0 && "Writer lock already acquired!");
  105. ++writers;
  106. return true;
  107. }
  108. bool unlock() {
  109. if (!mt_only || llvm_is_multithreaded())
  110. return impl.writer_release();
  111. // Single-threaded debugging code. This would be racy in multithreaded
  112. // mode, but provides not sanity checks in single threaded mode.
  113. assert(writers == 1 && "Writer lock not acquired before release!");
  114. --writers;
  115. return true;
  116. }
  117. private:
  118. SmartRWMutex(const SmartRWMutex<mt_only> & original);
  119. void operator=(const SmartRWMutex<mt_only> &);
  120. };
  121. typedef SmartRWMutex<false> RWMutex;
  122. /// ScopedReader - RAII acquisition of a reader lock
  123. template<bool mt_only>
  124. struct SmartScopedReader {
  125. SmartRWMutex<mt_only>& mutex;
  126. explicit SmartScopedReader(SmartRWMutex<mt_only>& m) : mutex(m) {
  127. mutex.lock_shared();
  128. }
  129. ~SmartScopedReader() {
  130. mutex.unlock_shared();
  131. }
  132. };
  133. typedef SmartScopedReader<false> ScopedReader;
  134. /// ScopedWriter - RAII acquisition of a writer lock
  135. template<bool mt_only>
  136. struct SmartScopedWriter {
  137. SmartRWMutex<mt_only>& mutex;
  138. explicit SmartScopedWriter(SmartRWMutex<mt_only>& m) : mutex(m) {
  139. mutex.lock();
  140. }
  141. ~SmartScopedWriter() {
  142. mutex.unlock();
  143. }
  144. };
  145. typedef SmartScopedWriter<false> ScopedWriter;
  146. }
  147. }
  148. #endif