xref: /aosp_15_r20/external/webrtc/rtc_base/synchronization/mutex_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #include "rtc_base/synchronization/mutex.h"
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include <atomic>
17 #include <memory>
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21 
22 #include "benchmark/benchmark.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/event.h"
25 #include "rtc_base/platform_thread.h"
26 #include "rtc_base/synchronization/yield.h"
27 #include "rtc_base/thread.h"
28 #include "test/gtest.h"
29 
30 namespace webrtc {
31 namespace {
32 
33 using ::rtc::Event;
34 using ::rtc::Thread;
35 
36 constexpr int kNumThreads = 16;
37 
38 template <class MutexType>
39 class RTC_LOCKABLE RawMutexLocker {
40  public:
RawMutexLocker(MutexType & mutex)41   explicit RawMutexLocker(MutexType& mutex) : mutex_(mutex) {}
Lock()42   void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { mutex_.Lock(); }
Unlock()43   void Unlock() RTC_UNLOCK_FUNCTION() { mutex_.Unlock(); }
44 
45  private:
46   MutexType& mutex_;
47 };
48 
49 class RTC_LOCKABLE RawMutexTryLocker {
50  public:
RawMutexTryLocker(Mutex & mutex)51   explicit RawMutexTryLocker(Mutex& mutex) : mutex_(mutex) {}
Lock()52   void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
53     while (!mutex_.TryLock()) {
54       YieldCurrentThread();
55     }
56   }
Unlock()57   void Unlock() RTC_UNLOCK_FUNCTION() { mutex_.Unlock(); }
58 
59  private:
60   Mutex& mutex_;
61 };
62 
63 template <class MutexType, class MutexLockType>
64 class MutexLockLocker {
65  public:
MutexLockLocker(MutexType & mutex)66   explicit MutexLockLocker(MutexType& mutex) : mutex_(mutex) {}
Lock()67   void Lock() { lock_ = std::make_unique<MutexLockType>(&mutex_); }
Unlock()68   void Unlock() { lock_ = nullptr; }
69 
70  private:
71   MutexType& mutex_;
72   std::unique_ptr<MutexLockType> lock_;
73 };
74 
75 template <class MutexType, class MutexLocker>
76 class LockRunner {
77  public:
78   template <typename... Args>
LockRunner(Args...args)79   explicit LockRunner(Args... args)
80       : threads_active_(0),
81         start_event_(true, false),
82         done_event_(true, false),
83         shared_value_(0),
84         mutex_(args...),
85         locker_(mutex_) {}
86 
Run()87   bool Run() {
88     // Signal all threads to start.
89     start_event_.Set();
90 
91     // Wait for all threads to finish.
92     return done_event_.Wait(kLongTime);
93   }
94 
SetExpectedThreadCount(int count)95   void SetExpectedThreadCount(int count) { threads_active_ = count; }
96 
shared_value()97   int shared_value() {
98     int shared_value;
99     locker_.Lock();
100     shared_value = shared_value_;
101     locker_.Unlock();
102     return shared_value;
103   }
104 
Loop()105   void Loop() {
106     ASSERT_TRUE(start_event_.Wait(kLongTime));
107     locker_.Lock();
108 
109     EXPECT_EQ(0, shared_value_);
110     int old = shared_value_;
111 
112     // Use a loop to increase the chance of race. If the `locker_`
113     // implementation is faulty, it would be improbable that the error slips
114     // through.
115     for (int i = 0; i < kOperationsToRun; ++i) {
116       benchmark::DoNotOptimize(++shared_value_);
117     }
118     EXPECT_EQ(old + kOperationsToRun, shared_value_);
119     shared_value_ = 0;
120 
121     locker_.Unlock();
122     if (threads_active_.fetch_sub(1) == 1) {
123       done_event_.Set();
124     }
125   }
126 
127  private:
128   static constexpr TimeDelta kLongTime = TimeDelta::Seconds(10);
129   static constexpr int kOperationsToRun = 1000;
130 
131   std::atomic<int> threads_active_;
132   Event start_event_;
133   Event done_event_;
134   int shared_value_;
135   MutexType mutex_;
136   MutexLocker locker_;
137 };
138 
139 template <typename Runner>
StartThreads(std::vector<std::unique_ptr<Thread>> & threads,Runner * handler)140 void StartThreads(std::vector<std::unique_ptr<Thread>>& threads,
141                   Runner* handler) {
142   for (int i = 0; i < kNumThreads; ++i) {
143     std::unique_ptr<Thread> thread(Thread::Create());
144     thread->Start();
145     thread->PostTask([handler] { handler->Loop(); });
146     threads.push_back(std::move(thread));
147   }
148 }
149 
TEST(MutexTest,ProtectsSharedResourceWithMutexAndRawMutexLocker)150 TEST(MutexTest, ProtectsSharedResourceWithMutexAndRawMutexLocker) {
151   std::vector<std::unique_ptr<Thread>> threads;
152   LockRunner<Mutex, RawMutexLocker<Mutex>> runner;
153   StartThreads(threads, &runner);
154   runner.SetExpectedThreadCount(kNumThreads);
155   EXPECT_TRUE(runner.Run());
156   EXPECT_EQ(0, runner.shared_value());
157 }
158 
TEST(MutexTest,ProtectsSharedResourceWithMutexAndRawMutexTryLocker)159 TEST(MutexTest, ProtectsSharedResourceWithMutexAndRawMutexTryLocker) {
160   std::vector<std::unique_ptr<Thread>> threads;
161   LockRunner<Mutex, RawMutexTryLocker> runner;
162   StartThreads(threads, &runner);
163   runner.SetExpectedThreadCount(kNumThreads);
164   EXPECT_TRUE(runner.Run());
165   EXPECT_EQ(0, runner.shared_value());
166 }
167 
TEST(MutexTest,ProtectsSharedResourceWithMutexAndMutexLocker)168 TEST(MutexTest, ProtectsSharedResourceWithMutexAndMutexLocker) {
169   std::vector<std::unique_ptr<Thread>> threads;
170   LockRunner<Mutex, MutexLockLocker<Mutex, MutexLock>> runner;
171   StartThreads(threads, &runner);
172   runner.SetExpectedThreadCount(kNumThreads);
173   EXPECT_TRUE(runner.Run());
174   EXPECT_EQ(0, runner.shared_value());
175 }
176 
177 }  // namespace
178 }  // namespace webrtc
179