Luna/kernel/src/thread/Thread.h

104 lines
1.7 KiB
C
Raw Normal View History

#pragma once
2023-01-05 21:50:53 +01:00
#include "arch/MMU.h"
#include "fs/VFS.h"
#include "memory/UserVM.h"
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
2022-12-18 18:43:17 +01:00
#include <luna/Stack.h>
2023-03-24 21:05:38 +01:00
#include <luna/StaticString.h>
#ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h"
#else
#error "Unknown architecture."
#endif
2022-12-07 15:14:58 +01:00
enum class ThreadState
{
Idle,
Runnable,
Sleeping,
Exited,
Dying
2022-12-07 15:14:58 +01:00
};
struct FileDescriptor
{
SharedPtr<VFS::Inode> inode;
usize offset { 0 };
int flags { 0 };
bool should_append();
2023-03-19 11:25:14 +01:00
bool should_block();
bool is_writable();
bool is_readable();
};
static constexpr int FD_MAX = 64;
2022-12-19 12:43:23 +01:00
struct Thread : public LinkedListNode<Thread>
{
Registers regs;
u64 id;
u64 parent_id;
u64 ticks = 0;
u64 ticks_in_user = 0;
u64 ticks_in_kernel = 0;
u64 ticks_left;
2022-12-07 15:55:58 +01:00
u64 sleep_ticks_left;
2022-12-18 18:43:17 +01:00
Stack stack;
2023-01-05 21:50:53 +01:00
Stack kernel_stack;
2022-12-18 18:43:17 +01:00
OwnedPtr<UserVM> vm_allocator;
Option<FileDescriptor> fd_table[FD_MAX] = {};
Result<int> allocate_fd(int min);
Result<FileDescriptor*> resolve_fd(int fd);
FPData fp_data;
2022-12-07 15:14:58 +01:00
ThreadState state = ThreadState::Runnable;
2023-01-05 21:50:53 +01:00
bool is_kernel { true };
u8 status { 0 };
2023-03-24 21:05:38 +01:00
StaticString<128> name;
2023-01-05 21:50:53 +01:00
PageDirectory* directory;
2022-12-07 15:14:58 +01:00
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();
2022-12-17 10:50:49 +01:00
2023-03-18 23:45:48 +01:00
void set_return(u64 ret);
2022-12-17 10:50:49 +01:00
static void init();
};
void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs);
bool is_in_kernel(Registers* regs);
Result<Thread*> new_thread();
2023-01-02 13:07:29 +01:00
extern LinkedList<Thread> g_threads;