Kernel: Add Semaphore to block threads

This commit is contained in:
Bananymous
2023-03-30 18:45:47 +03:00
parent 7b7f4eb141
commit c2e3b422cc
7 changed files with 129 additions and 7 deletions

View File

@@ -4,6 +4,7 @@
#include <kernel/Input/KeyEvent.h>
#include <kernel/Input/PS2Controller.h>
#include <kernel/Input/PS2Keymap.h>
#include <kernel/Semaphore.h>
namespace Kernel::Input
{
@@ -60,6 +61,8 @@ namespace Kernel::Input
State m_state { State::Normal };
Semaphore m_semaphore;
BAN::String m_name;
public:

View File

@@ -2,6 +2,7 @@
#include <BAN/LinkedList.h>
#include <BAN/Memory.h>
#include <kernel/Semaphore.h>
#include <kernel/Thread.h>
namespace Kernel
@@ -21,6 +22,9 @@ namespace Kernel
void set_current_thread_sleeping(uint64_t);
[[noreturn]] void set_current_thread_done();
void block_current_thread(Semaphore*);
void unblock_threads(Semaphore*);
BAN::RefPtr<Thread> current_thread();
private:
@@ -47,9 +51,18 @@ namespace Kernel
uint64_t wake_time;
};
struct BlockingThread
{
BlockingThread(const BAN::RefPtr<Thread>& thread, Semaphore* semaphore) : thread(thread), semaphore(semaphore) {}
BAN::RefPtr<Thread> thread;
Semaphore* semaphore;
uint8_t padding[sizeof(uint64_t) - sizeof(Semaphore*)];
};
BAN::RefPtr<Thread> m_idle_thread;
BAN::LinkedList<ActiveThread> m_active_threads;
BAN::LinkedList<SleepingThread> m_sleeping_threads;
BAN::LinkedList<BlockingThread> m_blocking_threads;
BAN::LinkedList<ActiveThread>::iterator m_current_thread;

View File

@@ -0,0 +1,24 @@
#pragma once
#include <kernel/SpinLock.h>
namespace Kernel
{
class Semaphore
{
public:
void block();
void unblock();
bool is_blocked() const { return m_blocked; }
private:
void set_blocked(bool blocked) { m_blocked = blocked; }
private:
bool m_blocked { false };
friend class Scheduler;
};
}