Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 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" |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 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; |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 34 | using ::rtc::Thread; |
| 35 | |
| 36 | constexpr int kNumThreads = 16; |
| 37 | |
| 38 | template <class MutexType> |
| 39 | class 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 | |
| 49 | class 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 | |
| 63 | template <class MutexType, class MutexLockType> |
| 64 | class 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 | |
| 75 | template <class MutexType, class MutexLocker> |
Danil Chapovalov | 1e6965a | 2022-09-05 09:27:57 | [diff] [blame] | 76 | class LockRunner { |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 77 | 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 Bonadei | af09c13 | 2021-10-27 07:14:32 | [diff] [blame] | 102 | return shared_value; |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 103 | } |
| 104 | |
Danil Chapovalov | 1e6965a | 2022-09-05 09:27:57 | [diff] [blame] | 105 | void Loop() { |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 106 | ASSERT_TRUE(start_event_.Wait(kLongTime)); |
| 107 | locker_.Lock(); |
| 108 | |
| 109 | EXPECT_EQ(0, shared_value_); |
| 110 | int old = shared_value_; |
| 111 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 112 | // Use a loop to increase the chance of race. If the `locker_` |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 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: |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 | [diff] [blame] | 128 | static constexpr TimeDelta kLongTime = TimeDelta::Seconds(10); |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 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 | |
Danil Chapovalov | 1e6965a | 2022-09-05 09:27:57 | [diff] [blame] | 139 | template <typename Runner> |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 140 | void StartThreads(std::vector<std::unique_ptr<Thread>>& threads, |
Danil Chapovalov | 1e6965a | 2022-09-05 09:27:57 | [diff] [blame] | 141 | Runner* handler) { |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 142 | for (int i = 0; i < kNumThreads; ++i) { |
| 143 | std::unique_ptr<Thread> thread(Thread::Create()); |
| 144 | thread->Start(); |
Danil Chapovalov | 1e6965a | 2022-09-05 09:27:57 | [diff] [blame] | 145 | thread->PostTask([handler] { handler->Loop(); }); |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 146 | threads.push_back(std::move(thread)); |
| 147 | } |
| 148 | } |
| 149 | |
| 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 | |
| 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 | |
| 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 | |
Markus Handell | f70fbc8 | 2020-06-03 22:41:20 | [diff] [blame] | 177 | } // namespace |
| 178 | } // namespace webrtc |