| 1 |
// <experimental/io_service> -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// Under Section 7 of GPL version 3, you are granted additional |
| 17 |
// permissions described in the GCC Runtime Library Exception, version |
| 18 |
// 3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License and |
| 21 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
// <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
/** @file experimental/io_context |
| 26 |
* This is a TS C++ Library header. |
| 27 |
* @ingroup networking-ts |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef _GLIBCXX_EXPERIMENTAL_IO_SERVICE |
| 31 |
#define _GLIBCXX_EXPERIMENTAL_IO_SERVICE 1 |
| 32 |
|
| 33 |
#ifdef _GLIBCXX_SYSHDR |
| 34 |
#pragma GCC system_header |
| 35 |
#endif |
| 36 |
|
| 37 |
#include <bits/requires_hosted.h> // experimental is currently omitted |
| 38 |
|
| 39 |
#if __cplusplus >= 201402L |
| 40 |
|
| 41 |
#include <atomic> |
| 42 |
#include <forward_list> |
| 43 |
#include <functional> |
| 44 |
#include <system_error> |
| 45 |
#include <thread> |
| 46 |
#include <vector> |
| 47 |
#include <experimental/netfwd> |
| 48 |
#include <experimental/executor> |
| 49 |
#include <bits/chrono.h> |
| 50 |
#if _GLIBCXX_HAVE_UNISTD_H |
| 51 |
# include <unistd.h> |
| 52 |
#endif |
| 53 |
#ifdef _GLIBCXX_HAVE_POLL_H |
| 54 |
# include <poll.h> |
| 55 |
#endif |
| 56 |
#ifdef _GLIBCXX_HAVE_FCNTL_H |
| 57 |
# include <fcntl.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 61 |
{ |
| 62 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 63 |
namespace experimental |
| 64 |
{ |
| 65 |
namespace net |
| 66 |
{ |
| 67 |
inline namespace v1 |
| 68 |
{ |
| 69 |
|
| 70 |
/** @addtogroup networking-ts |
| 71 |
* @{ |
| 72 |
*/ |
| 73 |
|
| 74 |
class __socket_impl; |
| 75 |
|
| 76 |
/// An ExecutionContext for I/O operations. |
| 77 |
class io_context : public execution_context |
| 78 |
{ |
| 79 |
public: |
| 80 |
// types: |
| 81 |
|
| 82 |
/// An executor for an io_context. |
| 83 |
class executor_type |
| 84 |
{ |
| 85 |
public: |
| 86 |
// construct / copy / destroy: |
| 87 |
|
| 88 |
executor_type(const executor_type& __other) noexcept = default; |
| 89 |
executor_type(executor_type&& __other) noexcept = default; |
| 90 |
|
| 91 |
executor_type& operator=(const executor_type& __other) noexcept = default; |
| 92 |
executor_type& operator=(executor_type&& __other) noexcept = default; |
| 93 |
|
| 94 |
// executor operations: |
| 95 |
|
| 96 |
bool running_in_this_thread() const noexcept |
| 97 |
{ |
| 98 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 99 |
lock_guard<execution_context::mutex_type> __lock(_M_ctx->_M_mtx); |
| 100 |
auto __end = _M_ctx->_M_call_stack.end(); |
| 101 |
return std::find(_M_ctx->_M_call_stack.begin(), __end, |
| 102 |
this_thread::get_id()) != __end; |
| 103 |
#else |
| 104 |
return _M_ctx->_M_run_count != 0; |
| 105 |
#endif |
| 106 |
} |
| 107 |
|
| 108 |
io_context& context() const noexcept { return *_M_ctx; } |
| 109 |
|
| 110 |
void on_work_started() const noexcept { ++_M_ctx->_M_work_count; } |
| 111 |
void on_work_finished() const noexcept { --_M_ctx->_M_work_count; } |
| 112 |
|
| 113 |
template<typename _Func, typename _ProtoAllocator> |
| 114 |
void |
| 115 |
dispatch(_Func&& __f, const _ProtoAllocator& __a) const |
| 116 |
{ |
| 117 |
if (running_in_this_thread()) |
| 118 |
decay_t<_Func>{std::forward<_Func>(__f)}(); |
| 119 |
else |
| 120 |
post(std::forward<_Func>(__f), __a); |
| 121 |
} |
| 122 |
|
| 123 |
template<typename _Func, typename _ProtoAllocator> |
| 124 |
void |
| 125 |
post(_Func&& __f, const _ProtoAllocator& __a) const |
| 126 |
{ |
| 127 |
lock_guard<execution_context::mutex_type> __lock(_M_ctx->_M_mtx); |
| 128 |
// TODO (re-use functionality in system_context) |
| 129 |
_M_ctx->_M_reactor._M_notify(); |
| 130 |
} |
| 131 |
|
| 132 |
template<typename _Func, typename _ProtoAllocator> |
| 133 |
void |
| 134 |
defer(_Func&& __f, const _ProtoAllocator& __a) const |
| 135 |
{ post(std::forward<_Func>(__f), __a); } |
| 136 |
|
| 137 |
private: |
| 138 |
friend io_context; |
| 139 |
|
| 140 |
explicit |
| 141 |
executor_type(io_context& __ctx) : _M_ctx(std::addressof(__ctx)) { } |
| 142 |
|
| 143 |
io_context* _M_ctx; |
| 144 |
}; |
| 145 |
|
| 146 |
using count_type = size_t; |
| 147 |
|
| 148 |
// construct / copy / destroy: |
| 149 |
|
| 150 |
io_context() : _M_work_count(0) { } |
| 151 |
|
| 152 |
explicit |
| 153 |
io_context(int /* __concurrency_hint */) : _M_work_count(0) { } |
| 154 |
|
| 155 |
io_context(const io_context&) = delete; |
| 156 |
io_context& operator=(const io_context&) = delete; |
| 157 |
|
| 158 |
// io_context operations: |
| 159 |
|
| 160 |
executor_type get_executor() noexcept { return executor_type(*this); } |
| 161 |
|
| 162 |
count_type |
| 163 |
run() |
| 164 |
{ |
| 165 |
count_type __n = 0; |
| 166 |
while (run_one()) |
| 167 |
if (__n != numeric_limits<count_type>::max()) |
| 168 |
++__n; |
| 169 |
return __n; |
| 170 |
} |
| 171 |
|
| 172 |
template<typename _Rep, typename _Period> |
| 173 |
count_type |
| 174 |
run_for(const chrono::duration<_Rep, _Period>& __rel_time) |
| 175 |
{ return run_until(chrono::steady_clock::now() + __rel_time); } |
| 176 |
|
| 177 |
template<typename _Clock, typename _Duration> |
| 178 |
count_type |
| 179 |
run_until(const chrono::time_point<_Clock, _Duration>& __abs_time) |
| 180 |
{ |
| 181 |
count_type __n = 0; |
| 182 |
while (run_one_until(__abs_time)) |
| 183 |
if (__n != numeric_limits<count_type>::max()) |
| 184 |
++__n; |
| 185 |
return __n; |
| 186 |
} |
| 187 |
|
| 188 |
count_type |
| 189 |
run_one() |
| 190 |
{ return _M_do_one(chrono::milliseconds{-1}); } |
| 191 |
|
| 192 |
template<typename _Rep, typename _Period> |
| 193 |
count_type |
| 194 |
run_one_for(const chrono::duration<_Rep, _Period>& __rel_time) |
| 195 |
{ return run_one_until(chrono::steady_clock::now() + __rel_time); } |
| 196 |
|
| 197 |
template<typename _Clock, typename _Duration> |
| 198 |
count_type |
| 199 |
run_one_until(const chrono::time_point<_Clock, _Duration>& __abs_time) |
| 200 |
{ |
| 201 |
auto __now = _Clock::now(); |
| 202 |
while (__now < __abs_time) |
| 203 |
{ |
| 204 |
using namespace std::chrono; |
| 205 |
auto __ms = duration_cast<milliseconds>(__abs_time - __now); |
| 206 |
if (_M_do_one(__ms)) |
| 207 |
return 1; |
| 208 |
__now = _Clock::now(); |
| 209 |
} |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
count_type |
| 214 |
poll() |
| 215 |
{ |
| 216 |
count_type __n = 0; |
| 217 |
while (poll_one()) |
| 218 |
if (__n != numeric_limits<count_type>::max()) |
| 219 |
++__n; |
| 220 |
return __n; |
| 221 |
} |
| 222 |
|
| 223 |
count_type |
| 224 |
poll_one() |
| 225 |
{ return _M_do_one(chrono::milliseconds{0}); } |
| 226 |
|
| 227 |
void stop() |
| 228 |
{ |
| 229 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 230 |
_M_stopped = true; |
| 231 |
_M_reactor._M_notify(); |
| 232 |
} |
| 233 |
|
| 234 |
bool stopped() const noexcept |
| 235 |
{ |
| 236 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 237 |
return _M_stopped; |
| 238 |
} |
| 239 |
|
| 240 |
void restart() |
| 241 |
{ |
| 242 |
_M_stopped = false; |
| 243 |
} |
| 244 |
|
| 245 |
private: |
| 246 |
|
| 247 |
template<typename _Clock, typename _WaitTraits> |
| 248 |
friend class basic_waitable_timer; |
| 249 |
|
| 250 |
friend __socket_impl; |
| 251 |
|
| 252 |
template<typename _Protocol> |
| 253 |
friend class __basic_socket_impl; |
| 254 |
|
| 255 |
template<typename _Protocol> |
| 256 |
friend class basic_socket; |
| 257 |
|
| 258 |
template<typename _Protocol> |
| 259 |
friend class basic_datagram_socket; |
| 260 |
|
| 261 |
template<typename _Protocol> |
| 262 |
friend class basic_stream_socket; |
| 263 |
|
| 264 |
template<typename _Protocol> |
| 265 |
friend class basic_socket_acceptor; |
| 266 |
|
| 267 |
count_type |
| 268 |
_M_outstanding_work() const |
| 269 |
{ return _M_work_count + !_M_ops.empty(); } |
| 270 |
|
| 271 |
struct __timer_queue_base : execution_context::service |
| 272 |
{ |
| 273 |
// return milliseconds until next timer expires, or milliseconds::max() |
| 274 |
virtual chrono::milliseconds _M_next() const = 0; |
| 275 |
virtual bool run_one() = 0; |
| 276 |
|
| 277 |
protected: |
| 278 |
explicit |
| 279 |
__timer_queue_base(execution_context& __ctx) : service(__ctx) |
| 280 |
{ |
| 281 |
auto& __ioc = static_cast<io_context&>(__ctx); |
| 282 |
lock_guard<execution_context::mutex_type> __lock(__ioc._M_mtx); |
| 283 |
__ioc._M_timers.push_back(this); |
| 284 |
} |
| 285 |
|
| 286 |
mutable execution_context::mutex_type _M_qmtx; |
| 287 |
}; |
| 288 |
|
| 289 |
template<typename _Timer, typename _Key = typename _Timer::_Key> |
| 290 |
struct __timer_queue : __timer_queue_base |
| 291 |
{ |
| 292 |
using key_type = __timer_queue; |
| 293 |
|
| 294 |
explicit |
| 295 |
__timer_queue(execution_context& __ctx) : __timer_queue_base(__ctx) |
| 296 |
{ } |
| 297 |
|
| 298 |
void shutdown() noexcept { } |
| 299 |
|
| 300 |
io_context& context() noexcept |
| 301 |
{ return static_cast<io_context&>(service::context()); } |
| 302 |
|
| 303 |
// Start an asynchronous wait. |
| 304 |
void |
| 305 |
push(const _Timer& __t, function<void(error_code)> __h) |
| 306 |
{ |
| 307 |
context().get_executor().on_work_started(); |
| 308 |
lock_guard<execution_context::mutex_type> __lock(_M_qmtx); |
| 309 |
_M_queue.emplace(__t, _M_next_id++, std::move(__h)); |
| 310 |
// no need to notify reactor unless this timer went to the front? |
| 311 |
} |
| 312 |
|
| 313 |
// Cancel all outstanding waits for __t |
| 314 |
size_t |
| 315 |
cancel(const _Timer& __t) |
| 316 |
{ |
| 317 |
lock_guard<execution_context::mutex_type> __lock(_M_qmtx); |
| 318 |
size_t __count = 0; |
| 319 |
auto __last = _M_queue.end(); |
| 320 |
for (auto __it = _M_queue.begin(), __end = __last; __it != __end; |
| 321 |
++__it) |
| 322 |
{ |
| 323 |
if (__it->_M_key == __t._M_key.get()) |
| 324 |
{ |
| 325 |
__it->cancel(); |
| 326 |
__last = __it; |
| 327 |
++__count; |
| 328 |
} |
| 329 |
} |
| 330 |
if (__count) |
| 331 |
_M_queue._M_sort_to(__last); |
| 332 |
return __count; |
| 333 |
} |
| 334 |
|
| 335 |
// Cancel oldest outstanding wait for __t |
| 336 |
bool |
| 337 |
cancel_one(const _Timer& __t) |
| 338 |
{ |
| 339 |
lock_guard<execution_context::mutex_type> __lock(_M_qmtx); |
| 340 |
const auto __end = _M_queue.end(); |
| 341 |
auto __oldest = __end; |
| 342 |
for (auto __it = _M_queue.begin(); __it != __end; ++__it) |
| 343 |
if (__it->_M_key == __t._M_key.get()) |
| 344 |
if (__oldest == __end || __it->_M_id < __oldest->_M_id) |
| 345 |
__oldest = __it; |
| 346 |
if (__oldest == __end) |
| 347 |
return false; |
| 348 |
__oldest->cancel(); |
| 349 |
_M_queue._M_sort_to(__oldest); |
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
chrono::milliseconds |
| 354 |
_M_next() const override |
| 355 |
{ |
| 356 |
typename _Timer::time_point __exp; |
| 357 |
{ |
| 358 |
lock_guard<execution_context::mutex_type> __lock(_M_qmtx); |
| 359 |
if (_M_queue.empty()) |
| 360 |
return chrono::milliseconds::max(); // no pending timers |
| 361 |
if (_M_queue.top()._M_key == nullptr) |
| 362 |
return chrono::milliseconds::zero(); // cancelled, run now |
| 363 |
__exp = _M_queue.top()._M_expiry; |
| 364 |
} |
| 365 |
auto __dur = _Timer::traits_type::to_wait_duration(__exp); |
| 366 |
if (__dur < __dur.zero()) |
| 367 |
__dur = __dur.zero(); |
| 368 |
return chrono::duration_cast<chrono::milliseconds>(__dur); |
| 369 |
} |
| 370 |
|
| 371 |
private: |
| 372 |
|
| 373 |
bool run_one() override |
| 374 |
{ |
| 375 |
auto __now = _Timer::clock_type::now(); |
| 376 |
function<void(error_code)> __h; |
| 377 |
error_code __ec; |
| 378 |
{ |
| 379 |
lock_guard<execution_context::mutex_type> __lock(_M_qmtx); |
| 380 |
|
| 381 |
if (_M_queue.top()._M_key == nullptr) // cancelled |
| 382 |
{ |
| 383 |
__h = std::move(_M_queue.top()._M_h); |
| 384 |
__ec = std::make_error_code(errc::operation_canceled); |
| 385 |
_M_queue.pop(); |
| 386 |
} |
| 387 |
else if (_M_queue.top()._M_expiry <= _Timer::clock_type::now()) |
| 388 |
{ |
| 389 |
__h = std::move(_M_queue.top()._M_h); |
| 390 |
_M_queue.pop(); |
| 391 |
} |
| 392 |
} |
| 393 |
if (__h) |
| 394 |
{ |
| 395 |
__h(__ec); |
| 396 |
context().get_executor().on_work_finished(); |
| 397 |
return true; |
| 398 |
} |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
using __timer_id_type = uint64_t; |
| 403 |
|
| 404 |
struct __pending_timer |
| 405 |
{ |
| 406 |
__pending_timer(const _Timer& __t, uint64_t __id, |
| 407 |
function<void(error_code)> __h) |
| 408 |
: _M_expiry(__t.expiry()), _M_key(__t._M_key.get()), _M_id(__id), |
| 409 |
_M_h(std::move(__h)) |
| 410 |
{ } |
| 411 |
|
| 412 |
typename _Timer::time_point _M_expiry; |
| 413 |
_Key* _M_key; |
| 414 |
__timer_id_type _M_id; |
| 415 |
function<void(error_code)> _M_h; |
| 416 |
|
| 417 |
void cancel() { _M_expiry = _M_expiry.min(); _M_key = nullptr; } |
| 418 |
|
| 419 |
bool |
| 420 |
operator<(const __pending_timer& __rhs) const |
| 421 |
{ return _M_expiry < __rhs._M_expiry; } |
| 422 |
}; |
| 423 |
|
| 424 |
struct __queue : priority_queue<__pending_timer> |
| 425 |
{ |
| 426 |
using iterator = |
| 427 |
typename priority_queue<__pending_timer>::container_type::iterator; |
| 428 |
|
| 429 |
// expose begin/end/erase for direct access to underlying container |
| 430 |
iterator begin() { return this->c.begin(); } |
| 431 |
iterator end() { return this->c.end(); } |
| 432 |
iterator erase(iterator __it) { return this->c.erase(__it); } |
| 433 |
|
| 434 |
void |
| 435 |
_M_sort_to(iterator __it) |
| 436 |
{ std::stable_sort(this->c.begin(), ++__it); } |
| 437 |
}; |
| 438 |
|
| 439 |
__queue _M_queue; |
| 440 |
__timer_id_type _M_next_id = 0; |
| 441 |
}; |
| 442 |
|
| 443 |
template<typename _Timer, typename _CompletionHandler> |
| 444 |
void |
| 445 |
async_wait(const _Timer& __timer, _CompletionHandler&& __h) |
| 446 |
{ |
| 447 |
auto& __queue = use_service<__timer_queue<_Timer>>(*this); |
| 448 |
__queue.push(__timer, std::move(__h)); |
| 449 |
_M_reactor._M_notify(); |
| 450 |
} |
| 451 |
|
| 452 |
// Cancel all wait operations initiated by __timer. |
| 453 |
template<typename _Timer> |
| 454 |
size_t |
| 455 |
cancel(const _Timer& __timer) |
| 456 |
{ |
| 457 |
if (!has_service<__timer_queue<_Timer>>(*this)) |
| 458 |
return 0; |
| 459 |
|
| 460 |
auto __c = use_service<__timer_queue<_Timer>>(*this).cancel(__timer); |
| 461 |
if (__c != 0) |
| 462 |
_M_reactor._M_notify(); |
| 463 |
return __c; |
| 464 |
} |
| 465 |
|
| 466 |
// Cancel the oldest wait operation initiated by __timer. |
| 467 |
template<typename _Timer> |
| 468 |
size_t |
| 469 |
cancel_one(const _Timer& __timer) |
| 470 |
{ |
| 471 |
if (!has_service<__timer_queue<_Timer>>(*this)) |
| 472 |
return 0; |
| 473 |
|
| 474 |
if (use_service<__timer_queue<_Timer>>(*this).cancel_one(__timer)) |
| 475 |
{ |
| 476 |
_M_reactor._M_notify(); |
| 477 |
return 1; |
| 478 |
} |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
// The caller must know what the wait-type __w will be interpreted. |
| 483 |
// In the current implementation the reactor is based on <poll.h> |
| 484 |
// so the parameter must be one of POLLIN, POLLOUT or POLLERR. |
| 485 |
template<typename _Op> |
| 486 |
void |
| 487 |
async_wait(int __fd, int __w, _Op&& __op) |
| 488 |
{ |
| 489 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 490 |
// TODO need push_back, use std::list not std::forward_list |
| 491 |
auto __tail = _M_ops.before_begin(), __it = _M_ops.begin(); |
| 492 |
while (__it != _M_ops.end()) |
| 493 |
{ |
| 494 |
++__it; |
| 495 |
++__tail; |
| 496 |
} |
| 497 |
using __type = __async_operation_impl<_Op>; |
| 498 |
_M_ops.emplace_after(__tail, |
| 499 |
make_unique<__type>(std::move(__op), __fd, __w)); |
| 500 |
_M_reactor._M_fd_interest(__fd, __w); |
| 501 |
} |
| 502 |
|
| 503 |
void _M_add_fd(int __fd) { _M_reactor._M_add_fd(__fd); } |
| 504 |
void _M_remove_fd(int __fd) { _M_reactor._M_remove_fd(__fd); } |
| 505 |
|
| 506 |
void cancel(int __fd, error_code&) |
| 507 |
{ |
| 508 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 509 |
const auto __end = _M_ops.end(); |
| 510 |
auto __it = _M_ops.begin(); |
| 511 |
auto __prev = _M_ops.before_begin(); |
| 512 |
while (__it != __end && (*__it)->_M_is_cancelled()) |
| 513 |
{ |
| 514 |
++__it; |
| 515 |
++__prev; |
| 516 |
} |
| 517 |
auto __cancelled = __prev; |
| 518 |
while (__it != __end) |
| 519 |
{ |
| 520 |
if ((*__it)->_M_fd == __fd) |
| 521 |
{ |
| 522 |
(*__it)->cancel(); |
| 523 |
++__it; |
| 524 |
_M_ops.splice_after(__cancelled, _M_ops, __prev); |
| 525 |
++__cancelled; |
| 526 |
} |
| 527 |
else |
| 528 |
{ |
| 529 |
++__it; |
| 530 |
++__prev; |
| 531 |
} |
| 532 |
} |
| 533 |
_M_reactor._M_not_interested(__fd); |
| 534 |
} |
| 535 |
|
| 536 |
struct __async_operation |
| 537 |
{ |
| 538 |
__async_operation(int __fd, int __ev) : _M_fd(__fd), _M_ev(__ev) { } |
| 539 |
|
| 540 |
virtual ~__async_operation() = default; |
| 541 |
|
| 542 |
int _M_fd; |
| 543 |
short _M_ev; |
| 544 |
|
| 545 |
void cancel() { _M_fd = -1; } |
| 546 |
bool _M_is_cancelled() const { return _M_fd == -1; } |
| 547 |
virtual void run(io_context&) = 0; |
| 548 |
}; |
| 549 |
|
| 550 |
template<typename _Op> |
| 551 |
struct __async_operation_impl : __async_operation |
| 552 |
{ |
| 553 |
__async_operation_impl(_Op&& __op, int __fd, int __ev) |
| 554 |
: __async_operation{__fd, __ev}, _M_op(std::move(__op)) { } |
| 555 |
|
| 556 |
_Op _M_op; |
| 557 |
|
| 558 |
void run(io_context& __ctx) |
| 559 |
{ |
| 560 |
if (_M_is_cancelled()) |
| 561 |
_M_op(std::make_error_code(errc::operation_canceled)); |
| 562 |
else |
| 563 |
_M_op(error_code{}); |
| 564 |
} |
| 565 |
}; |
| 566 |
|
| 567 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 568 |
atomic<count_type> _M_work_count; |
| 569 |
#else |
| 570 |
count_type _M_work_count; |
| 571 |
#endif |
| 572 |
mutable execution_context::mutex_type _M_mtx; |
| 573 |
queue<function<void()>> _M_op; |
| 574 |
bool _M_stopped = false; |
| 575 |
|
| 576 |
struct __monitor |
| 577 |
{ |
| 578 |
__monitor(io_context& __c) : _M_ctx(__c) |
| 579 |
{ |
| 580 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 581 |
lock_guard<execution_context::mutex_type> __lock(_M_ctx._M_mtx); |
| 582 |
_M_ctx._M_call_stack.push_back(this_thread::get_id()); |
| 583 |
#else |
| 584 |
_M_ctx._M_run_count++; |
| 585 |
#endif |
| 586 |
} |
| 587 |
|
| 588 |
~__monitor() |
| 589 |
{ |
| 590 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 591 |
lock_guard<execution_context::mutex_type> __lock(_M_ctx._M_mtx); |
| 592 |
_M_ctx._M_call_stack.pop_back(); |
| 593 |
#else |
| 594 |
_M_ctx._M_run_count--; |
| 595 |
#endif |
| 596 |
if (_M_ctx._M_outstanding_work() == 0) |
| 597 |
{ |
| 598 |
_M_ctx._M_stopped = true; |
| 599 |
_M_ctx._M_reactor._M_notify(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
__monitor(__monitor&&) = delete; |
| 604 |
|
| 605 |
io_context& _M_ctx; |
| 606 |
}; |
| 607 |
|
| 608 |
bool |
| 609 |
_M_do_one(chrono::milliseconds __timeout) |
| 610 |
{ |
| 611 |
const bool __block = __timeout != chrono::milliseconds::zero(); |
| 612 |
|
| 613 |
__reactor::__fdvec __fds; |
| 614 |
|
| 615 |
__monitor __mon{*this}; |
| 616 |
|
| 617 |
__timer_queue_base* __timerq = nullptr; |
| 618 |
unique_ptr<__async_operation> __async_op; |
| 619 |
|
| 620 |
while (true) |
| 621 |
{ |
| 622 |
if (__timerq) |
| 623 |
{ |
| 624 |
if (__timerq->run_one()) |
| 625 |
return true; |
| 626 |
else |
| 627 |
__timerq = nullptr; |
| 628 |
} |
| 629 |
|
| 630 |
if (__async_op) |
| 631 |
{ |
| 632 |
__async_op->run(*this); |
| 633 |
// TODO need to unregister __async_op |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
chrono::milliseconds __ms{0}; |
| 638 |
|
| 639 |
{ |
| 640 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 641 |
|
| 642 |
if (_M_stopped) |
| 643 |
return false; |
| 644 |
|
| 645 |
// find first timer with something to do |
| 646 |
for (auto __q : _M_timers) |
| 647 |
{ |
| 648 |
auto __next = __q->_M_next(); |
| 649 |
if (__next == __next.zero()) // ready to run immediately |
| 650 |
{ |
| 651 |
__timerq = __q; |
| 652 |
__ms = __next; |
| 653 |
break; |
| 654 |
} |
| 655 |
else if (__next != __next.max() && __block |
| 656 |
&& (__next < __ms || __timerq == nullptr)) |
| 657 |
{ |
| 658 |
__timerq = __q; |
| 659 |
__ms = __next; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
if (__timerq && __ms == __ms.zero()) |
| 664 |
continue; // restart loop to run a timer immediately |
| 665 |
|
| 666 |
if (!_M_ops.empty() && _M_ops.front()->_M_is_cancelled()) |
| 667 |
{ |
| 668 |
_M_ops.front().swap(__async_op); |
| 669 |
_M_ops.pop_front(); |
| 670 |
continue; |
| 671 |
} |
| 672 |
|
| 673 |
// TODO run any posted items |
| 674 |
|
| 675 |
if (__block) |
| 676 |
{ |
| 677 |
if (__timerq == nullptr) |
| 678 |
__ms = __timeout; |
| 679 |
else if (__ms.zero() <= __timeout && __timeout < __ms) |
| 680 |
__ms = __timeout; |
| 681 |
else if (__ms.count() > numeric_limits<int>::max()) |
| 682 |
__ms = chrono::milliseconds{numeric_limits<int>::max()}; |
| 683 |
} |
| 684 |
// else __ms == 0 and poll() will return immediately |
| 685 |
|
| 686 |
} |
| 687 |
|
| 688 |
auto __res = _M_reactor.wait(__fds, __ms); |
| 689 |
|
| 690 |
if (__res == __reactor::_S_retry) |
| 691 |
continue; |
| 692 |
|
| 693 |
if (__res == __reactor::_S_timeout) |
| 694 |
{ |
| 695 |
if (__timerq == nullptr) |
| 696 |
return false; |
| 697 |
else |
| 698 |
continue; // timed out, so restart loop and process the timer |
| 699 |
} |
| 700 |
|
| 701 |
__timerq = nullptr; |
| 702 |
|
| 703 |
if (__fds.empty()) // nothing to do |
| 704 |
return false; |
| 705 |
|
| 706 |
lock_guard<execution_context::mutex_type> __lock(_M_mtx); |
| 707 |
for (auto __it = _M_ops.begin(), __end = _M_ops.end(), |
| 708 |
__prev = _M_ops.before_begin(); __it != __end; ++__it, ++__prev) |
| 709 |
{ |
| 710 |
auto& __op = **__it; |
| 711 |
auto __pos = std::lower_bound(__fds.begin(), __fds.end(), |
| 712 |
__op._M_fd, |
| 713 |
[](const auto& __p, int __fd) { return __p.fd < __fd; }); |
| 714 |
if (__pos != __fds.end() && __pos->fd == __op._M_fd |
| 715 |
&& __pos->revents & __op._M_ev) |
| 716 |
{ |
| 717 |
__it->swap(__async_op); |
| 718 |
_M_ops.erase_after(__prev); |
| 719 |
break; // restart loop and run op |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
struct __reactor |
| 726 |
{ |
| 727 |
#ifdef _GLIBCXX_HAVE_POLL_H |
| 728 |
__reactor() : _M_fds(1) |
| 729 |
{ |
| 730 |
int __pipe[2]; |
| 731 |
if (::pipe(__pipe) == -1) |
| 732 |
__throw_system_error(errno); |
| 733 |
if (::fcntl(__pipe[0], F_SETFL, O_NONBLOCK) == -1 |
| 734 |
|| ::fcntl(__pipe[1], F_SETFL, O_NONBLOCK) == -1) |
| 735 |
{ |
| 736 |
int __e = errno; |
| 737 |
::close(__pipe[0]); |
| 738 |
::close(__pipe[1]); |
| 739 |
__throw_system_error(__e); |
| 740 |
} |
| 741 |
_M_fds.back().events = POLLIN; |
| 742 |
_M_fds.back().fd = __pipe[0]; |
| 743 |
_M_notify_wr = __pipe[1]; |
| 744 |
} |
| 745 |
|
| 746 |
~__reactor() |
| 747 |
{ |
| 748 |
::close(_M_fds.back().fd); |
| 749 |
::close(_M_notify_wr); |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
// write a notification byte to the pipe (ignoring errors) |
| 754 |
void _M_notify() |
| 755 |
{ |
| 756 |
int __n; |
| 757 |
do { |
| 758 |
__n = ::write(_M_notify_wr, "", 1); |
| 759 |
} while (__n == -1 && errno == EINTR); |
| 760 |
} |
| 761 |
|
| 762 |
// read all notification bytes from the pipe |
| 763 |
void _M_on_notify() |
| 764 |
{ |
| 765 |
// Drain the pipe. |
| 766 |
char __buf[64]; |
| 767 |
ssize_t __n; |
| 768 |
do { |
| 769 |
__n = ::read(_M_fds.back().fd, __buf, sizeof(__buf)); |
| 770 |
} while (__n != -1 || errno == EINTR); |
| 771 |
} |
| 772 |
|
| 773 |
void |
| 774 |
_M_add_fd(int __fd) |
| 775 |
{ |
| 776 |
auto __pos = _M_lower_bound(__fd); |
| 777 |
if (__pos->fd == __fd) |
| 778 |
__throw_system_error((int)errc::invalid_argument); |
| 779 |
_M_fds.insert(__pos, __fdvec::value_type{})->fd = __fd; |
| 780 |
_M_notify(); |
| 781 |
} |
| 782 |
|
| 783 |
void |
| 784 |
_M_remove_fd(int __fd) |
| 785 |
{ |
| 786 |
auto __pos = _M_lower_bound(__fd); |
| 787 |
if (__pos->fd == __fd) |
| 788 |
_M_fds.erase(__pos); |
| 789 |
// else bug! |
| 790 |
_M_notify(); |
| 791 |
} |
| 792 |
|
| 793 |
void |
| 794 |
_M_fd_interest(int __fd, int __w) |
| 795 |
{ |
| 796 |
auto __pos = _M_lower_bound(__fd); |
| 797 |
if (__pos->fd == __fd) |
| 798 |
__pos->events |= __w; |
| 799 |
// else bug! |
| 800 |
_M_notify(); |
| 801 |
} |
| 802 |
|
| 803 |
void |
| 804 |
_M_not_interested(int __fd) |
| 805 |
{ |
| 806 |
auto __pos = _M_lower_bound(__fd); |
| 807 |
if (__pos->fd == __fd) |
| 808 |
__pos->events = 0; |
| 809 |
_M_notify(); |
| 810 |
} |
| 811 |
|
| 812 |
#ifdef _GLIBCXX_HAVE_POLL_H |
| 813 |
using __fdvec = vector<::pollfd>; |
| 814 |
#else |
| 815 |
struct dummy_pollfd { int fd = -1; short events = 0, revents = 0; }; |
| 816 |
using __fdvec = vector<dummy_pollfd>; |
| 817 |
#endif |
| 818 |
|
| 819 |
// Find first element p such that !(p.fd < __fd) |
| 820 |
// N.B. always returns a dereferencable iterator. |
| 821 |
__fdvec::iterator |
| 822 |
_M_lower_bound(int __fd) |
| 823 |
{ |
| 824 |
return std::lower_bound(_M_fds.begin(), _M_fds.end() - 1, |
| 825 |
__fd, [](const auto& __p, int __fd) { return __p.fd < __fd; }); |
| 826 |
} |
| 827 |
|
| 828 |
enum __status { _S_retry, _S_timeout, _S_ok, _S_error }; |
| 829 |
|
| 830 |
__status |
| 831 |
wait(__fdvec& __fds, chrono::milliseconds __timeout) |
| 832 |
{ |
| 833 |
#ifdef _GLIBCXX_HAVE_POLL_H |
| 834 |
// XXX not thread-safe! |
| 835 |
__fds = _M_fds; // take snapshot to pass to poll() |
| 836 |
|
| 837 |
int __res = ::poll(__fds.data(), __fds.size(), __timeout.count()); |
| 838 |
|
| 839 |
if (__res == -1) |
| 840 |
{ |
| 841 |
__fds.clear(); |
| 842 |
if (errno == EINTR) |
| 843 |
return _S_retry; |
| 844 |
return _S_error; // XXX ??? |
| 845 |
} |
| 846 |
else if (__res == 0) |
| 847 |
{ |
| 848 |
__fds.clear(); |
| 849 |
return _S_timeout; |
| 850 |
} |
| 851 |
else if (__fds.back().revents != 0) // something changed, restart |
| 852 |
{ |
| 853 |
__fds.clear(); |
| 854 |
_M_on_notify(); |
| 855 |
return _S_retry; |
| 856 |
} |
| 857 |
|
| 858 |
auto __part = std::stable_partition(__fds.begin(), __fds.end() - 1, |
| 859 |
[](const __fdvec::value_type& __p) { return __p.revents != 0; }); |
| 860 |
__fds.erase(__part, __fds.end()); |
| 861 |
|
| 862 |
return _S_ok; |
| 863 |
#else |
| 864 |
(void) __timeout; |
| 865 |
__fds.clear(); |
| 866 |
return _S_error; |
| 867 |
#endif |
| 868 |
} |
| 869 |
|
| 870 |
__fdvec _M_fds; // _M_fds.back() is the read end of the self-pipe |
| 871 |
int _M_notify_wr; // write end of the self-pipe |
| 872 |
}; |
| 873 |
|
| 874 |
__reactor _M_reactor; |
| 875 |
|
| 876 |
vector<__timer_queue_base*> _M_timers; |
| 877 |
forward_list<unique_ptr<__async_operation>> _M_ops; |
| 878 |
|
| 879 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 880 |
vector<thread::id> _M_call_stack; |
| 881 |
#else |
| 882 |
int _M_run_count = 0; |
| 883 |
#endif |
| 884 |
}; |
| 885 |
|
| 886 |
inline bool |
| 887 |
operator==(const io_context::executor_type& __a, |
| 888 |
const io_context::executor_type& __b) noexcept |
| 889 |
{ |
| 890 |
// https://github.com/chriskohlhoff/asio-tr2/issues/201 |
| 891 |
using executor_type = io_context::executor_type; |
| 892 |
return std::addressof(executor_type(__a).context()) |
| 893 |
== std::addressof(executor_type(__b).context()); |
| 894 |
} |
| 895 |
|
| 896 |
inline bool |
| 897 |
operator!=(const io_context::executor_type& __a, |
| 898 |
const io_context::executor_type& __b) noexcept |
| 899 |
{ return !(__a == __b); } |
| 900 |
|
| 901 |
template<> struct is_executor<io_context::executor_type> : true_type {}; |
| 902 |
|
| 903 |
/// @} |
| 904 |
|
| 905 |
} // namespace v1 |
| 906 |
} // namespace net |
| 907 |
} // namespace experimental |
| 908 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 909 |
} // namespace std |
| 910 |
|
| 911 |
#endif // C++14 |
| 912 |
|
| 913 |
#endif // _GLIBCXX_EXPERIMENTAL_IO_SERVICE |