-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPool_Manager.cpp
56 lines (49 loc) · 1.54 KB
/
Pool_Manager.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
#include "Pool_Manager.h"
#include "Uniswap_V2.h"
std::unique_ptr<Pool_Manager> Pool_Manager::instance = nullptr;
std::mutex Pool_Manager::mutex;
Pool_Manager* Pool_Manager::getInstance() {
std::lock_guard<std::mutex> lock(mutex);
if (!instance) {
instance = std::unique_ptr<Pool_Manager>(new Pool_Manager());
}
return instance.get();
}
uint64_t Pool_Manager::addPool(std::shared_ptr<LiquidityPool> pool) {
std::lock_guard<std::mutex> lock(mutex);
uint64_t poolId;
if (!reusableIds.empty()) {
poolId = reusableIds.top();
reusableIds.pop();
} else {
poolId = nextPoolId++;
}
pools[poolId] = std::move(pool);
return poolId;
}
/**
* Use Weak_ptr to avoid dangling ptr issue.
* A thread can obtain a ptr to a pool, stay idle, and the pool is destroyed by another thread
*/
std::weak_ptr<LiquidityPool> Pool_Manager::getPool(uint64_t id) {
std::lock_guard<std::mutex> lock(mutex);
auto it = pools.find(id);
if (it != pools.end()) {
return it->second;
}
return std::weak_ptr<LiquidityPool>();
}
std::shared_ptr<LiquidityPool> Pool_Manager::removePool(uint64_t id) {
std::lock_guard<std::mutex> lock(mutex);
auto it = pools.find(id);
if (it != pools.end()) {
std::shared_ptr<LiquidityPool> pool = it->second;
if (pool) {
std::lock_guard<std::mutex> pool_lock(pool->getMutex());
pools.erase(it);
reusableIds.push(id);
return pool;
}
}
return nullptr;
}