src/corosio/src/io_context.cpp
100.0% Lines (24/24)
100.0% Functions (5/5)
50.0% Branches (1/2)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Steve Gerbino | |||
| 3 | // Copyright (c) 2026 Michael Vandeberg | |||
| 4 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #include <boost/corosio/io_context.hpp> | |||
| 12 | #include <boost/corosio/backend.hpp> | |||
| 13 | ||||
| 14 | #include <thread> | |||
| 15 | ||||
| 16 | #if BOOST_COROSIO_HAS_EPOLL | |||
| 17 | #include <boost/corosio/native/detail/epoll/epoll_scheduler.hpp> | |||
| 18 | #include <boost/corosio/native/detail/epoll/epoll_socket_service.hpp> | |||
| 19 | #include <boost/corosio/native/detail/epoll/epoll_acceptor_service.hpp> | |||
| 20 | #endif | |||
| 21 | ||||
| 22 | #if BOOST_COROSIO_HAS_SELECT | |||
| 23 | #include <boost/corosio/native/detail/select/select_scheduler.hpp> | |||
| 24 | #include <boost/corosio/native/detail/select/select_socket_service.hpp> | |||
| 25 | #include <boost/corosio/native/detail/select/select_acceptor_service.hpp> | |||
| 26 | #endif | |||
| 27 | ||||
| 28 | #if BOOST_COROSIO_HAS_KQUEUE | |||
| 29 | #include <boost/corosio/native/detail/kqueue/kqueue_scheduler.hpp> | |||
| 30 | #include <boost/corosio/native/detail/kqueue/kqueue_socket_service.hpp> | |||
| 31 | #include <boost/corosio/native/detail/kqueue/kqueue_acceptor_service.hpp> | |||
| 32 | #endif | |||
| 33 | ||||
| 34 | #if BOOST_COROSIO_HAS_IOCP | |||
| 35 | #include <boost/corosio/native/detail/iocp/win_scheduler.hpp> | |||
| 36 | #include <boost/corosio/native/detail/iocp/win_acceptor_service.hpp> | |||
| 37 | #include <boost/corosio/native/detail/iocp/win_signals.hpp> | |||
| 38 | #endif | |||
| 39 | ||||
| 40 | namespace boost::corosio { | |||
| 41 | ||||
| 42 | #if BOOST_COROSIO_HAS_EPOLL | |||
| 43 | detail::scheduler& | |||
| 44 | epoll_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | |||
| 45 | { | |||
| 46 | auto& sched = ctx.make_service<detail::epoll_scheduler>( | |||
| 47 | static_cast<int>(concurrency_hint)); | |||
| 48 | ||||
| 49 | ctx.make_service<detail::epoll_socket_service>(); | |||
| 50 | ctx.make_service<detail::epoll_acceptor_service>(); | |||
| 51 | ||||
| 52 | return sched; | |||
| 53 | } | |||
| 54 | #endif | |||
| 55 | ||||
| 56 | #if BOOST_COROSIO_HAS_SELECT | |||
| 57 | detail::scheduler& | |||
| 58 | 161 | select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | ||
| 59 | { | |||
| 60 | 322 | auto& sched = ctx.make_service<detail::select_scheduler>( | ||
| 61 | 161 | static_cast<int>(concurrency_hint)); | ||
| 62 | ||||
| 63 | 161 | ctx.make_service<detail::select_socket_service>(); | ||
| 64 | 161 | ctx.make_service<detail::select_acceptor_service>(); | ||
| 65 | ||||
| 66 | 161 | return sched; | ||
| 67 | } | |||
| 68 | #endif | |||
| 69 | ||||
| 70 | #if BOOST_COROSIO_HAS_KQUEUE | |||
| 71 | detail::scheduler& | |||
| 72 | 299 | kqueue_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | ||
| 73 | { | |||
| 74 | 598 | auto& sched = ctx.make_service<detail::kqueue_scheduler>( | ||
| 75 | 299 | static_cast<int>(concurrency_hint)); | ||
| 76 | ||||
| 77 | 299 | ctx.make_service<detail::kqueue_socket_service>(); | ||
| 78 | 299 | ctx.make_service<detail::kqueue_acceptor_service>(); | ||
| 79 | ||||
| 80 | 299 | return sched; | ||
| 81 | } | |||
| 82 | #endif | |||
| 83 | ||||
| 84 | #if BOOST_COROSIO_HAS_IOCP | |||
| 85 | detail::scheduler& | |||
| 86 | iocp_t::construct(capy::execution_context& ctx, unsigned concurrency_hint) | |||
| 87 | { | |||
| 88 | auto& sched = ctx.make_service<detail::win_scheduler>( | |||
| 89 | static_cast<int>(concurrency_hint)); | |||
| 90 | ||||
| 91 | auto& sockets = ctx.make_service<detail::win_sockets>(); | |||
| 92 | ctx.make_service<detail::win_acceptor_service>(sockets); | |||
| 93 | ctx.make_service<detail::win_signals>(); | |||
| 94 | ||||
| 95 | return sched; | |||
| 96 | } | |||
| 97 | #endif | |||
| 98 | ||||
| 99 | 137 | io_context::io_context() : io_context(std::thread::hardware_concurrency()) {} | ||
| 100 | ||||
| 101 | 276 | io_context::io_context(unsigned concurrency_hint) | ||
| 102 | 138 | : capy::execution_context(this) | ||
| 103 | 138 | , sched_(nullptr) | ||
| 104 | 138 | { | ||
| 105 | #if BOOST_COROSIO_HAS_IOCP | |||
| 106 | sched_ = &iocp_t::construct(*this, concurrency_hint); | |||
| 107 | #elif BOOST_COROSIO_HAS_EPOLL | |||
| 108 | sched_ = &epoll_t::construct(*this, concurrency_hint); | |||
| 109 | #elif BOOST_COROSIO_HAS_KQUEUE | |||
| 110 |
1/2✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
|
138 | sched_ = &kqueue_t::construct(*this, concurrency_hint); | |
| 111 | #elif BOOST_COROSIO_HAS_SELECT | |||
| 112 | sched_ = &select_t::construct(*this, concurrency_hint); | |||
| 113 | #endif | |||
| 114 | 276 | } | ||
| 115 | ||||
| 116 | 908 | io_context::~io_context() | ||
| 117 | 448 | { | ||
| 118 | 460 | shutdown(); | ||
| 119 | 460 | destroy(); | ||
| 120 | 908 | } | ||
| 121 | ||||
| 122 | } // namespace boost::corosio | |||
| 123 |