Luna/kernel/src/thread/Thread.h
apio b54a7f3a80
All checks were successful
continuous-integration/drone/push Build is passing
kernel+libc: Add O_* flags and parse them in open()
O_RDONLY, O_WRONLY, O_RDWR, O_TRUNC, O_CREAT and O_EXCL are fully implemented.

O_APPEND is partially implemented.

Other flags are not here yet.
2023-03-12 14:43:58 +01:00

94 lines
1.6 KiB
C

#pragma once
#include "arch/MMU.h"
#include "fs/VFS.h"
#include "memory/UserVM.h"
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
#include <luna/Stack.h>
#ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h"
#else
#error "Unknown architecture."
#endif
enum class ThreadState
{
Idle,
Runnable,
Sleeping,
Dying
};
struct FileDescriptor
{
SharedPtr<VFS::Inode> inode;
usize offset { 0 };
int flags { 0 };
bool should_append();
bool is_writable();
bool is_readable();
};
static constexpr int FD_MAX = 64;
struct Thread : public LinkedListNode<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;
Stack stack;
Stack kernel_stack;
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;
ThreadState state = ThreadState::Runnable;
bool is_kernel { true };
PageDirectory* directory;
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();
extern LinkedList<Thread> g_threads;