LCOV - code coverage report
Current view: top level - corosio/native/detail/reactor - reactor_scheduler.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 99.0 % 418 414 4
Test Date: 2026-11-19 20:59:47 Functions: 98.1 % 54 53 1

           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_SCHEDULER_HPP
      11                 : #define BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_SCHEDULER_HPP
      12                 : 
      13                 : #include <boost/corosio/detail/config.hpp>
      14                 : #include <boost/corosio/detail/object_ref.hpp>
      15                 : #include <boost/capy/ex/execution_context.hpp>
      16                 : #include <boost/capy/ex/abandon.hpp>
      17                 : 
      18                 : #include <boost/corosio/detail/ready_queue.hpp>
      19                 : #include <boost/corosio/detail/scheduler.hpp>
      20                 : #include <boost/corosio/detail/scheduler_op.hpp>
      21                 : #include <boost/corosio/detail/thread_local_ptr.hpp>
      22                 : #include <boost/corosio/detail/timer_service.hpp>
      23                 : #include <boost/corosio/native/detail/reactor/reactor_defer_policy.hpp>
      24                 : 
      25                 : #include <atomic>
      26                 : #include <chrono>
      27                 : #include <coroutine>
      28                 : #include <cstddef>
      29                 : #include <cstdint>
      30                 : #include <limits>
      31                 : #include <memory>
      32                 : 
      33                 : #include <boost/corosio/detail/conditionally_enabled_mutex.hpp>
      34                 : #include <boost/corosio/detail/conditionally_enabled_event.hpp>
      35                 : 
      36                 : namespace boost::corosio::detail {
      37                 : 
      38                 : // Forward declarations
      39                 : class reactor_scheduler;
      40                 : 
      41                 : /** A deregistered descriptor whose owner must outlive the reactor pass
      42                 :     that may still hold it.
      43                 : 
      44                 :     An event the reactor has already read from the kernel can name a
      45                 :     descriptor that another thread closes before the reactor queues it.
      46                 :     The closing thread hands over a reference instead of dropping it, so
      47                 :     the owner is neither freed nor recycled until that pass ends.
      48                 : */
      49                 : struct reactor_retirable
      50                 : {
      51                 :     // All four fields are guarded by the scheduler's mutex.
      52                 : 
      53                 :     /// Next in the scheduler's retired list.
      54                 :     reactor_retirable* retired_next_ = nullptr;
      55                 : 
      56                 :     /// Owner reference held until the pass ends.
      57                 :     object_ref retired_ref_;
      58                 : 
      59                 :     /// Next in a settling thread's list of references to drop.
      60                 :     reactor_retirable* dropped_next_ = nullptr;
      61                 : 
      62                 :     /// Owner reference waiting to be dropped outside the mutex.
      63                 :     object_ref dropped_ref_;
      64                 : 
      65                 :     /// Move @p keep into a queued completion, if one is pending.
      66                 :     virtual void hand_off(object_ref& keep) noexcept = 0;
      67                 : 
      68                 : protected:
      69 HIT       12614 :     ~reactor_retirable() = default;
      70                 : };
      71                 : 
      72                 : /** Per-thread state for a reactor scheduler.
      73                 : 
      74                 :     Each thread running a scheduler's event loop has one of these
      75                 :     on a thread-local stack. It holds a private work queue and
      76                 :     inline completion budget for speculative I/O fast paths.
      77                 : */
      78                 : struct BOOST_COROSIO_SYMBOL_VISIBLE reactor_scheduler_context
      79                 : {
      80                 :     /// Scheduler this context belongs to.
      81                 :     reactor_scheduler const* key;
      82                 : 
      83                 :     /// Next context frame on this thread's stack.
      84                 :     reactor_scheduler_context* next;
      85                 : 
      86                 :     /// Private work queue for reduced contention.
      87                 :     ready_queue private_queue;
      88                 : 
      89                 :     /// Unflushed work count for the private queue.
      90                 :     std::int64_t private_outstanding_work;
      91                 : 
      92                 :     /// Remaining inline completions allowed this cycle.
      93                 :     int inline_budget;
      94                 : 
      95                 :     /// Maximum inline budget (adaptive, 2-16).
      96                 :     int inline_budget_max;
      97                 : 
      98                 :     /// True if no other thread absorbed queued work last cycle.
      99                 :     bool unassisted;
     100                 : 
     101                 :     /// Construct a context frame linked to @a n.
     102                 :     reactor_scheduler_context(
     103                 :         reactor_scheduler const* k, reactor_scheduler_context* n);
     104                 : };
     105                 : 
     106                 : /// Thread-local context stack for reactor schedulers.
     107                 : inline thread_local_ptr<reactor_scheduler_context> reactor_context_stack;
     108                 : 
     109                 : /// Find the context frame for a scheduler on this thread.
     110                 : inline reactor_scheduler_context*
     111          964587 : reactor_find_context(reactor_scheduler const* self) noexcept
     112                 : {
     113          965128 :     for (auto* c = reactor_context_stack.get(); c != nullptr; c = c->next)
     114                 :     {
     115          901318 :         if (c->key == self)
     116          900777 :             return c;
     117                 :     }
     118           63810 :     return nullptr;
     119                 : }
     120                 : 
     121                 : /** Non-template base for reactor-backed scheduler implementations.
     122                 : 
     123                 :     Provides the complete threading model shared by epoll, kqueue,
     124                 :     and select schedulers: signal state machine, inline completion
     125                 :     budget, work counting, run/poll methods, and the do_one event
     126                 :     loop.
     127                 : 
     128                 :     Derived classes provide platform-specific hooks by overriding:
     129                 :     - `run_task(lock, ctx)` to run the reactor poll
     130                 :     - `interrupt_reactor()` to wake a blocked reactor
     131                 : 
     132                 :     De-templated from the original CRTP design to eliminate
     133                 :     duplicate instantiations when multiple backends are compiled
     134                 :     into the same binary. Virtual dispatch for run_task (called
     135                 :     once per reactor cycle, before a blocking syscall) has
     136                 :     negligible overhead.
     137                 : 
     138                 :     @par Thread Safety
     139                 :     All public member functions are thread-safe.
     140                 : */
     141                 : class reactor_scheduler : public scheduler
     142                 : {
     143                 : public:
     144                 :     using context_type = reactor_scheduler_context;
     145                 :     using mutex_type   = conditionally_enabled_mutex;
     146                 :     using lock_type    = mutex_type::scoped_lock;
     147                 :     using event_type   = conditionally_enabled_event;
     148                 : 
     149                 :     /// Post a coroutine for deferred execution.
     150                 :     void post(std::coroutine_handle<> h) const override;
     151                 : 
     152                 :     /// Post a scheduler operation for deferred execution.
     153                 :     void post(scheduler_op* h) const override;
     154                 : 
     155                 :     /** Release an owner reference once no reactor pass can hold `d`.
     156                 : 
     157                 :         Called after a descriptor is deregistered. If a reactor pass is
     158                 :         running, `keep` is parked on `d` until that pass ends. Then, or
     159                 :         at once if no pass is running, it moves to `d`'s queued
     160                 :         invocation if `d` is queued, and is dropped otherwise.
     161                 : 
     162                 :         @param d The deregistered descriptor.
     163                 :         @param keep A reference to the descriptor's owner.
     164                 :     */
     165                 :     void retire_descriptor(reactor_retirable& d, object_ref keep) const;
     166                 : 
     167                 :     /// Post a continuation for deferred execution.
     168                 :     void post(capy::continuation&) const override;
     169                 : 
     170                 :     /// Return true if called from a thread running this scheduler.
     171                 :     bool running_in_this_thread() const noexcept override;
     172                 : 
     173                 :     /// Request the scheduler to stop dispatching handlers.
     174                 :     void stop() override;
     175                 : 
     176                 :     /// Return true if the scheduler has been stopped.
     177                 :     bool stopped() const noexcept override;
     178                 : 
     179                 :     /// Reset the stopped state so `run()` can resume.
     180                 :     void restart() override;
     181                 : 
     182                 :     /// Run the event loop until no work remains.
     183                 :     std::size_t run() override;
     184                 : 
     185                 :     /// Run until one handler completes or no work remains.
     186                 :     std::size_t run_one() override;
     187                 : 
     188                 :     /// Run until one handler completes or @a usec elapses.
     189                 :     std::size_t wait_one(long usec) override;
     190                 : 
     191                 :     /// Run ready handlers without blocking.
     192                 :     std::size_t poll() override;
     193                 : 
     194                 :     /// Run at most one ready handler without blocking.
     195                 :     std::size_t poll_one() override;
     196                 : 
     197                 :     /// Increment the outstanding work count.
     198                 :     void work_started() noexcept override;
     199                 : 
     200                 :     /// Decrement the outstanding work count, stopping on zero.
     201                 :     void work_finished() noexcept override;
     202                 : 
     203                 :     /** Reset the thread's inline completion budget.
     204                 : 
     205                 :         Called at the start of each posted completion handler to
     206                 :         grant a fresh budget for speculative inline completions.
     207                 :     */
     208                 :     void reset_inline_budget() const noexcept;
     209                 : 
     210                 :     /** Consume one unit of inline budget if available.
     211                 : 
     212                 :         @return True if budget was available and consumed.
     213                 :     */
     214                 :     bool try_consume_inline_budget() const noexcept;
     215                 : 
     216                 :     /** Offset a forthcoming work_finished from work_cleanup.
     217                 : 
     218                 :         Called by descriptor_state when all I/O returned EAGAIN and
     219                 :         no handler will be executed. Must be called from a scheduler
     220                 :         thread.
     221                 :     */
     222                 :     /** Note that an op was parked outside the usual registration path.
     223                 : 
     224                 :         A reactor that builds its poll set from parked ops must rebuild
     225                 :         it; one the kernel tracks needs nothing.
     226                 :     */
     227             443 :     virtual void notify_reparked() const {}
     228                 : 
     229                 :     void compensating_work_started() const noexcept;
     230                 : 
     231                 :     /** Post an operation whose work is already counted.
     232                 : 
     233                 :         The posting thread touches nothing of the scheduler after this
     234                 :         returns, unlike a @ref post followed by @ref work_finished: once
     235                 :         posted, the operation can complete and let its context be
     236                 :         destroyed.
     237                 : 
     238                 :         @pre work_started() was called for @p op.
     239                 : 
     240                 :         @param op The operation to post.
     241                 :     */
     242                 :     void post_counted(scheduler_op* op) const;
     243                 : 
     244                 :     /** Post completed operations for deferred invocation.
     245                 : 
     246                 :         If called from a thread running this scheduler, operations
     247                 :         go to the thread's private queue (fast path). Otherwise,
     248                 :         operations are added to the global queue under mutex and a
     249                 :         waiter is signaled.
     250                 : 
     251                 :         @pre work_started() must have been called for each operation.
     252                 : 
     253                 :         @param ops Queue of operations to post.
     254                 :     */
     255                 :     void post_deferred_completions(ready_queue& ops) const;
     256                 : 
     257                 :     /** Apply runtime configuration to the scheduler.
     258                 : 
     259                 :         Called by `io_context` after construction. Values that do
     260                 :         not apply to this backend are silently ignored.
     261                 : 
     262                 :         @pre `max_events` is in [ 1, `INT_MAX` ] and `budget_max` is at
     263                 :             most `INT_MAX`; `io_context` validates both.
     264                 : 
     265                 :         @param max_events  Event buffer size for epoll/kqueue.
     266                 :         @param budget_init Starting inline completion budget.
     267                 :         @param budget_max  Hard ceiling on adaptive budget ramp-up.
     268                 :         @param unassisted  Budget when single-threaded.
     269                 :     */
     270                 :     virtual void configure_reactor(
     271                 :         unsigned max_events,
     272                 :         unsigned budget_init,
     273                 :         unsigned budget_max,
     274                 :         unsigned unassisted);
     275                 : 
     276                 :     /// Return the configured initial inline budget.
     277           11313 :     unsigned inline_budget_initial() const noexcept
     278                 :     {
     279           11313 :         return inline_budget_initial_;
     280                 :     }
     281                 : 
     282                 :     /// Return true when scheduler locking is disabled (fully-lockless tier).
     283            1460 :     bool scheduler_locking_disabled() const noexcept override
     284                 :     {
     285            1460 :         return scheduler_locking_disabled_;
     286                 :     }
     287                 : 
     288            4685 :     void configure_threading(threading_config cfg) noexcept override
     289                 :     {
     290            4685 :         scheduler_locking_disabled_ = !cfg.scheduler_locking;
     291                 :         // reactor_io_locking takes effect at descriptor registration (see the
     292                 :         // register_descriptor overrides), not here.
     293            4685 :         reactor_io_locking_ = cfg.reactor_io_locking;
     294            4685 :         one_thread_         = cfg.one_thread;
     295            4685 :         mutex_.set_enabled(cfg.scheduler_locking);
     296            4685 :         cond_.set_enabled(cfg.scheduler_locking);
     297            4685 :     }
     298                 : 
     299                 : protected:
     300                 :     bool scheduler_locking_disabled_ = false;
     301                 :     bool reactor_io_locking_         = true;
     302                 :     bool one_thread_                 = false;
     303                 : 
     304            4701 :     reactor_scheduler() = default;
     305                 : 
     306                 :     /// Destroy, destroying whatever was posted after the drain.
     307                 :     ~reactor_scheduler() override;
     308                 : 
     309                 :     /** Drain completed_ops during shutdown.
     310                 : 
     311                 :         Pops all operations from the global queue and destroys them,
     312                 :         skipping the task sentinel. Signals all waiting threads.
     313                 :         Derived classes call this from their shutdown() override
     314                 :         before performing platform-specific cleanup.
     315                 :     */
     316                 :     void shutdown_drain();
     317                 : 
     318                 :     /// Destroy every queued operation without running it.
     319                 :     void destroy_queued(lock_type& lock) noexcept;
     320                 : 
     321                 :     /// RAII guard that re-inserts the task sentinel after `run_task`.
     322                 :     struct task_cleanup
     323                 :     {
     324                 :         reactor_scheduler const* sched;
     325                 :         lock_type* lock;
     326                 :         context_type& ctx;
     327                 :         ~task_cleanup();
     328                 :     };
     329                 : 
     330                 :     mutable mutex_type mutex_{true};
     331                 :     mutable event_type cond_{true};
     332                 :     mutable ready_queue completed_ops_;
     333                 :     mutable std::atomic<std::int64_t> outstanding_work_{0};
     334                 :     std::atomic<bool> stopped_{false};
     335                 :     mutable std::atomic<bool> task_running_{false};
     336                 :     mutable bool task_interrupted_ = false;
     337                 :     // Set while the sentinel waits behind queued handlers (guarded by
     338                 :     // mutex_).
     339                 :     bool task_deferred_ = false;
     340                 :     // Whether waiting is paying off (guarded by mutex_).
     341                 :     reactor_defer_policy defer_policy_;
     342                 : 
     343                 :     // Descriptors retired during the running reactor pass (guarded by
     344                 :     // mutex_).
     345                 :     mutable reactor_retirable* retired_ = nullptr;
     346                 : 
     347                 :     /// Settle every descriptor retired before the current pass ended.
     348                 :     /// @pre `lock` holds `mutex_`; it is held again on return.
     349                 :     void settle_retired(lock_type& lock) const;
     350                 : 
     351                 :     /// Move what an enclosing handler on this thread queued privately
     352                 :     /// to the shared queue. @pre `mutex_` is held.
     353                 :     void adopt_enclosing_work(context_type const& ctx);
     354                 : 
     355                 :     // Runtime-configurable reactor tuning parameters.
     356                 :     // Defaults match the library's built-in values.
     357                 :     unsigned max_events_per_poll_   = 128;
     358                 :     unsigned inline_budget_initial_ = 2;
     359                 :     unsigned inline_budget_max_     = 16;
     360                 :     unsigned unassisted_budget_     = 4;
     361                 : 
     362                 :     /// Bit 0 of `state_`: set when the condvar should be signaled.
     363                 :     static constexpr std::size_t signaled_bit = 1;
     364                 : 
     365                 :     /// Increment per waiting thread in `state_`.
     366                 :     static constexpr std::size_t waiter_increment = 2;
     367                 :     mutable std::size_t state_                    = 0;
     368                 : 
     369                 :     /// Sentinel op that triggers a reactor poll when dequeued.
     370                 :     struct task_op final : scheduler_op
     371                 :     {
     372                 :         // LCOV_EXCL_START: the sentinel is intercepted by pointer
     373                 :         // identity; its virtuals exist for vtable completeness.
     374                 :         void operator()() override {}
     375                 :         void destroy() override {}
     376                 :         // LCOV_EXCL_STOP
     377                 :     };
     378                 :     task_op task_op_;
     379                 : 
     380                 :     /** Run the platform-specific reactor poll.
     381                 : 
     382                 :         @par Postconditions
     383                 :         `lock` is owned on return, however the poll ended. An
     384                 :         implementation that unlocks around the blocking call owes the
     385                 :         caller a matching re-acquire on every path out, including the
     386                 :         errors it retries rather than reports.
     387                 :     */
     388                 :     virtual void
     389                 :     run_task(lock_type& lock, context_type& ctx, long timeout_us) = 0;
     390                 : 
     391                 :     /// Wake a blocked reactor (e.g. write to eventfd or pipe).
     392                 :     virtual void interrupt_reactor() const = 0;
     393                 : 
     394                 :     /// Wake the reactor whenever the earliest timer changes.
     395                 :     void on_timer_service_attached(timer_service& svc) override;
     396                 : 
     397                 : private:
     398                 :     struct work_cleanup
     399                 :     {
     400                 :         reactor_scheduler* sched;
     401                 :         lock_type* lock;
     402                 :         context_type& ctx;
     403                 :         ~work_cleanup();
     404                 :     };
     405                 : 
     406                 :     std::size_t do_one(lock_type& lock, long timeout_us, context_type& ctx);
     407                 : 
     408                 :     void signal_all(lock_type& lock) const;
     409                 :     bool maybe_unlock_and_signal_one(lock_type& lock) const;
     410                 :     bool unlock_and_signal_one(lock_type& lock) const;
     411                 :     void clear_signal() const;
     412                 :     void wait_for_signal(lock_type& lock) const;
     413                 :     void wait_for_signal_for(lock_type& lock, long timeout_us) const;
     414                 :     void wake_one_thread_and_unlock(lock_type& lock) const;
     415                 : };
     416                 : 
     417                 : /** RAII guard that pushes/pops a scheduler context frame.
     418                 : 
     419                 :     On construction, pushes a new context frame onto the
     420                 :     thread-local stack. On destruction, drains any remaining
     421                 :     private queue items to the global queue and pops the frame.
     422                 : */
     423                 : struct reactor_thread_context_guard
     424                 : {
     425                 :     /// The context frame managed by this guard.
     426                 :     reactor_scheduler_context frame_;
     427                 : 
     428                 :     /// Marks the scheduler innermost for the executor's fast check.
     429                 :     running_scheduler_guard running_;
     430                 : 
     431                 :     /// Construct the guard, pushing a frame for @a sched.
     432           11313 :     explicit reactor_thread_context_guard(
     433                 :         reactor_scheduler const* sched) noexcept
     434           11313 :         : frame_(sched, reactor_context_stack.get())
     435           11313 :         , running_(sched)
     436                 :     {
     437           11313 :         reactor_context_stack.set(&frame_);
     438           11313 :     }
     439                 : 
     440                 :     /** Destroy the guard, popping the frame.
     441                 : 
     442                 :         The private queue is empty here by invariant: work_cleanup and
     443                 :         task_cleanup splice it to the global queue after every handler
     444                 :         and every reactor pass.
     445                 :     */
     446           11313 :     ~reactor_thread_context_guard() noexcept
     447                 :     {
     448           11313 :         reactor_context_stack.set(frame_.next);
     449           11313 :     }
     450                 : };
     451                 : 
     452                 : // ---- Inline implementations ------------------------------------------------
     453                 : 
     454           11313 : inline reactor_scheduler_context::reactor_scheduler_context(
     455           11313 :     reactor_scheduler const* k, reactor_scheduler_context* n)
     456           11313 :     : key(k)
     457           11313 :     , next(n)
     458           11313 :     , private_outstanding_work(0)
     459           11313 :     , inline_budget(0)
     460           11313 :     , inline_budget_max(static_cast<int>(k->inline_budget_initial()))
     461           11313 :     , unassisted(false)
     462                 : {
     463           11313 : }
     464                 : 
     465                 : inline void
     466              50 : reactor_scheduler::configure_reactor(
     467                 :     unsigned max_events,
     468                 :     unsigned budget_init,
     469                 :     unsigned budget_max,
     470                 :     unsigned unassisted)
     471                 : {
     472              50 :     BOOST_COROSIO_ASSERT(
     473                 :         max_events >= 1 &&
     474                 :         max_events <= static_cast<unsigned>((std::numeric_limits<int>::max)()));
     475              50 :     BOOST_COROSIO_ASSERT(
     476                 :         budget_max <= static_cast<unsigned>((std::numeric_limits<int>::max)()));
     477                 : 
     478                 :     // Clamp initial and unassisted to budget_max.
     479              50 :     if (budget_init > budget_max)
     480              20 :         budget_init = budget_max;
     481              50 :     if (unassisted > budget_max)
     482              20 :         unassisted = budget_max;
     483                 : 
     484              50 :     max_events_per_poll_   = max_events;
     485              50 :     inline_budget_initial_ = budget_init;
     486              50 :     inline_budget_max_     = budget_max;
     487              50 :     unassisted_budget_     = unassisted;
     488              50 : }
     489                 : 
     490                 : inline void
     491          107159 : reactor_scheduler::reset_inline_budget() const noexcept
     492                 : {
     493                 :     // When budget is disabled (max==0), all paths below would no-op
     494                 :     // (inline_budget stays 0). Skip the TLS lookup entirely.
     495          107159 :     if (inline_budget_max_ == 0)
     496              58 :         return;
     497          107101 :     if (auto* ctx = reactor_find_context(this))
     498                 :     {
     499                 :         // Cap when no other thread absorbed queued work
     500          107101 :         if (ctx->unassisted)
     501                 :         {
     502          106152 :             ctx->inline_budget_max = static_cast<int>(unassisted_budget_);
     503          106152 :             ctx->inline_budget     = static_cast<int>(unassisted_budget_);
     504          106152 :             return;
     505                 :         }
     506                 :         // Ramp up when previous cycle fully consumed budget.
     507                 :         // max(1, ...) ensures the doubling escapes zero.
     508                 :         // Doubled in a wider type: the cap may be as large as INT_MAX.
     509             949 :         if (ctx->inline_budget == 0)
     510                 :         {
     511                 :             long long const doubled =
     512             307 :                 2LL * (std::max)(1, ctx->inline_budget_max);
     513             307 :             long long const cap    = inline_budget_max_;
     514             307 :             ctx->inline_budget_max = static_cast<int>((std::min)(doubled, cap));
     515                 :         }
     516             642 :         else if (ctx->inline_budget < ctx->inline_budget_max)
     517                 :         {
     518             211 :             ctx->inline_budget_max = static_cast<int>(inline_budget_initial_);
     519                 :         }
     520             949 :         ctx->inline_budget = ctx->inline_budget_max;
     521                 :     }
     522                 : }
     523                 : 
     524                 : inline bool
     525          452024 : reactor_scheduler::try_consume_inline_budget() const noexcept
     526                 : {
     527          452024 :     if (inline_budget_max_ == 0)
     528              44 :         return false;
     529          451980 :     if (auto* ctx = reactor_find_context(this))
     530                 :     {
     531          451979 :         if (ctx->inline_budget > 0)
     532                 :         {
     533          361307 :             --ctx->inline_budget;
     534          361307 :             return true;
     535                 :         }
     536                 :     }
     537           90673 :     return false;
     538                 : }
     539                 : 
     540                 : inline void
     541            3862 : reactor_scheduler::post(std::coroutine_handle<> h) const
     542                 : {
     543                 :     struct post_handler final : scheduler_op
     544                 :     {
     545                 :         std::coroutine_handle<> h_;
     546                 : 
     547            3862 :         explicit post_handler(std::coroutine_handle<> h) : h_(h) {}
     548            7724 :         ~post_handler() override = default;
     549                 : 
     550            3790 :         void operator()() override
     551                 :         {
     552            3790 :             auto saved = h_;
     553            3790 :             delete this;
     554            3790 :             saved.resume();
     555            3774 :         }
     556                 : 
     557              72 :         void destroy() override
     558                 :         {
     559                 :             // A bare handle carries no owner to unwind from: it is
     560                 :             // destroyed directly.
     561              72 :             auto saved = h_;
     562              72 :             delete this;
     563              72 :             saved.destroy();
     564              72 :         }
     565                 :     };
     566                 : 
     567            3862 :     auto ph = std::make_unique<post_handler>(h);
     568                 : 
     569            3862 :     if (auto* ctx = reactor_find_context(this))
     570                 :     {
     571              34 :         ++ctx->private_outstanding_work;
     572              34 :         ctx->private_queue.push(ph.release());
     573              34 :         return;
     574                 :     }
     575                 : 
     576            3828 :     outstanding_work_.fetch_add(1, std::memory_order_relaxed);
     577                 : 
     578            3828 :     lock_type lock(mutex_);
     579            3828 :     completed_ops_.push(ph.release());
     580            3828 :     wake_one_thread_and_unlock(lock);
     581            3862 : }
     582                 : 
     583                 : inline void
     584          114815 : reactor_scheduler::post(scheduler_op* h) const
     585                 : {
     586          114815 :     if (auto* ctx = reactor_find_context(this))
     587                 :     {
     588          113180 :         ++ctx->private_outstanding_work;
     589          113180 :         ctx->private_queue.push(h);
     590          113180 :         return;
     591                 :     }
     592                 : 
     593            1635 :     outstanding_work_.fetch_add(1, std::memory_order_relaxed);
     594                 : 
     595            1635 :     lock_type lock(mutex_);
     596            1635 :     completed_ops_.push(h);
     597            1635 :     wake_one_thread_and_unlock(lock);
     598            1635 : }
     599                 : 
     600                 : inline void
     601          239059 : reactor_scheduler::post(capy::continuation& c) const
     602                 : {
     603          239059 :     if (auto* ctx = reactor_find_context(this))
     604                 :     {
     605          206369 :         ++ctx->private_outstanding_work;
     606          206369 :         ctx->private_queue.push(c);
     607          206369 :         return;
     608                 :     }
     609                 : 
     610           32690 :     outstanding_work_.fetch_add(1, std::memory_order_relaxed);
     611                 : 
     612           32690 :     lock_type lock(mutex_);
     613           32690 :     completed_ops_.push(c);
     614           32690 :     wake_one_thread_and_unlock(lock);
     615           32690 : }
     616                 : 
     617                 : inline bool
     618           24269 : reactor_scheduler::running_in_this_thread() const noexcept
     619                 : {
     620           24269 :     return reactor_find_context(this) != nullptr;
     621                 : }
     622                 : 
     623                 : inline void
     624            8901 : reactor_scheduler::stop()
     625                 : {
     626            8901 :     lock_type lock(mutex_);
     627            8901 :     if (!stopped_.load(std::memory_order_acquire))
     628                 :     {
     629            7851 :         stopped_.store(true, std::memory_order_release);
     630            7851 :         signal_all(lock);
     631            7851 :         interrupt_reactor();
     632                 :     }
     633            8901 : }
     634                 : 
     635                 : inline bool
     636            1370 : reactor_scheduler::stopped() const noexcept
     637                 : {
     638            1370 :     return stopped_.load(std::memory_order_acquire);
     639                 : }
     640                 : 
     641                 : inline void
     642            8079 : reactor_scheduler::restart()
     643                 : {
     644            8079 :     stopped_.store(false, std::memory_order_release);
     645            8079 : }
     646                 : 
     647                 : inline std::size_t
     648            6901 : reactor_scheduler::run()
     649                 : {
     650           13802 :     if (outstanding_work_.load(std::memory_order_acquire) == 0)
     651                 :     {
     652             229 :         stop();
     653             229 :         return 0;
     654                 :     }
     655                 : 
     656            6672 :     reactor_thread_context_guard ctx(this);
     657            6672 :     lock_type lock(mutex_);
     658            6672 :     adopt_enclosing_work(ctx.frame_);
     659                 : 
     660            6672 :     std::size_t n = 0;
     661                 :     for (;;)
     662                 :     {
     663          382863 :         if (!do_one(lock, -1, ctx.frame_))
     664            6663 :             break;
     665          376191 :         if (n != (std::numeric_limits<std::size_t>::max)())
     666          376191 :             ++n;
     667          376191 :         if (!lock.owns_lock())
     668           75319 :             lock.lock();
     669                 :     }
     670            6663 :     return n;
     671            6681 : }
     672                 : 
     673                 : inline std::size_t
     674             146 : reactor_scheduler::run_one()
     675                 : {
     676             292 :     if (outstanding_work_.load(std::memory_order_acquire) == 0)
     677                 :     {
     678               5 :         stop();
     679               5 :         return 0;
     680                 :     }
     681                 : 
     682             141 :     reactor_thread_context_guard ctx(this);
     683             141 :     lock_type lock(mutex_);
     684             141 :     adopt_enclosing_work(ctx.frame_);
     685             141 :     return do_one(lock, -1, ctx.frame_);
     686             143 : }
     687                 : 
     688                 : inline std::size_t
     689            3823 : reactor_scheduler::wait_one(long usec)
     690                 : {
     691            7646 :     if (outstanding_work_.load(std::memory_order_acquire) == 0)
     692                 :     {
     693             847 :         stop();
     694             847 :         return 0;
     695                 :     }
     696                 : 
     697            2976 :     reactor_thread_context_guard ctx(this);
     698            2976 :     lock_type lock(mutex_);
     699            2976 :     adopt_enclosing_work(ctx.frame_);
     700            2976 :     return do_one(lock, usec, ctx.frame_);
     701            2984 : }
     702                 : 
     703                 : inline std::size_t
     704            1483 : reactor_scheduler::poll()
     705                 : {
     706            2966 :     if (outstanding_work_.load(std::memory_order_acquire) == 0)
     707                 :     {
     708              31 :         stop();
     709              31 :         return 0;
     710                 :     }
     711                 : 
     712            1452 :     reactor_thread_context_guard ctx(this);
     713            1452 :     lock_type lock(mutex_);
     714            1452 :     adopt_enclosing_work(ctx.frame_);
     715                 : 
     716            1452 :     std::size_t n = 0;
     717                 :     for (;;)
     718                 :     {
     719            9640 :         if (!do_one(lock, 0, ctx.frame_))
     720            1449 :             break;
     721            8188 :         if (n != (std::numeric_limits<std::size_t>::max)())
     722            8188 :             ++n;
     723            8188 :         if (!lock.owns_lock())
     724            8184 :             lock.lock();
     725                 :     }
     726            1449 :     return n;
     727            1455 : }
     728                 : 
     729                 : inline std::size_t
     730              79 : reactor_scheduler::poll_one()
     731                 : {
     732             158 :     if (outstanding_work_.load(std::memory_order_acquire) == 0)
     733                 :     {
     734               7 :         stop();
     735               7 :         return 0;
     736                 :     }
     737                 : 
     738              72 :     reactor_thread_context_guard ctx(this);
     739              72 :     lock_type lock(mutex_);
     740              72 :     adopt_enclosing_work(ctx.frame_);
     741              72 :     return do_one(lock, 0, ctx.frame_);
     742              74 : }
     743                 : 
     744                 : inline void
     745           67457 : reactor_scheduler::work_started() noexcept
     746                 : {
     747           67457 :     outstanding_work_.fetch_add(1, std::memory_order_relaxed);
     748           67457 : }
     749                 : 
     750                 : inline void
     751          112861 : reactor_scheduler::work_finished() noexcept
     752                 : {
     753          225722 :     if (outstanding_work_.fetch_sub(1, std::memory_order_acq_rel) == 1)
     754            7677 :         stop();
     755          112861 : }
     756                 : 
     757                 : inline void
     758           21512 : reactor_scheduler::compensating_work_started() const noexcept
     759                 : {
     760           21512 :     auto* ctx = reactor_find_context(this);
     761           21512 :     if (ctx)
     762           21510 :         ++ctx->private_outstanding_work;
     763           21512 : }
     764                 : 
     765                 : inline void
     766            1987 : reactor_scheduler::post_counted(scheduler_op* op) const
     767                 : {
     768            1987 :     if (auto* ctx = reactor_find_context(this))
     769                 :     {
     770             602 :         ctx->private_queue.push(op);
     771             602 :         return;
     772                 :     }
     773                 : 
     774            1385 :     lock_type lock(mutex_);
     775            1385 :     completed_ops_.push(op);
     776            1385 :     wake_one_thread_and_unlock(lock);
     777            1385 : }
     778                 : 
     779                 : inline void
     780           16078 : reactor_scheduler::post_deferred_completions(ready_queue& ops) const
     781                 : {
     782           16078 :     if (ops.empty())
     783           16078 :         return;
     784                 : 
     785               2 :     if (auto* ctx = reactor_find_context(this))
     786                 :     {
     787               2 :         ctx->private_queue.splice(ops);
     788               2 :         return;
     789                 :     }
     790                 : 
     791 MIS           0 :     lock_type lock(mutex_);
     792               0 :     completed_ops_.splice(ops);
     793               0 :     wake_one_thread_and_unlock(lock);
     794               0 : }
     795                 : 
     796                 : inline void
     797 HIT       23092 : reactor_scheduler::retire_descriptor(
     798                 :     reactor_retirable& d, object_ref keep) const
     799                 : {
     800           23092 :     lock_type lock(mutex_);
     801           23092 :     if (!task_running_.load(std::memory_order_relaxed))
     802                 :     {
     803                 :         // No pass can hold an event, but an earlier one may have
     804                 :         // queued d.
     805           17921 :         d.hand_off(keep);
     806           17921 :         return;
     807                 :     }
     808                 :     // Already parked for this pass: the held reference suffices.
     809            5171 :     if (d.retired_ref_)
     810              98 :         return;
     811            5073 :     d.retired_ref_  = std::move(keep);
     812            5073 :     d.retired_next_ = retired_;
     813            5073 :     retired_        = &d;
     814           23092 : }
     815                 : 
     816                 : inline void
     817           11313 : reactor_scheduler::adopt_enclosing_work(context_type const& ctx)
     818                 : {
     819                 :     // A run nested in a handler would otherwise never see what that
     820                 :     // handler posted: it stays in the handler's frame until it returns.
     821           11319 :     for (auto* c = ctx.next; c != nullptr; c = c->next)
     822                 :     {
     823              12 :         if (c->key != this)
     824               6 :             continue;
     825               6 :         if (c->private_outstanding_work > 0)
     826                 :         {
     827               6 :             outstanding_work_.fetch_add(
     828                 :                 c->private_outstanding_work, std::memory_order_relaxed);
     829               6 :             c->private_outstanding_work = 0;
     830                 :         }
     831               6 :         completed_ops_.splice(c->private_queue);
     832               6 :         return;
     833                 :     }
     834                 : }
     835                 : 
     836                 : inline void
     837          176292 : reactor_scheduler::settle_retired(lock_type& lock) const
     838                 : {
     839                 :     // Handed off under the lock, since a later pass may retire the same
     840                 :     // descriptor again as soon as it is released. A reference that is
     841                 :     // not handed off may be the owner's last, and its release recycles
     842                 :     // through the pool mutex, so it is dropped with the lock released.
     843          176292 :     reactor_retirable* dropped = nullptr;
     844          181365 :     while (auto* d = retired_)
     845                 :     {
     846            5073 :         retired_         = d->retired_next_;
     847            5073 :         d->retired_next_ = nullptr;
     848            5073 :         auto keep        = std::move(d->retired_ref_);
     849            5073 :         d->hand_off(keep);
     850                 :         // A drop already pending keeps the owner alive, so this one
     851                 :         // cannot be the last and is released here.
     852            5073 :         if (!keep || d->dropped_ref_)
     853             360 :             continue;
     854            4713 :         d->dropped_ref_  = std::move(keep);
     855            4713 :         d->dropped_next_ = dropped;
     856            4713 :         dropped          = d;
     857           10146 :     }
     858          181005 :     while (auto* d = dropped)
     859                 :     {
     860            4713 :         dropped          = d->dropped_next_;
     861            4713 :         d->dropped_next_ = nullptr;
     862            4713 :         auto keep        = std::move(d->dropped_ref_);
     863            4713 :         lock.unlock();
     864            4713 :         keep.reset();
     865            4713 :         lock.lock();
     866            4713 :     }
     867          176292 : }
     868                 : 
     869                 : inline void
     870             411 : reactor_scheduler::on_timer_service_attached(timer_service& svc)
     871                 : {
     872             411 :     svc.set_on_earliest_changed(timer_service::callback(this, [](void* p) {
     873            3299 :         static_cast<reactor_scheduler*>(p)->interrupt_reactor();
     874            3299 :     }));
     875             411 : }
     876                 : 
     877                 : inline void
     878            4685 : reactor_scheduler::shutdown_drain()
     879                 : {
     880            4685 :     wait_cancels_posting();
     881                 : 
     882            4685 :     lock_type lock(mutex_);
     883            4685 :     settle_retired(lock);
     884            4685 :     destroy_queued(lock);
     885            4685 :     signal_all(lock);
     886            4685 : }
     887                 : 
     888            4701 : inline reactor_scheduler::~reactor_scheduler()
     889                 : {
     890                 :     // Services give up their work in shutdown(), but anything posted
     891                 :     // after the drain, such as from a destructor, is still destroyed.
     892            4701 :     lock_type lock(mutex_);
     893            4701 :     destroy_queued(lock);
     894            4701 : }
     895                 : 
     896                 : inline void
     897            9386 : reactor_scheduler::destroy_queued(lock_type& lock) noexcept
     898                 : {
     899           23546 :     while (auto e = completed_ops_.pop())
     900                 :     {
     901           14160 :         if (ready_is_continuation(e))
     902                 :         {
     903            7834 :             lock.unlock();
     904            7834 :             if (auto* c = ready_as_cont(e); c->h)
     905            7834 :                 capy::abandon(*c);
     906            7834 :             lock.lock();
     907                 :         }
     908                 :         else
     909                 :         {
     910            6326 :             auto* op = ready_as_op(e);
     911            6326 :             if (op == &task_op_)
     912            4685 :                 continue;
     913            1641 :             lock.unlock();
     914            1641 :             op->destroy();
     915            1641 :             lock.lock();
     916                 :         }
     917           14160 :     }
     918            9386 : }
     919                 : 
     920                 : inline void
     921           12544 : reactor_scheduler::signal_all(lock_type&) const
     922                 : {
     923           12544 :     state_ |= signaled_bit;
     924           12544 :     cond_.notify_all();
     925           12544 : }
     926                 : 
     927                 : inline bool
     928           41021 : reactor_scheduler::maybe_unlock_and_signal_one(lock_type& lock) const
     929                 : {
     930           41021 :     state_ |= signaled_bit;
     931           41021 :     if (state_ > signaled_bit)
     932                 :     {
     933                 :         // Signalled before unlocking: a post from another context's
     934                 :         // thread may be the last touch of this scheduler before the
     935                 :         // posted work runs, finishes and lets it be destroyed.
     936             210 :         cond_.notify_one();
     937             210 :         lock.unlock();
     938             210 :         return true;
     939                 :     }
     940           40811 :     return false;
     941                 : }
     942                 : 
     943                 : inline bool
     944          514562 : reactor_scheduler::unlock_and_signal_one(lock_type& lock) const
     945                 : {
     946          514562 :     state_ |= signaled_bit;
     947          514562 :     bool have_waiters = state_ > signaled_bit;
     948          514562 :     lock.unlock();
     949          514562 :     if (have_waiters)
     950            3440 :         cond_.notify_one();
     951          514562 :     return have_waiters;
     952                 : }
     953                 : 
     954                 : inline void
     955             821 : reactor_scheduler::clear_signal() const
     956                 : {
     957             821 :     state_ &= ~signaled_bit;
     958             821 : }
     959                 : 
     960                 : inline void
     961             804 : reactor_scheduler::wait_for_signal(lock_type& lock) const
     962                 : {
     963            1666 :     while ((state_ & signaled_bit) == 0)
     964                 :     {
     965             862 :         state_ += waiter_increment;
     966             862 :         cond_.wait(lock);
     967             862 :         state_ -= waiter_increment;
     968                 :     }
     969             804 : }
     970                 : 
     971                 : inline void
     972              17 : reactor_scheduler::wait_for_signal_for(lock_type& lock, long timeout_us) const
     973                 : {
     974              17 :     if ((state_ & signaled_bit) == 0)
     975                 :     {
     976              17 :         state_ += waiter_increment;
     977              17 :         cond_.wait_for(lock, std::chrono::microseconds(timeout_us));
     978              17 :         state_ -= waiter_increment;
     979                 :     }
     980              17 : }
     981                 : 
     982                 : inline void
     983           39538 : reactor_scheduler::wake_one_thread_and_unlock(lock_type& lock) const
     984                 : {
     985           39538 :     if (maybe_unlock_and_signal_one(lock))
     986             200 :         return;
     987                 : 
     988           39338 :     if (task_running_.load(std::memory_order_relaxed) && !task_interrupted_)
     989                 :     {
     990            1676 :         task_interrupted_ = true;
     991                 :         // Under the lock, for the same reason as the signal above.
     992            1676 :         interrupt_reactor();
     993            1676 :         lock.unlock();
     994                 :     }
     995                 :     else
     996                 :     {
     997           37662 :         lock.unlock();
     998                 :     }
     999                 : }
    1000                 : 
    1001          387283 : inline reactor_scheduler::work_cleanup::~work_cleanup()
    1002                 : {
    1003          387283 :     std::int64_t produced = ctx.private_outstanding_work;
    1004          387283 :     if (produced > 1)
    1005             974 :         sched->outstanding_work_.fetch_add(
    1006                 :             produced - 1, std::memory_order_relaxed);
    1007          386309 :     else if (produced < 1)
    1008           65853 :         sched->work_finished();
    1009          387283 :     ctx.private_outstanding_work = 0;
    1010                 : 
    1011          387283 :     if (!ctx.private_queue.empty())
    1012                 :     {
    1013          301243 :         lock->lock();
    1014          301243 :         sched->completed_ops_.splice(ctx.private_queue);
    1015                 :     }
    1016          387283 : }
    1017                 : 
    1018          171615 : inline reactor_scheduler::task_cleanup::~task_cleanup()
    1019                 : {
    1020          171615 :     if (ctx.private_outstanding_work > 0)
    1021                 :     {
    1022           10169 :         sched->outstanding_work_.fetch_add(
    1023           10169 :             ctx.private_outstanding_work, std::memory_order_relaxed);
    1024           10169 :         ctx.private_outstanding_work = 0;
    1025                 :     }
    1026                 : 
    1027          171615 :     if (!ctx.private_queue.empty())
    1028                 :     {
    1029           32100 :         if (!lock->owns_lock())
    1030               1 :             lock->lock();
    1031           32100 :         sched->completed_ops_.splice(ctx.private_queue);
    1032                 :     }
    1033          171615 : }
    1034                 : 
    1035                 : inline std::size_t
    1036          395692 : reactor_scheduler::do_one(lock_type& lock, long timeout_us, context_type& ctx)
    1037                 : {
    1038                 :     for (;;)
    1039                 :     {
    1040          698391 :         if (stopped_.load(std::memory_order_acquire))
    1041                 :         {
    1042                 :             // The next run is scored afresh, but a deferral left pending
    1043                 :             // stays marked: clearing the mark would let a loop of short
    1044                 :             // runs defer the sentinel forever.
    1045            6902 :             defer_policy_.reset();
    1046            6902 :             return 0;
    1047                 :         }
    1048                 : 
    1049          691489 :         auto const e     = completed_ops_.pop();
    1050          691489 :         scheduler_op* op = ready_is_continuation(e) ? nullptr : ready_as_op(e);
    1051                 : 
    1052                 :         // Handle reactor sentinel — time to poll for I/O
    1053          691489 :         if (op == &task_op_)
    1054                 :         {
    1055          303374 :             bool more_handlers = !completed_ops_.empty();
    1056                 : 
    1057                 :             // Handlers queued here usually start the next I/O
    1058                 :             // themselves, so running them first can save a reactor pass.
    1059                 :             // Defer at most once in a row so reposting work cannot
    1060                 :             // starve I/O. poll() and poll_one() don't defer: the reactor
    1061                 :             // turn they would skip may be their last before returning.
    1062          303374 :             if (task_deferred_)
    1063                 :             {
    1064          131282 :                 task_deferred_ = false;
    1065          131282 :                 defer_policy_.on_deferred_turn(more_handlers);
    1066                 :             }
    1067          172092 :             else if (more_handlers && timeout_us != 0)
    1068                 :             {
    1069          133744 :                 if (defer_policy_.may_defer())
    1070                 :                 {
    1071          131754 :                     task_deferred_ = true;
    1072          131754 :                     completed_ops_.push(&task_op_);
    1073          131754 :                     continue;
    1074                 :                 }
    1075            1990 :                 defer_policy_.on_polled_instead();
    1076                 :             }
    1077                 : 
    1078          214030 :             if (!more_handlers &&
    1079           84820 :                 outstanding_work_.load(std::memory_order_acquire) == 0)
    1080                 :             {
    1081               5 :                 completed_ops_.push(&task_op_);
    1082               5 :                 return 0;
    1083                 :             }
    1084                 : 
    1085          171615 :             long task_timeout_us = more_handlers ? 0 : timeout_us;
    1086          171615 :             task_interrupted_    = task_timeout_us == 0;
    1087          171615 :             task_running_.store(true, std::memory_order_release);
    1088                 : 
    1089                 :             // Wake a peer to take the pending handlers while this thread
    1090                 :             // polls the reactor; skipped when one_thread_ (no peer exists).
    1091          171615 :             if (more_handlers && !one_thread_)
    1092          129208 :                 unlock_and_signal_one(lock);
    1093                 : 
    1094                 :             try
    1095                 :             {
    1096          171615 :                 run_task(lock, ctx, task_timeout_us);
    1097                 :             }
    1098               8 :             catch (...)
    1099                 :             {
    1100                 :                 // Without the sentinel no later run could poll, and a peer
    1101                 :                 // parked behind this pass would wait for it forever.
    1102               8 :                 if (!lock.owns_lock())
    1103               7 :                     lock.lock();
    1104               8 :                 task_running_.store(false, std::memory_order_relaxed);
    1105               8 :                 completed_ops_.push(&task_op_);
    1106               8 :                 signal_all(lock);
    1107               8 :                 throw;
    1108               8 :             }
    1109                 : 
    1110          171607 :             task_running_.store(false, std::memory_order_relaxed);
    1111          171607 :             bool const idle = completed_ops_.empty();
    1112          171607 :             completed_ops_.push(&task_op_);
    1113          171607 :             settle_retired(lock);
    1114                 :             // A bounded call blocks at most once, then finishes as a poll
    1115                 :             // does, running what its pass queued.
    1116          171607 :             if (timeout_us > 0)
    1117            2360 :                 timeout_us = 0;
    1118                 :             // A poll ends once a pass leaves nothing queued; going round
    1119                 :             // again would only repeat the pass. A peer that found the
    1120                 :             // queue empty during the pass may be asleep, so it is woken
    1121                 :             // to take the sentinel.
    1122          171607 :             if (timeout_us == 0 && idle)
    1123                 :             {
    1124            1483 :                 maybe_unlock_and_signal_one(lock);
    1125            1483 :                 return 0;
    1126                 :             }
    1127          170124 :             continue;
    1128          170124 :         }
    1129                 : 
    1130                 :         // Handle ready entry (op or continuation)
    1131          388115 :         if (e != nullptr)
    1132                 :         {
    1133          387283 :             bool more = !completed_ops_.empty();
    1134                 : 
    1135          387283 :             if (more && !one_thread_)
    1136                 :             {
    1137                 :                 // Wake a peer for the remaining work; unassisted if none
    1138                 :                 // was parked to take it.
    1139          385354 :                 ctx.unassisted = !unlock_and_signal_one(lock);
    1140                 :             }
    1141                 :             else
    1142                 :             {
    1143                 :                 // No peer to wake (one_thread_, or nothing more queued).
    1144            1929 :                 ctx.unassisted = more;
    1145            1929 :                 lock.unlock();
    1146                 :             }
    1147                 : 
    1148          387283 :             [[maybe_unused]] work_cleanup on_exit{this, &lock, ctx};
    1149                 : 
    1150          387283 :             if (ready_is_continuation(e))
    1151          231225 :                 ready_as_cont(e)->h.resume();
    1152                 :             else
    1153          156058 :                 (*op)();
    1154          387267 :             return 1;
    1155          387283 :         }
    1156                 : 
    1157            1664 :         if (outstanding_work_.load(std::memory_order_acquire) == 0 ||
    1158                 :             timeout_us == 0)
    1159              11 :             return 0;
    1160                 : 
    1161             821 :         clear_signal();
    1162             821 :         if (timeout_us < 0)
    1163                 :         {
    1164             804 :             wait_for_signal(lock);
    1165                 :         }
    1166                 :         else
    1167                 :         {
    1168              17 :             wait_for_signal_for(lock, timeout_us);
    1169                 :             // A bounded call blocks once, so its caller can check its
    1170                 :             // deadline; another thread may hold the reactor for good.
    1171              17 :             timeout_us = 0;
    1172                 :         }
    1173          302699 :     }
    1174                 : }
    1175                 : 
    1176                 : } // namespace boost::corosio::detail
    1177                 : 
    1178                 : #endif // BOOST_COROSIO_NATIVE_DETAIL_REACTOR_REACTOR_SCHEDULER_HPP
        

Generated by: LCOV version 2.3