-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathgc.h
136 lines (115 loc) · 3.67 KB
/
gc.h
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
#pragma once
#include <vector>
#include <assert.h>
#include <unordered_set>
#include <list>
struct ICollectable
{
virtual ~ICollectable() {}
bool FWillFreeChildDebug() { return false; }
};
template<typename T>
class GarbageCollector
{
struct EpochHolder
{
uint64_t tstamp;
std::unique_ptr<std::vector<std::unique_ptr<T>>> m_spvecObjs;
EpochHolder() {
m_spvecObjs = std::make_unique<std::vector<std::unique_ptr<T>>>();
}
// Support move operators
EpochHolder(EpochHolder &&other) = default;
EpochHolder &operator=(EpochHolder &&) = default;
bool operator<(uint64_t tstamp) const
{
return this->tstamp < tstamp;
}
bool operator==(uint64_t tstamp) const
{
return this->tstamp == tstamp;
}
};
public:
~GarbageCollector() {
// Silence TSAN errors
m_lock.lock();
}
uint64_t startEpoch()
{
std::unique_lock<fastlock> lock(m_lock);
++m_epochNext;
m_setepochOutstanding.insert(m_epochNext);
return m_epochNext;
}
void shutdown()
{
std::unique_lock<fastlock> lock(m_lock);
m_listepochs.clear();
m_setepochOutstanding.clear();
}
bool empty() const
{
std::unique_lock<fastlock> lock(m_lock);
return m_listepochs.empty();
}
void endEpoch(uint64_t epoch, bool fNoFree = false)
{
std::unique_lock<fastlock> lock(m_lock);
serverAssert(m_setepochOutstanding.find(epoch) != m_setepochOutstanding.end());
bool fMinElement = *std::min_element(m_setepochOutstanding.begin(), m_setepochOutstanding.end());
m_setepochOutstanding.erase(epoch);
if (fNoFree)
return;
std::list<EpochHolder> listclean;
// No outstanding epochs?
if (m_setepochOutstanding.empty())
{
listclean = std::move(m_listepochs); // Everything goes!
}
else
{
uint64_t minepoch = *std::min_element(m_setepochOutstanding.begin(), m_setepochOutstanding.end());
if (minepoch == 0)
return; // No available epochs to free
// Clean any epochs available (after the lock)
for (auto itr = m_listepochs.begin(); itr != m_listepochs.end(); /* itr incremented in loop*/)
{
auto &e = *itr;
auto itrNext = itr;
++itrNext;
if (e < minepoch)
{
listclean.emplace_back(std::move(e));
m_listepochs.erase(itr);
}
itr = itrNext;
}
assert(listclean.empty() || fMinElement);
}
lock.unlock(); // don't hold it for the potentially long delete of vecclean
}
void enqueue(uint64_t epoch, std::unique_ptr<T> &&sp)
{
std::unique_lock<fastlock> lock(m_lock);
serverAssert(m_setepochOutstanding.find(epoch) != m_setepochOutstanding.end());
serverAssert(sp->FWillFreeChildDebug() == false);
auto itr = std::find(m_listepochs.begin(), m_listepochs.end(), m_epochNext+1);
if (itr == m_listepochs.end())
{
EpochHolder e;
e.tstamp = m_epochNext+1;
e.m_spvecObjs->push_back(std::move(sp));
m_listepochs.emplace_back(std::move(e));
}
else
{
itr->m_spvecObjs->push_back(std::move(sp));
}
}
private:
mutable fastlock m_lock { "Garbage Collector"};
std::list<EpochHolder> m_listepochs;
std::unordered_set<uint64_t> m_setepochOutstanding;
uint64_t m_epochNext = 0;
};