From db622c359b868db74befb4ab84cceac58687250f Mon Sep 17 00:00:00 2001 From: Artem Titov Date: Thu, 7 Apr 2022 13:37:32 +0000 Subject: [PATCH] Revert "Delete unused class GlobalMutex" This reverts commit 5831afb8328cbecdbdbee882280ef907a09d9f31. Reason for revert: The code is still in use: https://ci.chromium.org/ui/p/webrtc/builders/ci/Win32%20Builder%20(Clang)/10243/overview. It wasn't detected for some reasons. Original change's description: > Delete unused class GlobalMutex > > Bug: webrtc:13869 > Change-Id: Id3655bdd24630b78e83b606291605a37129fd4bc > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258131 > Reviewed-by: Mirko Bonadei > Commit-Queue: Niels Moller > Cr-Commit-Position: refs/heads/main@{#36478} Bug: webrtc:13869 Change-Id: Id44dd4f8ec31ac9e05fc0ee75b55916e627fd7bf No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258135 Bot-Commit: rubber-stamper@appspot.gserviceaccount.com Owners-Override: Artem Titov Commit-Queue: Artem Titov Cr-Commit-Position: refs/heads/main@{#36480} --- rtc_base/synchronization/BUILD.gn | 1 + rtc_base/synchronization/mutex.cc | 39 ++++++++++++++++++++++ rtc_base/synchronization/mutex.h | 35 +++++++++++++++++++ rtc_base/synchronization/mutex_unittest.cc | 25 ++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 rtc_base/synchronization/mutex.cc diff --git a/rtc_base/synchronization/BUILD.gn b/rtc_base/synchronization/BUILD.gn index 52c39d7b6a..9d891f3d8e 100644 --- a/rtc_base/synchronization/BUILD.gn +++ b/rtc_base/synchronization/BUILD.gn @@ -23,6 +23,7 @@ rtc_library("yield") { rtc_library("mutex") { sources = [ + "mutex.cc", "mutex.h", "mutex_critical_section.h", "mutex_pthread.h", diff --git a/rtc_base/synchronization/mutex.cc b/rtc_base/synchronization/mutex.cc new file mode 100644 index 0000000000..6c2d6ff7f0 --- /dev/null +++ b/rtc_base/synchronization/mutex.cc @@ -0,0 +1,39 @@ +/* + * Copyright 2020 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "rtc_base/synchronization/mutex.h" + +#include "rtc_base/checks.h" +#include "rtc_base/synchronization/yield.h" + +namespace webrtc { + +#if !defined(WEBRTC_ABSL_MUTEX) +void GlobalMutex::Lock() { + while (mutex_locked_.exchange(1)) { + YieldCurrentThread(); + } +} + +void GlobalMutex::Unlock() { + int old = mutex_locked_.exchange(0); + RTC_DCHECK_EQ(old, 1) << "Unlock called without calling Lock first"; +} + +GlobalMutexLock::GlobalMutexLock(GlobalMutex* mutex) : mutex_(mutex) { + mutex_->Lock(); +} + +GlobalMutexLock::~GlobalMutexLock() { + mutex_->Unlock(); +} +#endif // #if !defined(WEBRTC_ABSL_MUTEX) + +} // namespace webrtc diff --git a/rtc_base/synchronization/mutex.h b/rtc_base/synchronization/mutex.h index 2cf0e67c3d..c6af9e9838 100644 --- a/rtc_base/synchronization/mutex.h +++ b/rtc_base/synchronization/mutex.h @@ -72,6 +72,41 @@ class RTC_SCOPED_LOCKABLE MutexLock final { Mutex* mutex_; }; +// A mutex used to protect global variables. Do NOT use for other purposes. +#if defined(WEBRTC_ABSL_MUTEX) +using GlobalMutex = absl::Mutex; +using GlobalMutexLock = absl::MutexLock; +#else +class RTC_LOCKABLE GlobalMutex final { + public: + GlobalMutex(const GlobalMutex&) = delete; + GlobalMutex& operator=(const GlobalMutex&) = delete; + + constexpr explicit GlobalMutex(absl::ConstInitType /*unused*/) + : mutex_locked_(0) {} + + void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); + void Unlock() RTC_UNLOCK_FUNCTION(); + + private: + std::atomic mutex_locked_; // 0 means lock not taken, 1 means taken. +}; + +// GlobalMutexLock, for serializing execution through a scope. +class RTC_SCOPED_LOCKABLE GlobalMutexLock final { + public: + GlobalMutexLock(const GlobalMutexLock&) = delete; + GlobalMutexLock& operator=(const GlobalMutexLock&) = delete; + + explicit GlobalMutexLock(GlobalMutex* mutex) + RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_); + ~GlobalMutexLock() RTC_UNLOCK_FUNCTION(); + + private: + GlobalMutex* mutex_; +}; +#endif // if defined(WEBRTC_ABSL_MUTEX) + } // namespace webrtc #endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_ diff --git a/rtc_base/synchronization/mutex_unittest.cc b/rtc_base/synchronization/mutex_unittest.cc index fcca78d18b..a07e8fa429 100644 --- a/rtc_base/synchronization/mutex_unittest.cc +++ b/rtc_base/synchronization/mutex_unittest.cc @@ -177,5 +177,30 @@ TEST(MutexTest, ProtectsSharedResourceWithMutexAndMutexLocker) { EXPECT_EQ(0, runner.shared_value()); } +TEST(MutexTest, ProtectsSharedResourceWithGlobalMutexAndRawMutexLocker) { + std::vector> threads; + LockRunner> runner(absl::kConstInit); + StartThreads(threads, &runner); + runner.SetExpectedThreadCount(kNumThreads); + EXPECT_TRUE(runner.Run()); + EXPECT_EQ(0, runner.shared_value()); +} + +TEST(MutexTest, ProtectsSharedResourceWithGlobalMutexAndMutexLocker) { + std::vector> threads; + LockRunner> runner( + absl::kConstInit); + StartThreads(threads, &runner); + runner.SetExpectedThreadCount(kNumThreads); + EXPECT_TRUE(runner.Run()); + EXPECT_EQ(0, runner.shared_value()); +} + +TEST(MutexTest, GlobalMutexCanHaveStaticStorageDuration) { + ABSL_CONST_INIT static GlobalMutex global_lock(absl::kConstInit); + global_lock.Lock(); + global_lock.Unlock(); +} + } // namespace } // namespace webrtc