/
shm_reader.cc
74 lines (67 loc) · 2.14 KB
/
shm_reader.cc
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
#include <bits/stdc++.h>
#include "shm_common.h"
#include "cpupin.h"
using namespace std;
int lounger = 0;
volatile bool running = true;
Q* q;
void signalHandler(int s) {
running = false;
}
void reader() {
while(running) {
// The upside of checking empty() is to avoid waiting for reading
// while the downside is the additional latency
/*
if(q->empty()) {
this_thread::yield();
continue;
}
*/
if(lounger == 1) {
Entry cur = q->pop();
auto ts = now();
cout << "tid: " << cur.tid << " val: " << cur.val << " latency: " << (ts - cur.ts) << " cycles" << endl;
}
else if(lounger == 2) {
Entry cur;
// we can't check running flag here because we must commit the idx we got before exiting
// so program would hang if no writer exits, uncomment the empty check above to avoid this issue
while(!q->tryPop(cur))
;
auto ts = now();
cout << "tid: " << cur.tid << " val: " << cur.val << " latency: " << (ts - cur.ts) << " cycles" << endl;
}
else if(lounger == 3) {
while(!q->tryVisitPop([&](Entry&& cur) {
auto ts = now();
cout << "tid: " << cur.tid << " val: " << cur.val << " latency: " << (ts - cur.ts) << " cycles" << endl;
}))
;
}
else {
int64_t idx = q->getReadIdx();
Entry* data;
// similar to lounger == 2
while((data = q->getReadable(idx)) == nullptr)
;
auto ts = now();
cout << "tid: " << data->tid << " val: " << data->val << " latency: " << (ts - data->ts) << " cycles"
<< endl;
q->commitRead(idx);
}
}
}
int main(int argc, char** argv) {
if(argc > 1) lounger = stoi(argv[1]);
if(argc > 2) {
int cpuid = stoi(argv[2]);
if(!cpupin(cpuid)) return 1;
}
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
q = getQ();
if(q == nullptr) return 1;
reader();
return 0;
}