[Base] Fix HighResolutionTimer

- Test was failing on Linux 5.11 and GLIBC 2.33
- `timer_t(0)` is a valid handle, so a `valid_` flag was added to guard
  destruction
- Similar behaviour on Windows was fixed as well. The invalid values for
  `HANDLE` are API dependent.
This commit is contained in:
Joel Linn 2021-05-14 00:40:41 +02:00 committed by Rick Gibbed
parent 6dfa36d1b8
commit 28ca58c0e9
2 changed files with 31 additions and 20 deletions

View file

@ -155,29 +155,36 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) {
class PosixHighResolutionTimer : public HighResolutionTimer { class PosixHighResolutionTimer : public HighResolutionTimer {
public: public:
explicit PosixHighResolutionTimer(std::function<void()> callback) explicit PosixHighResolutionTimer(std::function<void()> callback)
: callback_(std::move(callback)), timer_(nullptr) {} : callback_(std::move(callback)), valid_(false) {}
~PosixHighResolutionTimer() override { ~PosixHighResolutionTimer() override {
if (timer_) timer_delete(timer_); if (valid_) timer_delete(timer_);
} }
bool Initialize(std::chrono::milliseconds period) { bool Initialize(std::chrono::milliseconds period) {
if (valid_) {
// Double initialization
assert_always();
return false;
}
// Create timer // Create timer
sigevent sev{}; sigevent sev{};
sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = GetSystemSignal(SignalType::kHighResolutionTimer); sev.sigev_signo = GetSystemSignal(SignalType::kHighResolutionTimer);
sev.sigev_value.sival_ptr = (void*)&callback_; sev.sigev_value.sival_ptr = (void*)&callback_;
if (timer_create(CLOCK_REALTIME, &sev, &timer_) == -1) return false; if (timer_create(CLOCK_MONOTONIC, &sev, &timer_) == -1) return false;
// Start timer // Start timer
itimerspec its{}; itimerspec its{};
its.it_value = DurationToTimeSpec(period); its.it_value = DurationToTimeSpec(period);
its.it_interval = its.it_value; its.it_interval = its.it_value;
return timer_settime(timer_, 0, &its, nullptr) != -1; valid_ = timer_settime(timer_, 0, &its, nullptr) != -1;
return valid_;
} }
private: private:
std::function<void()> callback_; std::function<void()> callback_;
timer_t timer_; timer_t timer_;
bool valid_; // all values for timer_t are legal so we need this
}; };
std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating( std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
@ -187,7 +194,7 @@ std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
if (!timer->Initialize(period)) { if (!timer->Initialize(period)) {
return nullptr; return nullptr;
} }
return std::unique_ptr<HighResolutionTimer>(timer.release()); return std::move(timer);
} }
class PosixConditionBase { class PosixConditionBase {
@ -419,7 +426,7 @@ class PosixCondition<Timer> : public PosixConditionBase {
sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = GetSystemSignal(SignalType::kTimer); sev.sigev_signo = GetSystemSignal(SignalType::kTimer);
sev.sigev_value.sival_ptr = this; sev.sigev_value.sival_ptr = this;
if (timer_create(CLOCK_REALTIME, &sev, &timer_) == -1) return false; if (timer_create(CLOCK_MONOTONIC, &sev, &timer_) == -1) return false;
} }
// Start timer // Start timer

View file

@ -111,30 +111,34 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) {
class Win32HighResolutionTimer : public HighResolutionTimer { class Win32HighResolutionTimer : public HighResolutionTimer {
public: public:
Win32HighResolutionTimer(std::function<void()> callback) Win32HighResolutionTimer(std::function<void()> callback)
: callback_(callback) {} : callback_(std::move(callback)) {}
~Win32HighResolutionTimer() override { ~Win32HighResolutionTimer() override {
if (handle_) { if (valid_) {
DeleteTimerQueueTimer(nullptr, handle_, INVALID_HANDLE_VALUE); DeleteTimerQueueTimer(nullptr, handle_, INVALID_HANDLE_VALUE);
handle_ = nullptr; handle_ = nullptr;
} }
} }
bool Initialize(std::chrono::milliseconds period) { bool Initialize(std::chrono::milliseconds period) {
return CreateTimerQueueTimer( if (valid_) {
&handle_, nullptr, // Double initialization
[](PVOID param, BOOLEAN timer_or_wait_fired) { assert_always();
auto timer = return false;
reinterpret_cast<Win32HighResolutionTimer*>(param); }
timer->callback_(); valid_ = !!CreateTimerQueueTimer(
}, &handle_, nullptr,
this, 0, DWORD(period.count()), WT_EXECUTEINTIMERTHREAD) [](PVOID param, BOOLEAN timer_or_wait_fired) {
? true auto timer = reinterpret_cast<Win32HighResolutionTimer*>(param);
: false; timer->callback_();
},
this, 0, DWORD(period.count()), WT_EXECUTEINTIMERTHREAD);
return valid_;
} }
private: private:
HANDLE handle_ = nullptr;
std::function<void()> callback_; std::function<void()> callback_;
HANDLE handle_ = nullptr;
bool valid_ = false; // Documentation does not state which HANDLE is invalid
}; };
std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating( std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
@ -143,7 +147,7 @@ std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
if (!timer->Initialize(period)) { if (!timer->Initialize(period)) {
return nullptr; return nullptr;
} }
return std::unique_ptr<HighResolutionTimer>(timer.release()); return std::move(timer);
} }
template <typename T> template <typename T>