Kernel: Sleep now actually sleeps and allows idling

This commit is contained in:
Bananymous
2023-02-19 17:53:29 +02:00
parent 66a4b69a29
commit 1bd8b0fe5c
10 changed files with 101 additions and 72 deletions

View File

@@ -19,27 +19,33 @@ namespace Kernel
const Thread& current_thread() const;
template<typename... Args>
void add_thread(const BAN::Function<void(Args...)>& func, Args... args)
[[nodiscard]] BAN::ErrorOr<void> add_thread(const BAN::Function<void(Args...)>& func, Args... args)
{
uintptr_t flags;
asm volatile("pushf; pop %0" : "=r"(flags));
asm volatile("cli");
MUST(m_threads.emplace_back(func, BAN::forward<Args>(args)...));
TRY(m_threads.emplace_back(func, BAN::forward<Args>(args)...));
if (flags & (1 << 9))
asm volatile("sti");
return {};
}
void switch_thread();
void reschedule();
void set_current_thread_sleeping();
void start();
static constexpr size_t ms_between_switch = 1;
static constexpr size_t ms_between_switch = 4;
private:
Scheduler() {}
void switch_thread();
private:
BAN::LinkedList<Thread> m_threads;
BAN::LinkedList<Thread>::iterator m_current_iterator;
uint64_t m_last_reschedule = 0;
friend class Thread;
};
}

View File

@@ -17,6 +17,7 @@ namespace Kernel
NotStarted,
Running,
Paused,
Sleeping,
Done,
};
@@ -36,11 +37,9 @@ namespace Kernel
uint32_t id() const { return m_id; }
void set_rsp(uintptr_t rsp) { m_rsp = rsp; }
void set_rbp(uintptr_t rbp) { m_rbp = rbp; }
void set_rip(uintptr_t rip) { m_rip = rip; }
void set_state(State state) { m_state = state; }
uintptr_t rsp() const { return m_rsp; }
uintptr_t rbp() const { return m_rbp; }
uintptr_t rip() const { return m_rip; }
State state() const { return m_state; }
@@ -55,7 +54,6 @@ namespace Kernel
State m_state = State::NotStarted;
uintptr_t m_args[4] = {};
uintptr_t m_rip = 0;
uintptr_t m_rbp = 0;
uintptr_t m_rsp = 0;
const uint32_t m_id = 0;