#include "thread/Thread.h" #include #include static Atomic g_next_id; LinkedList g_threads; void Thread::init() { g_next_id = 1; } Result new_thread() { Thread* const thread = TRY(make()); thread->id = g_next_id++; return thread; } Result Thread::allocate_fd(int min) { if (min < 0 || min >= FD_MAX) return err(EINVAL); for (int i = min; i < FD_MAX; i++) { // FIXME: Possible race condition if multiple threads share a FileDescriptorTable? Let's not worry about it for // now, we're still a long way away from reaching that point. if (!fd_table[i].has_value()) { return i; } } return err(EMFILE); } Result Thread::resolve_fd(int fd) { if (fd < 0 || fd >= FD_MAX) return err(EBADF); Option& maybe_descriptor = fd_table[fd]; if (!maybe_descriptor.has_value()) return err(EBADF); return maybe_descriptor.value_ptr(); }