src/corosio/src/io_context.cpp

100.0% Lines (16/16) 100.0% Functions (4/4) 100.0% Branches (2/2)
src/corosio/src/io_context.cpp
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 select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
59 {
60 auto& sched = ctx.make_service<detail::select_scheduler>(
61 static_cast<int>(concurrency_hint));
62
63 ctx.make_service<detail::select_socket_service>();
64 ctx.make_service<detail::select_acceptor_service>();
65
66 return sched;
67 }
68 #endif
69
70 #if BOOST_COROSIO_HAS_KQUEUE
71 detail::scheduler&
72 kqueue_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
73 {
74 auto& sched = ctx.make_service<detail::kqueue_scheduler>(
75 static_cast<int>(concurrency_hint));
76
77 ctx.make_service<detail::kqueue_socket_service>();
78 ctx.make_service<detail::kqueue_acceptor_service>();
79
80 return sched;
81 }
82 #endif
83
84 #if BOOST_COROSIO_HAS_IOCP
85 detail::scheduler&
86 290 iocp_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
87 {
88 580 auto& sched = ctx.make_service<detail::win_scheduler>(
89
1/1
✓ Branch 2 → 3 taken 290 times.
290 static_cast<int>(concurrency_hint));
90
91 290 auto& sockets = ctx.make_service<detail::win_sockets>();
92 290 ctx.make_service<detail::win_acceptor_service>(sockets);
93 290 ctx.make_service<detail::win_signals>();
94
95 290 return sched;
96 }
97 #endif
98
99 137 io_context::io_context() : io_context(std::thread::hardware_concurrency()) {}
100
101 138 io_context::io_context(unsigned concurrency_hint)
102 : capy::execution_context(this)
103 138 , sched_(nullptr)
104 {
105 #if BOOST_COROSIO_HAS_IOCP
106
1/1
✓ Branch 3 → 4 taken 138 times.
138 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 sched_ = &kqueue_t::construct(*this, concurrency_hint);
111 #elif BOOST_COROSIO_HAS_SELECT
112 sched_ = &select_t::construct(*this, concurrency_hint);
113 #endif
114 138 }
115
116 290 io_context::~io_context()
117 {
118 290 shutdown();
119 290 destroy();
120 290 }
121
122 } // namespace boost::corosio
123