61 lines
1.0 KiB
C
61 lines
1.0 KiB
C
#pragma once
|
|
|
|
#include "arch/CPU.h"
|
|
#include <luna/LinkedList.h>
|
|
#include <luna/Result.h>
|
|
|
|
#ifdef ARCH_X86_64
|
|
#include "arch/x86_64/CPU.h"
|
|
#else
|
|
#error "Unknown architecture."
|
|
#endif
|
|
|
|
enum class ThreadState
|
|
{
|
|
Idle,
|
|
Runnable,
|
|
Sleeping
|
|
};
|
|
|
|
struct Thread : public DoublyLinkedListNode<Thread>
|
|
{
|
|
Registers regs;
|
|
|
|
u64 id;
|
|
|
|
u64 ticks = 0;
|
|
u64 ticks_in_user = 0;
|
|
u64 ticks_in_kernel = 0;
|
|
|
|
u64 ticks_left;
|
|
u64 sleep_ticks_left;
|
|
|
|
ThreadState state = ThreadState::Runnable;
|
|
|
|
bool is_idle()
|
|
{
|
|
return state == ThreadState::Idle;
|
|
}
|
|
|
|
void init_regs_kernel();
|
|
void init_regs_user();
|
|
|
|
void set_arguments(u64 arg1, u64 arg2, u64 arg3, u64 arg4);
|
|
|
|
void set_ip(u64 ip);
|
|
u64 ip();
|
|
|
|
void set_sp(u64 sp);
|
|
u64 sp();
|
|
|
|
static void init();
|
|
};
|
|
|
|
void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs);
|
|
|
|
bool is_in_kernel(Registers* regs);
|
|
|
|
Result<Thread*> new_thread();
|
|
Result<Thread*> create_idle_thread();
|
|
|
|
extern DoublyLinkedList<Thread> g_threads; |