-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First implementation of kernel level timers #591
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
class Process; | ||
class Thread; | ||
struct Timer; | ||
struct RegisterDump; | ||
struct SchedulerData; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <AK/SinglyLinkedList.h> | ||
#include <Kernel/Scheduler.h> | ||
#include <Kernel/TimerQueue.h> | ||
|
||
|
||
SinglyLinkedList<Timer>* g_timer_queue; | ||
|
||
static u64 s_next_timer_due; | ||
static u64 s_timer_id_count; | ||
void TimerQueue::initialize() | ||
{ | ||
|
||
g_timer_queue = new SinglyLinkedList<Timer>(); | ||
} | ||
|
||
|
||
u64 TimerQueue::add_timer(Timer& timer) | ||
{ | ||
ASSERT(timer.expires > Scheduler::g_uptime); | ||
timer.id = ++s_timer_id_count; | ||
g_timer_queue->sorted_insert_slow(timer); | ||
s_next_timer_due = g_timer_queue->first().expires; | ||
return s_timer_id_count; | ||
} | ||
|
||
u64 add_timer(u64 duration, TimeUnit unit, void (callback)()) | ||
{ | ||
atuo timer = Timer(); | ||
timer.expires = g_uptime + duration * unit; | ||
timer.callback = callback; | ||
return add_timer(timer); | ||
} | ||
|
||
void TimerQueue::sorted_insert_slow(T&& value) | ||
{ | ||
auto* new_node = new Node(move(value)); | ||
new_node->value = value; | ||
|
||
if (!g_timer_queue->m_head || g_timer_queue->m_head->value > new_node->value) { | ||
new_node->next = g_timer_queue->m_head; | ||
if (!m_head) | ||
m_tail = new_node; | ||
m_head = new_node; | ||
return; | ||
} | ||
|
||
Node* curr = m_head; | ||
while (curr->next && curr->next->value < new_node->value) | ||
{ | ||
curr = curr->next; | ||
} | ||
|
||
new_node->next = curr->next; | ||
curr->next = new_node; | ||
if(g_timer_queue->m_tail == curr) | ||
g_timer_queue->m_tail = new_node; | ||
} | ||
|
||
void TimerQueue::fire_timer() | ||
{ | ||
ASSERT(s_next_timer_due == g_timer_queue->first().expires); | ||
while (! g_timer_queue->is_empty() && Scheduler::g_uptime > g_timer_queue->first().expires) | ||
{ | ||
auto timer = g_timer_queue->take_first(); | ||
timer.callback(); | ||
} | ||
if (! g_timer_queue->is_empty()) | ||
s_next_timer_due = g_timer_queue->first().expires; | ||
else | ||
s_next_timer_due = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#pragma once | ||
|
||
#include <Kernel/Arch/i386/PIT.h> | ||
|
||
struct Timer { | ||
u64 id; | ||
u64 expires; | ||
void (*callback)(); | ||
bool operator<(const Timer& rhs) const | ||
{ | ||
return expires < rhs.expires; | ||
} | ||
bool operator>( const Timer& rhs) const | ||
{ | ||
return expires > rhs.expires; | ||
} | ||
|
||
bool operator==( const X& rhs) const | ||
{ | ||
return id == rhs.id; | ||
} | ||
}; | ||
|
||
enum TimeUnit { | ||
MS = TICKS_PER_SECOND / 1000, | ||
S = TICKS_PER_SECOND, | ||
M = TICKS_PER_SECOND * 60 | ||
}; | ||
|
||
|
||
class TimerQueue { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pretty roundabout way to basically add a lot of global functions. // In header file:
class TimerQueue {
public:
static TimerQueue& the();
u64 add_timer(Timer&);
u64 add_timer(u64 duration, TimeUnit, void (callback)());
...
private:
SinglyLinkedList<Timer> m_timers;
};
In cpp file:
TimerQueue& TimerQueue::the()
{
if (!queue)
queue = new TimerQueue;
return s_the;
} Then you can use the single global instance of TimerQueue::the().add_timer(...); |
||
friend class SinglyLinkedList; | ||
public: | ||
static void initialize(); | ||
static u64 add_timer(Timer&); | ||
static u64 add_timer(u64 duration, TimeUnit, void (callback)()); | ||
static Timer& get_timer(u64 id); | ||
static bool update_timer(Timer&) | ||
static bool cancel_timer(u64 id); | ||
private: | ||
static void fire_timer(); | ||
static void sorted_list_insert_slow() | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reaching directly into the
SinglyLinkedList
class here is very strange.It would be nicer to implement this by adding an API such as
SinglyLinkedList::insert(Iterator&, T&&)
.Then you could iterate over the list until you find your insertion point and then call
insert()
with the iterator you're using.