20.00% Lines (1/5) 25.00% Functions (1/4)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // Copyright (c) 2026 Steve Gerbino 3   // Copyright (c) 2026 Steve Gerbino
4   // Copyright (c) 2026 Michael Vandeberg 4   // Copyright (c) 2026 Michael Vandeberg
5   // 5   //
6   // Distributed under the Boost Software License, Version 1.0. (See accompanying 6   // Distributed under the Boost Software License, Version 1.0. (See accompanying
7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8   // 8   //
9   // Official repository: https://github.com/cppalliance/corosio 9   // Official repository: https://github.com/cppalliance/corosio
10   // 10   //
11   11  
12   #ifndef BOOST_COROSIO_DETAIL_SCHEDULER_HPP 12   #ifndef BOOST_COROSIO_DETAIL_SCHEDULER_HPP
13   #define BOOST_COROSIO_DETAIL_SCHEDULER_HPP 13   #define BOOST_COROSIO_DETAIL_SCHEDULER_HPP
14   14  
15   #include <boost/corosio/detail/config.hpp> 15   #include <boost/corosio/detail/config.hpp>
16   16  
17   #include <system_error> 17   #include <system_error>
18   #include <boost/capy/continuation.hpp> 18   #include <boost/capy/continuation.hpp>
19   #include <coroutine> 19   #include <coroutine>
20   20  
21   #include <cstddef> 21   #include <cstddef>
22   22  
23   namespace boost::corosio::detail { 23   namespace boost::corosio::detail {
24   24  
25   class scheduler_op; 25   class scheduler_op;
26   26  
27   /** Define the abstract interface for the event loop scheduler. 27   /** Define the abstract interface for the event loop scheduler.
28   28  
29   Concrete backends (epoll, IOCP, kqueue, select) derive from 29   Concrete backends (epoll, IOCP, kqueue, select) derive from
30   this to implement the reactor/proactor event loop. The 30   this to implement the reactor/proactor event loop. The
31   @ref io_context delegates all scheduling operations here. 31   @ref io_context delegates all scheduling operations here.
32   32  
33   @see io_context 33   @see io_context
34   */ 34   */
35   struct BOOST_COROSIO_DECL scheduler 35   struct BOOST_COROSIO_DECL scheduler
36   { 36   {
HITCBC 37   1603 virtual ~scheduler() = default; 37   1802 virtual ~scheduler() = default;
38   38  
39   /// Post a coroutine handle for deferred execution. 39   /// Post a coroutine handle for deferred execution.
40   virtual void post(std::coroutine_handle<>) const = 0; 40   virtual void post(std::coroutine_handle<>) const = 0;
41   41  
42   /// Post a scheduler operation for deferred execution. 42   /// Post a scheduler operation for deferred execution.
43   virtual void post(scheduler_op*) const = 0; 43   virtual void post(scheduler_op*) const = 0;
44   44  
45   /// Post a continuation for deferred execution (zero-allocation). 45   /// Post a continuation for deferred execution (zero-allocation).
46   virtual void post(capy::continuation&) const = 0; 46   virtual void post(capy::continuation&) const = 0;
47   47  
48   /// Increment the outstanding work count. 48   /// Increment the outstanding work count.
49   virtual void work_started() noexcept = 0; 49   virtual void work_started() noexcept = 0;
50   50  
51   /// Decrement the outstanding work count. 51   /// Decrement the outstanding work count.
52   virtual void work_finished() noexcept = 0; 52   virtual void work_finished() noexcept = 0;
53   53  
54   /// Check if the calling thread is running the event loop. 54   /// Check if the calling thread is running the event loop.
55   virtual bool running_in_this_thread() const noexcept = 0; 55   virtual bool running_in_this_thread() const noexcept = 0;
56   56  
57   /// Signal the event loop to stop. 57   /// Signal the event loop to stop.
58   virtual void stop() = 0; 58   virtual void stop() = 0;
59   59  
60   /// Check if the event loop has been stopped. 60   /// Check if the event loop has been stopped.
61   virtual bool stopped() const noexcept = 0; 61   virtual bool stopped() const noexcept = 0;
62   62  
63   /// Reset the stopped state so `run()` can be called again. 63   /// Reset the stopped state so `run()` can be called again.
64   virtual void restart() = 0; 64   virtual void restart() = 0;
65   65  
66   /// Run the event loop, blocking until all work completes. 66   /// Run the event loop, blocking until all work completes.
67   virtual std::size_t run() = 0; 67   virtual std::size_t run() = 0;
68   68  
69   /// Run one handler, blocking until one completes. 69   /// Run one handler, blocking until one completes.
70   virtual std::size_t run_one() = 0; 70   virtual std::size_t run_one() = 0;
71   71  
72   /** Run one handler, blocking up to @p usec microseconds. 72   /** Run one handler, blocking up to @p usec microseconds.
73   73  
74   @param usec Maximum wait time in microseconds. 74   @param usec Maximum wait time in microseconds.
75   75  
76   @return The number of handlers executed (0 or 1). 76   @return The number of handlers executed (0 or 1).
77   */ 77   */
78   virtual std::size_t wait_one(long usec) = 0; 78   virtual std::size_t wait_one(long usec) = 0;
79   79  
80   /// Run all ready handlers without blocking. 80   /// Run all ready handlers without blocking.
81   virtual std::size_t poll() = 0; 81   virtual std::size_t poll() = 0;
82   82  
83   /// Run at most one ready handler without blocking. 83   /// Run at most one ready handler without blocking.
84   virtual std::size_t poll_one() = 0; 84   virtual std::size_t poll_one() = 0;
85   85  
86   /** Register the read end of the POSIX signal self-pipe. 86   /** Register the read end of the POSIX signal self-pipe.
87   87  
88   Called once (by the first signal_set to register a signal) so the 88   Called once (by the first signal_set to register a signal) so the
89   backend's event loop watches @p read_fd for readability. When the 89   backend's event loop watches @p read_fd for readability. When the
90   pipe becomes readable the backend drains it and calls 90   pipe becomes readable the backend drains it and calls
91   `posix_signal_service::deliver_signal` for each pending signal, in 91   `posix_signal_service::deliver_signal` for each pending signal, in
92   normal thread context. This keeps the C signal handler 92   normal thread context. This keeps the C signal handler
93   async-signal-safe: it only writes the signal number to the pipe. 93   async-signal-safe: it only writes the signal number to the pipe.
94   94  
95   POSIX backends override this; the default is a no-op (Windows/IOCP 95   POSIX backends override this; the default is a no-op (Windows/IOCP
96   uses synchronous C-runtime signal handling instead). 96   uses synchronous C-runtime signal handling instead).
97   97  
98   @param read_fd The read end of the global signal self-pipe. 98   @param read_fd The read end of the global signal self-pipe.
99   99  
100   @return The error code, empty on success. 100   @return The error code, empty on success.
101   */ 101   */
102   [[nodiscard]] virtual std::error_code 102   [[nodiscard]] virtual std::error_code
MISUBC 103   register_signal_reader([[maybe_unused]] int read_fd) 103   register_signal_reader([[maybe_unused]] int read_fd)
104   { 104   {
MISUBC 105   return {}; 105   return {};
106   } 106   }
107   107  
108   /// Decomposed threading configuration applied via @ref configure_threading. 108   /// Decomposed threading configuration applied via @ref configure_threading.
109   struct threading_config 109   struct threading_config
110   { 110   {
111   /// Scheduler mutex/condvar enabled. Off only in the `unsafe` tier. 111   /// Scheduler mutex/condvar enabled. Off only in the `unsafe` tier.
112   bool scheduler_locking = true; 112   bool scheduler_locking = true;
113   /// Per-descriptor (reactor) or ring (io_uring) I/O lock enabled. 113   /// Per-descriptor (reactor) or ring (io_uring) I/O lock enabled.
114   /// Off in the `unsafe_io` and `unsafe` tiers. 114   /// Off in the `unsafe_io` and `unsafe` tiers.
115   bool reactor_io_locking = true; 115   bool reactor_io_locking = true;
116   /// A single run thread is guaranteed (a lockless tier): elide 116   /// A single run thread is guaranteed (a lockless tier): elide
117   /// inter-run-thread wakeups. 117   /// inter-run-thread wakeups.
118   bool one_thread = false; 118   bool one_thread = false;
119   }; 119   };
120   120  
121   /// True in the fully-lockless (`unsafe`) tier. The resolver and POSIX 121   /// True in the fully-lockless (`unsafe`) tier. The resolver and POSIX
122   /// file services gate their `operation_not_supported` result on this. 122   /// file services gate their `operation_not_supported` result on this.
MISUBC 123   virtual bool scheduler_locking_disabled() const noexcept { return false; } 123   virtual bool scheduler_locking_disabled() const noexcept { return false; }
124   124  
125   /// Apply @ref threading_config. Default no-op. 125   /// Apply @ref threading_config. Default no-op.
MISUBC 126   virtual void configure_threading(threading_config) noexcept {} 126   virtual void configure_threading(threading_config) noexcept {}
127   }; 127   };
128   128  
129   } // namespace boost::corosio::detail 129   } // namespace boost::corosio::detail
130   130  
131   #endif 131   #endif