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 : 181 : ClientImpl::~ClientImpl() = default; 46 : : 47 : 147 : void ClientImpl::interrupt() { 48 : : { 49 [ + - ]: 147 : std::lock_guard sync_lock(helper_->mtx); 50 [ + - ]: 147 : NodeImpl::interrupt(); 51 : 147 : } 52 : : 53 : 147 : helper_->connected_cv.notify_all(); 54 : 147 : } 55 : : 56 : 17 : void ClientImpl::detect_connected(ConnectCallback&& callback) { 57 [ + - ]: 17 : std::unique_lock lock(helper_->callback_mtx); 58 : 17 : helper_->connected_callback = std::move(callback); 59 : : 60 [ + + + + : 17 : if (helper_->connected_callback && helper_->connected.load(std::memory_order_acquire)) { + + ] 61 [ + - ]: 6 : auto callback_copy = helper_->connected_callback; 62 [ + - ]: 6 : lock.unlock(); 63 [ + - ]: 6 : callback_copy(true); 64 : 6 : } 65 : 17 : } 66 : : 67 : 268 : bool ClientImpl::wait_for_connected(std::chrono::milliseconds timeout) { 68 [ + - + + ]: 268 : if VLIKELY (is_connected()) { 69 : 199 : return true; 70 : : } 71 : : 72 : 69 : Utils::yield_cpu(); 73 : : 74 [ + - ]: 69 : std::unique_lock lock(helper_->mtx); 75 : : 76 [ + - ]: 69 : reset_interrupted(); 77 : : 78 [ + + + + ]: 138 : auto predicate = [this]() -> bool { return helper_->connected.load(std::memory_order_acquire) || is_interrupted(); }; 79 : : 80 [ + + ]: 69 : 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 [ + - + + : 67 : return helper_->connected_cv.wait_for(lock, timeout, std::move(predicate)) && !is_interrupted(); + - + - ] 85 : : } 86 : 69 : } 87 : : 88 : 226 : void ClientImpl::update_connected() { 89 : 226 : Utils::yield_cpu(); 90 : : 91 [ + - ]: 226 : std::unique_lock lock(helper_->callback_mtx); 92 [ + - ]: 226 : const bool connected_now = is_connected(); 93 : : 94 [ + + ]: 226 : if (helper_->connected.exchange(connected_now, std::memory_order_acq_rel) == connected_now) { 95 : 85 : return; 96 : : } 97 : : 98 : : { 99 [ + - ]: 141 : std::lock_guard sync_lock(helper_->mtx); 100 : 141 : } 101 : : 102 : 141 : helper_->connected_cv.notify_all(); 103 : : 104 [ + + ]: 141 : if (helper_->connected_callback) { 105 [ + - ]: 15 : auto callback_copy = helper_->connected_callback; 106 [ + - ]: 15 : lock.unlock(); 107 [ + - ]: 15 : callback_copy(connected_now); 108 : 15 : } 109 [ + + ]: 226 : } 110 : : 111 [ + - ]: 181 : ClientImpl::ClientImpl() : NodeImpl(kClient), helper_(std::make_unique<ClientImplHelper>()) {} 112 : : 113 : : } // namespace vlink