-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathPool.h
57 lines (44 loc) · 1.18 KB
/
Pool.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
#ifndef RecoTracker_MkFitCore_src_Pool_h
#define RecoTracker_MkFitCore_src_Pool_h
#include "Matriplex/Memory.h"
#include "oneapi/tbb/concurrent_queue.h"
namespace mkfit {
/**
* Pool for helper objects. All functions are thread safe.
*/
template <typename TT>
class Pool {
public:
Pool() = default;
~Pool() { clear(); }
void clear() {
TT *x = nullptr;
while (m_stack.try_pop(x)) {
destroy(x);
}
}
size_t size() const { return m_stack.unsafe_size(); }
void populate(int threads = Config::numThreadsFinder) {
for (int i = 0; i < threads; ++i) {
m_stack.push(create());
}
}
auto makeOrGet() {
TT *x = nullptr;
if (not m_stack.try_pop(x)) {
x = create();
}
auto deleter = [this](TT *ptr) { this->addBack(ptr); };
return std::unique_ptr<TT, decltype(deleter)>(x, std::move(deleter));
}
private:
TT *create() { return new (Matriplex::aligned_alloc64(sizeof(TT))) TT; };
void destroy(TT *x) {
x->~TT();
std::free(x);
};
void addBack(TT *x) { m_stack.push(x); }
tbb::concurrent_queue<TT *> m_stack;
};
} // end namespace mkfit
#endif