[Base] Make HighResolutionTimer platform agnostic
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user