Compare commits
No commits in common. "751377de0a99595b7ee8cf8b7f5bd0e4c862e2d8" and "00cf267ac7dadf73cd5d65583d401413a18ade93" have entirely different histories.
751377de0a
...
00cf267ac7
@ -35,36 +35,7 @@ void heap_thread()
|
|||||||
CPU::disable_interrupts();
|
CPU::disable_interrupts();
|
||||||
dump_heap_usage();
|
dump_heap_usage();
|
||||||
kdbgln("Kernel uses %lu vm pages", KernelVM::used() / ARCH_PAGE_SIZE);
|
kdbgln("Kernel uses %lu vm pages", KernelVM::used() / ARCH_PAGE_SIZE);
|
||||||
kernel_exit();
|
while (true) kernel_sleep(UINT64_MAX);
|
||||||
}
|
|
||||||
|
|
||||||
void reap_thread()
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
CPU::disable_interrupts();
|
|
||||||
auto dying_threads = Scheduler::check_for_dying_threads();
|
|
||||||
CPU::enable_interrupts();
|
|
||||||
|
|
||||||
if (dying_threads.count())
|
|
||||||
{
|
|
||||||
Thread* thread_to_reap = dying_threads.expect_first();
|
|
||||||
Thread* next_thread = thread_to_reap;
|
|
||||||
|
|
||||||
while (thread_to_reap)
|
|
||||||
{
|
|
||||||
next_thread = dying_threads.next(thread_to_reap).value_or(nullptr);
|
|
||||||
kinfoln("reap: reaping thread with id %zu", thread_to_reap->id);
|
|
||||||
auto stack = thread_to_reap->stack;
|
|
||||||
kinfoln("deleting thread stack @ %#lx, has %zu bytes of stack", stack.bottom(), stack.bytes());
|
|
||||||
MemoryManager::unmap_owned_and_free_vm(stack.bottom(), stack.bytes() / ARCH_PAGE_SIZE).release_value();
|
|
||||||
delete thread_to_reap;
|
|
||||||
thread_to_reap = next_thread;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
kernel_sleep(250);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> init()
|
Result<void> init()
|
||||||
@ -96,7 +67,6 @@ Result<void> init()
|
|||||||
|
|
||||||
TRY(Scheduler::new_kernel_thread(async_thread));
|
TRY(Scheduler::new_kernel_thread(async_thread));
|
||||||
TRY(Scheduler::new_kernel_thread(heap_thread));
|
TRY(Scheduler::new_kernel_thread(heap_thread));
|
||||||
TRY(Scheduler::new_kernel_thread(reap_thread));
|
|
||||||
|
|
||||||
CPU::platform_finish_init();
|
CPU::platform_finish_init();
|
||||||
|
|
||||||
|
@ -29,8 +29,6 @@ namespace Scheduler
|
|||||||
Stack idle_stack{idle_stack_vm, ARCH_PAGE_SIZE};
|
Stack idle_stack{idle_stack_vm, ARCH_PAGE_SIZE};
|
||||||
g_idle.set_sp(idle_stack.top());
|
g_idle.set_sp(idle_stack.top());
|
||||||
|
|
||||||
g_idle.stack = idle_stack;
|
|
||||||
|
|
||||||
kinfoln("CREATED IDLE THREAD: id %lu with ip %lx and sp %lx", g_idle.id, g_idle.ip(), g_idle.sp());
|
kinfoln("CREATED IDLE THREAD: id %lu with ip %lx and sp %lx", g_idle.id, g_idle.ip(), g_idle.sp());
|
||||||
|
|
||||||
g_current = &g_idle;
|
g_current = &g_idle;
|
||||||
@ -58,8 +56,6 @@ namespace Scheduler
|
|||||||
Stack thread_stack{thread_stack_vm, ARCH_PAGE_SIZE * 4};
|
Stack thread_stack{thread_stack_vm, ARCH_PAGE_SIZE * 4};
|
||||||
thread->set_sp(thread_stack.top());
|
thread->set_sp(thread_stack.top());
|
||||||
|
|
||||||
thread->stack = thread_stack;
|
|
||||||
|
|
||||||
g_threads.append(thread);
|
g_threads.append(thread);
|
||||||
|
|
||||||
kinfoln("CREATED THREAD: id %lu with ip %lx and sp %lx", thread->id, thread->ip(), thread->sp());
|
kinfoln("CREATED THREAD: id %lu with ip %lx and sp %lx", thread->id, thread->ip(), thread->sp());
|
||||||
@ -167,32 +163,6 @@ namespace Scheduler
|
|||||||
|
|
||||||
if (!g_current->ticks_left) switch_task(regs);
|
if (!g_current->ticks_left) switch_task(regs);
|
||||||
}
|
}
|
||||||
|
|
||||||
DoublyLinkedList<Thread> check_for_dying_threads()
|
|
||||||
{
|
|
||||||
DoublyLinkedList<Thread> result;
|
|
||||||
|
|
||||||
Thread* thread_to_remove = nullptr;
|
|
||||||
|
|
||||||
g_threads.for_each([&](Thread* thread) {
|
|
||||||
if (thread_to_remove)
|
|
||||||
{
|
|
||||||
g_threads.remove(thread_to_remove);
|
|
||||||
result.append(thread_to_remove);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (thread->state == ThreadState::Dying) { thread_to_remove = thread; }
|
|
||||||
else { thread_to_remove = nullptr; }
|
|
||||||
});
|
|
||||||
|
|
||||||
if (thread_to_remove)
|
|
||||||
{
|
|
||||||
g_threads.remove(thread_to_remove);
|
|
||||||
result.append(thread_to_remove);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void kernel_sleep(u64 ms)
|
void kernel_sleep(u64 ms)
|
||||||
@ -201,10 +171,3 @@ void kernel_sleep(u64 ms)
|
|||||||
g_current->state = ThreadState::Sleeping;
|
g_current->state = ThreadState::Sleeping;
|
||||||
kernel_yield();
|
kernel_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[noreturn]] void kernel_exit()
|
|
||||||
{
|
|
||||||
g_current->state = ThreadState::Dying;
|
|
||||||
kernel_yield();
|
|
||||||
unreachable();
|
|
||||||
}
|
|
@ -17,10 +17,7 @@ namespace Scheduler
|
|||||||
void switch_task(Registers* regs);
|
void switch_task(Registers* regs);
|
||||||
|
|
||||||
void invoke(Registers* regs);
|
void invoke(Registers* regs);
|
||||||
|
|
||||||
DoublyLinkedList<Thread> check_for_dying_threads();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void kernel_yield();
|
extern "C" void kernel_yield();
|
||||||
void kernel_sleep(u64 ms);
|
void kernel_sleep(u64 ms);
|
||||||
[[noreturn]] void kernel_exit();
|
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <luna/LinkedList.h>
|
#include <luna/LinkedList.h>
|
||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
#include <luna/Stack.h>
|
|
||||||
|
|
||||||
#ifdef ARCH_X86_64
|
#ifdef ARCH_X86_64
|
||||||
#include "arch/x86_64/CPU.h"
|
#include "arch/x86_64/CPU.h"
|
||||||
@ -14,8 +13,7 @@ enum class ThreadState
|
|||||||
{
|
{
|
||||||
Idle,
|
Idle,
|
||||||
Runnable,
|
Runnable,
|
||||||
Sleeping,
|
Sleeping
|
||||||
Dying
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Thread : public DoublyLinkedListNode<Thread>
|
struct Thread : public DoublyLinkedListNode<Thread>
|
||||||
@ -31,8 +29,6 @@ struct Thread : public DoublyLinkedListNode<Thread>
|
|||||||
u64 ticks_left;
|
u64 ticks_left;
|
||||||
u64 sleep_ticks_left;
|
u64 sleep_ticks_left;
|
||||||
|
|
||||||
Stack stack;
|
|
||||||
|
|
||||||
ThreadState state = ThreadState::Runnable;
|
ThreadState state = ThreadState::Runnable;
|
||||||
|
|
||||||
bool is_idle()
|
bool is_idle()
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
struct Stack
|
struct Stack
|
||||||
{
|
{
|
||||||
Stack() = default;
|
|
||||||
Stack(u64 base, usize bytes);
|
Stack(u64 base, usize bytes);
|
||||||
|
|
||||||
u64 bottom()
|
u64 bottom()
|
||||||
|
Loading…
Reference in New Issue
Block a user