summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorBrian Gianforcaro <b.gianfo@gmail.com>2020-04-26 02:23:37 -0700
committerAndreas Kling <kling@serenityos.org>2020-04-26 21:31:52 +0200
commit1d68837456b9ee7a95fb97d985774c76cf4a5822 (patch)
treef16cbee6b1106aedaef802b22fd13080c9214a1a /Kernel
parent1a80aa999ab6ac3748781c0f65078e19c5920698 (diff)
downloadserenity-1d68837456b9ee7a95fb97d985774c76cf4a5822.zip
Kernel: Refactor TimeQueue::add_timer to use timeval
The current API of add_timer makes it hard to use as you are forced to do a bunch of time arithmetic at the caller. Ideally we would have overloads for common time types like timespec or timeval to keep the API as straight forward as possible. This change moves us in that direction. While I'm here, we should really also use the machines actual ticks per second, instead of the OPTIMAL_TICKS_PER_SECOND_RATE.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/TimerQueue.cpp12
-rw-r--r--Kernel/TimerQueue.h14
2 files changed, 16 insertions, 10 deletions
diff --git a/Kernel/TimerQueue.cpp b/Kernel/TimerQueue.cpp
index 0e2add6a3f..4222102b42 100644
--- a/Kernel/TimerQueue.cpp
+++ b/Kernel/TimerQueue.cpp
@@ -28,6 +28,7 @@
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <Kernel/Scheduler.h>
+#include <Kernel/Time/TimeManagement.h>
#include <Kernel/TimerQueue.h>
namespace Kernel {
@@ -41,9 +42,14 @@ TimerQueue& TimerQueue::the()
return *s_the;
}
+TimerQueue::TimerQueue()
+{
+ m_ticks_per_second = TimeManagement::the().ticks_per_second();
+}
+
u64 TimerQueue::add_timer(NonnullOwnPtr<Timer>&& timer)
{
- ASSERT(timer->expires > g_uptime);
+ ASSERT(timer->expires >= g_uptime);
timer->id = ++m_timer_id_count;
@@ -58,10 +64,10 @@ u64 TimerQueue::add_timer(NonnullOwnPtr<Timer>&& timer)
return m_timer_id_count;
}
-u64 TimerQueue::add_timer(u64 duration, TimeUnit unit, Function<void()>&& callback)
+u64 TimerQueue::add_timer(timeval& deadline, Function<void()>&& callback)
{
NonnullOwnPtr timer = make<Timer>();
- timer->expires = g_uptime + duration * unit;
+ timer->expires = g_uptime + seconds_to_ticks(deadline.tv_sec) + microseconds_to_ticks(deadline.tv_usec);
timer->callback = move(callback);
return add_timer(move(timer));
}
diff --git a/Kernel/TimerQueue.h b/Kernel/TimerQueue.h
index 4398a71448..d0fd9d0381 100644
--- a/Kernel/TimerQueue.h
+++ b/Kernel/TimerQueue.h
@@ -52,26 +52,26 @@ struct Timer {
}
};
-enum TimeUnit {
- MS = OPTIMAL_TICKS_PER_SECOND_RATE / 1000,
- S = OPTIMAL_TICKS_PER_SECOND_RATE,
- M = OPTIMAL_TICKS_PER_SECOND_RATE * 60
-};
-
class TimerQueue {
public:
static TimerQueue& the();
u64 add_timer(NonnullOwnPtr<Timer>&&);
- u64 add_timer(u64 duration, TimeUnit, Function<void()>&& callback);
+ u64 add_timer(timeval& timeout, Function<void()>&& callback);
bool cancel_timer(u64 id);
void fire();
private:
+ TimerQueue();
+
void update_next_timer_due();
+ u64 microseconds_to_ticks(u64 micro_seconds) { return micro_seconds * (m_ticks_per_second / 1'000'000); }
+ u64 seconds_to_ticks(u64 seconds) { return seconds * m_ticks_per_second; }
+
u64 m_next_timer_due { 0 };
u64 m_timer_id_count { 0 };
+ u64 m_ticks_per_second { 0 };
SinglyLinkedList<NonnullOwnPtr<Timer>> m_timer_queue;
};