-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
180 lines (156 loc) · 4.35 KB
/
Copy pathtests.cpp
File metadata and controls
180 lines (156 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "proposed-semaphore.hpp"
#include <gtest/gtest.h>
#include <benchmark/benchmark.h>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
namespace {
class OneCondVarSemaphore
{
public:
explicit OneCondVarSemaphore(size_t resource_count);
void wait();
void signal();
size_t get_number_of_waiting_threads() const;
private:
std::condition_variable cond_var;
mutable std::mutex mtx;
size_t now_serving;
size_t next_ticket;
size_t number_of_waiting_threads;
size_t resource_count;
};
class UnfairSemaphore
{
public:
explicit UnfairSemaphore(size_t resource_count);
void wait();
void signal();
size_t get_number_of_waiting_threads() const;
private:
std::condition_variable cond_var;
mutable std::mutex mtx;
size_t number_of_waiting_threads;
size_t resource_count;
};
} // namespace
template <typename T>
static bool run_fairness_check()
{
T semaphore(0);
std::vector<std::thread> threads;
std::vector<size_t> passing_order;
static const size_t number_of_threads = 1000;
for (size_t i = 0; i < number_of_threads; ++i)
{
threads.push_back(std::thread([&semaphore, &passing_order, i]
{
semaphore.wait();
passing_order.push_back(i);
semaphore.signal();
}));
while (semaphore.get_number_of_waiting_threads() != (i + 1))
std::this_thread::yield();
}
semaphore.signal();
for (auto &t : threads)
t.join();
for (size_t i = 0; i < passing_order.size(); ++i)
if (passing_order.at(i) != i)
return false;
return true;
}
template <typename T>
static void run_performance_benchmark(benchmark::State& state)
{
for (auto _ : state)
{
T semaphore(0);
std::vector<std::thread> threads;
static const size_t number_of_threads = 1000;
for (size_t i = 0; i < number_of_threads; ++i)
{
threads.push_back(std::thread([&semaphore]
{
semaphore.wait();
}));
}
while (semaphore.get_number_of_waiting_threads() != number_of_threads)
std::this_thread::yield();
for (size_t i = 0; i < number_of_threads; ++i)
semaphore.signal();
for (auto &t : threads)
t.join();
}
}
OneCondVarSemaphore::OneCondVarSemaphore(size_t resource_count) : now_serving(0), next_ticket(0),
number_of_waiting_threads(0), resource_count(resource_count) {}
void OneCondVarSemaphore::wait()
{
std::unique_lock<std::mutex> ul(mtx);
size_t my_ticket = next_ticket;
++next_ticket;
++number_of_waiting_threads;
cond_var.wait(ul, [=]() -> bool
{
return (my_ticket == now_serving) && (resource_count > 0);
});
--number_of_waiting_threads;
--resource_count;
++now_serving;
cond_var.notify_all();
}
void OneCondVarSemaphore::signal()
{
std::unique_lock<std::mutex> ul(mtx);
++resource_count;
cond_var.notify_all();
}
size_t OneCondVarSemaphore::get_number_of_waiting_threads() const
{
std::unique_lock<std::mutex> ul(mtx);
return number_of_waiting_threads;
}
UnfairSemaphore::UnfairSemaphore(size_t resource_count) : number_of_waiting_threads(0),
resource_count(resource_count) {}
void UnfairSemaphore::wait()
{
std::unique_lock<std::mutex> ul(mtx);
++number_of_waiting_threads;
cond_var.wait(ul, [=]() -> bool
{
return resource_count > 0;
});
--number_of_waiting_threads;
--resource_count;
}
void UnfairSemaphore::signal()
{
std::unique_lock<std::mutex> ul(mtx);
++resource_count;
cond_var.notify_all();
}
size_t UnfairSemaphore::get_number_of_waiting_threads() const
{
std::unique_lock<std::mutex> ul(mtx);
return number_of_waiting_threads;
}
TEST(FairnessCheck, PassesForProposedSemaphore)
{
EXPECT_TRUE(run_fairness_check<ProposedSemaphore>());
}
TEST(FairnessCheck, PassesForOneCondVarSemaphore)
{
EXPECT_TRUE(run_fairness_check<OneCondVarSemaphore>());
}
TEST(FairnessCheck, FailsForUnfairSemaphore)
{
EXPECT_FALSE(run_fairness_check<UnfairSemaphore>());
}
BENCHMARK(run_performance_benchmark<ProposedSemaphore>);
BENCHMARK(run_performance_benchmark<OneCondVarSemaphore>);
BENCHMARK(run_performance_benchmark<UnfairSemaphore>);
TEST(PerformanceBenchmark, RunAllRegisteredBenchmarks)
{
benchmark::RunSpecifiedBenchmarks();
}