forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutorService.cc
More file actions
164 lines (141 loc) · 5.07 KB
/
ExecutorService.cc
File metadata and controls
164 lines (141 loc) · 5.07 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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "ExecutorService.h"
#include <algorithm>
#include "LogUtils.h"
#include "TimeUtils.h"
DECLARE_LOG_OBJECT()
namespace pulsar {
ExecutorService::ExecutorService() {}
ExecutorService::~ExecutorService() { close(0); }
void ExecutorService::start() {
auto self = shared_from_this();
std::thread t{[this, self] {
LOG_DEBUG("Run io_context in a single thread");
while (!closed_) {
io_context_.restart();
auto work_guard = ASIO::make_work_guard(io_context_);
try {
io_context_.run();
LOG_DEBUG("Event loop of ExecutorService exits successfully");
} catch (const ASIO_SYSTEM_ERROR &e) {
LOG_ERROR("Failed to run io_context: " << e.what());
}
}
{
std::lock_guard<std::mutex> lock{mutex_};
ioServiceDone_ = true;
}
cond_.notify_all();
}};
t.detach();
}
ExecutorServicePtr ExecutorService::create() {
// make_shared cannot access the private constructor, so we need to expose the private constructor via a
// derived class.
struct ExecutorServiceImpl : public ExecutorService {};
auto executor = std::make_shared<ExecutorServiceImpl>();
executor->start();
return std::static_pointer_cast<ExecutorService>(executor);
}
/*
* factory method of ASIO::ip::tcp::socket associated with io_context_ instance
* @ returns shared_ptr to this socket
*/
SocketPtr ExecutorService::createSocket() {
try {
return SocketPtr(new ASIO::ip::tcp::socket(io_context_));
} catch (const ASIO_SYSTEM_ERROR &e) {
restart();
auto error = std::string("Failed to create socket: ") + e.what();
throw std::runtime_error(error);
}
}
TlsSocketPtr ExecutorService::createTlsSocket(SocketPtr &socket, ASIO::ssl::context &ctx) {
return std::shared_ptr<ASIO::ssl::stream<ASIO::ip::tcp::socket &>>(
new ASIO::ssl::stream<ASIO::ip::tcp::socket &>(*socket, ctx));
}
/*
* factory method of Resolver object associated with io_context_ instance
* @returns shraed_ptr to resolver object
*/
TcpResolverPtr ExecutorService::createTcpResolver() {
try {
return TcpResolverPtr(new ASIO::ip::tcp::resolver(io_context_));
} catch (const ASIO_SYSTEM_ERROR &e) {
restart();
auto error = std::string("Failed to create resolver: ") + e.what();
throw std::runtime_error(error);
}
}
DeadlineTimerPtr ExecutorService::createDeadlineTimer() {
try {
return DeadlineTimerPtr(new ASIO::steady_timer(io_context_));
} catch (const ASIO_SYSTEM_ERROR &e) {
restart();
auto error = std::string("Failed to create steady_timer: ") + e.what();
throw std::runtime_error(error);
}
}
void ExecutorService::restart() { io_context_.stop(); }
void ExecutorService::close(long timeoutMs) {
bool expectedState = false;
if (!closed_.compare_exchange_strong(expectedState, true)) {
return;
}
if (timeoutMs == 0) { // non-blocking
io_context_.stop();
return;
}
std::unique_lock<std::mutex> lock{mutex_};
io_context_.stop();
if (timeoutMs > 0) {
cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this] { return ioServiceDone_; });
} else { // < 0
cond_.wait(lock, [this] { return ioServiceDone_; });
}
}
void ExecutorService::postWork(std::function<void(void)> task) { ASIO::post(io_context_, std::move(task)); }
/////////////////////
ExecutorServiceProvider::ExecutorServiceProvider(int nthreads)
: executors_(std::max(1, nthreads)), executorIdx_(0), mutex_() {}
ExecutorServicePtr ExecutorServiceProvider::get(size_t idx) {
if (executors_.empty()) {
return nullptr;
}
idx %= executors_.size();
Lock lock(mutex_);
if (!executors_[idx]) {
executors_[idx] = ExecutorService::create();
}
return executors_[idx];
}
void ExecutorServiceProvider::close(long timeoutMs) {
Lock lock(mutex_);
TimeoutProcessor<std::chrono::milliseconds> timeoutProcessor{timeoutMs};
for (auto &&executor : executors_) {
timeoutProcessor.tik();
if (executor) {
executor->close(timeoutProcessor.getLeftTimeout());
}
timeoutProcessor.tok();
executor.reset();
}
}
} // namespace pulsar