From c04dcc2e7d834218ef2d4194331e383402495ae1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 20:07:22 +0200 Subject: Adding upstream version 2:20.4+dfsg. Signed-off-by: Daniel Baumann --- xbmc/threads/SystemClock.h | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 xbmc/threads/SystemClock.h (limited to 'xbmc/threads/SystemClock.h') diff --git a/xbmc/threads/SystemClock.h b/xbmc/threads/SystemClock.h new file mode 100644 index 0000000..92c4901 --- /dev/null +++ b/xbmc/threads/SystemClock.h @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2005-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#pragma once + +#include "utils/log.h" + +#include +#include +#include + +namespace XbmcThreads +{ + +template +struct is_chrono_duration : std::false_type +{ +}; + +template +struct is_chrono_duration> : std::true_type +{ +}; + +template::value> +class EndTime; + +template +class EndTime +{ +public: + explicit EndTime(const T duration) { Set(duration); } + + EndTime() = default; + EndTime(const EndTime& right) = delete; + ~EndTime() = default; + + static constexpr T Max() { return m_max; } + + void Set(const T duration) + { + m_startTime = std::chrono::steady_clock::now(); + + if (duration > m_max) + { + m_totalWaitTime = m_max; + CLog::Log(LOGWARNING, "duration ({}) greater than max ({}) - duration will be truncated!", + duration.count(), m_max.count()); + } + else + { + m_totalWaitTime = duration; + } + } + + bool IsTimePast() const + { + const auto now = std::chrono::steady_clock::now(); + + return ((now - m_startTime) >= m_totalWaitTime); + } + + T GetTimeLeft() const + { + const auto now = std::chrono::steady_clock::now(); + + const auto left = ((m_startTime + m_totalWaitTime) - now); + + if (left < T::zero()) + return T::zero(); + + return std::chrono::duration_cast(left); + } + + void SetExpired() { m_totalWaitTime = T::zero(); } + + void SetInfinite() { m_totalWaitTime = m_max; } + + T GetInitialTimeoutValue() const { return m_totalWaitTime; } + + std::chrono::steady_clock::time_point GetStartTime() const { return m_startTime; } + +private: + std::chrono::steady_clock::time_point m_startTime; + T m_totalWaitTime = T::zero(); + + static constexpr T m_max = + std::chrono::duration_cast(std::chrono::steady_clock::duration::max()); +}; + +} // namespace XbmcThreads -- cgit v1.2.3