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