mutex.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2020 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_H_
  11. #define RTC_BASE_SYNCHRONIZATION_MUTEX_H_
  12. #include <atomic>
  13. #include "absl/base/const_init.h"
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/system/unused.h"
  16. #include "rtc_base/thread_annotations.h"
  17. #if defined(WEBRTC_ABSL_MUTEX)
  18. #include "rtc_base/synchronization/mutex_abseil.h" // nogncheck
  19. #elif defined(WEBRTC_WIN)
  20. #include "rtc_base/synchronization/mutex_critical_section.h"
  21. #elif defined(WEBRTC_POSIX)
  22. #include "rtc_base/synchronization/mutex_pthread.h"
  23. #else
  24. #error Unsupported platform.
  25. #endif
  26. namespace webrtc {
  27. // The Mutex guarantees exclusive access and aims to follow Abseil semantics
  28. // (i.e. non-reentrant etc).
  29. class RTC_LOCKABLE Mutex final {
  30. public:
  31. Mutex() = default;
  32. Mutex(const Mutex&) = delete;
  33. Mutex& operator=(const Mutex&) = delete;
  34. void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
  35. impl_.Lock();
  36. }
  37. RTC_WARN_UNUSED_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
  38. return impl_.TryLock();
  39. }
  40. void Unlock() RTC_UNLOCK_FUNCTION() {
  41. impl_.Unlock();
  42. }
  43. private:
  44. MutexImpl impl_;
  45. };
  46. // MutexLock, for serializing execution through a scope.
  47. class RTC_SCOPED_LOCKABLE MutexLock final {
  48. public:
  49. MutexLock(const MutexLock&) = delete;
  50. MutexLock& operator=(const MutexLock&) = delete;
  51. explicit MutexLock(Mutex* mutex) RTC_EXCLUSIVE_LOCK_FUNCTION(mutex)
  52. : mutex_(mutex) {
  53. mutex->Lock();
  54. }
  55. ~MutexLock() RTC_UNLOCK_FUNCTION() { mutex_->Unlock(); }
  56. private:
  57. Mutex* mutex_;
  58. };
  59. // A mutex used to protect global variables. Do NOT use for other purposes.
  60. #if defined(WEBRTC_ABSL_MUTEX)
  61. using GlobalMutex = absl::Mutex;
  62. using GlobalMutexLock = absl::MutexLock;
  63. #else
  64. class RTC_LOCKABLE GlobalMutex final {
  65. public:
  66. GlobalMutex(const GlobalMutex&) = delete;
  67. GlobalMutex& operator=(const GlobalMutex&) = delete;
  68. constexpr explicit GlobalMutex(absl::ConstInitType /*unused*/)
  69. : mutex_locked_(0) {}
  70. void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION();
  71. void Unlock() RTC_UNLOCK_FUNCTION();
  72. private:
  73. std::atomic<int> mutex_locked_; // 0 means lock not taken, 1 means taken.
  74. };
  75. // GlobalMutexLock, for serializing execution through a scope.
  76. class RTC_SCOPED_LOCKABLE GlobalMutexLock final {
  77. public:
  78. GlobalMutexLock(const GlobalMutexLock&) = delete;
  79. GlobalMutexLock& operator=(const GlobalMutexLock&) = delete;
  80. explicit GlobalMutexLock(GlobalMutex* mutex)
  81. RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_);
  82. ~GlobalMutexLock() RTC_UNLOCK_FUNCTION();
  83. private:
  84. GlobalMutex* mutex_;
  85. };
  86. #endif // if defined(WEBRTC_ABSL_MUTEX)
  87. } // namespace webrtc
  88. #endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_