Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcppdap/src/rwmutex_test.cpp
3153 views
1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "rwmutex.h"
16
17
#include "gmock/gmock.h"
18
#include "gtest/gtest.h"
19
20
#include <array>
21
#include <thread>
22
#include <vector>
23
24
namespace {
25
constexpr const size_t NumThreads = 8;
26
}
27
28
// Check that WLock behaves like regular mutex.
29
TEST(RWMutex, WLock) {
30
dap::RWMutex rwmutex;
31
int counter = 0;
32
33
std::vector<std::thread> threads;
34
for (size_t i = 0; i < NumThreads; i++) {
35
threads.emplace_back([&] {
36
for (int j = 0; j < 1000; j++) {
37
dap::WLock lock(rwmutex);
38
counter++;
39
EXPECT_EQ(counter, 1);
40
counter--;
41
}
42
});
43
}
44
45
for (auto& thread : threads) {
46
thread.join();
47
}
48
49
EXPECT_EQ(counter, 0);
50
}
51
52
TEST(RWMutex, NoRLockWithWLock) {
53
dap::RWMutex rwmutex;
54
55
std::vector<std::thread> threads;
56
std::array<int, NumThreads> counters = {};
57
58
{ // With WLock held...
59
dap::WLock wlock(rwmutex);
60
61
for (size_t i = 0; i < counters.size(); i++) {
62
int* counter = &counters[i];
63
threads.emplace_back([&rwmutex, counter] {
64
dap::RLock lock(rwmutex);
65
for (int j = 0; j < 1000; j++) {
66
(*counter)++;
67
}
68
});
69
}
70
71
// RLocks should block
72
for (int counter : counters) {
73
EXPECT_EQ(counter, 0);
74
}
75
}
76
77
for (auto& thread : threads) {
78
thread.join();
79
}
80
81
for (int counter : counters) {
82
EXPECT_EQ(counter, 1000);
83
}
84
}
85
86
TEST(RWMutex, NoWLockWithRLock) {
87
dap::RWMutex rwmutex;
88
89
std::vector<std::thread> threads;
90
size_t counter = 0;
91
92
{ // With RLocks held...
93
dap::RLock rlockA(rwmutex);
94
dap::RLock rlockB(rwmutex);
95
dap::RLock rlockC(rwmutex);
96
97
for (size_t i = 0; i < NumThreads; i++) {
98
threads.emplace_back(std::thread([&] {
99
dap::WLock lock(rwmutex);
100
counter++;
101
}));
102
}
103
104
// ... WLocks should block
105
EXPECT_EQ(counter, 0U);
106
}
107
108
for (auto& thread : threads) {
109
thread.join();
110
}
111
112
EXPECT_EQ(counter, NumThreads);
113
}
114
115