src/corosio/src/io_context.cpp

100.0% Lines (21/21) 100.0% Functions (5/5)
src/corosio/src/io_context.cpp
Line 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 205 epoll_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
45 {
46 410 auto& sched = ctx.make_service<detail::epoll_scheduler>(
47 205 static_cast<int>(concurrency_hint));
48
49 205 ctx.make_service<detail::epoll_socket_service>();
50 205 ctx.make_service<detail::epoll_acceptor_service>();
51
52 205 return sched;
53 }
54 #endif
55
56 #if BOOST_COROSIO_HAS_SELECT
57 detail::scheduler&
58 135 select_t::construct(capy::execution_context& ctx, unsigned concurrency_hint)
59 {
60 270 auto& sched = ctx.make_service<detail::select_scheduler>(
61 135 static_cast<int>(concurrency_hint));
62
63 135 ctx.make_service<detail::select_socket_service>();
64 135 ctx.make_service<detail::select_acceptor_service>();
65
66 135 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 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 69 io_context::io_context() : io_context(std::thread::hardware_concurrency()) {}
100
101 70 io_context::io_context(unsigned concurrency_hint)
102 : capy::execution_context(this)
103 70 , sched_(nullptr)
104 {
105 #if BOOST_COROSIO_HAS_IOCP
106 sched_ = &iocp_t::construct(*this, concurrency_hint);
107 #elif BOOST_COROSIO_HAS_EPOLL
108 70 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 70 }
115
116 340 io_context::~io_context()
117 {
118 340 shutdown();
119 340 destroy();
120 340 }
121
122 } // namespace boost::corosio
123