Mutex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- 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::Mutex class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_MUTEX_H
  14. #define LLVM_SUPPORT_MUTEX_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 Mutex class.
  23. class MutexImpl
  24. {
  25. /// @name Constructors
  26. /// @{
  27. public:
  28. /// Initializes the lock but doesn't acquire it. if \p recursive is set
  29. /// to false, the lock will not be recursive which makes it cheaper but
  30. /// also more likely to deadlock (same thread can't acquire more than
  31. /// once).
  32. /// @brief Default Constructor.
  33. explicit MutexImpl(bool recursive = true);
  34. /// Releases and removes the lock
  35. /// @brief Destructor
  36. ~MutexImpl();
  37. /// @}
  38. /// @name Methods
  39. /// @{
  40. public:
  41. /// Attempts to unconditionally acquire the lock. If the lock is held by
  42. /// another thread, this method will wait until it can acquire the lock.
  43. /// @returns false if any kind of error occurs, true otherwise.
  44. /// @brief Unconditionally acquire the lock.
  45. bool acquire();
  46. /// Attempts to release the lock. If the lock is held by the current
  47. /// thread, the lock is released allowing other threads to acquire the
  48. /// lock.
  49. /// @returns false if any kind of error occurs, true otherwise.
  50. /// @brief Unconditionally release the lock.
  51. bool release();
  52. /// Attempts to acquire the lock without blocking. If the lock is not
  53. /// available, this function returns false quickly (without blocking). If
  54. /// the lock is available, it is acquired.
  55. /// @returns false if any kind of error occurs or the lock is not
  56. /// available, true otherwise.
  57. /// @brief Try to acquire the lock.
  58. bool tryacquire();
  59. //@}
  60. /// @name Platform Dependent Data
  61. /// @{
  62. private:
  63. #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0
  64. #if 0 // HLSL Change
  65. void* data_; ///< We don't know what the data will be
  66. #else
  67. char data_[sizeof(void*) == 8 ? 40 : 24]; // C_ASSERT this is CRITICAL_SECTION-sized
  68. #endif // HLSL Change
  69. #endif
  70. /// @}
  71. /// @name Do Not Implement
  72. /// @{
  73. private:
  74. MutexImpl(const MutexImpl &) = delete;
  75. void operator=(const MutexImpl &) = delete;
  76. /// @}
  77. };
  78. /// SmartMutex - A mutex with a compile time constant parameter that
  79. /// indicates whether this mutex should become a no-op when we're not
  80. /// running in multithreaded mode.
  81. template<bool mt_only>
  82. class SmartMutex {
  83. MutexImpl impl;
  84. unsigned acquired;
  85. bool recursive;
  86. public:
  87. explicit SmartMutex(bool rec = true) :
  88. impl(rec), acquired(0), recursive(rec) { }
  89. bool lock() {
  90. if (!mt_only || llvm_is_multithreaded()) {
  91. return impl.acquire();
  92. } else {
  93. // Single-threaded debugging code. This would be racy in
  94. // multithreaded mode, but provides not sanity checks in single
  95. // threaded mode.
  96. assert((recursive || acquired == 0) && "Lock already acquired!!");
  97. ++acquired;
  98. return true;
  99. }
  100. }
  101. bool unlock() {
  102. if (!mt_only || llvm_is_multithreaded()) {
  103. return impl.release();
  104. } else {
  105. // Single-threaded debugging code. This would be racy in
  106. // multithreaded mode, but provides not sanity checks in single
  107. // threaded mode.
  108. assert(((recursive && acquired) || (acquired == 1)) &&
  109. "Lock not acquired before release!");
  110. --acquired;
  111. return true;
  112. }
  113. }
  114. bool try_lock() {
  115. if (!mt_only || llvm_is_multithreaded())
  116. return impl.tryacquire();
  117. else return true;
  118. }
  119. private:
  120. SmartMutex(const SmartMutex<mt_only> & original);
  121. void operator=(const SmartMutex<mt_only> &);
  122. };
  123. /// Mutex - A standard, always enforced mutex.
  124. typedef SmartMutex<false> Mutex;
  125. template<bool mt_only>
  126. class SmartScopedLock {
  127. SmartMutex<mt_only>& mtx;
  128. public:
  129. SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
  130. mtx.lock();
  131. }
  132. ~SmartScopedLock() {
  133. mtx.unlock();
  134. }
  135. };
  136. typedef SmartScopedLock<false> ScopedLock;
  137. }
  138. }
  139. #endif