Counter Strike : Global Offensive Source Code

174 lines
5.3 KiB

  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_SYSTEM_RWMUTEX_H
  14. #define LLVM_SYSTEM_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. void* data_; ///< We don't know what the data will be
  63. /// @}
  64. /// @name Do Not Implement
  65. /// @{
  66. private:
  67. RWMutexImpl(const RWMutexImpl & original) LLVM_DELETED_FUNCTION;
  68. void operator=(const RWMutexImpl &) LLVM_DELETED_FUNCTION;
  69. /// @}
  70. };
  71. /// SmartMutex - An R/W mutex with a compile time constant parameter that
  72. /// indicates whether this mutex should become a no-op when we're not
  73. /// running in multithreaded mode.
  74. template<bool mt_only>
  75. class SmartRWMutex : public RWMutexImpl {
  76. unsigned readers, writers;
  77. public:
  78. explicit SmartRWMutex() : RWMutexImpl(), readers(0), writers(0) { }
  79. bool reader_acquire() {
  80. if (!mt_only || llvm_is_multithreaded())
  81. return RWMutexImpl::reader_acquire();
  82. // Single-threaded debugging code. This would be racy in multithreaded
  83. // mode, but provides not sanity checks in single threaded mode.
  84. ++readers;
  85. return true;
  86. }
  87. bool reader_release() {
  88. if (!mt_only || llvm_is_multithreaded())
  89. return RWMutexImpl::reader_release();
  90. // Single-threaded debugging code. This would be racy in multithreaded
  91. // mode, but provides not sanity checks in single threaded mode.
  92. assert(readers > 0 && "Reader lock not acquired before release!");
  93. --readers;
  94. return true;
  95. }
  96. bool writer_acquire() {
  97. if (!mt_only || llvm_is_multithreaded())
  98. return RWMutexImpl::writer_acquire();
  99. // Single-threaded debugging code. This would be racy in multithreaded
  100. // mode, but provides not sanity checks in single threaded mode.
  101. assert(writers == 0 && "Writer lock already acquired!");
  102. ++writers;
  103. return true;
  104. }
  105. bool writer_release() {
  106. if (!mt_only || llvm_is_multithreaded())
  107. return RWMutexImpl::writer_release();
  108. // Single-threaded debugging code. This would be racy in multithreaded
  109. // mode, but provides not sanity checks in single threaded mode.
  110. assert(writers == 1 && "Writer lock not acquired before release!");
  111. --writers;
  112. return true;
  113. }
  114. private:
  115. SmartRWMutex(const SmartRWMutex<mt_only> & original);
  116. void operator=(const SmartRWMutex<mt_only> &);
  117. };
  118. typedef SmartRWMutex<false> RWMutex;
  119. /// ScopedReader - RAII acquisition of a reader lock
  120. template<bool mt_only>
  121. struct SmartScopedReader {
  122. SmartRWMutex<mt_only>& mutex;
  123. explicit SmartScopedReader(SmartRWMutex<mt_only>& m) : mutex(m) {
  124. mutex.reader_acquire();
  125. }
  126. ~SmartScopedReader() {
  127. mutex.reader_release();
  128. }
  129. };
  130. typedef SmartScopedReader<false> ScopedReader;
  131. /// ScopedWriter - RAII acquisition of a writer lock
  132. template<bool mt_only>
  133. struct SmartScopedWriter {
  134. SmartRWMutex<mt_only>& mutex;
  135. explicit SmartScopedWriter(SmartRWMutex<mt_only>& m) : mutex(m) {
  136. mutex.writer_acquire();
  137. }
  138. ~SmartScopedWriter() {
  139. mutex.writer_release();
  140. }
  141. };
  142. typedef SmartScopedWriter<false> ScopedWriter;
  143. }
  144. }
  145. #endif