Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.5 KiB

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