-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_buffer.h
More file actions
397 lines (329 loc) · 9.77 KB
/
double_buffer.h
File metadata and controls
397 lines (329 loc) · 9.77 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#pragma once
#include "yaml-cpp/yaml.h"
#include <string>
#include <vector>
#include <memory>
#include <thread>
#include <unordered_map>
#include <memory>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <utility>
#include <mutex>
#include <iostream>
#include <pthread.h>
#include <atomic>
namespace inf {
namespace utils {
const int64_t DEFAULT_INTERVAL = 3;
/**
* @class SwitchMonitor.
* moniter the file info, and decicde wether the config file need reload
**/
class SwitchMonitor {
public:
/* ctor. */
SwitchMonitor() = default;
virtual ~SwitchMonitor() = default;
/**
* init the monitor file name
* monite the file status.
* @param std::string file_name
* @return bool true: ok, false: failed
*/
virtual bool init(const std::string& file_name) {
if (file_name.empty()) {
return false;
}
struct stat file_status;
if (file_name.empty() || stat(file_name.c_str(), &file_status) != 0) {
return false;
}
_file_status_table.insert(std::make_pair(file_name, file_status.st_mtime));
return true;
}
/**
* init the monitor file name, batch version
* @param std;:vector<std::string> file_names
* @return bool true: ok, false: failed
*/
virtual bool init(const std::vector<std::string>& file_names) {
if (file_names.empty()) {
return false;
}
struct stat file_status;
for (auto &file_name : file_names) {
if (file_name.empty() && stat(file_name.c_str(), &file_status) != 0) {
return false;
}
_file_status_table.insert(std::make_pair(file_name, file_status.st_mtime));
}
return true;
}
/**
* get monitor file list
* @return std::unoredred_map<std::string> file list
*/
std::unordered_map<std::string, time_t> get_monitor_file_list() const {
return _file_status_table;
}
/**
* whether the buffer need to be relaod.
* @return std::vector<std::string> file lists need to update
*/
std::vector<std::string> get_need_switch_file() {
std::vector<std::string> update_file_names;
for (auto &[file_name, last_modify_time] : _file_status_table) {
struct stat file_status;
if (stat(file_name.c_str(), &file_status) !=0) {
return update_file_names;
}
if (file_status.st_mtime > last_modify_time) {
update_file_names.push_back(file_name);
last_modify_time = file_status.st_mtime;
}
}
return update_file_names;
}
private:
/* file status map. */
std::unordered_map<std::string, time_t> _file_status_table;
};
/**
* @class DoubleData.
* double data has the BuferType and it's own loader
* Loader must implement realod() function to do specific things.
**/
template <typename BufferType, typename Loader>
class DoubleData {
public:
typedef std::shared_ptr<BufferType> BufferPtr;
typedef std::unique_ptr<Loader> LoaderPtr;
typedef std::unique_ptr<std::thread> ThreadPtr;
/* ctor. */
DoubleData(LoaderPtr loader, int64_t interval = DEFAULT_INTERVAL, bool is_monitor = false)
: _loader(std::move(loader)), _interval(interval), _is_monitor(is_monitor) {
};
/* dtor. */
virtual ~DoubleData() {
if (_is_monitor && _monitor_thread && _monitor_thread->joinable()) {
_monitor_thread->join();
}
}
/**
* init the double buffer, using Loader to load the buffer.
* @return 0:ok, -1:failed.
*/
int init() {
std::lock_guard<std::mutex> lock(_lock);
_current = std::make_shared<BufferType>();
_backup = std::make_shared<BufferType>();
*_current = _loader->load();
*_backup = *_current;
_monitor.init(_loader->get_load_file_name());
if (_is_monitor) {
_monitor_thread.reset(new std::thread(&DoubleData::run, this));
}
return 0;
}
void run () {
//detect file per interval
while (_is_monitor) {
auto update_files = _monitor.get_need_switch_file();
if (!update_files.empty()){
swap_data();
std::cout << "swith file now " << std::endl;
}
sleep(_interval);
}
}
/**
* change the backup and front data
* @return
*/
bool swap_data() {
//only one thread can manipulate
std::lock_guard<std::mutex> lock(_lock);
//make sure the backup data has no user.
// if (_backup.use_count() > 1) {
// return false;
// }
*_backup = _loader->load();
_current.swap(_backup);
return true;
}
/**
* get current data ptr, using this data on front.
* @return std::shared_ptr<BufferType>
*/
BufferPtr get_current() {
std::lock_guard<std::mutex> lock(_lock);
return _current;
}
/* get backup data. this interface is only use for test,
DO NOT USE IT IN BUSSINESS!!!*/
BufferPtr get_backup() {
return _backup;
}
private:
/* none-copy. */
DoubleData(const DoubleData &rhs) = delete;
DoubleData &operator=(const DoubleData &rhs) = delete;
/* front data ptr, always using this ptr. */
BufferPtr _current;
/* backup data ptr, update this data. */
BufferPtr _backup;
/* swap front and backup data lock. */
std::mutex _lock;
/* data loader. Loader must implement load() and return std::shared_ptr<BufferType> */
LoaderPtr _loader;
/* whether use auto update. */
std::atomic<bool> _is_monitor;
/* file monitor interval. */
int64_t _interval{60};
/* file SwitchMonitor. need to be init afer DoubleBuffer init()*/
SwitchMonitor _monitor;
/* thread ptr. monitor the conf file. */
ThreadPtr _monitor_thread;
};
/**
* @class YamlLoader.
* an Loader example
* load from yaml file, it's a recommend way to implement init and load funtion to use double buffer.
* init() function only load the config file name
* load() funciont parse the config file and return BufferType
**/
class YamlLoader {
public:
/* ctor. */
YamlLoader() = default;
virtual ~YamlLoader() = default;
/* init function, read file from yaml. */
bool init(const std::string &file_name) {
if (file_name.empty()) {
return false;
}
_config_file_name = file_name;
//whether the file exists
struct stat file_stat;
if (stat(file_name.c_str(), &file_stat) != 0) {
return false;
}
return true;
}
/* config file name, user relative path*/
std::string get_load_file_name() const {
return _config_file_name;
}
/* load funtion, always load latest file, and parse data. */
YAML::Node load() {
return YAML::LoadFile(_config_file_name.c_str());
}
private:
YamlLoader(const YamlLoader &rhs) = delete;
YamlLoader &operator=(const YamlLoader &rhs) = delete;
std::string _config_file_name {""};
};
//not implement now
class ConfManager {
public:
ConfManager() = default;
virtual ~ConfManager() = default;
static ConfManager& get_instance() {
static ConfManager instance;
return instance;
}
/**
* init conf file by file name
* not implement now.
* @param std::vector<std::string> file_names
* @return -1:Failed, 0:OK
*/
int init(const std::string& file_names) {
if (file_names.empty()) {
return -1;
}
return 0;
}
private:
typedef std::unordered_map<std::string, std::string> ConfMap;
typedef std::unique_ptr<ConfMap> ConfMapPtr;
typedef std::unordered_map<std::string, ConfMapPtr> ConfTable;
typedef std::unique_ptr<std::thread> ThreadPtr;
ConfManager(const ConfManager &rhs) = delete;
ConfManager &operator=(const ConfManager &rhs) = delete;
std::string _file_name;
ConfTable _conf_file_table;
static std::mutex _file_lock;
ThreadPtr _monitor_thread;
};
//rw lock implement
class RW_LOCK {
private:
/* ctor. */
RW_LOCK() {
_rw_lock = PTHREAD_RWLOCK_INITIALIZER;
}
/* dtor. */
virtual ~RW_LOCK() {
pthread_rwlock_destroy(&_rw_lock);
}
/* read lock, multi thread can read without data race. */
int read_lock() {
return pthread_rwlock_rdlock(&_rw_lock);
}
/* write lock, only one thread can hold this lock. when the lock is locked, read locks and other write locks will be blocked. */
int write_lock() {
return pthread_rwlock_wrlock(&_rw_lock);
}
int unlock() {
return pthread_rwlock_unlock(&_rw_lock);
}
private:
pthread_rwlock_t _rw_lock;
};
/**
* @class SopeLock Wrapper.
**/
template <typename LockType>
class ScopeLock {
/* ctor. */
ScopeLock();
/* dtor. */
virtual ~ScopeLock();
};
/**
* @class ReadLockWrapper.
**/
template <typename LockType>
class ReadLockWrapper : public ScopeLock<LockType>{
public:
/* ctor. */
ReadLockWrapper() {
_rw_lock.read_lock();
}
virtual ~ReadLockWrapper() {
_rw_lock.unlock();
}
private:
LockType _rw_lock;
};
/**
* @class WriteLockWrapper.
**/
template <typename LockType>
class WriteLockWrapper : public ScopeLock<LockType> {
/* ctor. */
WriteLockWrapper() {
_rw_lock.write_lock();
}
/* ctor. */
virtual ~WriteLockWrapper() {
_rw_lock.unlock();
}
private:
LockType _rw_lock;
};
} // end namespace utils
} // end namespace inf