-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ring.cpp
83 lines (81 loc) · 2.04 KB
/
test_ring.cpp
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
#include <cstdio>
#include <vector>
#include <thread>
#include "spsc_ring.hpp"
constexpr int pdsz = 256;
int singlethread(void) {
struct hh_t {
int log_id;
char data;
};
coring::spsc_ring<hh_t> my_ring(pdsz * 2);
std::vector<hh_t> produce_queue(pdsz);
for (int i = 0; i < pdsz; i++) {
produce_queue[i].log_id = i;
produce_queue[i].data = ('a' + i) & 0x7f;
}
std::vector<hh_t> consume_queue;
for (int i = 0; i < pdsz; ++i) {
assert(my_ring.try_push(produce_queue[i]));
}
hh_t* dt = my_ring.data();
int sz = my_ring.size();
for (int i = 0; i < pdsz; i++) {
auto p = my_ring.front();
consume_queue.emplace_back(*p);
my_ring.pop();
}
assert(consume_queue.size() == pdsz);
for (int i = 0; i < pdsz; i++) {
assert(consume_queue[i].log_id == i);
assert(consume_queue[i].data == (('a' + i) & 0x7f));
}
assert(my_ring.size() == 0);
return 0;
}
int multithread(void) {
struct hh_t {
int log_id;
char data;
};
coring::spsc_ring<hh_t> my_ring(pdsz * 2);
std::vector<hh_t> produce_queue(pdsz);
for (int i = 0; i < pdsz; i++) {
produce_queue[i].log_id = i;
produce_queue[i].data = ('a' + i) & 0x7f;
}
std::vector<hh_t> consume_queue;
std::jthread producer([&]() {
for (int i = 0; i < pdsz; ++i) {
while (__glibc_unlikely(!my_ring.try_push(produce_queue[i])))
;
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
});
std::jthread consumer([&]() {
for (int i = 0; i < pdsz; i++) {
auto p = my_ring.front();
while (p == nullptr) {
// LOG_DEBUG("fuck loop, consumer\n");
p = my_ring.front();
}
consume_queue.emplace_back(*p);
my_ring.pop();
}
});
producer.join();
consumer.join();
assert(consume_queue.size() == pdsz);
for (int i = 0; i < pdsz; i++) {
assert(consume_queue[i].log_id == i);
assert(consume_queue[i].data == (('a' + i) & 0x7f));
}
assert(my_ring.size() == 0);
return 0;
}
int main(void) {
singlethread();
multithread();
printf("All test passed\n");
return 0;
}