Mutex.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. void* data_; ///< We don't know what the data will be
  65. #endif
  66. /// @}
  67. /// @name Do Not Implement
  68. /// @{
  69. private:
  70. MutexImpl(const MutexImpl &) = delete;
  71. void operator=(const MutexImpl &) = delete;
  72. /// @}
  73. };
  74. /// SmartMutex - A mutex with a compile time constant parameter that
  75. /// indicates whether this mutex should become a no-op when we're not
  76. /// running in multithreaded mode.
  77. template<bool mt_only>
  78. class SmartMutex {
  79. MutexImpl impl;
  80. unsigned acquired;
  81. bool recursive;
  82. public:
  83. explicit SmartMutex(bool rec = true) :
  84. impl(rec), acquired(0), recursive(rec) { }
  85. bool lock() {
  86. if (!mt_only || llvm_is_multithreaded()) {
  87. return impl.acquire();
  88. } else {
  89. // Single-threaded debugging code. This would be racy in
  90. // multithreaded mode, but provides not sanity checks in single
  91. // threaded mode.
  92. assert((recursive || acquired == 0) && "Lock already acquired!!");
  93. ++acquired;
  94. return true;
  95. }
  96. }
  97. bool unlock() {
  98. if (!mt_only || llvm_is_multithreaded()) {
  99. return impl.release();
  100. } else {
  101. // Single-threaded debugging code. This would be racy in
  102. // multithreaded mode, but provides not sanity checks in single
  103. // threaded mode.
  104. assert(((recursive && acquired) || (acquired == 1)) &&
  105. "Lock not acquired before release!");
  106. --acquired;
  107. return true;
  108. }
  109. }
  110. bool try_lock() {
  111. if (!mt_only || llvm_is_multithreaded())
  112. return impl.tryacquire();
  113. else return true;
  114. }
  115. private:
  116. SmartMutex(const SmartMutex<mt_only> & original);
  117. void operator=(const SmartMutex<mt_only> &);
  118. };
  119. /// Mutex - A standard, always enforced mutex.
  120. typedef SmartMutex<false> Mutex;
  121. template<bool mt_only>
  122. class SmartScopedLock {
  123. SmartMutex<mt_only>& mtx;
  124. public:
  125. SmartScopedLock(SmartMutex<mt_only>& m) : mtx(m) {
  126. mtx.lock();
  127. }
  128. ~SmartScopedLock() {
  129. mtx.unlock();
  130. }
  131. };
  132. typedef SmartScopedLock<false> ScopedLock;
  133. }
  134. }
  135. #endif