-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticket_lock_test.cpp
More file actions
150 lines (122 loc) · 2.94 KB
/
ticket_lock_test.cpp
File metadata and controls
150 lines (122 loc) · 2.94 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
#include "thread/sync/ticket_lock.hpp"
#include <gtest/gtest.h>
#include <mutex>
#include <thread>
#include <vector>
class TicketLockTest : public ::testing::Test {
protected:
thread::sync::TicketLock lock;
};
TEST_F(TicketLockTest, BasicLockUnlock) {
lock.lock();
lock.unlock();
}
TEST_F(TicketLockTest, TryLockSuccess) {
EXPECT_TRUE(lock.try_lock());
lock.unlock();
}
TEST_F(TicketLockTest, TryLockFailure) {
lock.lock();
EXPECT_FALSE(lock.try_lock());
lock.unlock();
}
TEST_F(TicketLockTest, BasicLockable) {
int value = 0;
{
std::lock_guard<thread::sync::TicketLock> guard(lock);
value = 42;
}
EXPECT_EQ(value, 42);
{
std::unique_lock<thread::sync::TicketLock> guard(lock);
value = 100;
guard.unlock();
EXPECT_FALSE(guard.owns_lock());
guard.lock();
EXPECT_TRUE(guard.owns_lock());
value = 200;
}
EXPECT_EQ(value, 200);
}
TEST_F(TicketLockTest, MutualExclusion) {
int counter = 0;
const int num_threads = 10;
const int increments = 10000;
auto worker = [&]() {
for (int i = 0; i < increments; ++i) {
std::lock_guard<thread::sync::TicketLock> guard(lock);
++counter;
}
};
std::vector<std::thread> threads;
for (int i = 0; i < num_threads; ++i) {
threads.emplace_back(worker);
}
for (auto& t : threads) {
t.join();
}
EXPECT_EQ(counter, num_threads * increments);
}
TEST_F(TicketLockTest, HighContention) {
std::vector<int> results(8, 0);
const int iterations = 1000;
auto worker = [&](int thread_id) {
for (int i = 0; i < iterations; ++i) {
std::lock_guard<thread::sync::TicketLock> guard(lock);
results[thread_id]++;
volatile int x = 0;
for (int j = 0; j < 10; ++j) {
x += j;
}
}
};
std::vector<std::thread> threads;
for (size_t i = 0; i < results.size(); ++i) {
threads.emplace_back(worker, i);
}
for (auto& t : threads) {
t.join();
}
for (size_t i = 0; i < results.size(); ++i) {
EXPECT_EQ(results[i], iterations) << "Thread " << i << " failed";
}
}
TEST_F(TicketLockTest, MultipleSequentialLocks) {
int value = 0;
{
std::lock_guard<thread::sync::TicketLock> guard(lock);
value = 1;
}
{
std::lock_guard<thread::sync::TicketLock> guard(lock);
EXPECT_EQ(value, 1);
value = 2;
}
{
std::lock_guard<thread::sync::TicketLock> guard(lock);
EXPECT_EQ(value, 2);
value = 3;
}
EXPECT_EQ(value, 3);
}
TEST_F(TicketLockTest, TryLockInContention) {
std::atomic<int> successful_locks{0};
const int num_threads = 4;
auto worker = [&]() {
for (int i = 0; i < 100; ++i) {
if (lock.try_lock()) {
++successful_locks;
std::this_thread::sleep_for(std::chrono::microseconds(1));
lock.unlock();
}
}
};
std::vector<std::thread> threads;
for (int i = 0; i < num_threads; ++i) {
threads.emplace_back(worker);
}
for (auto& t : threads) {
t.join();
}
EXPECT_GT(successful_locks.load(), 0);
}