forked from Bananymous/banan-os
Kernel: Make thread unblocking O(1)
This is still bit broken. VirtualBox seems to freeze sometimes, but I could not recreate this on qemu (with and without kvm) or real hardware.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Atomic.h>
|
||||
#include <BAN/Formatter.h>
|
||||
#include <BAN/ForwardList.h>
|
||||
|
||||
#include <kernel/Arch.h>
|
||||
#include <kernel/GDT.h>
|
||||
#include <kernel/IDT.h>
|
||||
#include <kernel/InterruptStack.h>
|
||||
#include <kernel/ProcessorID.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
|
||||
namespace Kernel
|
||||
@@ -19,29 +19,6 @@ namespace Kernel
|
||||
Enabled,
|
||||
};
|
||||
|
||||
class ProcessorID
|
||||
{
|
||||
public:
|
||||
using value_type = uint32_t;
|
||||
|
||||
public:
|
||||
ProcessorID() = default;
|
||||
|
||||
uint32_t as_u32() const { return m_id; }
|
||||
bool operator==(ProcessorID other) const { return m_id == other.m_id; }
|
||||
|
||||
private:
|
||||
explicit ProcessorID(uint32_t id) : m_id(id) {}
|
||||
|
||||
private:
|
||||
uint32_t m_id = static_cast<uint32_t>(-1);
|
||||
|
||||
friend class Processor;
|
||||
friend class APIC;
|
||||
};
|
||||
|
||||
constexpr ProcessorID PROCESSOR_NONE { };
|
||||
|
||||
#if ARCH(x86_64) || ARCH(i686)
|
||||
class Processor
|
||||
{
|
||||
@@ -66,9 +43,8 @@ namespace Kernel
|
||||
uintptr_t vaddr;
|
||||
size_t page_count;
|
||||
} flush_tlb;
|
||||
Scheduler::NewThreadRequest new_thread;
|
||||
Scheduler::UnblockRequest unblock_thread;
|
||||
uintptr_t scheduler_preemption;
|
||||
SchedulerQueue::Node* new_thread;
|
||||
SchedulerQueue::Node* unblock_thread;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -209,14 +185,3 @@ namespace Kernel
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
namespace BAN::Formatter
|
||||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument(F putc, Kernel::ProcessorID processor_id, const ValueFormat& format)
|
||||
{
|
||||
print_argument(putc, processor_id.as_u32(), format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
42
kernel/include/kernel/ProcessorID.h
Normal file
42
kernel/include/kernel/ProcessorID.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Formatter.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class ProcessorID
|
||||
{
|
||||
public:
|
||||
using value_type = uint32_t;
|
||||
|
||||
public:
|
||||
ProcessorID() = default;
|
||||
|
||||
uint32_t as_u32() const { return m_id; }
|
||||
bool operator==(ProcessorID other) const { return m_id == other.m_id; }
|
||||
|
||||
private:
|
||||
explicit ProcessorID(uint32_t id) : m_id(id) {}
|
||||
|
||||
private:
|
||||
uint32_t m_id = static_cast<uint32_t>(-1);
|
||||
|
||||
friend class Processor;
|
||||
friend class APIC;
|
||||
};
|
||||
|
||||
inline constexpr ProcessorID PROCESSOR_NONE { };
|
||||
|
||||
}
|
||||
|
||||
namespace BAN::Formatter
|
||||
{
|
||||
|
||||
template<typename F>
|
||||
void print_argument(F putc, Kernel::ProcessorID processor_id, const ValueFormat& format)
|
||||
{
|
||||
print_argument(putc, processor_id.as_u32(), format);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <BAN/ForwardList.h>
|
||||
#include <BAN/NoCopyMove.h>
|
||||
#include <kernel/InterruptStack.h>
|
||||
#include <kernel/ProcessorID.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -22,13 +23,20 @@ namespace Kernel
|
||||
: thread(thread)
|
||||
{}
|
||||
|
||||
Thread* const thread;
|
||||
|
||||
Node* next { nullptr };
|
||||
Node* prev { nullptr };
|
||||
|
||||
Thread* thread;
|
||||
ThreadBlocker* blocker { nullptr };
|
||||
uint64_t wake_time_ns { static_cast<uint64_t>(-1) };
|
||||
|
||||
ThreadBlocker* blocker { nullptr };
|
||||
Node* block_chain_next { nullptr };
|
||||
Node* block_chain_prev { nullptr };
|
||||
|
||||
ProcessorID processor_id { PROCESSOR_NONE };
|
||||
bool blocked { false };
|
||||
|
||||
uint64_t last_start_ns { 0 };
|
||||
uint64_t time_used_ns { 0 };
|
||||
};
|
||||
@@ -58,22 +66,11 @@ namespace Kernel
|
||||
struct NewThreadRequest
|
||||
{
|
||||
SchedulerQueue::Node* node;
|
||||
bool blocked;
|
||||
};
|
||||
|
||||
struct UnblockRequest
|
||||
{
|
||||
enum class Type
|
||||
{
|
||||
ThreadBlocker,
|
||||
ThreadID,
|
||||
};
|
||||
Type type;
|
||||
union
|
||||
{
|
||||
ThreadBlocker* blocker;
|
||||
pid_t tid;
|
||||
};
|
||||
SchedulerQueue::Node* node;
|
||||
};
|
||||
|
||||
public:
|
||||
@@ -88,8 +85,7 @@ namespace Kernel
|
||||
BAN::ErrorOr<void> add_thread(Thread*);
|
||||
|
||||
void block_current_thread(ThreadBlocker* thread_blocker, uint64_t wake_time_ns);
|
||||
void unblock_threads(ThreadBlocker*);
|
||||
void unblock_thread(pid_t tid);
|
||||
void unblock_thread(Thread*);
|
||||
|
||||
Thread& current_thread();
|
||||
Thread& idle_thread();
|
||||
@@ -104,20 +100,17 @@ namespace Kernel
|
||||
void update_most_loaded_node_queue(SchedulerQueue::Node*, SchedulerQueue* target_queue);
|
||||
void remove_node_from_most_loaded(SchedulerQueue::Node*);
|
||||
|
||||
bool do_unblock(ThreadBlocker*);
|
||||
bool do_unblock(pid_t);
|
||||
void do_load_balancing();
|
||||
|
||||
class ProcessorID find_least_loaded_processor() const;
|
||||
|
||||
void handle_unblock_request(const UnblockRequest&);
|
||||
void handle_new_thread_request(const NewThreadRequest&);
|
||||
void add_thread(SchedulerQueue::Node*);
|
||||
void unblock_thread(SchedulerQueue::Node*);
|
||||
|
||||
private:
|
||||
SchedulerQueue m_run_queue;
|
||||
SchedulerQueue m_block_queue;
|
||||
SchedulerQueue::Node* m_current { nullptr };
|
||||
bool m_current_will_block { false };
|
||||
|
||||
uint32_t m_thread_count { 0 };
|
||||
|
||||
@@ -141,6 +134,7 @@ namespace Kernel
|
||||
|
||||
Thread* m_idle_thread { nullptr };
|
||||
|
||||
friend class ThreadBlocker;
|
||||
friend class Processor;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
#include <BAN/NoCopyMove.h>
|
||||
#include <BAN/RefPtr.h>
|
||||
#include <BAN/UniqPtr.h>
|
||||
#include <kernel/Memory/VirtualRange.h>
|
||||
#include <kernel/InterruptStack.h>
|
||||
#include <kernel/Memory/VirtualRange.h>
|
||||
#include <kernel/ThreadBlocker.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
@@ -96,25 +97,27 @@ namespace Kernel
|
||||
void on_exit();
|
||||
|
||||
private:
|
||||
static constexpr size_t m_kernel_stack_size = PAGE_SIZE * 64;
|
||||
static constexpr size_t m_userspace_stack_size = PAGE_SIZE * 64;
|
||||
BAN::UniqPtr<VirtualRange> m_kernel_stack;
|
||||
BAN::UniqPtr<VirtualRange> m_userspace_stack;
|
||||
const pid_t m_tid { 0 };
|
||||
State m_state { State::NotStarted };
|
||||
Process* m_process { nullptr };
|
||||
bool m_is_userspace { false };
|
||||
bool m_delete_process { false };
|
||||
static constexpr size_t m_kernel_stack_size { PAGE_SIZE * 64 };
|
||||
static constexpr size_t m_userspace_stack_size { PAGE_SIZE * 64 };
|
||||
BAN::UniqPtr<VirtualRange> m_kernel_stack;
|
||||
BAN::UniqPtr<VirtualRange> m_userspace_stack;
|
||||
const pid_t m_tid { 0 };
|
||||
State m_state { State::NotStarted };
|
||||
Process* m_process { nullptr };
|
||||
bool m_is_userspace { false };
|
||||
bool m_delete_process { false };
|
||||
|
||||
InterruptStack m_interrupt_stack { };
|
||||
InterruptRegisters m_interrupt_registers { };
|
||||
SchedulerQueue::Node* m_scheduler_node { nullptr };
|
||||
|
||||
uint64_t m_signal_pending_mask { 0 };
|
||||
uint64_t m_signal_block_mask { 0 };
|
||||
SpinLock m_signal_lock;
|
||||
InterruptStack m_interrupt_stack { };
|
||||
InterruptRegisters m_interrupt_registers { };
|
||||
|
||||
uint64_t m_signal_pending_mask { 0 };
|
||||
uint64_t m_signal_block_mask { 0 };
|
||||
SpinLock m_signal_lock;
|
||||
static_assert(_SIGMAX < 64);
|
||||
|
||||
BAN::Atomic<uint32_t> m_mutex_count { 0 };
|
||||
BAN::Atomic<uint32_t> m_mutex_count { 0 };
|
||||
|
||||
#if __enable_sse
|
||||
alignas(16) uint8_t m_sse_storage[512] {};
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/Lock/SpinLock.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
@@ -12,6 +15,16 @@ namespace Kernel
|
||||
void block_with_timeout_ns(uint64_t timeout_ns);
|
||||
void block_with_wake_time_ns(uint64_t wake_time_ns);
|
||||
void unblock();
|
||||
|
||||
private:
|
||||
void add_thread_to_block_queue(SchedulerQueue::Node*);
|
||||
void remove_blocked_thread(SchedulerQueue::Node*);
|
||||
|
||||
private:
|
||||
SpinLock m_lock;
|
||||
SchedulerQueue::Node* m_block_chain { nullptr };
|
||||
|
||||
friend class Scheduler;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user