LCOV - code coverage report
Current view: top level - src/impl - client_impl.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 44 44 100.0 %
Date: 2026-07-26 14:05:51 Functions: 7 8 87.5 %
Branches: 38 58 65.5 %

           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/client_impl.h"
      25                 :            : 
      26                 :            : #include <atomic>
      27                 :            : #include <mutex>
      28                 :            : #include <utility>
      29                 :            : 
      30                 :            : #include "./base/condition_variable.h"
      31                 :            : #include "./base/utils.h"
      32                 :            : 
      33                 :            : namespace vlink {
      34                 :            : 
      35                 :            : // ClientImplHelper
      36                 :            : struct ClientImplHelper final {
      37                 :            :   std::atomic_bool connected{false};
      38                 :            :   NodeImpl::ConnectCallback connected_callback;
      39                 :            :   ConditionVariable connected_cv;
      40                 :            :   std::mutex mtx;
      41                 :            :   std::recursive_mutex callback_mtx;
      42                 :            : };
      43                 :            : 
      44                 :            : // ClientImpl
      45                 :        155 : ClientImpl::~ClientImpl() = default;
      46                 :            : 
      47                 :        123 : void ClientImpl::interrupt() {
      48                 :            :   {
      49         [ +  - ]:        123 :     std::lock_guard sync_lock(helper_->mtx);
      50         [ +  - ]:        123 :     NodeImpl::interrupt();
      51                 :        123 :   }
      52                 :            : 
      53                 :        123 :   helper_->connected_cv.notify_all();
      54                 :        123 : }
      55                 :            : 
      56                 :         13 : void ClientImpl::detect_connected(ConnectCallback&& callback) {
      57         [ +  - ]:         13 :   std::unique_lock lock(helper_->callback_mtx);
      58                 :         13 :   helper_->connected_callback = std::move(callback);
      59                 :            : 
      60         [ +  + ]:         13 :   if (helper_->connected.load(std::memory_order_acquire)) {
      61         [ +  - ]:          5 :     auto callback_copy = helper_->connected_callback;
      62         [ +  - ]:          5 :     lock.unlock();
      63         [ +  - ]:          5 :     callback_copy(true);
      64                 :          5 :   }
      65                 :         13 : }
      66                 :            : 
      67                 :        188 : bool ClientImpl::wait_for_connected(std::chrono::milliseconds timeout) {
      68   [ +  -  +  + ]:        188 :   if VLIKELY (is_connected()) {
      69                 :        154 :     return true;
      70                 :            :   }
      71                 :            : 
      72                 :         34 :   Utils::yield_cpu();
      73                 :            : 
      74         [ +  - ]:         34 :   std::unique_lock lock(helper_->mtx);
      75                 :            : 
      76         [ +  - ]:         34 :   reset_interrupted();
      77                 :            : 
      78   [ +  +  +  + ]:         68 :   auto predicate = [this]() -> bool { return helper_->connected.load(std::memory_order_acquire) || is_interrupted(); };
      79                 :            : 
      80         [ +  + ]:         34 :   if VUNLIKELY (timeout.count() < 0) {
      81         [ +  - ]:          2 :     helper_->connected_cv.wait(lock, std::move(predicate));
      82                 :          2 :     return helper_->connected.load(std::memory_order_acquire);
      83                 :            :   } else {
      84   [ +  -  +  +  :         32 :     return helper_->connected_cv.wait_for(lock, timeout, std::move(predicate)) && !is_interrupted();
             +  -  +  - ]
      85                 :            :   }
      86                 :         34 : }
      87                 :            : 
      88                 :        185 : void ClientImpl::update_connected() {
      89                 :        185 :   Utils::yield_cpu();
      90                 :            : 
      91         [ +  - ]:        185 :   std::unique_lock lock(helper_->callback_mtx);
      92         [ +  - ]:        185 :   const bool connected_now = is_connected();
      93                 :            : 
      94         [ +  + ]:        185 :   if (helper_->connected.exchange(connected_now, std::memory_order_acq_rel) == connected_now) {
      95                 :         74 :     return;
      96                 :            :   }
      97                 :            : 
      98                 :            :   {
      99         [ +  - ]:        111 :     std::lock_guard sync_lock(helper_->mtx);
     100                 :        111 :   }
     101                 :            : 
     102                 :        111 :   helper_->connected_cv.notify_all();
     103                 :            : 
     104         [ +  + ]:        111 :   if (helper_->connected_callback) {
     105         [ +  - ]:         11 :     auto callback_copy = helper_->connected_callback;
     106         [ +  - ]:         11 :     lock.unlock();
     107         [ +  - ]:         11 :     callback_copy(connected_now);
     108                 :         11 :   }
     109         [ +  + ]:        185 : }
     110                 :            : 
     111         [ +  - ]:        155 : ClientImpl::ClientImpl() : NodeImpl(kClient), helper_(std::make_unique<ClientImplHelper>()) {}
     112                 :            : 
     113                 :            : }  // namespace vlink

Generated by: LCOV version 1.14