src/corosio/src/io_context.cpp

100.0% Lines (23/23) 100.0% List of functions (5/5)
io_context.cpp
f(x) Functions (5)
Line 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 #include <boost/corosio/native/detail/iocp/win_file_service.hpp>
43 #include <boost/corosio/native/detail/iocp/win_random_access_file_service.hpp>
44 #endif
45
46 namespace boost::corosio {
47
48 #if BOOST_COROSIO_HAS_EPOLL
49 detail::scheduler&
50 320x epoll_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
51 {
52 640x auto& sched = ctx.make_service<detail::epoll_scheduler>(
53 320x static_cast<int>(concurrency_hint));
54
55 320x ctx.make_service<detail::epoll_tcp_service>();
56 320x ctx.make_service<detail::epoll_tcp_acceptor_service>();
57 320x ctx.make_service<detail::epoll_udp_service>();
58
59 320x return sched;
60 }
61 #endif
62
63 #if BOOST_COROSIO_HAS_SELECT
64 detail::scheduler&
65 195x select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
66 {
67 390x auto& sched = ctx.make_service<detail::select_scheduler>(
68 195x static_cast<int>(concurrency_hint));
69
70 195x ctx.make_service<detail::select_tcp_service>();
71 195x ctx.make_service<detail::select_tcp_acceptor_service>();
72 195x ctx.make_service<detail::select_udp_service>();
73
74 195x return sched;
75 }
76 #endif
77
78 #if BOOST_COROSIO_HAS_KQUEUE
79 detail::scheduler&
80 kqueue_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
81 {
82 auto& sched = ctx.make_service<detail::kqueue_scheduler>(
83 static_cast<int>(concurrency_hint));
84
85 ctx.make_service<detail::kqueue_tcp_service>();
86 ctx.make_service<detail::kqueue_tcp_acceptor_service>();
87 ctx.make_service<detail::kqueue_udp_service>();
88
89 return sched;
90 }
91 #endif
92
93 #if BOOST_COROSIO_HAS_IOCP
94 detail::scheduler&
95 iocp_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
96 {
97 auto& sched = ctx.make_service<detail::win_scheduler>(
98 static_cast<int>(concurrency_hint));
99
100 auto& sockets = ctx.make_service<detail::win_tcp_service>();
101 ctx.make_service<detail::win_tcp_acceptor_service>(sockets);
102 ctx.make_service<detail::win_udp_service>();
103 ctx.make_service<detail::win_signals>();
104 ctx.make_service<detail::win_file_service>();
105 ctx.make_service<detail::win_random_access_file_service>();
106
107 return sched;
108 }
109 #endif
110
111 124x io_context::io_context() : io_context(std::thread::hardware_concurrency()) {}
112
113 125x io_context::io_context(unsigned concurrency_hint)
114 : capy::execution_context(this)
115 125x , sched_(nullptr)
116 {
117 #if BOOST_COROSIO_HAS_IOCP
118 sched_ = &iocp_t::construct(*this, concurrency_hint);
119 #elif BOOST_COROSIO_HAS_EPOLL
120 125x sched_ = &epoll_t::construct(*this, concurrency_hint);
121 #elif BOOST_COROSIO_HAS_KQUEUE
122 sched_ = &kqueue_t::construct(*this, concurrency_hint);
123 #elif BOOST_COROSIO_HAS_SELECT
124 sched_ = &select_t::construct(*this, concurrency_hint);
125 #endif
126 125x }
127
128 515x io_context::~io_context()
129 {
130 515x shutdown();
131 515x destroy();
132 515x }
133
134 } // namespace boost::corosio
135