#pragma once #include "arch/MMU.h" #include "fs/VFS.h" #include "memory/UserVM.h" #include #include #include #include #include #include #ifdef ARCH_X86_64 #include "arch/x86_64/CPU.h" #else #error "Unknown architecture." #endif enum class ThreadState { Idle, Runnable, Sleeping, Exited, Dying }; struct FileDescriptor { SharedPtr inode; usize offset { 0 }; int flags { 0 }; bool should_append(); bool should_block(); bool is_writable(); bool is_readable(); }; static constexpr int FD_MAX = 64; struct Credentials { u32 uid { 0 }; u32 euid { 0 }; u32 suid { 0 }; u32 gid { 0 }; u32 egid { 0 }; u32 sgid { 0 }; }; struct Thread : public LinkedListNode { Registers regs; u64 id; u64 parent_id; Credentials auth; 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 vm_allocator; Option fd_table[FD_MAX] = {}; Result allocate_fd(int min); Result resolve_fd(int fd); Result> resolve_atfile(int dirfd, const String& path, bool allow_empty_path, SharedPtr* parent_inode = nullptr); FPData fp_data; ThreadState state = ThreadState::Runnable; bool is_kernel { true }; u8 status { 0 }; StaticString<128> name; String current_directory_path = {}; SharedPtr current_directory = {}; 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(); void set_return(u64 ret); static void init(); }; void switch_context(Thread* old_thread, Thread* new_thread, Registers* regs); bool is_in_kernel(Registers* regs); Result new_thread(); extern LinkedList g_threads;