include/boost/corosio/native/detail/reactor/reactor_defer_policy.hpp

100.0% Lines (16 / 16) 100.0% Functions (4 / 4)
reactor_defer_policy.hpp
f(x) Functions (4)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Steve Gerbino
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_DEFER_POLICY_HPP
11 #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_DEFER_POLICY_HPP
12
13 namespace boost::corosio::detail {
14
15 /** Decide whether the reactor sentinel waits behind queued handlers.
16
17 When the sentinel reaches the front of the queue with handlers still
18 queued, running those handlers first usually saves a reactor pass:
19 they tend to start the next I/O themselves. That bet loses when the
20 handlers drain the queue, so the deferred turn has no other work to
21 overlap with and a poll that would have found completed I/O comes too
22 late. The policy scores each deferral by what the deferred turn found
23 and stops deferring while losses are frequent.
24
25 A deferred turn that found the queue empty costs more than one that
26 found handlers repays, so deferring continues only while fewer than
27 about one deferral in `miss_cost + 1` empties the queue. Each turn
28 that polls instead of deferring lets the score decay, so a workload
29 that changes is tried again.
30
31 @par Thread Safety
32 Distinct objects: Safe.
33 Shared objects: Unsafe. The scheduler calls it under its own mutex.
34 */
35 class reactor_defer_policy
36 {
37 int score_ = 0;
38
39 public:
40 /// Score added by a deferred turn that found the queue empty.
41 static constexpr int miss_cost = 8;
42
43 /// Score at or above which the sentinel polls instead of deferring.
44 static constexpr int threshold = 8;
45
46 /// Highest score, which bounds how long deferring stays off.
47 static constexpr int max_score = 64;
48
49 /// Check whether the sentinel may wait behind queued handlers.
50 142437x bool may_defer() const noexcept
51 {
52 142437x return score_ < threshold;
53 }
54
55 /** Record what a deferred turn of the sentinel found.
56
57 @param handlers_queued `true` if handlers were still queued, so
58 the deferral cost nothing.
59 */
60 132881x void on_deferred_turn(bool handlers_queued) noexcept
61 {
62 132881x if (handlers_queued)
63 {
64 125767x if (score_ > 0)
65 945x --score_;
66 }
67 else
68 {
69 7114x score_ =
70 7114x score_ > max_score - miss_cost ? max_score : score_ + miss_cost;
71 }
72 132881x }
73
74 /// Record a turn that polled because deferring was off.
75 10151x void on_polled_instead() noexcept
76 {
77 10151x if (score_ > 0)
78 10151x --score_;
79 10151x }
80
81 /// Forget the history, as for a fresh run.
82 6505x void reset() noexcept
83 {
84 6505x score_ = 0;
85 6505x }
86 };
87
88 } // namespace boost::corosio::detail
89
90 #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_DEFER_POLICY_HPP
91