summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2020-03-14 01:03:47 +0200
committerAndreas Kling <kling@serenityos.org>2020-03-19 15:48:00 +0100
commit7268499c7683cee158c9fbfb9dd745a3d54890f2 (patch)
tree2ad155950d5cab16d12ba39d6ef102dbcb65a8fb /Libraries
parentb536547c5236089e868bf763ffdc45026a4a1023 (diff)
downloadserenity-7268499c7683cee158c9fbfb9dd745a3d54890f2.zip
LibCore: Use monotonic time when handling timers
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibCore/ElapsedTimer.cpp11
-rw-r--r--Libraries/LibCore/EventLoop.cpp15
2 files changed, 21 insertions, 5 deletions
diff --git a/Libraries/LibCore/ElapsedTimer.cpp b/Libraries/LibCore/ElapsedTimer.cpp
index c117450367..66254b7abb 100644
--- a/Libraries/LibCore/ElapsedTimer.cpp
+++ b/Libraries/LibCore/ElapsedTimer.cpp
@@ -28,20 +28,27 @@
#include <AK/Time.h>
#include <LibCore/ElapsedTimer.h>
#include <sys/time.h>
+#include <time.h>
namespace Core {
void ElapsedTimer::start()
{
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
{
ASSERT(is_valid());
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;
timeval_sub(now, m_start_time, diff);
return diff.tv_sec * 1000 + diff.tv_usec / 1000;
diff --git a/Libraries/LibCore/EventLoop.cpp b/Libraries/LibCore/EventLoop.cpp
index ff413b8d21..3675111df7 100644
--- a/Libraries/LibCore/EventLoop.cpp
+++ b/Libraries/LibCore/EventLoop.cpp
@@ -420,7 +420,10 @@ void EventLoop::wait_for_event(WaitMode mode)
bool should_wait_forever = false;
if (mode == WaitMode::WaitForEvents) {
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);
timeval_sub(timeout, now, timeout);
if (timeout.tv_sec < 0) {
@@ -446,7 +449,10 @@ void EventLoop::wait_for_event(WaitMode mode)
}
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) {
@@ -521,7 +527,10 @@ int EventLoop::register_timer(Object& object, int milliseconds, bool should_relo
timer->owner = object.make_weak_ptr();
timer->interval = milliseconds;
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->should_reload = should_reload;
timer->fire_when_not_visible = fire_when_not_visible;