YSTest: YTimer.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YTimer.cpp
浏览该文件的文档.
1 /*
2 © 2010-2015 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_YTimer
30
31 namespace YSLib
32 {
33
34 namespace Timers
35 {
36
37 namespace
38 {
39
40 bool NotInitialized(true);
41
43 void
44 InitClock() ynothrow
45 {
46 if(YB_UNLIKELY(NotInitialized))
47 {
48 // NOTE: To initialize.
49 GetTicks();
50 NotInitialized = {};
51 }
52 }
53
54
55 inline TimePoint
56 GetTickPoint()
57 {
58 return TimePoint(TimeSpan(GetTicks()));
59 }
60
61 } // unnamed namespace;
62
63 void
64 Delay(TimeSpan delay)
65 {
66 const auto end(GetTickPoint() + delay);
67
68 while(GetTickPoint() < end)
69 ;
70 }
71
72
73 Timer::Timer(Duration d, bool b) ynothrow
74 : nBase(), Interval(d)
75 {
76 InitClock();
77 if(b)
78 Activate(*this);
79 }
80 ImplDeDtor(Timer)
81
82 bool
83 Timer::Refresh()
84 {
85 YAssert(Interval != Duration::zero(), "Zero interval found.");
86
87 const auto tick(HighResolutionClock::now());
88
89 if(YB_LIKELY(tick < nBase + Interval))
90 return {};
91 nBase = tick - (tick - nBase) % Interval;
92 return true;
93 }
94
95 Duration
96 Timer::RefreshDelta()
97 {
98 YAssert(Interval != Duration::zero(), "Zero interval found.");
99 const auto tick(HighResolutionClock::now());
100 const auto delta(tick - nBase);
101
102 if(YB_UNLIKELY(!(tick < nBase + Interval)))
103 nBase = tick - delta % Interval;
104 return delta;
105 }
106
107 Duration
108 Timer::RefreshRemainder()
109 {
110 YAssert(Interval != Duration::zero(), "Zero interval found.");
111 const auto tick(HighResolutionClock::now());
112 const auto remainder((tick - nBase) % Interval);
113
114 if(YB_UNLIKELY(!(tick < nBase + Interval)))
115 nBase = tick - remainder;
116 return remainder;
117 }
118
119 } // namespace Timers;
120
121 } // namespace YSLib;
122
YSLib::Timers::ImplDeDtor
ImplDeDtor(Timer) bool Timer
Definition: YTimer.cpp:80
YSLib::Timers::Timer::Timer
Timer(Duration={}, bool=true) ynothrow
构造：使用时间间隔和激活状态。
Definition: YTimer.cpp:73
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:142
YModules.h
YSLib 服务模块配置文件。
YSLib::Timers::TimePoint
HighResolutionClock::time_point TimePoint
时刻。
Definition: YClock.h:74
YSLib::Timers::Timer::RefreshDelta
Duration RefreshDelta()
刷新：对非零时间间隔判断有效性并都更新时间基点。
Definition: YTimer.cpp:96
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
ystdex::cpp2017::end
auto end(_tRange &c) -> decltype(c.end())
Definition: range.hpp:345
YSLib::Timers::Delay
YF_API void Delay(TimeSpan)
阻塞延时。
Definition: YTimer.cpp:64
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::Activate
Activate
Definition: YApplication.h:210
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:827
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::Timers::Timer
计时器。
Definition: YTimer.h:78
YSLib::Timers::Timer::Interval
Duration Interval
重复刷新有效的最小时间间隔。
Definition: YTimer.h:88
YSLib::Timers::Timer::RefreshRemainder
Duration RefreshRemainder()
刷新：对非零时间间隔判断有效性并都更新时间基点。
Definition: YTimer.cpp:108
platform::GetTicks
YF_API std::uint32_t GetTicks() ynothrow
取 tick 数。
Definition: Timer.cpp:76
YSLib::Timers::Timer::nBase
TimePoint nBase
时间基点：计时的起点。
Definition: YTimer.h:81
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:828
YSLib::Timers::TimeSpan
std::chrono::duration< Duration::rep, std::milli > TimeSpan
低精度时间间隔。
Definition: YClock.h:81
YSLib::Timers::Duration
HighResolutionClock::duration Duration
高精度时间间隔。
Definition: YClock.h:68