Make Process::for_each...() functions inline and allocation-free.

AK::Function is very handy, but costs us an 8-byte allocation.
Let's not have kmalloc() calls in the scheduler hot path.
This commit is contained in:
Andreas Kling 2018-11-08 16:09:05 +01:00
parent abdf24cb73
commit 992769c9d4
Notes: sideshowbarker 2024-07-19 18:31:35 +09:00
3 changed files with 59 additions and 50 deletions

View file

@ -901,52 +901,6 @@ void Process::crash()
ASSERT_NOT_REACHED();
}
void Process::for_each(Function<bool(Process&)> callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (!callback(*process))
break;
process = next_process;
}
}
void Process::for_each_in_pgrp(pid_t pgid, Function<bool(Process&)> callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->pgid() == pgid) {
if (!callback(*process))
break;
}
process = next_process;
}
}
void Process::for_each_in_state(State state, Function<bool(Process&)> callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->state() == state)
callback(*process);
process = next_process;
}
}
void Process::for_each_not_in_state(State state, Function<bool(Process&)> callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->state() != state)
callback(*process);
process = next_process;
}
}
Process* Process::from_pid(pid_t pid)
{
ASSERT_INTERRUPTS_DISABLED();

View file

@ -95,10 +95,10 @@ public:
void setWakeupTime(DWORD t) { m_wakeupTime = t; }
DWORD wakeupTime() const { return m_wakeupTime; }
static void for_each(Function<bool(Process&)>);
static void for_each_in_pgrp(pid_t, Function<bool(Process&)>);
static void for_each_in_state(State, Function<bool(Process&)>);
static void for_each_not_in_state(State, Function<bool(Process&)>);
template<typename Callback> static void for_each(Callback);
template<typename Callback> static void for_each_in_pgrp(pid_t, Callback);
template<typename Callback> static void for_each_in_state(State, Callback);
template<typename Callback> static void for_each_not_in_state(State, Callback);
bool tick() { ++m_ticks; return --m_ticksLeft; }
void set_ticks_left(dword t) { m_ticksLeft = t; }
@ -322,3 +322,53 @@ extern void block(Process::State);
extern void sleep(DWORD ticks);
extern InlineLinkedList<Process>* g_processes;
template<typename Callback>
inline void Process::for_each(Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (!callback(*process))
break;
process = next_process;
}
}
template<typename Callback>
inline void Process::for_each_in_pgrp(pid_t pgid, Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->pgid() == pgid) {
if (!callback(*process))
break;
}
process = next_process;
}
}
template<typename Callback>
inline void Process::for_each_in_state(State state, Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->state() == state)
callback(*process);
process = next_process;
}
}
template<typename Callback>
inline void Process::for_each_not_in_state(State state, Callback callback)
{
ASSERT_INTERRUPTS_DISABLED();
for (auto* process = g_processes->head(); process;) {
auto* next_process = process->next();
if (process->state() != state)
callback(*process);
process = next_process;
}
}

View file

@ -92,6 +92,11 @@ static inline dword cpuFlags()
return flags;
}
inline bool are_interrupts_enabled()
{
return cpuFlags() & 0x200;
}
class InterruptDisabler {
public:
InterruptDisabler()