forked from flexibity-team/boost-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialPortMgr.hpp
More file actions
59 lines (51 loc) · 1.45 KB
/
serialPortMgr.hpp
File metadata and controls
59 lines (51 loc) · 1.45 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
/*
* serialPortMgr.hpp
*
* Created on: Aug 13, 2015
* Author: Roman Savrulin <romeo.deepmind@gmail.com>
*/
#ifndef INCLUDE_FLEXIBITY_SERIALPORTMGR_HPP_
#define INCLUDE_FLEXIBITY_SERIALPORTMGR_HPP_
#include "flexibity/log.h"
#include "flexibity/ioSerial.hpp"
#include <jsoncpp/json/json.h>
#include <map>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <flexibity/genericMgr.hpp>
namespace Flexibity{
class serialPortMgr:
public genericMgr<ioSerial::sPtr> {
public:
typedef shared_ptr<serialPortMgr> sPtr;
static constexpr const char* threadPoolOption = "threadPool";
serialPortMgr(const Json::Value &cfg):
_serial_io_service(),
_threadPool(){
ILOG_INITSev(INFO);
setInstanceName(name());
populateItems(cfg, [&](const Json::Value& iCfg){
return make_shared<ioSerial>(_serial_io_service, iCfg);
});
size_t _size = 1;
size_t tp = cfg[threadPoolOption].asUInt();
if(tp != 0)
_size = tp;
for ( size_t i = 0; i < _size; ++i){
_threadPool.create_thread(boost::bind(&boost::asio::io_service::run, &_serial_io_service));
}
IINFO(_threadPool.size() << " serial worker thread(s) created");
}
virtual ~serialPortMgr(){
IINFO("waiting for " << _threadPool.size() << " thread(s) to exit");
_serial_io_service.stop();
_threadPool.join_all();
clear();
IINFO("Done");
}
boost::asio::io_service _serial_io_service;
private:
boost::thread_group _threadPool;
};
}
#endif /* INCLUDE_FLEXIBITY_SERIALPORTMGR_HPP_ */