Add a Thread class which can be part of a DoublyLinkedList
This commit is contained in:
parent
b9b7d1e201
commit
ad11aa719e
49
kernel/src/arch/x86_64/Thread.cpp
Normal file
49
kernel/src/arch/x86_64/Thread.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "thread/Thread.h"
|
||||
#include <luna/String.h>
|
||||
|
||||
bool is_in_kernel(Registers* regs)
|
||||
{
|
||||
return regs->cs == 8;
|
||||
}
|
||||
|
||||
void Thread::set_ip(u64 ip)
|
||||
{
|
||||
regs.rip = ip;
|
||||
}
|
||||
|
||||
u64 Thread::ip()
|
||||
{
|
||||
return regs.rip;
|
||||
}
|
||||
|
||||
void Thread::set_sp(u64 sp)
|
||||
{
|
||||
regs.rsp = sp;
|
||||
}
|
||||
|
||||
u64 Thread::sp()
|
||||
{
|
||||
return regs.rsp;
|
||||
}
|
||||
|
||||
void Thread::init_regs_kernel()
|
||||
{
|
||||
memset(®s, 0, sizeof(Registers));
|
||||
regs.cs = 0x08;
|
||||
regs.ss = 0x10;
|
||||
}
|
||||
|
||||
void Thread::set_arguments(u64 arg1, u64 arg2, u64 arg3, u64 arg4)
|
||||
{
|
||||
regs.rdi = arg1;
|
||||
regs.rsi = arg2;
|
||||
regs.rdx = arg3;
|
||||
regs.rcx = arg4;
|
||||
}
|
||||
|
||||
void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs)
|
||||
{
|
||||
if (!old_thread->is_idle) memcpy(&old_thread->regs, regs, sizeof(Registers));
|
||||
|
||||
memcpy(regs, &new_thread->regs, sizeof(Registers));
|
||||
}
|
15
kernel/src/thread/Thread.cpp
Normal file
15
kernel/src/thread/Thread.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "thread/Thread.h"
|
||||
#include <luna/Alloc.h>
|
||||
|
||||
static u64 g_next_id = 1;
|
||||
|
||||
DoublyLinkedList<Thread> g_threads;
|
||||
|
||||
Result<Thread*> new_thread()
|
||||
{
|
||||
Thread* thread = TRY(make<Thread>());
|
||||
|
||||
thread->id = g_next_id++;
|
||||
|
||||
return thread;
|
||||
}
|
46
kernel/src/thread/Thread.h
Normal file
46
kernel/src/thread/Thread.h
Normal file
@ -0,0 +1,46 @@
|
||||
#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
|
||||
|
||||
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;
|
||||
|
||||
bool is_idle = false;
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
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;
|
Loading…
Reference in New Issue
Block a user