LCOV - code coverage report
Current view: top level - src/impl - publisher_impl.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 48 48 100.0 %
Date: 2026-06-27 19:56:04 Functions: 9 10 90.0 %
Branches: 39 58 67.2 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2026 by Thun Lu. All rights reserved.
       3                 :            :  * Author: Thun Lu <thun.lu@zohomail.cn>
       4                 :            :  * Repo:   https://github.com/thun-res/vlink
       5                 :            :  *  _    __   __      _           __
       6                 :            :  * | |  / /  / /     (_) ____    / /__
       7                 :            :  * | | / /  / /     / / / __ \  / //_/
       8                 :            :  * | |/ /  / /___  / / / / / / / ,<
       9                 :            :  * |___/  /_____/ /_/ /_/ /_/ /_/|_|
      10                 :            :  *
      11                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
      12                 :            :  * you may not use this file except in compliance with the License.
      13                 :            :  * You may obtain a copy of the License at
      14                 :            :  *
      15                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  *
      17                 :            :  * Unless required by applicable law or agreed to in writing, software
      18                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      19                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      20                 :            :  * See the License for the specific language governing permissions and
      21                 :            :  * limitations under the License.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "./impl/publisher_impl.h"
      25                 :            : 
      26                 :            : #include <mutex>
      27                 :            : #include <utility>
      28                 :            : 
      29                 :            : #include "./base/condition_variable.h"
      30                 :            : #include "./base/utils.h"
      31                 :            : 
      32                 :            : namespace vlink {
      33                 :            : 
      34                 :            : // PublisherImplHelper
      35                 :            : struct PublisherImplHelper final {
      36                 :            :   bool has_subscribers{false};
      37                 :            :   NodeImpl::ConnectCallback connected_callback;
      38                 :            :   ConditionVariable connected_cv;
      39                 :            :   std::mutex mtx;
      40                 :            :   std::recursive_mutex callback_mtx;
      41                 :            : };
      42                 :            : 
      43                 :            : // PublisherImpl
      44                 :        326 : PublisherImpl::~PublisherImpl() = default;
      45                 :            : 
      46                 :        295 : void PublisherImpl::interrupt() {
      47                 :        295 :   NodeImpl::interrupt();
      48                 :            : 
      49                 :            :   {
      50         [ +  - ]:        295 :     std::lock_guard sync_lock(helper_->mtx);
      51                 :        295 :   }
      52                 :            : 
      53                 :        295 :   helper_->connected_cv.notify_all();
      54                 :        295 : }
      55                 :            : 
      56                 :         14 : void PublisherImpl::detect_subscribers(ConnectCallback&& callback) {
      57         [ +  - ]:         14 :   std::unique_lock lock(helper_->callback_mtx);
      58                 :         14 :   helper_->connected_callback = std::move(callback);
      59                 :            : 
      60         [ +  + ]:         14 :   if (helper_->has_subscribers) {
      61         [ +  - ]:          1 :     auto callback_copy = helper_->connected_callback;
      62         [ +  - ]:          1 :     lock.unlock();
      63         [ +  - ]:          1 :     callback_copy(true);
      64                 :          1 :   }
      65                 :         14 : }
      66                 :            : 
      67                 :        194 : bool PublisherImpl::wait_for_subscribers(std::chrono::milliseconds timeout) {
      68   [ +  -  +  + ]:        194 :   if VLIKELY (has_subscribers()) {
      69                 :        151 :     return true;
      70                 :            :   }
      71                 :            : 
      72                 :         43 :   Utils::yield_cpu();
      73                 :            : 
      74         [ +  - ]:         43 :   std::unique_lock lock(helper_->mtx);
      75                 :            : 
      76         [ +  - ]:         43 :   reset_interrupted();
      77                 :            : 
      78   [ +  +  +  + ]:         86 :   auto predicate = [this]() -> bool { return has_subscribers() || is_interrupted(); };
      79                 :            : 
      80         [ +  + ]:         43 :   if VUNLIKELY (timeout.count() < 0) {
      81                 :          1 :     helper_->connected_cv.wait(lock, std::move(predicate));
      82         [ +  - ]:          1 :     return has_subscribers();
      83                 :            :   } else {
      84   [ +  +  +  -  :         42 :     return helper_->connected_cv.wait_for(lock, timeout, std::move(predicate)) && !is_interrupted();
                   +  + ]
      85                 :            :   }
      86                 :         43 : }
      87                 :            : 
      88                 :          1 : bool PublisherImpl::write(const IntraData& intra_data) {
      89                 :            :   (void)intra_data;
      90                 :            : 
      91   [ +  -  +  - ]:          2 :   VLOG_W("Function [write(const IntraData&)] is not supported.");
      92                 :            : 
      93                 :          1 :   return false;
      94                 :            : }
      95                 :            : 
      96                 :        680 : void PublisherImpl::update_subscribers() {
      97                 :        680 :   Utils::yield_cpu();
      98                 :            : 
      99         [ +  - ]:        680 :   std::unique_lock lock(helper_->callback_mtx);
     100                 :            : 
     101   [ +  -  +  + ]:        680 :   if (helper_->has_subscribers == has_subscribers()) {
     102                 :        455 :     return;
     103                 :            :   }
     104                 :            : 
     105                 :        225 :   helper_->has_subscribers = !helper_->has_subscribers;
     106                 :            : 
     107                 :            :   {
     108         [ +  - ]:        225 :     std::lock_guard sync_lock(helper_->mtx);
     109                 :        225 :   }
     110                 :            : 
     111                 :        225 :   helper_->connected_cv.notify_all();
     112                 :            : 
     113         [ +  + ]:        225 :   if (helper_->connected_callback) {
     114                 :         14 :     bool has_subs = helper_->has_subscribers;
     115         [ +  - ]:         14 :     auto callback_copy = helper_->connected_callback;
     116         [ +  - ]:         14 :     lock.unlock();
     117         [ +  - ]:         14 :     callback_copy(has_subs);
     118                 :         14 :   }
     119         [ +  + ]:        680 : }
     120                 :            : 
     121         [ +  - ]:        326 : PublisherImpl::PublisherImpl() : NodeImpl(kPublisher), helper_(std::make_unique<PublisherImplHelper>()) {}
     122                 :            : 
     123                 :            : }  // namespace vlink

Generated by: LCOV version 1.14