1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +00:00

Kernel: Move some time related code from Scheduler into TimeManagement

Use the TimerQueue to expire blocking operations, which is one less thing
the Scheduler needs to check on every iteration.

Also, add a BlockTimeout class that will automatically handle relative or
absolute timeouts as well as overriding timeouts (e.g. socket timeouts)
more consistently.

Also, rework the TimerQueue class to be able to fire events from
any processor, which requires Timer to be RefCounted. Also allow
creating id-less timers for use by blocking operations.
This commit is contained in:
Tom 2020-11-15 11:58:19 -07:00 committed by Andreas Kling
parent e0e26c6c67
commit 6cb640eeba
19 changed files with 461 additions and 263 deletions

View file

@ -27,8 +27,9 @@
#pragma once
#include <AK/Function.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/NonnullRefPtr.h>
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/SinglyLinkedList.h>
#include <Kernel/Time/TimeManagement.h>
@ -36,7 +37,7 @@ namespace Kernel {
typedef u64 TimerId;
struct Timer {
struct Timer : public RefCounted<Timer> {
TimerId id;
u64 expires;
Function<void()> callback;
@ -52,6 +53,12 @@ struct Timer {
{
return id == rhs.id;
}
Timer(u64 expires, Function<void()>&& callback)
: expires(expires)
, callback(move(callback))
{
}
};
class TimerQueue {
@ -59,21 +66,24 @@ public:
TimerQueue();
static TimerQueue& the();
TimerId add_timer(NonnullOwnPtr<Timer>&&);
TimerId add_timer(NonnullRefPtr<Timer>&&);
RefPtr<Timer> add_timer_without_id(const timespec& timeout, Function<void()>&& callback);
TimerId add_timer(timeval& timeout, Function<void()>&& callback);
bool cancel_timer(TimerId id);
bool cancel_timer(const NonnullRefPtr<Timer>&);
void fire();
private:
void update_next_timer_due();
void add_timer_locked(NonnullRefPtr<Timer>);
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; }
timespec ticks_to_time(u64 ticks) const;
u64 time_to_ticks(const timespec&) const;
u64 m_next_timer_due { 0 };
u64 m_timer_id_count { 0 };
u64 m_ticks_per_second { 0 };
SinglyLinkedList<NonnullOwnPtr<Timer>> m_timer_queue;
SinglyLinkedList<NonnullRefPtr<Timer>> m_timer_queue;
};
}