src/corosio/src/io_context.cpp

100.0% Lines (17/17) 100.0% List of functions (4/4) 100.0% Branches (2/2)
f(x) Functions (4)
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_tcp_service.hpp>
19 #include <boost/corosio/native/detail/epoll/epoll_tcp_acceptor_service.hpp>
20 #include <boost/corosio/native/detail/epoll/epoll_udp_service.hpp>
21 #endif
22
23 #if BOOST_COROSIO_HAS_SELECT
24 #include <boost/corosio/native/detail/select/select_scheduler.hpp>
25 #include <boost/corosio/native/detail/select/select_tcp_service.hpp>
26 #include <boost/corosio/native/detail/select/select_tcp_acceptor_service.hpp>
27 #include <boost/corosio/native/detail/select/select_udp_service.hpp>
28 #endif
29
30 #if BOOST_COROSIO_HAS_KQUEUE
31 #include <boost/corosio/native/detail/kqueue/kqueue_scheduler.hpp>
32 #include <boost/corosio/native/detail/kqueue/kqueue_tcp_service.hpp>
33 #include <boost/corosio/native/detail/kqueue/kqueue_tcp_acceptor_service.hpp>
34 #include <boost/corosio/native/detail/kqueue/kqueue_udp_service.hpp>
35 #endif
36
37 #if BOOST_COROSIO_HAS_IOCP
38 #include <boost/corosio/native/detail/iocp/win_scheduler.hpp>
39 #include <boost/corosio/native/detail/iocp/win_tcp_acceptor_service.hpp>
40 #include <boost/corosio/native/detail/iocp/win_udp_service.hpp>
41 #include <boost/corosio/native/detail/iocp/win_signals.hpp>
42 #endif
43
44 namespace boost::corosio {
45
46 #if BOOST_COROSIO_HAS_EPOLL
47 detail::scheduler&
48 epoll_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
49 {
50 auto& sched = ctx.make_service<detail::epoll_scheduler>(
51 static_cast<int>(concurrency_hint));
52
53 ctx.make_service<detail::epoll_tcp_service>();
54 ctx.make_service<detail::epoll_tcp_acceptor_service>();
55 ctx.make_service<detail::epoll_udp_service>();
56
57 return sched;
58 }
59 #endif
60
61 #if BOOST_COROSIO_HAS_SELECT
62 detail::scheduler&
63 select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
64 {
65 auto& sched = ctx.make_service<detail::select_scheduler>(
66 static_cast<int>(concurrency_hint));
67
68 ctx.make_service<detail::select_tcp_service>();
69 ctx.make_service<detail::select_tcp_acceptor_service>();
70 ctx.make_service<detail::select_udp_service>();
71
72 return sched;
73 }
74 #endif
75
76 #if BOOST_COROSIO_HAS_KQUEUE
77 detail::scheduler&
78 kqueue_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
79 {
80 auto& sched = ctx.make_service<detail::kqueue_scheduler>(
81 static_cast<int>(concurrency_hint));
82
83 ctx.make_service<detail::kqueue_tcp_service>();
84 ctx.make_service<detail::kqueue_tcp_acceptor_service>();
85 ctx.make_service<detail::kqueue_udp_service>();
86
87 return sched;
88 }
89 #endif
90
91 #if BOOST_COROSIO_HAS_IOCP
92 detail::scheduler&
93 369x iocp_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
94 {
95 738x auto& sched = ctx.make_service<detail::win_scheduler>(
96
1/1
✓ Branch 2 → 3 taken 369 times.
369x static_cast<int>(concurrency_hint));
97
98 369x auto& sockets = ctx.make_service<detail::win_tcp_service>();
99 369x ctx.make_service<detail::win_tcp_acceptor_service>(sockets);
100 369x ctx.make_service<detail::win_udp_service>();
101 369x ctx.make_service<detail::win_signals>();
102
103 369x return sched;
104 }
105 #endif
106
107 143x io_context::io_context() : io_context(std::thread::hardware_concurrency()) {}
108
109 144x io_context::io_context(unsigned concurrency_hint)
110 : capy::execution_context(this)
111 144x , sched_(nullptr)
112 {
113 #if BOOST_COROSIO_HAS_IOCP
114
1/1
✓ Branch 3 → 4 taken 144 times.
144x sched_ = &iocp_t::construct(*this, concurrency_hint);
115 #elif BOOST_COROSIO_HAS_EPOLL
116 sched_ = &epoll_t::construct(*this, concurrency_hint);
117 #elif BOOST_COROSIO_HAS_KQUEUE
118 sched_ = &kqueue_t::construct(*this, concurrency_hint);
119 #elif BOOST_COROSIO_HAS_SELECT
120 sched_ = &select_t::construct(*this, concurrency_hint);
121 #endif
122 144x }
123
124 369x io_context::~io_context()
125 {
126 369x shutdown();
127 369x destroy();
128 369x }
129
130 } // namespace boost::corosio
131