-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
260 lines (231 loc) · 8.29 KB
/
main.cpp
File metadata and controls
260 lines (231 loc) · 8.29 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#define BOOST_TEST_MODULE ALL TESTS
#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_ALTERNATIVE_INIT_API
#include <boost/test/included/unit_test.hpp>
#include <cassert>
#include <iostream>
#include <thread>
#include <unistd.h>
#include "chroniclock/seqlock.hpp"
#include "chroniclock/rwlock.hpp"
#include "chroniclock/Timer.h"
#include "rigtorp/seqlock.hpp"
#define STATS 0
#define RWTEST 1
BOOST_AUTO_TEST_CASE(does_it_work) {
int data = 50;
chroniclock::seqlock<int> s;
s.store(data);
int tmp = 10;
BOOST_TEST(s.load(tmp));
BOOST_TEST(tmp == 50);
data = 60;
s.store(data);
BOOST_TEST(s.load(tmp));
BOOST_TEST(tmp == 60);
}
BOOST_AUTO_TEST_CASE(basic_test) {
int data = 30;
chroniclock::seqlock<int> s;
s.store(data);
auto func = [&s]() {
int tmp = 0;
while (true) {
if (s.load(tmp) && tmp == 100) {
return;
}
}
};
std::thread thread1(func);
s.store(100);
thread1.join(); // test passes if thread1 doesn't run forever
}
int main(int argc, char **argv) {
int error_code = boost::unit_test::unit_test_main(init_unit_test, argc, argv);
if (error_code != 0) return error_code;
if (STATS) {
Timer t("../stats/seqlock.txt");
//Timer t("../stats/seqlock_nopadding.txt");
struct Info {
int x, y, z;
};
const int NTHREADS = 50;
const int NTESTS = 5;
for (int nums = 0; nums < NTESTS; nums++) {
// Testing starts: initializing variables
chroniclock::seqlock<Info> s;
std::atomic<uint32_t> ct(0);
std::vector<std::thread> threads;
auto func = [&s, &ct]() {
while (ct.load(std::memory_order_acquire) == 0);
struct Info tmp;
for (int i = 0; i < (int)1e7; i++) {
while (!s.load(tmp));
assert(tmp.x == tmp.z/2 && tmp.y == tmp.x + tmp.z);
}
ct.fetch_sub(1, std::memory_order_release);
};
// Add function to NTHREADS threads, which will try to load
// 1e7 instances of struct Info
for (int i = 0; i < NTHREADS; i++) {
threads.push_back(std::thread(func));
}
int start = 10;
bool begin = false;
t.start();
// Will keep pushing Info until 1e7 has been read
while (true) {
struct Info tmp = { start/2, start/2 + start, start };
start++;
s.store(tmp);
if (!begin) {
begin = true;
ct.fetch_add(threads.size(), std::memory_order_release);
}
if (ct.load(std::memory_order_acquire) == 0) {
break;
}
}
for (auto &thr : threads) thr.join();
t.stop();
std::cout << "start: " << start << '\n';
}
t.printStats(); // Test complete, printing stats
// RIGTORP TEST
t.reset("../stats/seqlock_rigtorp.txt");
for (int nums = 0; nums < NTESTS; nums++) {
// Testing starts: initializing variables
rigtorp::Seqlock<Info> s;
std::atomic<uint32_t> ct(0);
std::vector<std::thread> threads;
auto func = [&s, &ct]() {
while (ct.load(std::memory_order_acquire) == 0);
struct Info tmp;
for (int i = 0; i < (int)1e7; i++) {
tmp = s.load();
assert(tmp.x == tmp.z/2 && tmp.y == tmp.x + tmp.z);
}
ct.fetch_sub(1, std::memory_order_release);
};
// Add function to NTHREADS threads, which will try to load
// 1e7 instances of struct Info
for (int i = 0; i < NTHREADS; i++) {
threads.push_back(std::thread(func));
}
int start = 10;
bool begin = false;
t.start();
// Will keep pushing Info until 1e7 has been read
while (true) {
struct Info tmp = { start/2, start/2 + start, start };
start++;
s.store(tmp);
if (!begin) {
begin = true;
ct.fetch_add(threads.size(), std::memory_order_release);
}
if (ct.load(std::memory_order_acquire) == 0) {
break;
}
}
for (auto &thr : threads) thr.join();
t.stop();
std::cout << "start: " << start << '\n';
}
t.printStats(); // Test complete, printing stats
}
if (RWTEST) {
Timer t("../stats/seqlock-spaced.txt");
//Timer t("../stats/seqlock_nopadding.txt");
struct Info {
int x, y, z;
};
const int NTHREADS = 50;
const int NTESTS = 5;
#define AMOUNT (int)1e7
for (int nums = 0; nums < NTESTS; nums++) {
// Testing starts: initializing variables
chroniclock::seqlock<Info> s;
std::atomic<uint32_t> ct(0);
std::vector<std::thread> threads;
auto func = [&s, &ct]() {
while (ct.load(std::memory_order_acquire) == 0);
struct Info tmp;
for (int i = 0; i < AMOUNT; i++) {
while (!s.load(tmp));
assert(tmp.x == tmp.z/2 && tmp.y == tmp.x + tmp.z);
}
ct.fetch_sub(1, std::memory_order_release);
};
// Add function to NTHREADS threads, which will try to load
// 1e7 instances of struct Info
for (int i = 0; i < NTHREADS; i++) {
threads.push_back(std::thread(func));
}
int start = 10;
bool begin = false;
t.start();
// Will keep pushing Info until 1e7 has been read
while (true) {
struct Info tmp = { start/2, start/2 + start, start };
start++;
s.store(tmp);
if (!begin) {
begin = true;
ct.fetch_add(threads.size(), std::memory_order_release);
}
if (ct.load(std::memory_order_acquire) == 0) {
break;
}
usleep(500);
}
for (auto &thr : threads) thr.join();
t.stop();
std::cout << "start: " << start << '\n';
}
t.printStats(); // Test complete, printing stats
t.reset("../stats/rwlock-spaced.txt");
for (int nums = 0; nums < NTESTS; nums++) {
// Testing starts: initializing variables
chroniclock::rwlock<Info> s;
std::atomic<uint32_t> ct(0);
std::vector<std::thread> threads;
auto func = [&s, &ct]() {
while (ct.load(std::memory_order_acquire) == 0);
struct Info tmp;
for (int i = 0; i < AMOUNT; i++) {
s.load(tmp);
assert(tmp.x == tmp.z/2 && tmp.y == tmp.x + tmp.z);
}
ct.fetch_sub(1, std::memory_order_release);
};
// Add function to NTHREADS threads, which will try to load
// 1e7 instances of struct Info
for (int i = 0; i < NTHREADS; i++) {
threads.push_back(std::thread(func));
}
int start = 10;
bool begin = false;
t.start();
// Will keep pushing Info until 1e7 has been read
while (true) {
struct Info tmp = { start/2, start/2 + start, start };
start++;
s.store(tmp);
if (!begin) {
begin = true;
ct.fetch_add(threads.size(), std::memory_order_release);
}
if (ct.load(std::memory_order_acquire) == 0) {
break;
}
usleep(500);
}
for (auto &thr : threads) thr.join();
t.stop();
std::cout << "start: " << start << '\n';
}
t.printStats(); // Test complete, printing stats
}
return 0;
}