LCOV - code coverage report
Current view: top level - /jenkins/workspace/boost-root/libs/capy/src/test - run_blocking.cpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 96.2 % 53 51 2
Test Date: 2026-02-17 18:14:47 Functions: 92.9 % 14 13 1

           TLA  Line data    Source code
       1                 : //
       2                 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
       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/capy
       8                 : //
       9                 : 
      10                 : #include <boost/capy/test/run_blocking.hpp>
      11                 : 
      12                 : #include <condition_variable>
      13                 : #include <mutex>
      14                 : #include <queue>
      15                 : 
      16                 : namespace boost {
      17                 : namespace capy {
      18                 : namespace test {
      19                 : 
      20                 : struct blocking_context::impl
      21                 : {
      22                 :     std::mutex mtx;
      23                 :     std::condition_variable cv;
      24                 :     std::queue<std::coroutine_handle<>> queue;
      25                 :     std::exception_ptr ep;
      26                 :     bool done = false;
      27                 : };
      28                 : 
      29 HIT        2766 : blocking_context::blocking_context()
      30            2766 :     : impl_(new impl)
      31                 : {
      32            2766 : }
      33                 : 
      34            2766 : blocking_context::~blocking_context()
      35                 : {
      36            2766 :     delete impl_;
      37            2766 : }
      38                 : 
      39                 : blocking_executor
      40            2766 : blocking_context::get_executor() noexcept
      41                 : {
      42            2766 :     return blocking_executor{this};
      43                 : }
      44                 : 
      45                 : void
      46            1758 : blocking_context::signal_done() noexcept
      47                 : {
      48            1758 :     std::lock_guard<std::mutex> lock(impl_->mtx);
      49            1758 :     impl_->done = true;
      50            1758 :     impl_->cv.notify_one();
      51            1758 : }
      52                 : 
      53                 : void
      54            1000 : blocking_context::signal_done(
      55                 :     std::exception_ptr ep) noexcept
      56                 : {
      57            1000 :     std::lock_guard<std::mutex> lock(impl_->mtx);
      58            1000 :     impl_->ep = ep;
      59            1000 :     impl_->done = true;
      60            1000 :     impl_->cv.notify_one();
      61            1000 : }
      62                 : 
      63                 : void
      64            2758 : blocking_context::run()
      65                 : {
      66                 :     for(;;)
      67                 :     {
      68            2833 :         std::coroutine_handle<> h;
      69                 :         {
      70            2833 :             std::unique_lock<std::mutex> lock(impl_->mtx);
      71            2833 :             impl_->cv.wait(lock, [&] {
      72            2833 :                 return impl_->done || !impl_->queue.empty();
      73                 :             });
      74            2833 :             if(impl_->done && impl_->queue.empty())
      75            2758 :                 break;
      76              75 :             h = impl_->queue.front();
      77              75 :             impl_->queue.pop();
      78            2833 :         }
      79              75 :         h.resume();
      80              75 :     }
      81            2758 :     if(impl_->ep)
      82            1000 :         std::rethrow_exception(impl_->ep);
      83            1758 : }
      84                 : 
      85                 : void
      86              75 : blocking_context::enqueue(
      87                 :     std::coroutine_handle<> h)
      88                 : {
      89                 :     {
      90              75 :         std::lock_guard<std::mutex> lock(impl_->mtx);
      91              75 :         impl_->queue.push(h);
      92              75 :     }
      93              75 :     impl_->cv.notify_one();
      94              75 : }
      95                 : 
      96                 : //----------------------------------------------------------
      97                 : 
      98                 : bool
      99 MIS           0 : blocking_executor::operator==(
     100                 :     blocking_executor const& other) const noexcept
     101                 : {
     102               0 :     return ctx_ == other.ctx_;
     103                 : }
     104                 : 
     105                 : blocking_context&
     106 HIT        2758 : blocking_executor::context() const noexcept
     107                 : {
     108            2758 :     return *ctx_;
     109                 : }
     110                 : 
     111                 : void
     112            2758 : blocking_executor::on_work_started() const noexcept
     113                 : {
     114            2758 : }
     115                 : 
     116                 : void
     117            2758 : blocking_executor::on_work_finished() const noexcept
     118                 : {
     119            2758 : }
     120                 : 
     121                 : std::coroutine_handle<>
     122            2783 : blocking_executor::dispatch(
     123                 :     std::coroutine_handle<> h) const
     124                 : {
     125            2783 :     return h;
     126                 : }
     127                 : 
     128                 : void
     129              75 : blocking_executor::post(
     130                 :     std::coroutine_handle<> h) const
     131                 : {
     132              75 :     ctx_->enqueue(h);
     133              75 : }
     134                 : 
     135                 : } // namespace test
     136                 : } // namespace capy
     137                 : } // namespace boost
        

Generated by: LCOV version 2.3