Kea 3.1.1
io_service.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <asiolink/io_service.h>
11
12#include <unistd.h> // for some IPC/network system calls
13#include <netinet/in.h>
14#include <boost/shared_ptr.hpp>
15#include <sys/socket.h>
16
17#include <chrono>
18
19using namespace std::chrono;
20
21namespace isc {
22namespace asiolink {
23
24class IOServiceImpl {
29
30private:
31 IOServiceImpl(const IOService& source);
32 IOServiceImpl& operator=(const IOService& source);
33public:
36 io_service_(),
37 work_(boost::asio::make_work_guard(io_service_)) {
38 };
39
41 ~IOServiceImpl() = default;
43
48 void run() {
49 io_service_.run();
50 };
51
59 size_t runOne() {
60 return (static_cast<size_t>(io_service_.run_one()));
61 };
62
77 size_t runOneFor(size_t wait_time_usecs, bool& timed_out) {
78 size_t cnt = io_service_.run_one_for(microseconds(wait_time_usecs));
79 timed_out = (!cnt && !io_service_.stopped());
80 return (cnt);
81 };
82
89 size_t poll() {
90 return (static_cast<size_t>(io_service_.poll()));
91 };
92
99 size_t pollOne() {
100 return (static_cast<size_t>(io_service_.poll_one()));
101 };
102
106 void stop() {
107 io_service_.stop();
108 }
109
113 bool stopped() const {
114 return (io_service_.stopped());
115 }
116
118 void restart() {
119 io_service_.restart();
120 }
121
124 void stopWork() {
125 work_.reset();
126 }
127
134 boost::asio::io_context& getInternalIOService() {
135 return (io_service_);
136 }
137
141 void post(const std::function<void ()>& callback) {
142 boost::asio::post(io_service_, callback);
143 }
144
145private:
146 boost::asio::io_context io_service_;
147 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_;
148};
149
151}
152
155
156void
158 io_impl_->run();
159}
160
161size_t
163 return (io_impl_->runOne());
164}
165
166size_t
167IOService::runOneFor(size_t wait_time_usecs, bool& timed_out) {
168 return (io_impl_->runOneFor(wait_time_usecs, timed_out));
169};
170
171size_t
173 return (io_impl_->poll());
174}
175
176size_t
178 return (io_impl_->pollOne());
179}
180
181void
183 io_impl_->stop();
184}
185
186bool
188 return (io_impl_->stopped());
189}
190
191void
193 io_impl_->restart();
194}
195
196void
198 io_impl_->stopWork();
199}
200
201boost::asio::io_context&
203 return (io_impl_->getInternalIOService());
204}
205
206void
207IOService::post(const std::function<void ()>& callback) {
208 return (io_impl_->post(callback));
209}
210
211void
212IOService::stopAndPoll(bool ignore_errors) {
213 stop();
214 restart();
215 if (ignore_errors) {
216 try {
217 poll();
218 } catch (...) {
219 // Ignore all exceptions.
220 }
221 } else {
222 poll();
223 }
224}
225
226} // namespace asiolink
227} // namespace isc
Defines the logger used by the top-level component of kea-lfc.