mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:37: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:
parent
2e416b1b87
commit
1f9b8f0e7c
3 changed files with 49 additions and 59 deletions
|
@ -6,21 +6,7 @@
|
|||
#include <Kernel/RTC.h>
|
||||
#include <Kernel/Scheduler.h>
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
static SchedulerData* g_scheduler_data;
|
||||
SchedulerData* g_scheduler_data;
|
||||
|
||||
void Scheduler::init_thread(Thread& thread)
|
||||
{
|
||||
|
@ -37,34 +23,6 @@ void Scheduler::update_state_for_thread(Thread& thread)
|
|||
list.append(thread);
|
||||
}
|
||||
|
||||
IterationDecision Scheduler::for_each_runnable_func(Function<IterationDecision(Thread&)>&& 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;
|
||||
}
|
||||
|
||||
IterationDecision Scheduler::for_each_nonrunnable_func(Function<IterationDecision(Thread&)>&& 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;
|
||||
}
|
||||
|
||||
//#define LOG_EVERY_CONTEXT_SWITCH
|
||||
//#define SCHEDULER_DEBUG
|
||||
//#define SCHEDULER_RUNNABLE_DEBUG
|
||||
|
|
|
@ -8,11 +8,13 @@
|
|||
class Process;
|
||||
class Thread;
|
||||
struct RegisterDump;
|
||||
struct SchedulerData;
|
||||
|
||||
extern Thread* current;
|
||||
extern Thread* g_last_fpu_thread;
|
||||
extern Thread* g_finalizer;
|
||||
extern u64 g_uptime;
|
||||
extern SchedulerData* g_scheduler_data;
|
||||
|
||||
class Scheduler {
|
||||
public:
|
||||
|
@ -30,28 +32,14 @@ public:
|
|||
static void beep();
|
||||
|
||||
template<typename Callback>
|
||||
static inline IterationDecision for_each_runnable(Callback callback)
|
||||
{
|
||||
return for_each_runnable_func([callback](Thread& thread) {
|
||||
return callback(thread);
|
||||
});
|
||||
}
|
||||
static inline IterationDecision for_each_runnable(Callback);
|
||||
|
||||
template<typename Callback>
|
||||
static inline IterationDecision for_each_nonrunnable(Callback callback)
|
||||
{
|
||||
return for_each_nonrunnable_func([callback](Thread& thread) {
|
||||
return callback(thread);
|
||||
});
|
||||
}
|
||||
static inline IterationDecision for_each_nonrunnable(Callback);
|
||||
|
||||
static void init_thread(Thread& thread);
|
||||
static void update_state_for_thread(Thread& thread);
|
||||
|
||||
private:
|
||||
static void prepare_for_iret_to_new_process();
|
||||
static IterationDecision for_each_runnable_func(Function<IterationDecision(Thread&)>&& callback);
|
||||
static IterationDecision for_each_nonrunnable_func(Function<IterationDecision(Thread&)>&& callback);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue