[Base] Make HighResolutionTimer platform agnostic
This commit is contained in:
parent
75357caeaf
commit
9b4168cce9
|
@ -27,6 +27,7 @@
|
|||
#include "xenia/base/assert.h"
|
||||
#include "xenia/base/literals.h"
|
||||
#include "xenia/base/platform.h"
|
||||
#include "xenia/base/threading_timer_queue.h"
|
||||
|
||||
namespace xe {
|
||||
namespace threading {
|
||||
|
@ -141,18 +142,38 @@ 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.
|
||||
// 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. This is a simplified
|
||||
// wrapper around QueueTimerRecurring which automatically cancels the timer on
|
||||
// destruction.
|
||||
class HighResolutionTimer {
|
||||
HighResolutionTimer(std::chrono::milliseconds interval,
|
||||
std::function<void()> callback) {
|
||||
assert_not_null(callback);
|
||||
wait_item_ = QueueTimerRecurring(
|
||||
[callback = std::move(callback)](void*) { callback(); }, nullptr,
|
||||
TimerQueueWaitItem::clock::now(), interval);
|
||||
}
|
||||
|
||||
public:
|
||||
virtual ~HighResolutionTimer() = default;
|
||||
~HighResolutionTimer() {
|
||||
if (auto wait_item = wait_item_.lock()) {
|
||||
wait_item->Disarm();
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
std::chrono::milliseconds period, std::function<void()> callback) {
|
||||
return std::unique_ptr<HighResolutionTimer>(
|
||||
new HighResolutionTimer(period, std::move(callback)));
|
||||
}
|
||||
|
||||
private:
|
||||
std::weak_ptr<TimerQueueWaitItem> wait_item_;
|
||||
};
|
||||
|
||||
// Results for a WaitHandle operation.
|
||||
|
|
|
@ -232,71 +232,6 @@ bool SetTlsValue(TlsHandle handle, uintptr_t value) {
|
|||
reinterpret_cast<void*>(value)) == 0;
|
||||
}
|
||||
|
||||
class PosixHighResolutionTimer : public HighResolutionTimer {
|
||||
public:
|
||||
explicit PosixHighResolutionTimer(std::function<void()> callback)
|
||||
: valid_(false) {
|
||||
callback_info_ = new timer_callback_info_t(std::move(callback));
|
||||
}
|
||||
~PosixHighResolutionTimer() override {
|
||||
if (valid_) {
|
||||
callback_info_->disarmed = true;
|
||||
timer_delete(timerid_);
|
||||
// Deliberately leaks memory when wait queue is full instead of blogs,
|
||||
// check logs
|
||||
static_cast<void>(timers_garbage_collector_.TryScheduleAfter(
|
||||
callback_info_, timers_garbage_collector_delay));
|
||||
} else {
|
||||
delete callback_info_;
|
||||
}
|
||||
}
|
||||
|
||||
bool Initialize(std::chrono::milliseconds period) {
|
||||
if (valid_) {
|
||||
// Double initialization
|
||||
assert_always();
|
||||
return false;
|
||||
}
|
||||
// Create timer
|
||||
sigevent sev{};
|
||||
#if XE_HAS_SIGEV_THREAD_ID
|
||||
sev.sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID;
|
||||
sev.sigev_notify_thread_id = gettid();
|
||||
#else
|
||||
sev.sigev_notify = SIGEV_SIGNAL;
|
||||
callback_info_->target_thread = pthread_self();
|
||||
#endif
|
||||
sev.sigev_signo = GetSystemSignal(SignalType::kHighResolutionTimer);
|
||||
sev.sigev_value.sival_ptr = callback_info_;
|
||||
if (timer_create(CLOCK_MONOTONIC, &sev, &timerid_) == -1) return false;
|
||||
|
||||
// Start timer
|
||||
itimerspec its{};
|
||||
its.it_value = DurationToTimeSpec(period);
|
||||
its.it_interval = its.it_value;
|
||||
valid_ = timer_settime(timerid_, 0, &its, nullptr) != -1;
|
||||
if (!valid_) {
|
||||
timer_delete(timerid_);
|
||||
}
|
||||
return valid_;
|
||||
}
|
||||
|
||||
private:
|
||||
timer_callback_info_t* callback_info_;
|
||||
timer_t timerid_;
|
||||
bool valid_; // all values for timer_t are legal so we need this
|
||||
};
|
||||
|
||||
std::unique_ptr<HighResolutionTimer> HighResolutionTimer::CreateRepeating(
|
||||
std::chrono::milliseconds period, std::function<void()> callback) {
|
||||
install_signal_handler(SignalType::kHighResolutionTimer);
|
||||
auto timer = std::make_unique<PosixHighResolutionTimer>(std::move(callback));
|
||||
if (!timer->Initialize(period)) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::move(timer);
|
||||
}
|
||||
|
||||
class PosixConditionBase {
|
||||
public:
|
||||
virtual bool Signal() = 0;
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include "xenia/base/logging.h"
|
||||
#include "xenia/base/platform_win.h"
|
||||
#include "xenia/base/threading.h"
|
||||
#include "xenia/base/threading_timer_queue.h"
|
||||
|
||||
#define LOG_LASTERROR() \
|
||||
{ XELOGI("Win32 Error 0x{:08X} in " __FUNCTION__ "(...)", GetLastError()); }
|
||||
|
@ -112,48 +113,6 @@ 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_(std::move(callback)) {}
|
||||
~Win32HighResolutionTimer() override {
|
||||
if (valid_) {
|
||||
DeleteTimerQueueTimer(nullptr, handle_, INVALID_HANDLE_VALUE);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool Initialize(std::chrono::milliseconds period) {
|
||||
if (valid_) {
|
||||
// Double initialization
|
||||
assert_always();
|
||||
return false;
|
||||
}
|
||||
valid_ = !!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);
|
||||
return valid_;
|
||||
}
|
||||
|
||||
private:
|
||||
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::chrono::milliseconds period, std::function<void()> callback) {
|
||||
auto timer = std::make_unique<Win32HighResolutionTimer>(std::move(callback));
|
||||
if (!timer->Initialize(period)) {
|
||||
return nullptr;
|
||||
}
|
||||
return std::move(timer);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Win32Handle : public T {
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue