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

Kernel: Don't create Function objects in the scheduling code

Each Function is a heap allocation, so let's make an effort to avoid
doing that during scheduling. Because of header dependencies, I had to
put the runnables iteration helpers in Thread.h, which is a bit meh but
at least this cuts out all the kmalloc() traffic in pick_next().
This commit is contained in:
Andreas Kling 2019-08-07 20:43:54 +02:00
parent 2e416b1b87
commit 1f9b8f0e7c
3 changed files with 49 additions and 59 deletions

View file

@ -384,3 +384,47 @@ inline const LogStream& operator<<(const LogStream& stream, const Thread& value)
{
return stream << "Thread{" << &value << "}(" << value.pid() << ":" << value.tid() << ")";
}
struct SchedulerData {
typedef IntrusiveList<Thread, &Thread::m_runnable_list_node> ThreadList;
ThreadList m_runnable_threads;
ThreadList m_nonrunnable_threads;
ThreadList& thread_list_for_state(Thread::State state)
{
if (Thread::is_runnable_state(state))
return m_runnable_threads;
return m_nonrunnable_threads;
}
};
template<typename Callback>
inline IterationDecision Scheduler::for_each_runnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
auto& tl = g_scheduler_data->m_runnable_threads;
for (auto it = tl.begin(); it != tl.end();) {
auto thread = *it;
it = ++it;
if (callback(*thread) == IterationDecision::Break)
return IterationDecision::Break;
}
return IterationDecision::Continue;
}
template<typename Callback>
inline IterationDecision Scheduler::for_each_nonrunnable(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
auto& tl = g_scheduler_data->m_nonrunnable_threads;
for (auto it = tl.begin(); it != tl.end();) {
auto thread = *it;
it = ++it;
if (callback(*thread) == IterationDecision::Break)
return IterationDecision::Break;
}
return IterationDecision::Continue;
}