blob: a5ebc5f7d4cf5cc4962691e7b5b968e9d87982bd [file] [log] [blame]
Markus Handellf70fbc82020-06-03 22:41:201/*
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"
Markus Handellf70fbc82020-06-03 22:41:2025#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
30namespace webrtc {
31namespace {
32
33using ::rtc::Event;
Markus Handellf70fbc82020-06-03 22:41:2034using ::rtc::Thread;
35
36constexpr int kNumThreads = 16;
37
38template <class MutexType>
39class RTC_LOCKABLE RawMutexLocker {
40 public:
41 explicit RawMutexLocker(MutexType& mutex) : mutex_(mutex) {}
42 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { mutex_.Lock(); }
43 void Unlock() RTC_UNLOCK_FUNCTION() { mutex_.Unlock(); }
44
45 private:
46 MutexType& mutex_;
47};
48
49class RTC_LOCKABLE RawMutexTryLocker {
50 public:
51 explicit RawMutexTryLocker(Mutex& mutex) : mutex_(mutex) {}
52 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
53 while (!mutex_.TryLock()) {
54 YieldCurrentThread();
55 }
56 }
57 void Unlock() RTC_UNLOCK_FUNCTION() { mutex_.Unlock(); }
58
59 private:
60 Mutex& mutex_;
61};
62
63template <class MutexType, class MutexLockType>
64class MutexLockLocker {
65 public:
66 explicit MutexLockLocker(MutexType& mutex) : mutex_(mutex) {}
67 void Lock() { lock_ = std::make_unique<MutexLockType>(&mutex_); }
68 void Unlock() { lock_ = nullptr; }
69
70 private:
71 MutexType& mutex_;
72 std::unique_ptr<MutexLockType> lock_;
73};
74
75template <class MutexType, class MutexLocker>
Danil Chapovalov1e6965a2022-09-05 09:27:5776class LockRunner {
Markus Handellf70fbc82020-06-03 22:41:2077 public:
78 template <typename... 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
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
95 void SetExpectedThreadCount(int count) { threads_active_ = count; }
96
97 int shared_value() {
98 int shared_value;
99 locker_.Lock();
100 shared_value = shared_value_;
101 locker_.Unlock();
Mirko Bonadeiaf09c132021-10-27 07:14:32102 return shared_value;
Markus Handellf70fbc82020-06-03 22:41:20103 }
104
Danil Chapovalov1e6965a2022-09-05 09:27:57105 void Loop() {
Markus Handellf70fbc82020-06-03 22:41:20106 ASSERT_TRUE(start_event_.Wait(kLongTime));
107 locker_.Lock();
108
109 EXPECT_EQ(0, shared_value_);
110 int old = shared_value_;
111
Artem Titov96e3b992021-07-26 14:03:14112 // Use a loop to increase the chance of race. If the `locker_`
Markus Handellf70fbc82020-06-03 22:41:20113 // 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:
Markus Handell2cfc1af2022-08-19 08:16:48128 static constexpr TimeDelta kLongTime = TimeDelta::Seconds(10);
Markus Handellf70fbc82020-06-03 22:41:20129 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
Danil Chapovalov1e6965a2022-09-05 09:27:57139template <typename Runner>
Markus Handellf70fbc82020-06-03 22:41:20140void StartThreads(std::vector<std::unique_ptr<Thread>>& threads,
Danil Chapovalov1e6965a2022-09-05 09:27:57141 Runner* handler) {
Markus Handellf70fbc82020-06-03 22:41:20142 for (int i = 0; i < kNumThreads; ++i) {
143 std::unique_ptr<Thread> thread(Thread::Create());
144 thread->Start();
Danil Chapovalov1e6965a2022-09-05 09:27:57145 thread->PostTask([handler] { handler->Loop(); });
Markus Handellf70fbc82020-06-03 22:41:20146 threads.push_back(std::move(thread));
147 }
148}
149
150TEST(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
159TEST(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
168TEST(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
Markus Handellf70fbc82020-06-03 22:41:20177} // namespace
178} // namespace webrtc