Merge pull request #26308 from jjd-uk/estuary_playerprocess
[xbmc.git] / xbmc / threads / SystemClock.h
blob92c490134a96a7a72e3e16883e968623ffc31c2d
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "utils/log.h"
13 #include <chrono>
14 #include <limits>
15 #include <thread>
17 namespace XbmcThreads
20 template<typename>
21 struct is_chrono_duration : std::false_type
25 template<typename Rep, typename Period>
26 struct is_chrono_duration<std::chrono::duration<Rep, Period>> : std::true_type
30 template<typename T = std::chrono::milliseconds, bool = is_chrono_duration<T>::value>
31 class EndTime;
33 template<typename T>
34 class EndTime<T, true>
36 public:
37 explicit EndTime(const T duration) { Set(duration); }
39 EndTime() = default;
40 EndTime(const EndTime& right) = delete;
41 ~EndTime() = default;
43 static constexpr T Max() { return m_max; }
45 void Set(const T duration)
47 m_startTime = std::chrono::steady_clock::now();
49 if (duration > m_max)
51 m_totalWaitTime = m_max;
52 CLog::Log(LOGWARNING, "duration ({}) greater than max ({}) - duration will be truncated!",
53 duration.count(), m_max.count());
55 else
57 m_totalWaitTime = duration;
61 bool IsTimePast() const
63 const auto now = std::chrono::steady_clock::now();
65 return ((now - m_startTime) >= m_totalWaitTime);
68 T GetTimeLeft() const
70 const auto now = std::chrono::steady_clock::now();
72 const auto left = ((m_startTime + m_totalWaitTime) - now);
74 if (left < T::zero())
75 return T::zero();
77 return std::chrono::duration_cast<T>(left);
80 void SetExpired() { m_totalWaitTime = T::zero(); }
82 void SetInfinite() { m_totalWaitTime = m_max; }
84 T GetInitialTimeoutValue() const { return m_totalWaitTime; }
86 std::chrono::steady_clock::time_point GetStartTime() const { return m_startTime; }
88 private:
89 std::chrono::steady_clock::time_point m_startTime;
90 T m_totalWaitTime = T::zero();
92 static constexpr T m_max =
93 std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::max());
96 } // namespace XbmcThreads