-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.cpp
More file actions
148 lines (114 loc) · 3.34 KB
/
storage.cpp
File metadata and controls
148 lines (114 loc) · 3.34 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
#include "storage.hpp"
storage_t* storage; // for extern in storage.hpp
item_t::item_t(): started(false) ,completed(false), pin_count(0){
pthread_rwlock_init(&rw_lock, NULL);
}
item_t::~item_t(){
pthread_rwlock_destroy(&rw_lock);
}
void item_t::pin(){
pthread_rwlock_wrlock(&rw_lock);
++pin_count;
pthread_rwlock_unlock(&rw_lock);
}
void item_t::un_pin(int fd){
pthread_rwlock_wrlock(&rw_lock);
if (pin_count > 0){
--pin_count;
}
if (fd <= -1){
pthread_rwlock_unlock(&rw_lock);
return;
}
for (auto it = waiting_clients.begin(); it < waiting_clients.end(); ++it){
if (it->get_fd() == fd){
waiting_clients.erase(it);
break;
}
}
pthread_rwlock_unlock(&rw_lock);
}
void item_t::put_data(const std::string& s) noexcept{
pthread_rwlock_wrlock(&rw_lock);
data.append(s);
size_t len = waiting_clients.size();
for (size_t i = 0; i < len; ++i){
waiting_clients[i].notify();
}
waiting_clients.clear();
pthread_rwlock_unlock(&rw_lock);
}
int item_t::get_data(std::string& dst, size_t offset, size_t limit, const wait_context_t& wait_context) noexcept{
pthread_rwlock_wrlock(&rw_lock);
if (data.length() == offset && completed){
pthread_rwlock_unlock(&rw_lock);
return -1;
}
if (data.length() <= offset){
waiting_clients.push_back(wait_context);
wait_context.get_sel_con()->change_descriptor_mode(wait_context.get_fd(), 0);
pthread_rwlock_unlock(&rw_lock);
return 0;
}
dst = data.substr(offset, limit);
size_t ret = dst.length();
pthread_rwlock_unlock(&rw_lock);
return ret;
}
void item_t::set_completed(bool val) noexcept{
pthread_rwlock_wrlock(&rw_lock);
completed = val;
if (completed){
size_t len = waiting_clients.size();
for (size_t i = 0; i < len; ++i){
waiting_clients[i].notify();
}
waiting_clients.clear();
}
pthread_rwlock_unlock(&rw_lock);
}
bool item_t::set_started(bool val) noexcept {
pthread_rwlock_wrlock(&rw_lock);
bool prev = started;
started = val;
pthread_rwlock_unlock(&rw_lock);
return prev;
}
storage_t::storage_t() {
pthread_mutex_init(&lock, NULL);
}
std::pair<std::string, std::shared_ptr<item_t>> storage_t::get_item(const std::string& key) noexcept{
pthread_mutex_lock(&lock);
auto it = hash_map.find(key);
if (it == hash_map.end()){
std::shared_ptr<item_t> item = std::make_shared<item_t>();
auto result = hash_map.emplace(key, item);
result.first->second->pin();
pthread_mutex_unlock(&lock);
return *result.first;
}
it->second->pin();
pthread_mutex_unlock(&lock);
return *it;
}
storage_t::~storage_t(){
pthread_mutex_destroy(&lock);
}
void init_global_storage(){
storage = new storage_t();
}
void storage_t::remove_item(const std::string& key) noexcept{
pthread_mutex_lock(&lock);
hash_map.erase(key);
pthread_mutex_unlock(&lock);
}
bool storage_t::try_remove_if_unused(std::pair<std::string, std::shared_ptr<item_t>>& pair){
pthread_mutex_lock(&lock);
if (pair.second->get_pin_count() == 0){
hash_map.erase(pair.first);
pthread_mutex_unlock(&lock);
return true;
}
pthread_mutex_unlock(&lock);
return false;
}