mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:37:35 +00:00
LibCore: Use monotonic time when handling timers
This commit is contained in:
parent
b536547c52
commit
7268499c76
2 changed files with 21 additions and 5 deletions
|
@ -28,20 +28,27 @@
|
||||||
#include <AK/Time.h>
|
#include <AK/Time.h>
|
||||||
#include <LibCore/ElapsedTimer.h>
|
#include <LibCore/ElapsedTimer.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
void ElapsedTimer::start()
|
void ElapsedTimer::start()
|
||||||
{
|
{
|
||||||
m_valid = true;
|
m_valid = true;
|
||||||
gettimeofday(&m_start_time, nullptr);
|
timespec now_spec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now_spec);
|
||||||
|
m_start_time.tv_sec = now_spec.tv_sec;
|
||||||
|
m_start_time.tv_usec = now_spec.tv_nsec / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ElapsedTimer::elapsed() const
|
int ElapsedTimer::elapsed() const
|
||||||
{
|
{
|
||||||
ASSERT(is_valid());
|
ASSERT(is_valid());
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
gettimeofday(&now, nullptr);
|
timespec now_spec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now_spec);
|
||||||
|
now.tv_sec = now_spec.tv_sec;
|
||||||
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
||||||
struct timeval diff;
|
struct timeval diff;
|
||||||
timeval_sub(now, m_start_time, diff);
|
timeval_sub(now, m_start_time, diff);
|
||||||
return diff.tv_sec * 1000 + diff.tv_usec / 1000;
|
return diff.tv_sec * 1000 + diff.tv_usec / 1000;
|
||||||
|
|
|
@ -420,7 +420,10 @@ void EventLoop::wait_for_event(WaitMode mode)
|
||||||
bool should_wait_forever = false;
|
bool should_wait_forever = false;
|
||||||
if (mode == WaitMode::WaitForEvents) {
|
if (mode == WaitMode::WaitForEvents) {
|
||||||
if (!s_timers->is_empty() && queued_events_is_empty) {
|
if (!s_timers->is_empty() && queued_events_is_empty) {
|
||||||
gettimeofday(&now, nullptr);
|
timespec now_spec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now_spec);
|
||||||
|
now.tv_sec = now_spec.tv_sec;
|
||||||
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
||||||
get_next_timer_expiration(timeout);
|
get_next_timer_expiration(timeout);
|
||||||
timeval_sub(timeout, now, timeout);
|
timeval_sub(timeout, now, timeout);
|
||||||
if (timeout.tv_sec < 0) {
|
if (timeout.tv_sec < 0) {
|
||||||
|
@ -446,7 +449,10 @@ void EventLoop::wait_for_event(WaitMode mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!s_timers->is_empty()) {
|
if (!s_timers->is_empty()) {
|
||||||
gettimeofday(&now, nullptr);
|
timespec now_spec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now_spec);
|
||||||
|
now.tv_sec = now_spec.tv_sec;
|
||||||
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& it : *s_timers) {
|
for (auto& it : *s_timers) {
|
||||||
|
@ -521,7 +527,10 @@ int EventLoop::register_timer(Object& object, int milliseconds, bool should_relo
|
||||||
timer->owner = object.make_weak_ptr();
|
timer->owner = object.make_weak_ptr();
|
||||||
timer->interval = milliseconds;
|
timer->interval = milliseconds;
|
||||||
timeval now;
|
timeval now;
|
||||||
gettimeofday(&now, nullptr);
|
timespec now_spec;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now_spec);
|
||||||
|
now.tv_sec = now_spec.tv_sec;
|
||||||
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
||||||
timer->reload(now);
|
timer->reload(now);
|
||||||
timer->should_reload = should_reload;
|
timer->should_reload = should_reload;
|
||||||
timer->fire_when_not_visible = fire_when_not_visible;
|
timer->fire_when_not_visible = fire_when_not_visible;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue