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 "./base/deadline_timer.h" 25 : : 26 : : namespace vlink { 27 : : 28 : : // DeadlineTimer 29 : 10 : DeadlineTimer::DeadlineTimer() noexcept = default; 30 : : 31 : 13 : DeadlineTimer::DeadlineTimer(int64_t interval, Accuracy accuracy) noexcept : accuracy_(accuracy) { 32 : 13 : set_deadline(interval); 33 : 13 : } 34 : : 35 : 1 : DeadlineTimer::DeadlineTimer(const DeadlineTimer& other) noexcept 36 : 2 : : deadline_(other.deadline_.load(std::memory_order_acquire)), accuracy_(other.accuracy_) {} 37 : : 38 : 1 : DeadlineTimer::DeadlineTimer(DeadlineTimer&& other) noexcept 39 : 2 : : deadline_(other.deadline_.load(std::memory_order_acquire)), accuracy_(other.accuracy_) {} 40 : : 41 : 25 : DeadlineTimer::~DeadlineTimer() noexcept = default; 42 : : 43 : 1 : DeadlineTimer& DeadlineTimer::operator=(const DeadlineTimer& other) noexcept { 44 [ - + ]: 1 : if VUNLIKELY (this == &other) { 45 : 0 : return *this; 46 : : } 47 : : 48 : 1 : accuracy_ = other.accuracy_; 49 : 2 : deadline_.store(other.deadline_.load(std::memory_order_acquire), std::memory_order_release); 50 : : 51 : 1 : return *this; 52 : : } 53 : : 54 : 1 : DeadlineTimer& DeadlineTimer::operator=(DeadlineTimer&& other) noexcept { 55 [ - + ]: 1 : if VUNLIKELY (this == &other) { 56 : 0 : return *this; 57 : : } 58 : : 59 : 1 : accuracy_ = other.accuracy_; 60 : 2 : deadline_.store(other.deadline_.load(std::memory_order_acquire), std::memory_order_release); 61 : : 62 : 1 : return *this; 63 : : } 64 : : 65 : 19 : void DeadlineTimer::set_deadline(int64_t interval) noexcept { 66 [ + + ]: 19 : if VUNLIKELY (interval <= 0) { 67 : 1 : deadline_.store(0, std::memory_order_release); 68 : 1 : return; 69 : : } 70 : : 71 : : // Use the same monotonic time source as set_deadline_abs() callers are 72 : : // expected to use via ElapsedTimer::get_cpu_timestamp(accuracy_). 73 : 18 : uint64_t start_time = ElapsedTimer::get_cpu_timestamp(accuracy_); 74 : 18 : uint64_t new_deadline = start_time + static_cast<uint64_t>(interval); 75 : : 76 : 18 : deadline_.store(new_deadline, std::memory_order_release); 77 : : } 78 : : 79 : 3 : void DeadlineTimer::set_deadline_abs(uint64_t abs_deadline) noexcept { 80 : 3 : deadline_.store(abs_deadline, std::memory_order_release); 81 : 3 : } 82 : : 83 : 2 : void DeadlineTimer::reset() noexcept { deadline_.store(0, std::memory_order_release); } 84 : : 85 : 28 : uint64_t DeadlineTimer::deadline() const noexcept { return deadline_.load(std::memory_order_acquire); } 86 : : 87 : 5 : int64_t DeadlineTimer::remaining_time() const noexcept { 88 : 5 : uint64_t deadline_val = deadline_.load(std::memory_order_acquire); 89 : : 90 [ - + ]: 5 : if (deadline_val == 0) { 91 : 0 : return 0; 92 : : } 93 : : 94 : 5 : uint64_t now = ElapsedTimer::get_cpu_timestamp(accuracy_); 95 : : 96 [ + + ]: 5 : if (now >= deadline_val) { 97 : 1 : return 0; 98 : : } 99 : : 100 : 4 : return static_cast<int64_t>(deadline_val - now); 101 : : } 102 : : 103 : 8 : bool DeadlineTimer::has_expired() const noexcept { 104 : 8 : uint64_t deadline_val = deadline_.load(std::memory_order_acquire); 105 : : 106 [ + + ]: 8 : if (deadline_val == 0) { 107 : 2 : return false; 108 : : } 109 : : 110 : 6 : uint64_t now = ElapsedTimer::get_cpu_timestamp(accuracy_); 111 : : 112 : 6 : return now >= deadline_val; 113 : : } 114 : : 115 : 32 : bool DeadlineTimer::is_valid() const noexcept { return deadline_.load(std::memory_order_acquire) != 0; } 116 : : 117 : 3 : DeadlineTimer::Accuracy DeadlineTimer::get_accuracy() const noexcept { return accuracy_; } 118 : : 119 : : } // namespace vlink