LCOV - code coverage report
Current view: top level - corosio/native/detail/reactor - reactor_defer_policy.hpp (source / functions) Coverage Total Hit
Test: coverage_remapped.info Lines: 100.0 % 16 16
Test Date: 2026-11-19 16:59:19 Functions: 100.0 % 4 4

           TLA  Line data    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 HIT      142437 :     bool may_defer() const noexcept
      51                 :     {
      52          142437 :         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          132881 :     void on_deferred_turn(bool handlers_queued) noexcept
      61                 :     {
      62          132881 :         if (handlers_queued)
      63                 :         {
      64          125767 :             if (score_ > 0)
      65             945 :                 --score_;
      66                 :         }
      67                 :         else
      68                 :         {
      69            7114 :             score_ =
      70            7114 :                 score_ > max_score - miss_cost ? max_score : score_ + miss_cost;
      71                 :         }
      72          132881 :     }
      73                 : 
      74                 :     /// Record a turn that polled because deferring was off.
      75           10151 :     void on_polled_instead() noexcept
      76                 :     {
      77           10151 :         if (score_ > 0)
      78           10151 :             --score_;
      79           10151 :     }
      80                 : 
      81                 :     /// Forget the history, as for a fresh run.
      82            6505 :     void reset() noexcept
      83                 :     {
      84            6505 :         score_ = 0;
      85            6505 :     }
      86                 : };
      87                 : 
      88                 : } // namespace boost::corosio::detail
      89                 : 
      90                 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_DEFER_POLICY_HPP
        

Generated by: LCOV version 2.3