diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2021-02-27 23:56:16 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-02 08:36:08 +0100 |
commit | 94e0a603f2fc0f26798a208bd9e50448c08c1d9f (patch) | |
tree | 74061503f84bcb92c076c052e00f5abff5b888d3 | |
parent | 340813e0873336fb88f909d6f29f7fcf2147efcb (diff) | |
download | serenity-94e0a603f2fc0f26798a208bd9e50448c08c1d9f.zip |
Kernel: Make TimerQueue use AK::Time internally
-rw-r--r-- | Kernel/TimerQueue.cpp | 29 | ||||
-rw-r--r-- | Kernel/TimerQueue.h | 12 |
2 files changed, 17 insertions, 24 deletions
diff --git a/Kernel/TimerQueue.cpp b/Kernel/TimerQueue.cpp index 54f31d216f..8c0da10c14 100644 --- a/Kernel/TimerQueue.cpp +++ b/Kernel/TimerQueue.cpp @@ -38,24 +38,13 @@ namespace Kernel { static AK::Singleton<TimerQueue> s_the; static SpinLock<u8> g_timerqueue_lock; -ALWAYS_INLINE static u64 time_to_ns(const timespec& ts) -{ - return (u64)ts.tv_sec * 1000000000ull + ts.tv_nsec; -} - -ALWAYS_INLINE static timespec ns_to_time(u64 ns) -{ - return { (time_t)(ns / 1000000000ull), (long)(ns % 1000000000ull) }; -} - timespec Timer::remaining() const { - if (m_remaining == 0) - return {}; - return ns_to_time(m_remaining); + // FIXME: Should use AK::Time internally + return m_remaining.to_timespec(); } -u64 Timer::now(bool is_firing) const +Time Timer::now(bool is_firing) const { // NOTE: If is_firing is true then TimePrecision::Precise isn't really useful here. // We already have a quite precise time stamp because we just updated the time in the @@ -76,7 +65,8 @@ u64 Timer::now(bool is_firing) const break; } } - return time_to_ns(TimeManagement::the().current_time(clock_id).value()); + // FIXME: Should use AK::Time internally + return Time::from_timespec(TimeManagement::the().current_time(clock_id).value()); } TimerQueue& TimerQueue::the() @@ -99,7 +89,7 @@ RefPtr<Timer> TimerQueue::add_timer_without_id(clockid_t clock_id, const timespe // *must* be a RefPtr<Timer>. Otherwise calling cancel_timer() could // inadvertently cancel another timer that has been created between // returning from the timer handler and a call to cancel_timer(). - auto timer = adopt(*new Timer(clock_id, time_to_ns(deadline), move(callback))); + auto timer = adopt(*new Timer(clock_id, Time::from_timespec(deadline), move(callback))); ScopedSpinLock lock(g_timerqueue_lock); timer->m_id = 0; // Don't generate a timer id @@ -119,7 +109,7 @@ TimerId TimerQueue::add_timer(NonnullRefPtr<Timer>&& timer) void TimerQueue::add_timer_locked(NonnullRefPtr<Timer> timer) { - u64 timer_expiration = timer->m_expires; + Time timer_expiration = timer->m_expires; VERIFY(!timer->is_queued()); @@ -149,9 +139,10 @@ void TimerQueue::add_timer_locked(NonnullRefPtr<Timer> timer) TimerId TimerQueue::add_timer(clockid_t clock_id, timeval& deadline, Function<void()>&& callback) { + // FIXME: Should use AK::Time internally auto expires = TimeManagement::the().current_time(clock_id).value(); timespec_add_timeval(expires, deadline, expires); - return add_timer(adopt(*new Timer(clock_id, time_to_ns(expires), move(callback)))); + return add_timer(adopt(*new Timer(clock_id, Time::from_timespec(expires), move(callback)))); } bool TimerQueue::cancel_timer(TimerId id) @@ -299,7 +290,7 @@ void TimerQueue::update_next_timer_due(Queue& queue) if (auto* next_timer = queue.list.head()) queue.next_timer_due = next_timer->m_expires; else - queue.next_timer_due = 0; + queue.next_timer_due = {}; } } diff --git a/Kernel/TimerQueue.h b/Kernel/TimerQueue.h index 7c5040bc5b..2e897eb0ca 100644 --- a/Kernel/TimerQueue.h +++ b/Kernel/TimerQueue.h @@ -31,6 +31,7 @@ #include <AK/NonnullRefPtr.h> #include <AK/OwnPtr.h> #include <AK/RefCounted.h> +#include <AK/Time.h> #include <Kernel/Time/TimeManagement.h> namespace Kernel { @@ -43,7 +44,7 @@ class Timer : public RefCounted<Timer> friend class InlineLinkedListNode<Timer>; public: - Timer(clockid_t clock_id, u64 expires, Function<void()>&& callback) + Timer(clockid_t clock_id, Time expires, Function<void()>&& callback) : m_clock_id(clock_id) , m_expires(expires) , m_callback(move(callback)) @@ -59,8 +60,8 @@ public: private: TimerId m_id; clockid_t m_clock_id; - u64 m_expires; - u64 m_remaining { 0 }; + Time m_expires; + Time m_remaining {}; Function<void()> m_callback; Timer* m_next { nullptr }; Timer* m_prev { nullptr }; @@ -80,7 +81,7 @@ private: } bool is_queued() const { return m_queued; } void set_queued(bool queued) { m_queued = queued; } - u64 now(bool) const; + Time now(bool) const; }; class TimerQueue { @@ -91,6 +92,7 @@ public: static TimerQueue& the(); TimerId add_timer(NonnullRefPtr<Timer>&&); + // FIXME: Should use AK::Time internally RefPtr<Timer> add_timer_without_id(clockid_t, const timespec&, Function<void()>&&); TimerId add_timer(clockid_t, timeval& timeout, Function<void()>&& callback); bool cancel_timer(TimerId id); @@ -104,7 +106,7 @@ public: private: struct Queue { InlineLinkedList<Timer> list; - u64 next_timer_due { 0 }; + Time next_timer_due {}; }; void remove_timer_locked(Queue&, Timer&); void update_next_timer_due(Queue&); |