forked from qicosmos/cinatra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_service_pool.hpp
More file actions
115 lines (93 loc) · 2.45 KB
/
Copy pathio_service_pool.hpp
File metadata and controls
115 lines (93 loc) · 2.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
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
#pragma once
#include "use_asio.hpp"
#include <vector>
#include <memory>
#include <thread>
#include "utils.hpp"
namespace cinatra
{
class io_service_pool : private noncopyable{
public:
explicit io_service_pool(std::size_t pool_size) : next_io_service_(0) {
if (pool_size == 0)
pool_size = 1; //set default value as 1
for (std::size_t i = 0; i < pool_size; ++i)
{
io_service_ptr io_service(new boost::asio::io_service);
work_ptr work(new boost::asio::io_service::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
}
}
void run() {
std::vector<std::shared_ptr<std::thread> > threads;
for (std::size_t i = 0; i < io_services_.size(); ++i){
threads.emplace_back(std::make_shared<std::thread>(
[](io_service_ptr svr) {
svr->run();
}, io_services_[i]));
}
for (std::size_t i = 0; i < threads.size(); ++i)
threads[i]->join();
}
intptr_t run_one() {
return -1;
}
intptr_t poll() {
return -1;
}
intptr_t poll_one() {
return -1;
}
void stop() {
work_.clear();
for (std::size_t i = 0; i < io_services_.size(); ++i)
io_services_[i]->stop();
}
boost::asio::io_service& get_io_service() {
boost::asio::io_service& io_service = *io_services_[next_io_service_];
++next_io_service_;
if (next_io_service_ == io_services_.size())
next_io_service_ = 0;
return io_service;
}
private:
using io_service_ptr = std::shared_ptr<boost::asio::io_service>;
using work_ptr = std::shared_ptr<boost::asio::io_service::work>;
std::vector<io_service_ptr> io_services_;
std::vector<work_ptr> work_;
std::size_t next_io_service_;
};
class io_service_inplace : private noncopyable{
public:
explicit io_service_inplace() {
io_services_ = std::make_shared<boost::asio::io_service>();
work_ = std::make_shared<boost::asio::io_service::work>(*io_services_);
}
void run() {
io_services_->run();
}
intptr_t run_one() {
return io_services_->run_one();
}
intptr_t poll() {
return io_services_->poll();
}
intptr_t poll_one() {
return io_services_->poll_one();
}
void stop() {
work_ = nullptr;
if (io_services_)
io_services_->stop();
}
boost::asio::io_service& get_io_service() {
return *io_services_;
}
private:
using io_service_ptr = std::shared_ptr<boost::asio::io_service>;
using work_ptr = std::shared_ptr<boost::asio::io_service::work>;
io_service_ptr io_services_;
work_ptr work_;
};
}