Replacing the tick count timer with xplat abstraction (also better rate).
Fixes #346.
This commit is contained in:
@@ -36,6 +36,8 @@ uint64_t Clock::QueryHostSystemTime() {
|
||||
return (uint64_t(t.dwHighDateTime) << 32) | t.dwLowDateTime;
|
||||
}
|
||||
|
||||
uint32_t Clock::QueryHostUptimeMillis() { return ::GetTickCount(); }
|
||||
uint32_t Clock::QueryHostUptimeMillis() {
|
||||
return uint32_t(QueryHostTickCount() / (host_tick_frequency() / 1000));
|
||||
}
|
||||
|
||||
} // namespace xe
|
||||
|
||||
@@ -97,6 +97,20 @@ bool FreeTlsHandle(TlsHandle handle);
|
||||
uintptr_t GetTlsValue(TlsHandle handle);
|
||||
bool SetTlsValue(TlsHandle handle, uintptr_t value);
|
||||
|
||||
// A high-resolution timer capable of firing at millisecond-precision.
|
||||
// All timers created in this way are executed in the same thread so
|
||||
// callbacks must be kept short or else all timers will be impacted.
|
||||
class HighResolutionTimer {
|
||||
public:
|
||||
virtual ~HighResolutionTimer() = default;
|
||||
|
||||
// Creates a new repeating timer with the given period.
|
||||
// The given function will be called back as close to the given period as
|
||||
// possible.
|
||||
static std::unique_ptr<HighResolutionTimer> CreateRepeating(
|
||||
std::chrono::milliseconds period, std::function<void()> callback);
|
||||
};
|
||||
|
||||
// Results for a WaitHandle operation.
|
||||
enum class WaitResult {
|
||||
// The state of the specified object is signaled.
|
||||
|
||||
@@ -108,6 +108,44 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) {
|
||||
return TlsSetValue(handle, reinterpret_cast<void*>(value)) ? true : false;
|
||||
}
|
||||
|
||||
class Win32HighResolutionTimer : public HighResolutionTimer {
|
||||
public:
|
||||
Win32HighResolutionTimer(std::function<void()> callback)
|
||||
: callback_(callback) {}
|
||||
~Win32HighResolutionTimer() override {
|
||||
if (handle_) {
|
||||
DeleteTimerQueueTimer(nullptr, handle_, INVALID_HANDLE_VALUE);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool Initialize(std::chrono::milliseconds period) {
|
||||
return CreateTimerQueueTimer(
|
||||
&handle_, nullptr,
|
||||
[](PVOID param, BOOLEAN timer_or_wait_fired) {
|
||||
auto timer =
|
||||
reinterpret_cast<Win32HighResolutionTimer*>(param);
|
||||
timer->callback_();
|
||||
},
|
||||
this, 0, DWORD(period.count()), WT_EXECUTEINTIMERTHREAD)
|
||||
? true
|
||||
: false;
|
||||
}
|
||||
|
||||
private:
|
||||
HANDLE handle_ = nullptr;
|
||||
std::function<void()> callback_;
|
||||
};
|
||||
|
||||
std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
|
||||
std::chrono::milliseconds period, std::function<void()> callback) {
|
||||
auto timer = std::make_unique<Win32HighResolutionTimer>(std::move(callback));
|
||||
if (!timer->Initialize(period)) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::unique_ptr<HighResolutionTimer>(timer.release());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Win32Handle : public T {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user